Skip to content
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
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
31 changes: 19 additions & 12 deletions src/test/hover.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import * as assert from 'assert';

import { Hover, Position, MarkedString, TextDocument, getLanguageService, JSONSchema, LanguageServiceParams } from '../jsonLanguageService';
import { Hover, Position, TextDocument, getLanguageService, JSONSchema, LanguageServiceParams } from '../jsonLanguageService';

suite('JSON Hover', () => {

Expand Down Expand Up @@ -33,7 +33,7 @@ suite('JSON Hover', () => {

test('Simple schema', async function () {

const content = '{"a": 42, "b": "hello", "c": false}';
const content = '{"a": 42, "b": "hello", "c": false, "complex-description": false}';
const schema: JSONSchema = {
type: 'object',
description: 'a very special object',
Expand All @@ -49,20 +49,27 @@ suite('JSON Hover', () => {
'c': {
type: 'boolean',
description: 'C'
}
},
'complex-description': {
type: 'boolean',
description: 'For example:\n\n<script>\n alert(1)\n</script>\n\n Test [1]'
},
}
};
await testComputeInfo(content, schema, { line: 0, character: 0 }).then((result) => {
assert.deepEqual(result.contents, [MarkedString.fromPlainText('a very special object')]);
assert.deepEqual(result.contents, ['a&nbsp;very&nbsp;special&nbsp;object']);
});
await testComputeInfo(content, schema, { line: 0, character: 1 }).then((result) => {
assert.deepEqual(result.contents, [MarkedString.fromPlainText('A')]);
assert.deepEqual(result.contents, ['A']);
});
await testComputeInfo(content, schema, { line: 0, character: 32 }).then((result) => {
assert.deepEqual(result.contents, [MarkedString.fromPlainText('C')]);
assert.deepEqual(result.contents, ['C']);
});
await testComputeInfo(content, schema, { line: 0, character: 37 }).then((result) => {
assert.deepEqual(result.contents, ['For&nbsp;example:\\\n\\\n\\<script\\>\\\n&nbsp;&nbsp;alert\\(1\\)\\\n\\</script\\>\\\n\\\n&nbsp;&nbsp;&nbsp;&nbsp;Test&nbsp;\\[1\\]']);
});
await testComputeInfo(content, schema, { line: 0, character: 7 }).then((result) => {
assert.deepEqual(result.contents, [MarkedString.fromPlainText('A')]);
assert.deepEqual(result.contents, ['A']);
});
});

Expand All @@ -89,13 +96,13 @@ suite('JSON Hover', () => {
}]
};
await testComputeInfo(content, schema, { line: 0, character: 0 }).then((result) => {
assert.deepEqual(result.contents, [MarkedString.fromPlainText('a very special object')]);
assert.deepEqual(result.contents, ['a&nbsp;very&nbsp;special&nbsp;object']);
});
await testComputeInfo(content, schema, { line: 0, character: 1 }).then((result) => {
assert.deepEqual(result.contents, [MarkedString.fromPlainText('A')]);
assert.deepEqual(result.contents, ['A']);
});
await testComputeInfo(content, schema, { line: 0, character: 10 }).then((result) => {
assert.deepEqual(result.contents, [MarkedString.fromPlainText('B\n\nIt\'s B')]);
assert.deepEqual(result.contents, ['B\n\nIt\'s&nbsp;B']);
});
});

Expand Down Expand Up @@ -154,10 +161,10 @@ suite('JSON Hover', () => {
};

await testComputeInfo('{ "prop1": "e1', schema, { line: 0, character: 12 }).then(result => {
assert.deepEqual(result.contents, ['line1\n\nline2\n\nline3\n\n\nline4\n']);
assert.deepEqual(result.contents, ['line1\\\nline2\\\n\\\nline3\\\n\\\n\\\nline4']);
});
await testComputeInfo('{ "prop2": "e1', schema, { line: 0, character: 12 }).then(result => {
assert.deepEqual(result.contents, ['line1\n\nline2\r\n\r\nline3']);
assert.deepEqual(result.contents, ['line1\r\\\nline2\r\\\n\r\\\nline3']);
});
});

Expand Down