massive update, probably broken
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-json / node_modules / vscode-json-languageservice / lib / umd / 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 (function (factory) {
6     if (typeof module === "object" && typeof module.exports === "object") {
7         var v = factory(require, exports);
8         if (v !== undefined) module.exports = v;
9     }
10     else if (typeof define === "function" && define.amd) {
11         define(["require", "exports", "../parser/jsonParser", "../jsonLanguageTypes"], factory);
12     }
13 })(function (require, exports) {
14     "use strict";
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;
25         }
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);
31             }
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;
38                     if (!node) {
39                         return this.promise.resolve(null);
40                     }
41                 }
42             }
43             var hoverRange = jsonLanguageTypes_1.Range.create(document.positionAt(hoverRangeNode.offset), document.positionAt(hoverRangeNode.offset + hoverRangeNode.length));
44             var createHover = function (contents) {
45                 var result = {
46                     contents: contents,
47                     range: hoverRange
48                 };
49                 return result;
50             };
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);
55                 if (promise) {
56                     return promise.then(function (htmlContent) { return createHover(htmlContent); });
57                 }
58             }
59             return this.schemaService.getSchemaForResource(document.uri, doc).then(function (schema) {
60                 if (schema && node) {
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);
69                             if (s.schema.enum) {
70                                 var idx = s.schema.enum.indexOf(Parser.getNodeValue(node));
71                                 if (s.schema.markdownEnumDescriptions) {
72                                     markdownEnumValueDescription_1 = s.schema.markdownEnumDescriptions[idx];
73                                 }
74                                 else if (s.schema.enumDescriptions) {
75                                     markdownEnumValueDescription_1 = toMarkdown(s.schema.enumDescriptions[idx]);
76                                 }
77                                 if (markdownEnumValueDescription_1) {
78                                     enumValue_1 = s.schema.enum[idx];
79                                     if (typeof enumValue_1 !== 'string') {
80                                         enumValue_1 = JSON.stringify(enumValue_1);
81                                     }
82                                 }
83                             }
84                         }
85                         return true;
86                     });
87                     var result = '';
88                     if (title_1) {
89                         result = toMarkdown(title_1);
90                     }
91                     if (markdownDescription_1) {
92                         if (result.length > 0) {
93                             result += "\n\n";
94                         }
95                         result += markdownDescription_1;
96                     }
97                     if (markdownEnumValueDescription_1) {
98                         if (result.length > 0) {
99                             result += "\n\n";
100                         }
101                         result += "`" + toMarkdownCodeBlock(enumValue_1) + "`: " + markdownEnumValueDescription_1;
102                     }
103                     return createHover([result]);
104                 }
105                 return null;
106             });
107         };
108         return JSONHover;
109     }());
110     exports.JSONHover = JSONHover;
111     function toMarkdown(plain) {
112         if (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
115         }
116         return undefined;
117     }
118     function toMarkdownCodeBlock(content) {
119         // see https://daringfireball.net/projects/markdown/syntax#precode
120         if (content.indexOf('`') !== -1) {
121             return '`` ' + content + ' ``';
122         }
123         return content;
124     }
125 });