Skip to content

Commit fe97658

Browse files
committed
Take advantage of new LSP extension factory for syntax highlight feature
1 parent 0eebf0c commit fe97658

File tree

2 files changed

+32
-82
lines changed

2 files changed

+32
-82
lines changed

packages/jupyterlab-lsp/src/feature.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import { IFeature, ILSPDocumentConnectionManager } from '@jupyterlab/lsp';
1+
import {
2+
IFeature,
3+
ILSPDocumentConnectionManager,
4+
EditorAdapter
5+
} from '@jupyterlab/lsp';
26
import { ISettingRegistry } from '@jupyterlab/settingregistry';
37
import { PromiseDelegate } from '@lumino/coreutils';
48
import { Signal } from '@lumino/signaling';
@@ -19,17 +23,25 @@ export namespace Feature {
1923
}
2024

2125
export abstract class Feature implements IFeature {
26+
/**
27+
* The feature identifier. It must be the same as the feature plugin id.
28+
*/
2229
abstract readonly id: string;
30+
/**
31+
* LSP capabilities implemented by the feature.
32+
*/
2333
abstract readonly capabilities?: lsProtocol.ClientCapabilities;
34+
35+
/**
36+
* Editor extension factory linked to the LSP feature.
37+
*/
38+
extensionFactory?: EditorAdapter.ILSPEditorExtensionFactory = undefined;
39+
2440
protected connectionManager: ILSPDocumentConnectionManager;
2541

2642
constructor(options: Feature.IOptions) {
2743
this.connectionManager = options.connectionManager;
2844
}
29-
30-
//getConnection(): ILSPConnection {
31-
32-
//}
3345
}
3446

3547
export class FeatureSettings<T> implements IFeatureSettings<T> {

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

Lines changed: 15 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { EditorView } from '@codemirror/view';
2-
import type { ViewUpdate } from '@codemirror/view';
32
import {
43
JupyterFrontEnd,
54
JupyterFrontEndPlugin
@@ -10,7 +9,6 @@ import {
109
} from '@jupyterlab/codeeditor';
1110
import {
1211
CodeMirrorEditor,
13-
IEditorExtensionRegistry,
1412
IEditorLanguageRegistry,
1513
EditorExtensionRegistry
1614
} from '@jupyterlab/codemirror';
@@ -25,7 +23,6 @@ import { LabIcon } from '@jupyterlab/ui-components';
2523

2624
import syntaxSvg from '../../style/icons/syntax-highlight.svg';
2725
import { CodeSyntax as LSPSyntaxHighlightingSettings } from '../_syntax_highlighting';
28-
import { ContextAssembler } from '../context';
2926
import { FeatureSettings, Feature } from '../feature';
3027
import { PLUGIN_ID } from '../tokens';
3128
import { VirtualDocument } from '../virtual/document';
@@ -43,81 +40,33 @@ export class SyntaxHighlightingFeature extends Feature {
4340

4441
constructor(protected options: SyntaxHighlightingFeature.IOptions) {
4542
super(options);
46-
const connectionManager = options.connectionManager;
47-
const contextAssembler = options.contextAssembler;
4843

49-
options.editorExtensionRegistry.addExtension({
44+
this.extensionFactory = {
5045
name: 'lsp:syntaxHighlighting',
51-
factory: options => {
52-
let intialized = false;
46+
factory: factoryOptions => {
47+
const { editor: editorAccessor, widgetAdapter: adapter } =
48+
factoryOptions;
5349

54-
const updateHandler = async (
55-
viewUpdate: ViewUpdate,
56-
awaitUpdate = true
57-
) => {
58-
const adapter = [...connectionManager.adapters.values()].find(
59-
adapter => adapter.widget.node.contains(viewUpdate.view.contentDOM)
60-
);
50+
const updateHandler = async (awaitUpdate = true) => {
51+
await adapter.ready;
6152

62-
// TODO https://github.com/jupyterlab/jupyterlab/issues/14711#issuecomment-1624442627
63-
// const editor = adapter.editors.find(e => e.model === options.model);
64-
65-
if (adapter) {
66-
await adapter.ready;
67-
const accessorFromNode = contextAssembler.editorFromNode(
68-
adapter,
69-
viewUpdate.view.contentDOM
70-
);
71-
if (!accessorFromNode) {
72-
console.warn(
73-
'Editor accessor not found from node, falling back to activeEditor'
74-
);
75-
}
76-
const editorAccessor = accessorFromNode
77-
? accessorFromNode
78-
: adapter.activeEditor;
79-
80-
if (!editorAccessor) {
81-
console.warn('No accessor');
82-
return;
83-
}
84-
await this.updateMode(
85-
adapter,
86-
viewUpdate.view,
87-
editorAccessor,
88-
awaitUpdate
89-
);
90-
}
53+
await this.updateMode(adapter, editorAccessor, awaitUpdate);
9154
};
9255

93-
const updateListener = EditorView.updateListener.of(
94-
async viewUpdate => {
95-
if (!viewUpdate.docChanged) {
96-
if (intialized) {
97-
return;
98-
}
99-
// TODO: replace this with a simple Promise.all([editorAccessor.ready, adapter.ready]).then(() => updateMode(options.editor))
100-
// once JupyterLab 4.1 with improved factory API is out.
101-
// For now we wait 2.5 seconds hoping the adapter will be connected
102-
// and the document will be ready
103-
setTimeout(async () => {
104-
await updateHandler(viewUpdate, false);
105-
}, 2500);
106-
intialized = true;
107-
}
108-
109-
await updateHandler(viewUpdate);
110-
}
111-
);
56+
const updateListener = EditorView.updateListener.of(async () => {
57+
await updateHandler();
58+
});
59+
Promise.all([editorAccessor.ready, adapter.ready])
60+
.then(() => updateHandler(false))
61+
.catch(console.warn);
11262

11363
// update the mode at first update even if no changes to ensure the
11464
// correct mode gets applied on load.
115-
11665
return EditorExtensionRegistry.createImmutableExtension([
11766
updateListener
11867
]);
11968
}
120-
});
69+
};
12170
}
12271

12372
private getMode(language: string): string | undefined {
@@ -149,7 +98,6 @@ export class SyntaxHighlightingFeature extends Feature {
14998

15099
async updateMode(
151100
adapter: WidgetLSPAdapter<any>,
152-
view: EditorView,
153101
editorAccessor: Document.IEditor,
154102
awaitUpdate = true
155103
) {
@@ -215,9 +163,7 @@ export namespace SyntaxHighlightingFeature {
215163
export interface IOptions extends Feature.IOptions {
216164
settings: FeatureSettings<LSPSyntaxHighlightingSettings>;
217165
mimeTypeService: IEditorMimeTypeService;
218-
editorExtensionRegistry: IEditorExtensionRegistry;
219166
languageRegistry: IEditorLanguageRegistry;
220-
contextAssembler: ContextAssembler;
221167
}
222168
export const id = PLUGIN_ID + ':syntax_highlighting';
223169
}
@@ -228,7 +174,6 @@ export const SYNTAX_HIGHLIGHTING_PLUGIN: JupyterFrontEndPlugin<void> = {
228174
ILSPFeatureManager,
229175
IEditorServices,
230176
ISettingRegistry,
231-
IEditorExtensionRegistry,
232177
IEditorLanguageRegistry,
233178
ILSPDocumentConnectionManager
234179
],
@@ -238,7 +183,6 @@ export const SYNTAX_HIGHLIGHTING_PLUGIN: JupyterFrontEndPlugin<void> = {
238183
featureManager: ILSPFeatureManager,
239184
editorServices: IEditorServices,
240185
settingRegistry: ISettingRegistry,
241-
editorExtensionRegistry: IEditorExtensionRegistry,
242186
languageRegistry: IEditorLanguageRegistry,
243187
connectionManager: ILSPDocumentConnectionManager
244188
) => {
@@ -250,17 +194,11 @@ export const SYNTAX_HIGHLIGHTING_PLUGIN: JupyterFrontEndPlugin<void> = {
250194
if (settings.composite.disable) {
251195
return;
252196
}
253-
const contextAssembler = new ContextAssembler({
254-
app,
255-
connectionManager
256-
});
257197
const feature = new SyntaxHighlightingFeature({
258198
settings,
259199
connectionManager,
260-
editorExtensionRegistry,
261200
mimeTypeService: editorServices.mimeTypeService,
262-
languageRegistry,
263-
contextAssembler
201+
languageRegistry
264202
});
265203
featureManager.register(feature);
266204
// return feature;

0 commit comments

Comments
 (0)