Skip to content
Merged
Changes from 2 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
87 changes: 45 additions & 42 deletions src/services/jsonHover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,61 +60,64 @@ export class JSONHover {
}

return this.schemaService.getSchemaForResource(document.uri, doc).then((schema) => {
if (schema && node) {
const matchingSchemas = doc.getMatchingSchemas(schema.schema, node.offset);

let title: string | undefined = undefined;
let markdownDescription: string | undefined = undefined;
let markdownEnumValueDescription: string | undefined = undefined, enumValue: string | undefined = undefined;
matchingSchemas.every((s) => {
if (s.node === node && !s.inverted && s.schema) {
title = title || s.schema.title;
markdownDescription = markdownDescription || s.schema.markdownDescription || toMarkdown(s.schema.description);
if (s.schema.enum) {
const idx = s.schema.enum.indexOf(Parser.getNodeValue(node));
if (s.schema.markdownEnumDescriptions) {
markdownEnumValueDescription = s.schema.markdownEnumDescriptions[idx];
} else if (s.schema.enumDescriptions) {
markdownEnumValueDescription = toMarkdown(s.schema.enumDescriptions[idx]);
}
if (markdownEnumValueDescription) {
enumValue = s.schema.enum[idx];
if (typeof enumValue !== 'string') {
enumValue = JSON.stringify(enumValue);
}
}
if (!schema) {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't realize the change created such a big diff, the main change here is just an early return, and a for of instead of every

return null
}

let title: string | undefined = undefined;
let markdownDescription: string | undefined = undefined;
let markdownEnumValueDescription: string | undefined = undefined, enumValue: string | undefined = undefined;

const matchingSchemas = doc.getMatchingSchemas(schema.schema, node.offset).filter((s) => s.node === node && !s.inverted).map((s) => s.schema);
for (const schema of matchingSchemas) {
title = title || schema.title;
markdownDescription = markdownDescription || schema.markdownDescription || toMarkdown(schema.description);
if (schema.enum) {
const idx = schema.enum.indexOf(Parser.getNodeValue(node));
if (schema.markdownEnumDescriptions) {
markdownEnumValueDescription = schema.markdownEnumDescriptions[idx];
} else if (schema.enumDescriptions) {
markdownEnumValueDescription = toMarkdown(schema.enumDescriptions[idx]);
}
if (markdownEnumValueDescription) {
enumValue = schema.enum[idx];
if (typeof enumValue !== 'string') {
enumValue = JSON.stringify(enumValue);
}
}
return true;
});
let result = '';
if (title) {
result = toMarkdown(title);
}
if (markdownDescription) {
if (result.length > 0) {
result += "\n\n";
}
result += markdownDescription;
}

let result = '';
if (title) {
result = toMarkdown(title);
}
if (markdownDescription) {
if (result.length > 0) {
result += "\n\n";
}
if (markdownEnumValueDescription) {
if (result.length > 0) {
result += "\n\n";
}
result += `\`${toMarkdownCodeBlock(enumValue!)}\`: ${markdownEnumValueDescription}`;
result += markdownDescription;
}
if (markdownEnumValueDescription) {
if (result.length > 0) {
result += "\n\n";
}
return createHover([result]);
result += `\`${toMarkdownCodeBlock(enumValue!)}\`: ${markdownEnumValueDescription}`;
}
return null;
return createHover([result]);
});
}
}

function toMarkdown(plain: string): string;
function toMarkdown(plain: string | undefined): string | undefined;
function toMarkdown(plain: string | undefined): string | undefined {
if (plain) {
const res = plain.replace(/([^\n\r])(\r?\n)([^\n\r])/gm, '$1\n\n$3'); // single new lines to \n\n (Markdown paragraph)
return res.replace(/[\\`*_{}[\]()#+\-.!]/g, "\\$&"); // escape markdown syntax tokens: http://daringfireball.net/projects/markdown/syntax#backslash
return plain
.trim()
.replace(/[\\`*_{}[\]()<>#+\-.!]/g, '\\$&') // escape markdown syntax tokens: http://daringfireball.net/projects/markdown/syntax#backslash
.replace(/([ \t]+)/g, (_match, g1) => '&nbsp;'.repeat(g1.length)) // escape spaces tabs
.replace(/\n/g, '\\\n'); // escape new lines
Comment on lines +116 to +120
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the main fix

}
return undefined;
}
Expand Down