1
1
import { EditorView } from '@codemirror/view' ;
2
- import type { ViewUpdate } from '@codemirror/view' ;
3
2
import {
4
3
JupyterFrontEnd ,
5
4
JupyterFrontEndPlugin
@@ -10,7 +9,6 @@ import {
10
9
} from '@jupyterlab/codeeditor' ;
11
10
import {
12
11
CodeMirrorEditor ,
13
- IEditorExtensionRegistry ,
14
12
IEditorLanguageRegistry ,
15
13
EditorExtensionRegistry
16
14
} from '@jupyterlab/codemirror' ;
@@ -25,7 +23,6 @@ import { LabIcon } from '@jupyterlab/ui-components';
25
23
26
24
import syntaxSvg from '../../style/icons/syntax-highlight.svg' ;
27
25
import { CodeSyntax as LSPSyntaxHighlightingSettings } from '../_syntax_highlighting' ;
28
- import { ContextAssembler } from '../context' ;
29
26
import { FeatureSettings , Feature } from '../feature' ;
30
27
import { PLUGIN_ID } from '../tokens' ;
31
28
import { VirtualDocument } from '../virtual/document' ;
@@ -43,81 +40,33 @@ export class SyntaxHighlightingFeature extends Feature {
43
40
44
41
constructor ( protected options : SyntaxHighlightingFeature . IOptions ) {
45
42
super ( options ) ;
46
- const connectionManager = options . connectionManager ;
47
- const contextAssembler = options . contextAssembler ;
48
43
49
- options . editorExtensionRegistry . addExtension ( {
44
+ this . extensionFactory = {
50
45
name : 'lsp:syntaxHighlighting' ,
51
- factory : options => {
52
- let intialized = false ;
46
+ factory : factoryOptions => {
47
+ const { editor : editorAccessor , widgetAdapter : adapter } =
48
+ factoryOptions ;
53
49
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 ;
61
52
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 ) ;
91
54
} ;
92
55
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 ) ;
112
62
113
63
// update the mode at first update even if no changes to ensure the
114
64
// correct mode gets applied on load.
115
-
116
65
return EditorExtensionRegistry . createImmutableExtension ( [
117
66
updateListener
118
67
] ) ;
119
68
}
120
- } ) ;
69
+ } ;
121
70
}
122
71
123
72
private getMode ( language : string ) : string | undefined {
@@ -149,7 +98,6 @@ export class SyntaxHighlightingFeature extends Feature {
149
98
150
99
async updateMode (
151
100
adapter : WidgetLSPAdapter < any > ,
152
- view : EditorView ,
153
101
editorAccessor : Document . IEditor ,
154
102
awaitUpdate = true
155
103
) {
@@ -215,9 +163,7 @@ export namespace SyntaxHighlightingFeature {
215
163
export interface IOptions extends Feature . IOptions {
216
164
settings : FeatureSettings < LSPSyntaxHighlightingSettings > ;
217
165
mimeTypeService : IEditorMimeTypeService ;
218
- editorExtensionRegistry : IEditorExtensionRegistry ;
219
166
languageRegistry : IEditorLanguageRegistry ;
220
- contextAssembler : ContextAssembler ;
221
167
}
222
168
export const id = PLUGIN_ID + ':syntax_highlighting' ;
223
169
}
@@ -228,7 +174,6 @@ export const SYNTAX_HIGHLIGHTING_PLUGIN: JupyterFrontEndPlugin<void> = {
228
174
ILSPFeatureManager ,
229
175
IEditorServices ,
230
176
ISettingRegistry ,
231
- IEditorExtensionRegistry ,
232
177
IEditorLanguageRegistry ,
233
178
ILSPDocumentConnectionManager
234
179
] ,
@@ -238,7 +183,6 @@ export const SYNTAX_HIGHLIGHTING_PLUGIN: JupyterFrontEndPlugin<void> = {
238
183
featureManager : ILSPFeatureManager ,
239
184
editorServices : IEditorServices ,
240
185
settingRegistry : ISettingRegistry ,
241
- editorExtensionRegistry : IEditorExtensionRegistry ,
242
186
languageRegistry : IEditorLanguageRegistry ,
243
187
connectionManager : ILSPDocumentConnectionManager
244
188
) => {
@@ -250,17 +194,11 @@ export const SYNTAX_HIGHLIGHTING_PLUGIN: JupyterFrontEndPlugin<void> = {
250
194
if ( settings . composite . disable ) {
251
195
return ;
252
196
}
253
- const contextAssembler = new ContextAssembler ( {
254
- app,
255
- connectionManager
256
- } ) ;
257
197
const feature = new SyntaxHighlightingFeature ( {
258
198
settings,
259
199
connectionManager,
260
- editorExtensionRegistry,
261
200
mimeTypeService : editorServices . mimeTypeService ,
262
- languageRegistry,
263
- contextAssembler
201
+ languageRegistry
264
202
} ) ;
265
203
featureManager . register ( feature ) ;
266
204
// return feature;
0 commit comments