Skip to content

Commit a4e820b

Browse files
committed
Use extensionFactory for highlights feature
1 parent fe97658 commit a4e820b

File tree

1 file changed

+26
-51
lines changed

1 file changed

+26
-51
lines changed

packages/jupyterlab-lsp/src/features/highlights.ts

Lines changed: 26 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ import {
66
import { CodeEditor } from '@jupyterlab/codeeditor';
77
import {
88
CodeMirrorEditor,
9-
IEditorExtensionRegistry,
109
EditorExtensionRegistry
1110
} from '@jupyterlab/codemirror';
1211
import {
1312
IVirtualPosition,
1413
ILSPFeatureManager,
1514
IEditorPosition,
1615
ILSPDocumentConnectionManager,
17-
WidgetLSPAdapter
16+
WidgetLSPAdapter,
17+
Document
1818
} from '@jupyterlab/lsp';
1919
import { ISettingRegistry } from '@jupyterlab/settingregistry';
2020
import { LabIcon } from '@jupyterlab/ui-components';
@@ -23,7 +23,6 @@ import type * as lsProtocol from 'vscode-languageserver-protocol';
2323

2424
import highlightSvg from '../../style/icons/highlight.svg';
2525
import { CodeHighlights as LSPHighlightsSettings } from '../_highlights';
26-
import { ContextAssembler } from '../context';
2726
import {
2827
PositionConverter,
2928
rootPositionToVirtualPosition,
@@ -78,7 +77,6 @@ export class HighlightsFeature extends Feature {
7877
constructor(options: HighlightsFeature.IOptions) {
7978
super(options);
8079
this.settings = options.settings;
81-
const connectionManager = options.connectionManager;
8280
this.markManager = createMarkManager({
8381
[DocumentHighlightKind.Text]: { class: 'cm-lsp-highlight-Text' },
8482
[DocumentHighlightKind.Read]: { class: 'cm-lsp-highlight-Read' },
@@ -91,63 +89,43 @@ export class HighlightsFeature extends Feature {
9189
this._debouncedGetHighlight = this.createDebouncer();
9290
});
9391

94-
options.editorExtensionRegistry.addExtension({
92+
this.extensionFactory = {
9593
name: 'lsp:highlights',
96-
factory: options => {
94+
factory: factoryOptions => {
95+
const { editor: editorAccessor, widgetAdapter: adapter } =
96+
factoryOptions;
9797
const updateListener = EditorView.updateListener.of(viewUpdate => {
9898
if (
9999
viewUpdate.docChanged ||
100100
viewUpdate.selectionSet ||
101101
viewUpdate.focusChanged
102102
) {
103-
// TODO a better way to get the adapter here?
104-
const adapter = [...connectionManager.adapters.values()].find(
105-
adapter => adapter.widget.node.contains(viewUpdate.view.dom)
103+
this.onCursorActivity(editorAccessor, adapter).catch(
104+
this.console.warn
106105
);
107-
if (!adapter) {
108-
this.console.warn('Adapter not found');
109-
return;
110-
}
111-
this.onCursorActivity(adapter).catch(this.console.warn);
112106
}
113107
});
114108
const eventListeners = EditorView.domEventHandlers({
115-
blur: (e, view) => {
109+
blur: (_, view) => {
116110
this.onBlur(view);
117111
},
118-
focus: event => {
119-
const adapter = [...connectionManager.adapters.values()].find(
120-
adapter =>
121-
adapter.widget.node.contains(
122-
event.currentTarget! as HTMLElement
123-
)
112+
focus: () => {
113+
this.onCursorActivity(editorAccessor, adapter).catch(
114+
this.console.warn
124115
);
125-
if (!adapter) {
126-
this.console.warn('Adapter not found');
127-
return;
128-
}
129-
this.onCursorActivity(adapter).catch(this.console.warn);
130116
},
131-
keydown: event => {
132-
const adapter = [...connectionManager.adapters.values()].find(
133-
adapter =>
134-
adapter.widget.node.contains(
135-
event.currentTarget! as HTMLElement
136-
)
117+
keydown: () => {
118+
this.onCursorActivity(editorAccessor, adapter).catch(
119+
this.console.warn
137120
);
138-
if (!adapter) {
139-
this.console.warn('Adapter not found');
140-
return;
141-
}
142-
this.onCursorActivity(adapter).catch(this.console.warn);
143121
}
144122
});
145123
return EditorExtensionRegistry.createImmutableExtension([
146124
updateListener,
147125
eventListeners
148126
]);
149127
}
150-
});
128+
};
151129
}
152130

153131
protected onBlur(view: EditorView) {
@@ -260,17 +238,21 @@ export class HighlightsFeature extends Feature {
260238
});
261239
};
262240

263-
protected async onCursorActivity(adapter: WidgetLSPAdapter<any>) {
241+
protected async onCursorActivity(
242+
editorAccessor: Document.IEditor,
243+
adapter: WidgetLSPAdapter<any>
244+
) {
264245
if (!adapter.virtualDocument) {
265246
this.console.log('virtualDocument not ready on adapter');
266247
return;
267248
}
268249
await adapter.virtualDocument!.updateManager.updateDone;
269250

270-
// TODO: this is the same problem as in signature
271-
// TODO: the assumption that updated editor = active editor will fail on RTC. How to get `CodeEditor.IEditor` and `Document.IEditor` from `EditorView`? we got `CodeEditor.IModel` from `options.model` but may need more context here.
272-
const editorAccessor = adapter.activeEditor;
273-
const editor = editorAccessor!.getEditor()!;
251+
const editor = editorAccessor.getEditor();
252+
if (!editor) {
253+
this.console.log('editor not found ready');
254+
return;
255+
}
274256
const position = editor.getCursorPosition();
275257
const editorPosition = PositionConverter.ce_to_cm(
276258
position
@@ -370,8 +352,6 @@ export class HighlightsFeature extends Feature {
370352
export namespace HighlightsFeature {
371353
export interface IOptions extends Feature.IOptions {
372354
settings: FeatureSettings<LSPHighlightsSettings>;
373-
editorExtensionRegistry: IEditorExtensionRegistry;
374-
contextAssembler: ContextAssembler;
375355
}
376356
export const id = PLUGIN_ID + ':highlights';
377357
}
@@ -381,18 +361,15 @@ export const HIGHLIGHTS_PLUGIN: JupyterFrontEndPlugin<void> = {
381361
requires: [
382362
ILSPFeatureManager,
383363
ISettingRegistry,
384-
IEditorExtensionRegistry,
385364
ILSPDocumentConnectionManager
386365
],
387366
autoStart: true,
388367
activate: async (
389368
app: JupyterFrontEnd,
390369
featureManager: ILSPFeatureManager,
391370
settingRegistry: ISettingRegistry,
392-
editorExtensionRegistry: IEditorExtensionRegistry,
393371
connectionManager: ILSPDocumentConnectionManager
394372
) => {
395-
const contextAssembler = new ContextAssembler({ app, connectionManager });
396373
const settings = new FeatureSettings<LSPHighlightsSettings>(
397374
settingRegistry,
398375
HighlightsFeature.id
@@ -403,9 +380,7 @@ export const HIGHLIGHTS_PLUGIN: JupyterFrontEndPlugin<void> = {
403380
}
404381
const feature = new HighlightsFeature({
405382
settings,
406-
editorExtensionRegistry,
407-
connectionManager,
408-
contextAssembler
383+
connectionManager
409384
});
410385
featureManager.register(feature);
411386
}

0 commit comments

Comments
 (0)