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 *--------------------------------------------------------------------------------------------*/
6 if (typeof module === "object" && typeof module.exports === "object") {
7 var v = factory(require, exports);
8 if (v !== undefined) module.exports = v;
10 else if (typeof define === "function" && define.amd) {
11 define(["require", "exports", "../parser/jsonParser", "../jsonLanguageTypes"], factory);
13 })(function (require, exports) {
15 Object.defineProperty(exports, "__esModule", { value: true });
16 exports.JSONHover = void 0;
17 var Parser = require("../parser/jsonParser");
18 var jsonLanguageTypes_1 = require("../jsonLanguageTypes");
19 var JSONHover = /** @class */ (function () {
20 function JSONHover(schemaService, contributions, promiseConstructor) {
21 if (contributions === void 0) { contributions = []; }
22 this.schemaService = schemaService;
23 this.contributions = contributions;
24 this.promise = promiseConstructor || Promise;
26 JSONHover.prototype.doHover = function (document, position, doc) {
27 var offset = document.offsetAt(position);
28 var node = doc.getNodeFromOffset(offset);
29 if (!node || (node.type === 'object' || node.type === 'array') && offset > node.offset + 1 && offset < node.offset + node.length - 1) {
30 return this.promise.resolve(null);
32 var hoverRangeNode = node;
33 // use the property description when hovering over an object key
34 if (node.type === 'string') {
35 var parent = node.parent;
36 if (parent && parent.type === 'property' && parent.keyNode === node) {
37 node = parent.valueNode;
39 return this.promise.resolve(null);
43 var hoverRange = jsonLanguageTypes_1.Range.create(document.positionAt(hoverRangeNode.offset), document.positionAt(hoverRangeNode.offset + hoverRangeNode.length));
44 var createHover = function (contents) {
51 var location = Parser.getNodePath(node);
52 for (var i = this.contributions.length - 1; i >= 0; i--) {
53 var contribution = this.contributions[i];
54 var promise = contribution.getInfoContribution(document.uri, location);
56 return promise.then(function (htmlContent) { return createHover(htmlContent); });
59 return this.schemaService.getSchemaForResource(document.uri, doc).then(function (schema) {
61 var matchingSchemas = doc.getMatchingSchemas(schema.schema, node.offset);
62 var title_1 = undefined;
63 var markdownDescription_1 = undefined;
64 var markdownEnumValueDescription_1 = undefined, enumValue_1 = undefined;
65 matchingSchemas.every(function (s) {
66 if (s.node === node && !s.inverted && s.schema) {
67 title_1 = title_1 || s.schema.title;
68 markdownDescription_1 = markdownDescription_1 || s.schema.markdownDescription || toMarkdown(s.schema.description);
70 var idx = s.schema.enum.indexOf(Parser.getNodeValue(node));
71 if (s.schema.markdownEnumDescriptions) {
72 markdownEnumValueDescription_1 = s.schema.markdownEnumDescriptions[idx];
74 else if (s.schema.enumDescriptions) {
75 markdownEnumValueDescription_1 = toMarkdown(s.schema.enumDescriptions[idx]);
77 if (markdownEnumValueDescription_1) {
78 enumValue_1 = s.schema.enum[idx];
79 if (typeof enumValue_1 !== 'string') {
80 enumValue_1 = JSON.stringify(enumValue_1);
89 result = toMarkdown(title_1);
91 if (markdownDescription_1) {
92 if (result.length > 0) {
95 result += markdownDescription_1;
97 if (markdownEnumValueDescription_1) {
98 if (result.length > 0) {
101 result += "`" + toMarkdownCodeBlock(enumValue_1) + "`: " + markdownEnumValueDescription_1;
103 return createHover([result]);
110 exports.JSONHover = JSONHover;
111 function toMarkdown(plain) {
113 var res = plain.replace(/([^\n\r])(\r?\n)([^\n\r])/gm, '$1\n\n$3'); // single new lines to \n\n (Markdown paragraph)
114 return res.replace(/[\\`*_{}[\]()#+\-.!]/g, "\\$&"); // escape markdown syntax tokens: http://daringfireball.net/projects/markdown/syntax#backslash
118 function toMarkdownCodeBlock(content) {
119 // see https://daringfireball.net/projects/markdown/syntax#precode
120 if (content.indexOf('`') !== -1) {
121 return '`` ' + content + ' ``';