massive update, probably broken
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-json / node_modules / vscode-json-languageservice / lib / esm / services / jsonHover.js
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;
13     }
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);
19         }
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;
26                 if (!node) {
27                     return this.promise.resolve(null);
28                 }
29             }
30         }
31         var hoverRange = Range.create(document.positionAt(hoverRangeNode.offset), document.positionAt(hoverRangeNode.offset + hoverRangeNode.length));
32         var createHover = function (contents) {
33             var result = {
34                 contents: contents,
35                 range: hoverRange
36             };
37             return result;
38         };
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);
43             if (promise) {
44                 return promise.then(function (htmlContent) { return createHover(htmlContent); });
45             }
46         }
47         return this.schemaService.getSchemaForResource(document.uri, doc).then(function (schema) {
48             if (schema && node) {
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);
57                         if (s.schema.enum) {
58                             var idx = s.schema.enum.indexOf(Parser.getNodeValue(node));
59                             if (s.schema.markdownEnumDescriptions) {
60                                 markdownEnumValueDescription_1 = s.schema.markdownEnumDescriptions[idx];
61                             }
62                             else if (s.schema.enumDescriptions) {
63                                 markdownEnumValueDescription_1 = toMarkdown(s.schema.enumDescriptions[idx]);
64                             }
65                             if (markdownEnumValueDescription_1) {
66                                 enumValue_1 = s.schema.enum[idx];
67                                 if (typeof enumValue_1 !== 'string') {
68                                     enumValue_1 = JSON.stringify(enumValue_1);
69                                 }
70                             }
71                         }
72                     }
73                     return true;
74                 });
75                 var result = '';
76                 if (title_1) {
77                     result = toMarkdown(title_1);
78                 }
79                 if (markdownDescription_1) {
80                     if (result.length > 0) {
81                         result += "\n\n";
82                     }
83                     result += markdownDescription_1;
84                 }
85                 if (markdownEnumValueDescription_1) {
86                     if (result.length > 0) {
87                         result += "\n\n";
88                     }
89                     result += "`" + toMarkdownCodeBlock(enumValue_1) + "`: " + markdownEnumValueDescription_1;
90                 }
91                 return createHover([result]);
92             }
93             return null;
94         });
95     };
96     return JSONHover;
97 }());
98 export { JSONHover };
99 function toMarkdown(plain) {
100     if (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
103     }
104     return undefined;
105 }
106 function toMarkdownCodeBlock(content) {
107     // see https://daringfireball.net/projects/markdown/syntax#precode
108     if (content.indexOf('`') !== -1) {
109         return '`` ' + content + ' ``';
110     }
111     return content;
112 }