@jupyter-lsp/jupyterlab-lsp
Version:
Language Server Protocol integration for JupyterLab
90 lines • 3.69 kB
JavaScript
import { KernelKind } from '@jupyter-lsp/completion-theme';
import { ContextCompleterProvider, KernelCompleterProvider } from '@jupyterlab/completer';
import { editorPositionToRootPosition, PositionConverter, documentAtRootPosition } from '../../converter';
export class EnhancedContextCompleterProvider extends ContextCompleterProvider {
constructor(options) {
super();
this.options = options;
this.label = 'context';
}
async fetch(request, context) {
const result = await super.fetch(request, context);
result.items = result.items.map((item, order) => {
var _a, _b;
return {
...item,
icon: (_b = this.iconFor((_a = item.type) !== null && _a !== void 0 ? _a : 'Text')) !== null && _b !== void 0 ? _b : this.iconFor('Text'),
type: item.type === '<unknown>' ? undefined : item.type,
sortText: String.fromCharCode(order),
source: this.label
};
});
return result;
}
iconFor(type) {
const icon = this.options.iconsThemeManager.getIcon(type);
return icon ? icon : undefined;
}
}
export class EnhancedKernelCompleterProvider extends KernelCompleterProvider {
constructor(options) {
super();
this.options = options;
this.label = 'kernel';
}
async fetch(request, context) {
const result = await super.fetch(request, context);
result.items = result.items.map((item, order) => {
var _a, _b;
return {
...item,
icon: (_b = this.iconFor((_a = item.type) !== null && _a !== void 0 ? _a : KernelKind)) !== null && _b !== void 0 ? _b : this.iconFor(KernelKind),
sortText: String.fromCharCode(order),
source: this.label
};
});
return result;
}
async isApplicable(context) {
// Note: this method logs errors instead of throwing to ensure we do not ever
// break the upstream kernel completer, even if there is an error elsewhere.
const upstream = await super.isApplicable(context);
if (upstream === false) {
return false;
}
const manager = this.options.connectionManager;
const widget = context.widget;
if (typeof widget.context === 'undefined') {
// there is no path for Console as it is not a DocumentWidget
return upstream;
}
const adapter = manager.adapters.get(widget.context.path);
if (!adapter) {
return upstream;
}
if (!context.editor) {
// TODO: why is editor optional in the first place?
console.error('No editor');
return upstream;
}
const editor = context.editor;
const editorPosition = PositionConverter.ce_to_cm(editor.getCursorPosition());
const block = adapter.editors.find(value => value.ceEditor.getEditor() == editor);
if (!block) {
console.error('Could not get block with editor');
return upstream;
}
const rootPosition = editorPositionToRootPosition(adapter, block.ceEditor, editorPosition);
if (!rootPosition) {
console.error('Could not get root position');
return upstream;
}
const virtualDocument = documentAtRootPosition(adapter, rootPosition);
return virtualDocument === adapter.virtualDocument;
}
iconFor(type) {
const icon = this.options.iconsThemeManager.getIcon(type);
return icon ? icon : undefined;
}
}
//# sourceMappingURL=overrides.js.map