11import { EditorView } from '@codemirror/view' ;
2- import type { ViewUpdate } from '@codemirror/view' ;
32import {
43 JupyterFrontEnd ,
54 JupyterFrontEndPlugin
@@ -10,7 +9,6 @@ import {
109} from '@jupyterlab/codeeditor' ;
1110import {
1211 CodeMirrorEditor ,
13- IEditorExtensionRegistry ,
1412 IEditorLanguageRegistry ,
1513 EditorExtensionRegistry
1614} from '@jupyterlab/codemirror' ;
@@ -25,7 +23,6 @@ import { LabIcon } from '@jupyterlab/ui-components';
2523
2624import syntaxSvg from '../../style/icons/syntax-highlight.svg' ;
2725import { CodeSyntax as LSPSyntaxHighlightingSettings } from '../_syntax_highlighting' ;
28- import { ContextAssembler } from '../context' ;
2926import { FeatureSettings , Feature } from '../feature' ;
3027import { PLUGIN_ID } from '../tokens' ;
3128import { 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