UNPKG

langium

Version:

A language engineering tool for the Language Server Protocol

72 lines 3.01 kB
/****************************************************************************** * Copyright 2021 TypeFox GmbH * This program and the accompanying materials are made available under the * terms of the MIT License, which is available in the project root. ******************************************************************************/ import { findCommentNode, findDeclarationNodeAtOffset } from '../utils/cst-utils.js'; import { isKeyword } from '../languages/generated/ast.js'; import { isJSDoc, parseJSDoc } from '../documentation/jsdoc.js'; import { isAstNodeWithComment } from '../serializer/json-serializer.js'; export class AstNodeHoverProvider { constructor(services) { this.references = services.references.References; this.grammarConfig = services.parser.GrammarConfig; } getHoverContent(document, params) { var _a, _b; const rootNode = (_b = (_a = document.parseResult) === null || _a === void 0 ? void 0 : _a.value) === null || _b === void 0 ? void 0 : _b.$cstNode; if (rootNode) { const offset = document.textDocument.offsetAt(params.position); const cstNode = findDeclarationNodeAtOffset(rootNode, offset, this.grammarConfig.nameRegexp); if (cstNode && cstNode.offset + cstNode.length > offset) { const targetNode = this.references.findDeclaration(cstNode); if (targetNode) { return this.getAstNodeHoverContent(targetNode); } // Add support for documentation on keywords if (isKeyword(cstNode.grammarSource)) { return this.getKeywordHoverContent(cstNode.grammarSource); } } } return undefined; } getKeywordHoverContent(node) { var _a; let comment = isAstNodeWithComment(node) ? node.$comment : undefined; if (!comment) { comment = (_a = findCommentNode(node.$cstNode, ['ML_COMMENT'])) === null || _a === void 0 ? void 0 : _a.text; } if (comment && isJSDoc(comment)) { const content = parseJSDoc(comment).toMarkdown(); if (content) { return { contents: { kind: 'markdown', value: content } }; } } return undefined; } } export class MultilineCommentHoverProvider extends AstNodeHoverProvider { constructor(services) { super(services); this.documentationProvider = services.documentation.DocumentationProvider; } getAstNodeHoverContent(node) { const content = this.documentationProvider.getDocumentation(node); if (content) { return { contents: { kind: 'markdown', value: content } }; } return undefined; } } //# sourceMappingURL=hover-provider.js.map