Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Interactive Examples Editor Error Highlighting Improvements #1528

Merged
merged 6 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,11 @@ class Discriminator(
)

if (isPatternToken(actualDiscriminatorValue) || ExampleProcessor.isSubstitutionToken(actualDiscriminatorValue)) return Result.Success()

if (actualDiscriminatorValue.toStringLiteral() !in values) {
val message =
"Expected the value of discriminator property to be $discriminatorCsvClause but it was ${actualDiscriminatorValue.toStringLiteral()}"
val message = "Expected the value of discriminator property to be $discriminatorCsvClause but it was ${
actualDiscriminatorValue.toStringLiteral()
.takeUnless { it.isEmpty() } ?: "\"\""
}"

return Result.Failure(
message,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ data class ExactValuePattern(override val pattern: Value, override val typeAlias
true -> Result.Success()
else -> {
if (discriminator) {
val errorMessage = "Expected the value of discriminator property to be ${pattern.displayableValue()} but it was ${sampleData?.displayableValue()}"
val errorMessage = "Expected the value of discriminator property to be ${pattern.displayableValue()} but it was ${
sampleData?.displayableValue().takeUnless { it.isNullOrEmpty() } ?: "\"\""
}"
Result.Failure(errorMessage, failureReason = FailureReason.DiscriminatorMismatch)
} else
mismatchResult(pattern, sampleData, resolver.mismatchMessages)
Expand Down
28 changes: 17 additions & 11 deletions core/src/main/resources/static/example-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -630,15 +630,15 @@ function createExampleDropDown(example) {
decorationsField,
editorFacet,
window.EditorView.updateListener.of((update) => {
if (update.docChanged) {
isSaved = false;
const editorElement = editor.dom;
updateBorderColorExampleBlock(editorElement, examplePreDiv);
if (example.errorList && example.errorList.length > 0) {
highlightErrorLines(editor, example.errorList, example.exampleJson);
}
savedEditorResponse = update.state.doc.toString();
}
if (!update.docChanged) return;
const docContent = update.state.doc.toString();

isSaved = false;
const editorElement = editor.dom;
updateBorderColorExampleBlock(editorElement, examplePreDiv);
if (!example.errorList.length > 0) return;
highlightErrorLines(editor, example.errorList, docContent);
savedEditorResponse = docContent;
})
],
}),
Expand Down Expand Up @@ -746,15 +746,20 @@ function highlightErrorLines(editor, metadata, exampleJson) {
existingMarkers.get(lineNumber).push(meta.description);
const combinedDescriptions = existingMarkers.get(lineNumber).join('\n\n');
const className = "specmatic-editor-line-error";
const tokenStart = lineLength.from;
const tokenEnd = lineLength.to;
const existingDecoration = decorations.filter(decoration => decoration.from === tokenStart && decoration.to === tokenEnd);
if (existingDecoration.length !== 0) return;

decorations.push(
window.Decoration.line({
window.Decoration.mark({
class: className,
attributes: {
"data-validation-error-message": combinedDescriptions
}
}).range(lineLength.from)
}).range(tokenStart, tokenEnd)
);

const existingError = errorMetadata.find(err => err.line === lineNumber + 1);
if (existingError) {
existingError.message = combinedDescriptions;
Expand All @@ -768,6 +773,7 @@ function highlightErrorLines(editor, metadata, exampleJson) {
}
});
decorations.sort((a, b) => a.from - b.from);

const decorationSet = window.Decoration.set(decorations);
const transaction = editor.state.update({
effects: setDecorationsEffect.of(decorationSet)
Expand Down
Loading