UNPKG

@jupyter-lsp/jupyterlab-lsp

Version:

Language Server Protocol integration for JupyterLab

141 lines 4.91 kB
import * as lsProtocol from 'vscode-languageserver-types'; export class CompletionItem { get isDocumentationMarkdown() { return this._isDocumentationMarkdown; } constructor(options) { this.options = options; const match = options.match; this.label = match.label; this._setDocumentation(match.documentation); this._resolved = false; this._detail = match.detail; this._match = match; this.self = this; this.source = options.source; this.icon = options.icon ? options.icon : undefined; // Upstream is sometimes using spread operator to copy the object (in reconciliator), // which does not copy getters because these are not enumerable; we should use // `Object.assign` upstream, but them, but for now marking relevant properties as enumerable is enough // Ideally this would be fixed and tested e2e in JupyterLab 4.0.7. // https://github.com/jupyterlab/jupyterlab/issues/15125 makeGetterEnumerable(this, 'insertText'); makeGetterEnumerable(this, 'sortText'); makeGetterEnumerable(this, 'filterText'); } get type() { return this.options.type; } /** * Completion to be inserted. */ get insertText() { return (this._currentInsertText || this._match.insertText || this._match.label); } set insertText(text) { this._currentInsertText = text; } get sortText() { return this._currentSortText || this._match.sortText || this._match.label; } set sortText(text) { this._currentSortText = text; } get filterText() { return this._match.filterText; } set filterText(text) { this._match.filterText = text; } get detail() { return this._detail; } needsResolution() { if (this.documentation) { return false; } if (this._resolved) { return false; } return this._supportsResolution(); } isResolved() { return this._resolved; } /** * Resolve (fetch) details such as documentation. */ async resolve() { if (this._resolved) { return this; } if (!this._supportsResolution()) { return this; } const connection = this.options.connection; const resolvedCompletionItem = await connection.clientRequests['completionItem/resolve'].request(this._match); if (resolvedCompletionItem === null) { return this; } this._setDocumentation(resolvedCompletionItem === null || resolvedCompletionItem === void 0 ? void 0 : resolvedCompletionItem.documentation); this._detail = resolvedCompletionItem === null || resolvedCompletionItem === void 0 ? void 0 : resolvedCompletionItem.detail; // TODO: implement in pylsp and enable with proper LSP communication // this.label = resolvedCompletionItem.label; this._resolved = true; return this; } /** * A human-readable string with additional information * about this item, like type or symbol information. */ get documentation() { if (this._documentation) { return this._documentation; } return undefined; } /** * Indicates if the item is deprecated. */ get deprecated() { if (this._match.deprecated) { return this._match.deprecated; } return (this._match.tags != null && this._match.tags.some(tag => tag == lsProtocol.CompletionItemTag.Deprecated)); } _setDocumentation(documentation) { if (lsProtocol.MarkupContent.is(documentation)) { this._documentation = documentation.value; this._isDocumentationMarkdown = documentation.kind === 'markdown'; } else { this._documentation = documentation; this._isDocumentationMarkdown = false; } } _supportsResolution() { var _a, _b; const connection = this.options.connection; return ((_b = (_a = connection.serverCapabilities.completionProvider) === null || _a === void 0 ? void 0 : _a.resolveProvider) !== null && _b !== void 0 ? _b : false); } } function makeGetterEnumerable(instance, name) { const generatedDescriptor = findDescriptor(instance, name); Object.defineProperty(instance, name, { enumerable: true, get: generatedDescriptor.get, set: generatedDescriptor.set }); } function findDescriptor(instance, name) { while (instance) { const desc = Object.getOwnPropertyDescriptor(instance, name); if (desc) { return desc; } instance = Object.getPrototypeOf(instance); } throw Error(`No ${name} descriptor found.`); } //# sourceMappingURL=item.js.map