langium
Version:
A language engineering tool for the Language Server Protocol
66 lines • 2.74 kB
JavaScript
/******************************************************************************
* Copyright 2023 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 { getDocument } from '../utils/ast-utils.js';
import { isJSDoc, parseJSDoc } from './jsdoc.js';
export class JSDocDocumentationProvider {
constructor(services) {
this.indexManager = services.shared.workspace.IndexManager;
this.commentProvider = services.documentation.CommentProvider;
}
getDocumentation(node) {
const comment = this.commentProvider.getComment(node);
if (comment && isJSDoc(comment)) {
const parsedJSDoc = parseJSDoc(comment);
return parsedJSDoc.toMarkdown({
renderLink: (link, display) => {
return this.documentationLinkRenderer(node, link, display);
},
renderTag: (tag) => {
return this.documentationTagRenderer(node, tag);
}
});
}
return undefined;
}
documentationLinkRenderer(node, name, display) {
const description = this.findNameInLocalSymbols(node, name) ?? this.findNameInGlobalScope(node, name);
if (description && description.nameSegment) {
const line = description.nameSegment.range.start.line + 1;
const character = description.nameSegment.range.start.character + 1;
const uri = description.documentUri.with({ fragment: `L${line},${character}` });
return `[${display}](${uri.toString()})`;
}
else {
return undefined;
}
}
documentationTagRenderer(_node, _tag) {
// Fall back to the default tag rendering
return undefined;
}
findNameInLocalSymbols(node, name) {
const document = getDocument(node);
const precomputed = document.localSymbols;
if (!precomputed) {
return undefined;
}
let currentNode = node;
do {
const allDescriptions = precomputed.getStream(currentNode);
const description = allDescriptions.find(e => e.name === name);
if (description) {
return description;
}
currentNode = currentNode.$container;
} while (currentNode);
return undefined;
}
findNameInGlobalScope(node, name) {
const description = this.indexManager.allElements().find(e => e.name === name);
return description;
}
}
//# sourceMappingURL=documentation-provider.js.map