123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- /*---------------------------------------------------------------------------------------------
- * Copyright (c) Microsoft Corporation. All rights reserved.
- * Licensed under the MIT License. See License.txt in the project root for license information.
- *--------------------------------------------------------------------------------------------*/
- import * as Parser from '../parser/jsonParser';
- import { Range } from '../jsonLanguageTypes';
- var JSONHover = /** @class */ (function () {
- function JSONHover(schemaService, contributions, promiseConstructor) {
- if (contributions === void 0) { contributions = []; }
- this.schemaService = schemaService;
- this.contributions = contributions;
- this.promise = promiseConstructor || Promise;
- }
- JSONHover.prototype.doHover = function (document, position, doc) {
- var offset = document.offsetAt(position);
- var node = doc.getNodeFromOffset(offset);
- if (!node || (node.type === 'object' || node.type === 'array') && offset > node.offset + 1 && offset < node.offset + node.length - 1) {
- return this.promise.resolve(null);
- }
- var hoverRangeNode = node;
- // use the property description when hovering over an object key
- if (node.type === 'string') {
- var parent = node.parent;
- if (parent && parent.type === 'property' && parent.keyNode === node) {
- node = parent.valueNode;
- if (!node) {
- return this.promise.resolve(null);
- }
- }
- }
- var hoverRange = Range.create(document.positionAt(hoverRangeNode.offset), document.positionAt(hoverRangeNode.offset + hoverRangeNode.length));
- var createHover = function (contents) {
- var result = {
- contents: contents,
- range: hoverRange
- };
- return result;
- };
- var location = Parser.getNodePath(node);
- for (var i = this.contributions.length - 1; i >= 0; i--) {
- var contribution = this.contributions[i];
- var promise = contribution.getInfoContribution(document.uri, location);
- if (promise) {
- return promise.then(function (htmlContent) { return createHover(htmlContent); });
- }
- }
- return this.schemaService.getSchemaForResource(document.uri, doc).then(function (schema) {
- if (schema && node) {
- var matchingSchemas = doc.getMatchingSchemas(schema.schema, node.offset);
- var title_1 = undefined;
- var markdownDescription_1 = undefined;
- var markdownEnumValueDescription_1 = undefined, enumValue_1 = undefined;
- matchingSchemas.every(function (s) {
- if (s.node === node && !s.inverted && s.schema) {
- title_1 = title_1 || s.schema.title;
- markdownDescription_1 = markdownDescription_1 || s.schema.markdownDescription || toMarkdown(s.schema.description);
- if (s.schema.enum) {
- var idx = s.schema.enum.indexOf(Parser.getNodeValue(node));
- if (s.schema.markdownEnumDescriptions) {
- markdownEnumValueDescription_1 = s.schema.markdownEnumDescriptions[idx];
- }
- else if (s.schema.enumDescriptions) {
- markdownEnumValueDescription_1 = toMarkdown(s.schema.enumDescriptions[idx]);
- }
- if (markdownEnumValueDescription_1) {
- enumValue_1 = s.schema.enum[idx];
- if (typeof enumValue_1 !== 'string') {
- enumValue_1 = JSON.stringify(enumValue_1);
- }
- }
- }
- }
- return true;
- });
- var result = '';
- if (title_1) {
- result = toMarkdown(title_1);
- }
- if (markdownDescription_1) {
- if (result.length > 0) {
- result += "\n\n";
- }
- result += markdownDescription_1;
- }
- if (markdownEnumValueDescription_1) {
- if (result.length > 0) {
- result += "\n\n";
- }
- result += "`" + toMarkdownCodeBlock(enumValue_1) + "`: " + markdownEnumValueDescription_1;
- }
- return createHover([result]);
- }
- return null;
- });
- };
- return JSONHover;
- }());
- export { JSONHover };
- function toMarkdown(plain) {
- if (plain) {
- var 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 undefined;
- }
- function toMarkdownCodeBlock(content) {
- // see https://daringfireball.net/projects/markdown/syntax#precode
- if (content.indexOf('`') !== -1) {
- return '`` ' + content + ' ``';
- }
- return content;
- }
|