1 /*---------------------------------------------------------------------------------------------
2 * Copyright (c) Microsoft Corporation. All rights reserved.
3 * Licensed under the MIT License. See License.txt in the project root for license information.
4 *--------------------------------------------------------------------------------------------*/
5 import * as Parser from '../parser/jsonParser';
6 import { Range } from '../jsonLanguageTypes';
7 var JSONHover = /** @class */ (function () {
8 function JSONHover(schemaService, contributions, promiseConstructor) {
9 if (contributions === void 0) { contributions = []; }
10 this.schemaService = schemaService;
11 this.contributions = contributions;
12 this.promise = promiseConstructor || Promise;
14 JSONHover.prototype.doHover = function (document, position, doc) {
15 var offset = document.offsetAt(position);
16 var node = doc.getNodeFromOffset(offset);
17 if (!node || (node.type === 'object' || node.type === 'array') && offset > node.offset + 1 && offset < node.offset + node.length - 1) {
18 return this.promise.resolve(null);
20 var hoverRangeNode = node;
21 // use the property description when hovering over an object key
22 if (node.type === 'string') {
23 var parent = node.parent;
24 if (parent && parent.type === 'property' && parent.keyNode === node) {
25 node = parent.valueNode;
27 return this.promise.resolve(null);
31 var hoverRange = Range.create(document.positionAt(hoverRangeNode.offset), document.positionAt(hoverRangeNode.offset + hoverRangeNode.length));
32 var createHover = function (contents) {
39 var location = Parser.getNodePath(node);
40 for (var i = this.contributions.length - 1; i >= 0; i--) {
41 var contribution = this.contributions[i];
42 var promise = contribution.getInfoContribution(document.uri, location);
44 return promise.then(function (htmlContent) { return createHover(htmlContent); });
47 return this.schemaService.getSchemaForResource(document.uri, doc).then(function (schema) {
49 var matchingSchemas = doc.getMatchingSchemas(schema.schema, node.offset);
50 var title_1 = undefined;
51 var markdownDescription_1 = undefined;
52 var markdownEnumValueDescription_1 = undefined, enumValue_1 = undefined;
53 matchingSchemas.every(function (s) {
54 if (s.node === node && !s.inverted && s.schema) {
55 title_1 = title_1 || s.schema.title;
56 markdownDescription_1 = markdownDescription_1 || s.schema.markdownDescription || toMarkdown(s.schema.description);
58 var idx = s.schema.enum.indexOf(Parser.getNodeValue(node));
59 if (s.schema.markdownEnumDescriptions) {
60 markdownEnumValueDescription_1 = s.schema.markdownEnumDescriptions[idx];
62 else if (s.schema.enumDescriptions) {
63 markdownEnumValueDescription_1 = toMarkdown(s.schema.enumDescriptions[idx]);
65 if (markdownEnumValueDescription_1) {
66 enumValue_1 = s.schema.enum[idx];
67 if (typeof enumValue_1 !== 'string') {
68 enumValue_1 = JSON.stringify(enumValue_1);
77 result = toMarkdown(title_1);
79 if (markdownDescription_1) {
80 if (result.length > 0) {
83 result += markdownDescription_1;
85 if (markdownEnumValueDescription_1) {
86 if (result.length > 0) {
89 result += "`" + toMarkdownCodeBlock(enumValue_1) + "`: " + markdownEnumValueDescription_1;
91 return createHover([result]);
99 function toMarkdown(plain) {
101 var res = plain.replace(/([^\n\r])(\r?\n)([^\n\r])/gm, '$1\n\n$3'); // single new lines to \n\n (Markdown paragraph)
102 return res.replace(/[\\`*_{}[\]()#+\-.!]/g, "\\$&"); // escape markdown syntax tokens: http://daringfireball.net/projects/markdown/syntax#backslash
106 function toMarkdownCodeBlock(content) {
107 // see https://daringfireball.net/projects/markdown/syntax#precode
108 if (content.indexOf('`') !== -1) {
109 return '`` ' + content + ' ``';