Skip to content

Commit

Permalink
Take advantage of new LSP extension factory for syntax highlight feature
Browse files Browse the repository at this point in the history
  • Loading branch information
krassowski committed Feb 24, 2024
1 parent 0eebf0c commit fe97658
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 82 deletions.
22 changes: 17 additions & 5 deletions packages/jupyterlab-lsp/src/feature.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { IFeature, ILSPDocumentConnectionManager } from '@jupyterlab/lsp';
import {
IFeature,
ILSPDocumentConnectionManager,
EditorAdapter
} from '@jupyterlab/lsp';
import { ISettingRegistry } from '@jupyterlab/settingregistry';
import { PromiseDelegate } from '@lumino/coreutils';
import { Signal } from '@lumino/signaling';
Expand All @@ -19,17 +23,25 @@ export namespace Feature {
}

export abstract class Feature implements IFeature {
/**
* The feature identifier. It must be the same as the feature plugin id.
*/
abstract readonly id: string;
/**
* LSP capabilities implemented by the feature.
*/
abstract readonly capabilities?: lsProtocol.ClientCapabilities;

/**
* Editor extension factory linked to the LSP feature.
*/
extensionFactory?: EditorAdapter.ILSPEditorExtensionFactory = undefined;

protected connectionManager: ILSPDocumentConnectionManager;

constructor(options: Feature.IOptions) {
this.connectionManager = options.connectionManager;
}

//getConnection(): ILSPConnection {

//}
}

export class FeatureSettings<T> implements IFeatureSettings<T> {
Expand Down
92 changes: 15 additions & 77 deletions packages/jupyterlab-lsp/src/features/syntax_highlighting.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { EditorView } from '@codemirror/view';
import type { ViewUpdate } from '@codemirror/view';
import {
JupyterFrontEnd,
JupyterFrontEndPlugin
Expand All @@ -10,7 +9,6 @@ import {
} from '@jupyterlab/codeeditor';
import {
CodeMirrorEditor,
IEditorExtensionRegistry,
IEditorLanguageRegistry,
EditorExtensionRegistry
} from '@jupyterlab/codemirror';
Expand All @@ -25,7 +23,6 @@ import { LabIcon } from '@jupyterlab/ui-components';

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

constructor(protected options: SyntaxHighlightingFeature.IOptions) {
super(options);
const connectionManager = options.connectionManager;
const contextAssembler = options.contextAssembler;

options.editorExtensionRegistry.addExtension({
this.extensionFactory = {
name: 'lsp:syntaxHighlighting',
factory: options => {
let intialized = false;
factory: factoryOptions => {
const { editor: editorAccessor, widgetAdapter: adapter } =
factoryOptions;

const updateHandler = async (
viewUpdate: ViewUpdate,
awaitUpdate = true
) => {
const adapter = [...connectionManager.adapters.values()].find(
adapter => adapter.widget.node.contains(viewUpdate.view.contentDOM)
);
const updateHandler = async (awaitUpdate = true) => {
await adapter.ready;

// TODO https://github.com/jupyterlab/jupyterlab/issues/14711#issuecomment-1624442627
// const editor = adapter.editors.find(e => e.model === options.model);

if (adapter) {
await adapter.ready;
const accessorFromNode = contextAssembler.editorFromNode(
adapter,
viewUpdate.view.contentDOM
);
if (!accessorFromNode) {
console.warn(
'Editor accessor not found from node, falling back to activeEditor'
);
}
const editorAccessor = accessorFromNode
? accessorFromNode
: adapter.activeEditor;

if (!editorAccessor) {
console.warn('No accessor');
return;
}
await this.updateMode(
adapter,
viewUpdate.view,
editorAccessor,
awaitUpdate
);
}
await this.updateMode(adapter, editorAccessor, awaitUpdate);
};

const updateListener = EditorView.updateListener.of(
async viewUpdate => {
if (!viewUpdate.docChanged) {
if (intialized) {
return;
}
// TODO: replace this with a simple Promise.all([editorAccessor.ready, adapter.ready]).then(() => updateMode(options.editor))
// once JupyterLab 4.1 with improved factory API is out.
// For now we wait 2.5 seconds hoping the adapter will be connected
// and the document will be ready
setTimeout(async () => {
await updateHandler(viewUpdate, false);
}, 2500);
intialized = true;
}

await updateHandler(viewUpdate);
}
);
const updateListener = EditorView.updateListener.of(async () => {
await updateHandler();
});
Promise.all([editorAccessor.ready, adapter.ready])
.then(() => updateHandler(false))
.catch(console.warn);

// update the mode at first update even if no changes to ensure the
// correct mode gets applied on load.

return EditorExtensionRegistry.createImmutableExtension([
updateListener
]);
}
});
};
}

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

async updateMode(
adapter: WidgetLSPAdapter<any>,
view: EditorView,
editorAccessor: Document.IEditor,
awaitUpdate = true
) {
Expand Down Expand Up @@ -215,9 +163,7 @@ export namespace SyntaxHighlightingFeature {
export interface IOptions extends Feature.IOptions {
settings: FeatureSettings<LSPSyntaxHighlightingSettings>;
mimeTypeService: IEditorMimeTypeService;
editorExtensionRegistry: IEditorExtensionRegistry;
languageRegistry: IEditorLanguageRegistry;
contextAssembler: ContextAssembler;
}
export const id = PLUGIN_ID + ':syntax_highlighting';
}
Expand All @@ -228,7 +174,6 @@ export const SYNTAX_HIGHLIGHTING_PLUGIN: JupyterFrontEndPlugin<void> = {
ILSPFeatureManager,
IEditorServices,
ISettingRegistry,
IEditorExtensionRegistry,
IEditorLanguageRegistry,
ILSPDocumentConnectionManager
],
Expand All @@ -238,7 +183,6 @@ export const SYNTAX_HIGHLIGHTING_PLUGIN: JupyterFrontEndPlugin<void> = {
featureManager: ILSPFeatureManager,
editorServices: IEditorServices,
settingRegistry: ISettingRegistry,
editorExtensionRegistry: IEditorExtensionRegistry,
languageRegistry: IEditorLanguageRegistry,
connectionManager: ILSPDocumentConnectionManager
) => {
Expand All @@ -250,17 +194,11 @@ export const SYNTAX_HIGHLIGHTING_PLUGIN: JupyterFrontEndPlugin<void> = {
if (settings.composite.disable) {
return;
}
const contextAssembler = new ContextAssembler({
app,
connectionManager
});
const feature = new SyntaxHighlightingFeature({
settings,
connectionManager,
editorExtensionRegistry,
mimeTypeService: editorServices.mimeTypeService,
languageRegistry,
contextAssembler
languageRegistry
});
featureManager.register(feature);
// return feature;
Expand Down

0 comments on commit fe97658

Please sign in to comment.