@jupyterlab/lsp 4.0.6 → 4.1.0-alpha.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. package/lib/adapters/adapter.d.ts +4 -1
  2. package/lib/adapters/adapter.js +21 -0
  3. package/lib/adapters/adapter.js.map +1 -1
  4. package/lib/adapters/editorAdapter.d.ts +74 -0
  5. package/lib/adapters/editorAdapter.js +53 -0
  6. package/lib/adapters/editorAdapter.js.map +1 -0
  7. package/lib/adapters/index.d.ts +3 -0
  8. package/lib/adapters/index.js +6 -0
  9. package/lib/adapters/index.js.map +1 -0
  10. package/lib/adapters/tracker.d.ts +106 -0
  11. package/lib/adapters/tracker.js +175 -0
  12. package/lib/adapters/tracker.js.map +1 -0
  13. package/lib/connection_manager.d.ts +11 -5
  14. package/lib/connection_manager.js +10 -0
  15. package/lib/connection_manager.js.map +1 -1
  16. package/lib/feature.d.ts +7 -2
  17. package/lib/feature.js +17 -4
  18. package/lib/feature.js.map +1 -1
  19. package/lib/index.d.ts +1 -1
  20. package/lib/index.js +1 -1
  21. package/lib/index.js.map +1 -1
  22. package/lib/lsp.d.ts +1 -0
  23. package/lib/lsp.js.map +1 -1
  24. package/lib/tokens.d.ts +106 -9
  25. package/lib/tokens.js +8 -0
  26. package/lib/tokens.js.map +1 -1
  27. package/lib/virtual/document.d.ts +1 -2
  28. package/lib/virtual/document.js +22 -5
  29. package/lib/virtual/document.js.map +1 -1
  30. package/package.json +10 -11
  31. package/src/adapters/adapter.ts +34 -2
  32. package/src/adapters/editorAdapter.ts +131 -0
  33. package/src/adapters/index.ts +6 -0
  34. package/src/adapters/tracker.ts +222 -0
  35. package/src/connection_manager.ts +23 -7
  36. package/src/feature.ts +20 -5
  37. package/src/index.ts +1 -1
  38. package/src/lsp.ts +2 -0
  39. package/src/tokens.ts +126 -12
  40. package/src/virtual/document.ts +26 -6
  41. package/style/index.css +2 -0
  42. package/style/index.js +2 -0
@@ -25,6 +25,7 @@ import {
25
25
  ISocketConnectionOptions
26
26
  } from '../tokens';
27
27
  import { VirtualDocument } from '../virtual/document';
28
+ import { EditorAdapter } from './editorAdapter';
28
29
 
29
30
  type IButton = Dialog.IButton;
30
31
  const createButton = Dialog.createButton;
@@ -74,8 +75,9 @@ export interface IAdapterOptions {
74
75
  * as this would make the logic of inspections caching impossible to maintain, thus the WidgetAdapter
75
76
  * has to handle that, keeping multiple connections and multiple virtual documents.
76
77
  */
77
- export abstract class WidgetLSPAdapter<T extends IDocumentWidget>
78
- implements IDisposable
78
+ export abstract class WidgetLSPAdapter<
79
+ T extends IDocumentWidget = IDocumentWidget
80
+ > implements IDisposable
79
81
  {
80
82
  // note: it could be using namespace/IOptions pattern,
81
83
  // but I do not know how to make it work with the generic type T
@@ -91,6 +93,10 @@ export abstract class WidgetLSPAdapter<T extends IDocumentWidget>
91
93
  this.widget.context.saveState.connect(this.onSaveState, this);
92
94
  this.connectionManager.closed.connect(this.onConnectionClosed, this);
93
95
  this.widget.disposed.connect(this.dispose, this);
96
+
97
+ this._editorToAdapter = new WeakMap();
98
+ this.editorAdded.connect(this._onEditorAdded, this);
99
+ this.editorRemoved.connect(this._onEditorRemoved, this);
94
100
  }
95
101
 
96
102
  /**
@@ -263,6 +269,8 @@ export abstract class WidgetLSPAdapter<T extends IDocumentWidget>
263
269
  if (this._isDisposed) {
264
270
  return;
265
271
  }
272
+ this.editorAdded.disconnect(this._onEditorAdded, this);
273
+ this.editorRemoved.disconnect(this._onEditorRemoved, this);
266
274
  this._isDisposed = true;
267
275
  this.disconnect();
268
276
  this._virtualDocument = null;
@@ -592,6 +600,30 @@ export abstract class WidgetLSPAdapter<T extends IDocumentWidget>
592
600
  private _isConnected: boolean;
593
601
  private _updateFinished: Promise<void>;
594
602
  private _virtualDocument: VirtualDocument | null = null;
603
+ private _editorToAdapter: WeakMap<Document.IEditor, EditorAdapter>;
604
+
605
+ private _onEditorAdded(
606
+ sender: WidgetLSPAdapter<T>,
607
+ change: IEditorChangedData
608
+ ): void {
609
+ const { editor } = change;
610
+ const editorAdapter = new EditorAdapter({
611
+ editor: editor,
612
+ widgetAdapter: this,
613
+ extensions: this.options.featureManager.extensionFactories()
614
+ });
615
+ this._editorToAdapter.set(editor, editorAdapter);
616
+ }
617
+
618
+ private _onEditorRemoved(
619
+ sender: WidgetLSPAdapter<T>,
620
+ change: IEditorChangedData
621
+ ): void {
622
+ const { editor } = change;
623
+ const adapter = this._editorToAdapter.get(editor);
624
+ adapter?.dispose();
625
+ this._editorToAdapter.delete(editor);
626
+ }
595
627
 
596
628
  /**
597
629
  * Callback called when a foreign document is closed,
@@ -0,0 +1,131 @@
1
+ // Copyright (c) Jupyter Development Team.
2
+ // Distributed under the terms of the Modified BSD License.
3
+
4
+ import {
5
+ IConfigurableExtension,
6
+ IEditorExtensionFactory
7
+ } from '@jupyterlab/codemirror';
8
+
9
+ import { IDisposable } from '@lumino/disposable';
10
+
11
+ import { Signal } from '@lumino/signaling';
12
+
13
+ import { WidgetLSPAdapter } from './adapter';
14
+ import { Document } from '../tokens';
15
+
16
+ /**
17
+ * The CodeEditor.IEditor adapter.
18
+ */
19
+ export class EditorAdapter implements IDisposable {
20
+ /**
21
+ * Instantiate a new EditorAdapter.
22
+ *
23
+ * @param options The instantiation options for a EditorAdapter.
24
+ */
25
+ constructor(options: EditorAdapter.IOptions) {
26
+ this._widgetAdapter = options.widgetAdapter;
27
+ this._extensions = options.extensions;
28
+
29
+ void options.editor.ready().then(editor => {
30
+ this._injectExtensions(options.editor);
31
+ });
32
+ }
33
+
34
+ /**
35
+ * Whether the handler is disposed.
36
+ */
37
+ isDisposed: boolean;
38
+
39
+ /**
40
+ * Dispose the handler.
41
+ */
42
+ dispose(): void {
43
+ if (this.isDisposed) {
44
+ return;
45
+ }
46
+ this.isDisposed = true;
47
+ Signal.clearData(this);
48
+ }
49
+
50
+ /**
51
+ * Setup the editor.
52
+ */
53
+ private _injectExtensions(editor: Document.IEditor): void {
54
+ const codeEditor = editor.getEditor()!;
55
+ if (codeEditor.isDisposed) {
56
+ return;
57
+ }
58
+
59
+ this._extensions.forEach(factory => {
60
+ const ext = factory.factory({
61
+ path: this._widgetAdapter.widget.context.path,
62
+ editor: editor,
63
+ widgetAdapter: this._widgetAdapter,
64
+ model: codeEditor.model,
65
+ inline: true
66
+ });
67
+
68
+ if (!ext) {
69
+ return;
70
+ }
71
+
72
+ codeEditor.injectExtension(ext.instance(codeEditor));
73
+ });
74
+ }
75
+
76
+ private _widgetAdapter: WidgetLSPAdapter<any>;
77
+ private _extensions: EditorAdapter.ILSPEditorExtensionFactory[];
78
+ }
79
+
80
+ /**
81
+ * A namespace for EditorAdapter `statics`.
82
+ */
83
+ export namespace EditorAdapter {
84
+ /**
85
+ * Instantiation options for `EditorAdapter`.
86
+ */
87
+ export interface IOptions {
88
+ /**
89
+ * Editor wrapper enabling revealing the editor when detached (out of view).
90
+ */
91
+ editor: Document.IEditor;
92
+
93
+ /**
94
+ * The widget lsp adapter.
95
+ */
96
+ widgetAdapter: WidgetLSPAdapter;
97
+
98
+ /**
99
+ * The list of CodeMirror extension factories
100
+ */
101
+ extensions: ILSPEditorExtensionFactory[];
102
+ }
103
+
104
+ export interface ILSPEditorExtensionFactory
105
+ extends Omit<IEditorExtensionFactory<any>, 'factory'> {
106
+ /**
107
+ * Extension factory.
108
+ *
109
+ * @param options
110
+ * @returns The extension builder or null if the extension is not active for that document
111
+ */
112
+ readonly factory: (
113
+ options: IFactoryOptions
114
+ ) => IConfigurableExtension<any> | null;
115
+ }
116
+
117
+ export interface IFactoryOptions extends IEditorExtensionFactory.IOptions {
118
+ /**
119
+ * A path to a source file.
120
+ */
121
+ path: string;
122
+ /**
123
+ * The code editor.
124
+ */
125
+ editor: Document.IEditor;
126
+ /**
127
+ * The widget lsp adapter.
128
+ */
129
+ widgetAdapter: WidgetLSPAdapter<any>;
130
+ }
131
+ }
@@ -0,0 +1,6 @@
1
+ // Copyright (c) Jupyter Development Team.
2
+ // Distributed under the terms of the Modified BSD License.
3
+
4
+ export * from './adapter';
5
+ export * from './editorAdapter';
6
+ export * from './tracker';
@@ -0,0 +1,222 @@
1
+ // Copyright (c) Jupyter Development Team.
2
+ // Distributed under the terms of the Modified BSD License.
3
+
4
+ import { DocumentWidget } from '@jupyterlab/docregistry';
5
+
6
+ import { ISignal, Signal } from '@lumino/signaling';
7
+
8
+ import { WidgetLSPAdapter } from './adapter';
9
+ import { IShell, IWidgetLSPAdapterTracker } from '../tokens';
10
+
11
+ /**
12
+ * A class that keeps track of widget adapter instances.
13
+ *
14
+ * @typeparam T - The type of widget being tracked. Defaults to `WidgetLSPAdapter`.
15
+ */
16
+ export class WidgetLSPAdapterTracker<
17
+ T extends WidgetLSPAdapter = WidgetLSPAdapter
18
+ > implements IWidgetLSPAdapterTracker<T>
19
+ {
20
+ /**
21
+ * Create a new widget tracker.
22
+ *
23
+ * @param options - The instantiation options for a widget tracker.
24
+ */
25
+ constructor(options: WidgetLSPAdapterTracker.IOptions) {
26
+ const shell = (this._shell = options.shell);
27
+
28
+ shell.currentChanged.connect((_, args) => {
29
+ let newValue = args.newValue;
30
+
31
+ if (!newValue || !(newValue instanceof DocumentWidget)) {
32
+ console.log('No current widget');
33
+ return;
34
+ }
35
+
36
+ const adapter = this.find(value => value.widget === newValue);
37
+
38
+ if (!adapter) {
39
+ return;
40
+ }
41
+
42
+ this._current = adapter;
43
+ this._currentChanged.emit(adapter);
44
+ });
45
+ }
46
+
47
+ /**
48
+ * A signal emitted when the current adapter changes.
49
+ */
50
+ get currentChanged(): ISignal<this, T | null> {
51
+ return this._currentChanged;
52
+ }
53
+
54
+ /**
55
+ * The current adapter is the most recently focused or added adapter.
56
+ *
57
+ * #### Notes
58
+ * It is the most recently focused adapter, or the most recently added
59
+ * adapter if no adapter has taken focus.
60
+ */
61
+ get currentAdapter(): T | null {
62
+ return this._current;
63
+ }
64
+
65
+ /**
66
+ * The number of adapter held by the tracker.
67
+ */
68
+ get size(): number {
69
+ return this._adapters.size;
70
+ }
71
+
72
+ /**
73
+ * A signal emitted when an adapter is added.
74
+ */
75
+ get adapterAdded(): ISignal<this, T> {
76
+ return this._adapterAdded;
77
+ }
78
+
79
+ /**
80
+ * A signal emitted when an adapter is updated.
81
+ */
82
+ get adapterUpdated(): ISignal<this, T> {
83
+ return this._adapterUpdated;
84
+ }
85
+
86
+ /**
87
+ * Add a new adapter to the tracker.
88
+ *
89
+ * @param adapter - The adapter being added.
90
+ *
91
+ * #### Notes.
92
+ * The newly added adapter becomes the current adapter unless the shell
93
+ * already had a DocumentWidget as the activeWidget.
94
+ */
95
+ add(adapter: T): void {
96
+ if (adapter.isDisposed) {
97
+ const warning = 'A disposed object cannot be added.';
98
+ console.warn(warning, adapter);
99
+ throw new Error(warning);
100
+ }
101
+
102
+ if (this._adapters.has(adapter)) {
103
+ const warning = 'This object already exists in the pool.';
104
+ console.warn(warning, adapter);
105
+ throw new Error(warning);
106
+ }
107
+
108
+ this._adapters.add(adapter);
109
+ this._adapterAdded.emit(adapter);
110
+
111
+ adapter.disposed.connect(() => {
112
+ this._adapters.delete(adapter);
113
+
114
+ if (adapter === this._current) {
115
+ this._current = null;
116
+ this._currentChanged.emit(this._current);
117
+ }
118
+ }, this);
119
+
120
+ // Only update the current adapter, when there is no shell.activeWidget
121
+ // or the active widget is not a DocumentWidget
122
+ // We will be able to use other panels while keeping the current adapter.
123
+ const active = this._shell.activeWidget;
124
+ if (!active || !(active instanceof DocumentWidget)) {
125
+ this._current = adapter;
126
+ this._currentChanged.emit(adapter);
127
+ }
128
+ }
129
+
130
+ /**
131
+ * Test whether the tracker is disposed.
132
+ */
133
+ get isDisposed(): boolean {
134
+ return this._isDisposed;
135
+ }
136
+
137
+ /**
138
+ * Dispose of the resources held by the tracker.
139
+ */
140
+ dispose(): void {
141
+ if (this.isDisposed) {
142
+ return;
143
+ }
144
+ this._isDisposed = true;
145
+ this._adapters.clear();
146
+ Signal.clearData(this);
147
+ }
148
+
149
+ /**
150
+ * Find the first adapter in the tracker that satisfies a filter function.
151
+ *
152
+ * @param - fn The filter function to call on each adapter.
153
+ *
154
+ * #### Notes
155
+ * If no adapter is found, the value returned is `undefined`.
156
+ */
157
+ find(fn: (adapter: T) => boolean): T | undefined {
158
+ const values = this._adapters.values();
159
+ for (const value of values) {
160
+ if (fn(value)) {
161
+ return value;
162
+ }
163
+ }
164
+ return undefined;
165
+ }
166
+
167
+ /**
168
+ * Iterate through each adapter in the tracker.
169
+ *
170
+ * @param fn - The function to call on each adapter.
171
+ */
172
+ forEach(fn: (adapter: T) => void): void {
173
+ this._adapters.forEach(fn);
174
+ }
175
+
176
+ /**
177
+ * Filter the adapter in the tracker based on a predicate.
178
+ *
179
+ * @param fn - The function by which to filter.
180
+ */
181
+ filter(fn: (adapter: T) => boolean): T[] {
182
+ const filtered: T[] = [];
183
+ this.forEach(value => {
184
+ if (fn(value)) {
185
+ filtered.push(value);
186
+ }
187
+ });
188
+ return filtered;
189
+ }
190
+
191
+ /**
192
+ * Check if this tracker has the specified adapter.
193
+ *
194
+ * @param adapter - The adapter whose existence is being checked.
195
+ */
196
+ has(adapter: T): boolean {
197
+ return this._adapters.has(adapter);
198
+ }
199
+
200
+ private _isDisposed = false;
201
+ private _shell: IShell;
202
+ private _current: T | null = null;
203
+ private _adapters = new Set<T>();
204
+ private _adapterAdded = new Signal<this, T>(this);
205
+ private _adapterUpdated = new Signal<this, T>(this);
206
+ private _currentChanged = new Signal<this, T | null>(this);
207
+ }
208
+
209
+ /**
210
+ * A namespace for `WidgetLSPAdapterTracker` statics.
211
+ */
212
+ export namespace WidgetLSPAdapterTracker {
213
+ /**
214
+ * The instantiation options for the WidgetLSPAdapterTracker.
215
+ */
216
+ export interface IOptions {
217
+ /**
218
+ * The JupyterLab shell for tracking all widgets.
219
+ */
220
+ shell: IShell;
221
+ }
222
+ }
@@ -2,10 +2,9 @@
2
2
  // Distributed under the terms of the Modified BSD License.
3
3
 
4
4
  import { PageConfig, URLExt } from '@jupyterlab/coreutils';
5
- import { IDocumentWidget } from '@jupyterlab/docregistry';
6
5
  import { ISignal, Signal } from '@lumino/signaling';
7
6
 
8
- import { WidgetLSPAdapter } from './adapters/adapter';
7
+ import { WidgetLSPAdapter } from './adapters';
9
8
  import { LSPConnection } from './connection';
10
9
  import { ClientCapabilities } from './lsp';
11
10
  import { AskServersToSendTraceNotifications } from './plugin';
@@ -16,6 +15,7 @@ import {
16
15
  ILSPConnection,
17
16
  ILSPDocumentConnectionManager,
18
17
  ISocketConnectionOptions,
18
+ IWidgetLSPAdapterTracker,
19
19
  TLanguageServerConfigurations,
20
20
  TLanguageServerId,
21
21
  TServerKeys
@@ -40,6 +40,11 @@ export class DocumentConnectionManager
40
40
  this._ignoredLanguages = new Set();
41
41
  this.languageServerManager = options.languageServerManager;
42
42
  Private.setLanguageServerManager(options.languageServerManager);
43
+
44
+ options.adapterTracker.adapterAdded.connect((_, adapter) => {
45
+ const path = adapter.widget.context.path;
46
+ this.registerAdapter(path, adapter);
47
+ });
43
48
  }
44
49
 
45
50
  /**
@@ -49,9 +54,10 @@ export class DocumentConnectionManager
49
54
  readonly connections: Map<VirtualDocument.uri, LSPConnection>;
50
55
 
51
56
  /**
57
+ * @deprecated
52
58
  * Map between the path of the document and its adapter
53
59
  */
54
- readonly adapters: Map<string, WidgetLSPAdapter<IDocumentWidget>>;
60
+ readonly adapters: Map<string, WidgetLSPAdapter>;
55
61
 
56
62
  /**
57
63
  * Map between the URI of the virtual document and the document itself.
@@ -199,16 +205,21 @@ export class DocumentConnectionManager
199
205
  }
200
206
 
201
207
  /**
208
+ * @deprecated
209
+ *
202
210
  * Register a widget adapter with this manager
203
211
  *
204
212
  * @param path - path to the inner document of the adapter
205
213
  * @param adapter - the adapter to be registered
206
214
  */
207
- registerAdapter(
208
- path: string,
209
- adapter: WidgetLSPAdapter<IDocumentWidget>
210
- ): void {
215
+ registerAdapter(path: string, adapter: WidgetLSPAdapter): void {
211
216
  this.adapters.set(path, adapter);
217
+
218
+ adapter.widget.context.pathChanged.connect((context, newPath) => {
219
+ this.adapters.delete(path);
220
+ this.adapters.set(newPath, adapter);
221
+ });
222
+
212
223
  adapter.disposed.connect(() => {
213
224
  if (adapter.virtualDocument) {
214
225
  this.documents.delete(adapter.virtualDocument.uri);
@@ -535,6 +546,11 @@ export namespace DocumentConnectionManager {
535
546
  * The language server manager instance.
536
547
  */
537
548
  languageServerManager: ILanguageServerManager;
549
+
550
+ /**
551
+ * The WidgetLSPAdapter's tracker.
552
+ */
553
+ adapterTracker: IWidgetLSPAdapterTracker;
538
554
  }
539
555
 
540
556
  /**
package/src/feature.ts CHANGED
@@ -6,13 +6,14 @@ import mergeWith from 'lodash.mergewith';
6
6
 
7
7
  import { ClientCapabilities } from './lsp';
8
8
  import { IFeature, ILSPFeatureManager } from './tokens';
9
+ import { EditorAdapter } from './adapters/editorAdapter';
9
10
 
10
11
  /**
11
12
  * Class to manager the registered features of the language servers.
12
13
  */
13
14
  export class FeatureManager implements ILSPFeatureManager {
14
15
  constructor() {
15
- this._featuresRegistered = new Signal(this);
16
+ this._featureRegistered = new Signal(this);
16
17
  }
17
18
  /**
18
19
  * List of registered features
@@ -22,8 +23,8 @@ export class FeatureManager implements ILSPFeatureManager {
22
23
  /**
23
24
  * Signal emitted when a new feature is registered.
24
25
  */
25
- get featuresRegistered(): ISignal<ILSPFeatureManager, IFeature> {
26
- return this._featuresRegistered;
26
+ get featureRegistered(): ISignal<ILSPFeatureManager, IFeature> {
27
+ return this._featureRegistered;
27
28
  }
28
29
 
29
30
  /**
@@ -36,7 +37,7 @@ export class FeatureManager implements ILSPFeatureManager {
36
37
  );
37
38
  } else {
38
39
  this.features.push(feature);
39
- this._featuresRegistered.emit(feature);
40
+ this._featureRegistered.emit(feature);
40
41
  }
41
42
  }
42
43
 
@@ -54,5 +55,19 @@ export class FeatureManager implements ILSPFeatureManager {
54
55
  return capabilities;
55
56
  }
56
57
 
57
- private _featuresRegistered: Signal<ILSPFeatureManager, IFeature>;
58
+ /**
59
+ * Get the extension factories of all clients.
60
+ */
61
+ extensionFactories(): EditorAdapter.ILSPEditorExtensionFactory[] {
62
+ const factories: EditorAdapter.ILSPEditorExtensionFactory[] = [];
63
+ for (const feature of this.features) {
64
+ if (!feature.extensionFactory) {
65
+ continue;
66
+ }
67
+ factories.push(feature.extensionFactory);
68
+ }
69
+ return factories;
70
+ }
71
+
72
+ private _featureRegistered: Signal<ILSPFeatureManager, IFeature>;
58
73
  }
package/src/index.ts CHANGED
@@ -5,7 +5,7 @@
5
5
  * @module lsp
6
6
  */
7
7
 
8
- export * from './adapters/adapter';
8
+ export * from './adapters';
9
9
  export * from './connection_manager';
10
10
  export * from './extractors';
11
11
  export * from './feature';
package/src/lsp.ts CHANGED
@@ -5,6 +5,8 @@ import type * as lsp from 'vscode-languageserver-protocol';
5
5
 
6
6
  export type ClientCapabilities = lsp.ClientCapabilities;
7
7
 
8
+ export type ServerCapabilities = lsp.ServerCapabilities;
9
+
8
10
  export enum DiagnosticSeverity {
9
11
  Error = 1,
10
12
  Warning = 2,