@difizen/libro-lsp 0.1.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 (104) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +0 -0
  3. package/es/adapters/adapter.d.ts +255 -0
  4. package/es/adapters/adapter.d.ts.map +1 -0
  5. package/es/adapters/adapter.js +647 -0
  6. package/es/adapters/notebook-adapter.d.ts +150 -0
  7. package/es/adapters/notebook-adapter.d.ts.map +1 -0
  8. package/es/adapters/notebook-adapter.js +588 -0
  9. package/es/adapters/status-message.d.ts +48 -0
  10. package/es/adapters/status-message.d.ts.map +1 -0
  11. package/es/adapters/status-message.js +110 -0
  12. package/es/connection-manager.d.ts +199 -0
  13. package/es/connection-manager.d.ts.map +1 -0
  14. package/es/connection-manager.js +685 -0
  15. package/es/connection.d.ts +149 -0
  16. package/es/connection.d.ts.map +1 -0
  17. package/es/connection.js +591 -0
  18. package/es/extractors/index.d.ts +4 -0
  19. package/es/extractors/index.d.ts.map +1 -0
  20. package/es/extractors/index.js +6 -0
  21. package/es/extractors/manager.d.ts +31 -0
  22. package/es/extractors/manager.d.ts.map +1 -0
  23. package/es/extractors/manager.js +90 -0
  24. package/es/extractors/text-extractor.d.ts +56 -0
  25. package/es/extractors/text-extractor.d.ts.map +1 -0
  26. package/es/extractors/text-extractor.js +72 -0
  27. package/es/extractors/types.d.ts +68 -0
  28. package/es/extractors/types.d.ts.map +1 -0
  29. package/es/extractors/types.js +1 -0
  30. package/es/feature.d.ts +29 -0
  31. package/es/feature.d.ts.map +1 -0
  32. package/es/feature.js +85 -0
  33. package/es/index.d.ts +19 -0
  34. package/es/index.d.ts.map +1 -0
  35. package/es/index.js +21 -0
  36. package/es/lsp-app-contribution.d.ts +19 -0
  37. package/es/lsp-app-contribution.d.ts.map +1 -0
  38. package/es/lsp-app-contribution.js +155 -0
  39. package/es/lsp-protocol.d.ts +10 -0
  40. package/es/lsp-protocol.d.ts.map +1 -0
  41. package/es/lsp-protocol.js +1 -0
  42. package/es/lsp.d.ts +136 -0
  43. package/es/lsp.d.ts.map +1 -0
  44. package/es/lsp.js +141 -0
  45. package/es/manager.d.ts +142 -0
  46. package/es/manager.d.ts.map +1 -0
  47. package/es/manager.js +423 -0
  48. package/es/module.d.ts +3 -0
  49. package/es/module.d.ts.map +1 -0
  50. package/es/module.js +21 -0
  51. package/es/plugin.d.ts +56 -0
  52. package/es/plugin.d.ts.map +1 -0
  53. package/es/plugin.js +0 -0
  54. package/es/positioning.d.ts +66 -0
  55. package/es/positioning.d.ts.map +1 -0
  56. package/es/positioning.js +96 -0
  57. package/es/schema.d.ts +240 -0
  58. package/es/schema.d.ts.map +1 -0
  59. package/es/schema.js +0 -0
  60. package/es/tokens.d.ts +677 -0
  61. package/es/tokens.d.ts.map +1 -0
  62. package/es/tokens.js +183 -0
  63. package/es/utils.d.ts +33 -0
  64. package/es/utils.d.ts.map +1 -0
  65. package/es/utils.js +168 -0
  66. package/es/virtual/document.d.ts +546 -0
  67. package/es/virtual/document.d.ts.map +1 -0
  68. package/es/virtual/document.js +1263 -0
  69. package/es/ws-connection/server-capability-registration.d.ts +19 -0
  70. package/es/ws-connection/server-capability-registration.d.ts.map +1 -0
  71. package/es/ws-connection/server-capability-registration.js +51 -0
  72. package/es/ws-connection/types.d.ts +76 -0
  73. package/es/ws-connection/types.d.ts.map +1 -0
  74. package/es/ws-connection/types.js +1 -0
  75. package/es/ws-connection/ws-connection.d.ts +105 -0
  76. package/es/ws-connection/ws-connection.d.ts.map +1 -0
  77. package/es/ws-connection/ws-connection.js +301 -0
  78. package/package.json +67 -0
  79. package/src/adapters/adapter.ts +611 -0
  80. package/src/adapters/notebook-adapter.ts +463 -0
  81. package/src/adapters/status-message.ts +93 -0
  82. package/src/connection-manager.ts +626 -0
  83. package/src/connection.ts +570 -0
  84. package/src/extractors/index.ts +6 -0
  85. package/src/extractors/manager.ts +82 -0
  86. package/src/extractors/text-extractor.ts +94 -0
  87. package/src/extractors/types.ts +78 -0
  88. package/src/feature.ts +60 -0
  89. package/src/index.spec.ts +10 -0
  90. package/src/index.ts +21 -0
  91. package/src/lsp-app-contribution.ts +83 -0
  92. package/src/lsp-protocol.ts +10 -0
  93. package/src/lsp.ts +160 -0
  94. package/src/manager.ts +358 -0
  95. package/src/module.ts +32 -0
  96. package/src/plugin.ts +62 -0
  97. package/src/positioning.ts +121 -0
  98. package/src/schema.ts +249 -0
  99. package/src/tokens.ts +843 -0
  100. package/src/utils.ts +109 -0
  101. package/src/virtual/document.ts +1250 -0
  102. package/src/ws-connection/server-capability-registration.ts +77 -0
  103. package/src/ws-connection/types.ts +102 -0
  104. package/src/ws-connection/ws-connection.ts +320 -0
@@ -0,0 +1,77 @@
1
+ /*
2
+ * Copyright (c) Jupyter Development Team.
3
+ * Distributed under the terms of the Modified BSD License.
4
+ */
5
+
6
+ // Disclaimer/acknowledgement: Fragments are based on https://github.com/wylieconlon/lsp-editor-adapter,
7
+ // which is copyright of wylieconlon and contributors and ISC licenced.
8
+ // ISC licence is, quote, "functionally equivalent to the simplified BSD and MIT licenses,
9
+ // but without language deemed unnecessary following the Berne Convention." (Wikipedia).
10
+ // Introduced modifications are BSD licenced, copyright JupyterLab development team.
11
+
12
+ import type {
13
+ Registration,
14
+ ServerCapabilities,
15
+ Unregistration,
16
+ } from 'vscode-languageserver-protocol';
17
+
18
+ interface IFlexibleServerCapabilities extends ServerCapabilities {
19
+ [key: string]: any;
20
+ }
21
+
22
+ /**
23
+ * Register the capabilities with the server capabilities provider
24
+ *
25
+ * @param serverCapabilities - server capabilities provider.
26
+ * @param registration - capabilities to be registered.
27
+ * @return - the new server capabilities provider
28
+ */
29
+ function registerServerCapability(
30
+ serverCapabilities: ServerCapabilities,
31
+ registration: Registration,
32
+ ): ServerCapabilities | null {
33
+ const serverCapabilitiesCopy = JSON.parse(
34
+ JSON.stringify(serverCapabilities),
35
+ ) as IFlexibleServerCapabilities;
36
+ const { method, registerOptions } = registration;
37
+ const providerName = method.substring(13) + 'Provider';
38
+
39
+ if (providerName) {
40
+ if (!registerOptions) {
41
+ serverCapabilitiesCopy[providerName] = true;
42
+ } else {
43
+ serverCapabilitiesCopy[providerName] = JSON.parse(
44
+ JSON.stringify(registerOptions),
45
+ );
46
+ }
47
+ } else {
48
+ console.warn('Could not register server capability.', registration);
49
+ return null;
50
+ }
51
+
52
+ return serverCapabilitiesCopy;
53
+ }
54
+
55
+ /**
56
+ * Unregister the capabilities with the server capabilities provider
57
+ *
58
+ * @param serverCapabilities - server capabilities provider.
59
+ * @param registration - capabilities to be unregistered.
60
+ * @return - the new server capabilities provider
61
+ */
62
+ function unregisterServerCapability(
63
+ serverCapabilities: ServerCapabilities,
64
+ unregistration: Unregistration,
65
+ ): ServerCapabilities {
66
+ const serverCapabilitiesCopy = JSON.parse(
67
+ JSON.stringify(serverCapabilities),
68
+ ) as IFlexibleServerCapabilities;
69
+ const { method } = unregistration;
70
+ const providerName = method.substring(13) + 'Provider';
71
+
72
+ delete serverCapabilitiesCopy[providerName];
73
+
74
+ return serverCapabilitiesCopy;
75
+ }
76
+
77
+ export { registerServerCapability, unregisterServerCapability };
@@ -0,0 +1,102 @@
1
+ /*
2
+ * Copyright (c) Jupyter Development Team.
3
+ * Distributed under the terms of the Modified BSD License.
4
+ */
5
+
6
+ // Disclaimer/acknowledgement: Fragments are based on https://github.com/wylieconlon/lsp-editor-adapter,
7
+ // which is copyright of wylieconlon and contributors and ISC licenced.
8
+ // ISC licence is, quote, "functionally equivalent to the simplified BSD and MIT licenses,
9
+ // but without language deemed unnecessary following the Berne Convention." (Wikipedia).
10
+ // Introduced modifications are BSD licenced, copyright JupyterLab development team.
11
+
12
+ import type * as lsp from 'vscode-languageserver-protocol';
13
+
14
+ export interface IDocumentInfo {
15
+ /**
16
+ * URI of the virtual document.
17
+ */
18
+ uri: string;
19
+
20
+ /**
21
+ * Version of the virtual document.
22
+ */
23
+ version: number;
24
+
25
+ /**
26
+ * Text content of the document.
27
+ */
28
+ text: string;
29
+
30
+ /**
31
+ * Language id of the document.
32
+ */
33
+ languageId: string;
34
+ }
35
+
36
+ export interface ILspConnection {
37
+ /**
38
+ * Is the language server is connected?
39
+ */
40
+ isConnected: boolean;
41
+ /**
42
+ * Is the language server is initialized?
43
+ */
44
+ isInitialized: boolean;
45
+
46
+ /**
47
+ * Is the language server is connected and initialized?
48
+ */
49
+ isReady: boolean;
50
+
51
+ /**
52
+ * Initialize a connection over a web socket that speaks the LSP protocol
53
+ */
54
+ connect(socket: WebSocket): void;
55
+
56
+ /**
57
+ * Close the connection
58
+ */
59
+ close(): void;
60
+
61
+ // This should support every method from https://microsoft.github.io/language-server-protocol/specification
62
+ /**
63
+ * The initialize request tells the server which options the client supports
64
+ */
65
+ sendInitialize(): void;
66
+ /**
67
+ * Inform the server that the document was opened
68
+ */
69
+ sendOpen(documentInfo: IDocumentInfo): void;
70
+
71
+ /**
72
+ * Sends the full text of the document to the server
73
+ */
74
+ sendChange(documentInfo: IDocumentInfo): void;
75
+
76
+ /**
77
+ * Send save notification to the server.
78
+ */
79
+ sendSaved(documentInfo: IDocumentInfo): void;
80
+
81
+ /**
82
+ * Send configuration change to the server.
83
+ */
84
+ sendConfigurationChange(settings: lsp.DidChangeConfigurationParams): void;
85
+ }
86
+
87
+ export interface ILspOptions {
88
+ /**
89
+ * LSP handler endpoint.
90
+ */
91
+ serverUri: string;
92
+
93
+ /**
94
+ * Language Id.
95
+ */
96
+ languageId: string;
97
+
98
+ /**
99
+ * The root URI set by server.
100
+ */
101
+ rootUri: string;
102
+ }
@@ -0,0 +1,320 @@
1
+ /*
2
+ * Copyright (c) Jupyter Development Team.
3
+ * Distributed under the terms of the Modified BSD License.
4
+ */
5
+
6
+ // Disclaimer/acknowledgement: Fragments are based on https://github.com/wylieconlon/lsp-editor-adapter,
7
+ // which is copyright of wylieconlon and contributors and ISC licenced.
8
+ // ISC licence is, quote, "functionally equivalent to the simplified BSD and MIT licenses,
9
+ // but without language deemed unnecessary following the Berne Convention." (Wikipedia).
10
+ // Introduced modifications are BSD licenced, copyright JupyterLab development team.
11
+
12
+ import type { Disposable, Event } from '@difizen/mana-app';
13
+ import { Emitter } from '@difizen/mana-app';
14
+ import type * as protocol from 'vscode-languageserver-protocol';
15
+ //TODO: vscode-ws-jsonrpc has new version
16
+ import type { MessageConnection } from 'vscode-ws-jsonrpc';
17
+ import { ConsoleLogger, listen } from 'vscode-ws-jsonrpc';
18
+
19
+ import {
20
+ registerServerCapability,
21
+ unregisterServerCapability,
22
+ } from './server-capability-registration.js';
23
+ import type { IDocumentInfo, ILspConnection, ILspOptions } from './types.js';
24
+
25
+ export class LspWsConnection implements ILspConnection, Disposable {
26
+ constructor(options: ILspOptions) {
27
+ this._rootUri = options.rootUri;
28
+ }
29
+
30
+ /**
31
+ * Is the language server is connected?
32
+ */
33
+ get isConnected(): boolean {
34
+ return this._isConnected;
35
+ }
36
+
37
+ /**
38
+ * Is the language server is initialized?
39
+ */
40
+ get isInitialized(): boolean {
41
+ return this._isInitialized;
42
+ }
43
+
44
+ /**
45
+ * Is the language server is connected and initialized?
46
+ */
47
+ get isReady() {
48
+ return this._isConnected && this._isInitialized;
49
+ }
50
+
51
+ /**
52
+ * A signal emitted when the connection is disposed.
53
+ */
54
+ get onDisposed(): Event<void> {
55
+ return this._disposed.event;
56
+ }
57
+
58
+ /**
59
+ * Check if the connection is disposed
60
+ */
61
+ get isDisposed(): boolean {
62
+ return this._isDisposed;
63
+ }
64
+
65
+ /**
66
+ * Initialize a connection over a web socket that speaks the LSP protocol
67
+ */
68
+ connect(socket: WebSocket): void {
69
+ this.socket = socket;
70
+ listen({
71
+ webSocket: this.socket,
72
+ logger: new ConsoleLogger(),
73
+ onConnection: (connection: MessageConnection) => {
74
+ connection.listen();
75
+ this._isConnected = true;
76
+
77
+ this.connection = connection;
78
+ this.sendInitialize();
79
+
80
+ const registerCapabilityDisposable = this.connection.onRequest(
81
+ 'client/registerCapability',
82
+ (params: protocol.RegistrationParams) => {
83
+ params.registrations.forEach(
84
+ (capabilityRegistration: protocol.Registration) => {
85
+ try {
86
+ this.serverCapabilities = registerServerCapability(
87
+ this.serverCapabilities,
88
+ capabilityRegistration,
89
+ )!;
90
+ } catch (err) {
91
+ console.error(err);
92
+ }
93
+ },
94
+ );
95
+ },
96
+ );
97
+ this._disposables.push(registerCapabilityDisposable);
98
+
99
+ const unregisterCapabilityDisposable = this.connection.onRequest(
100
+ 'client/unregisterCapability',
101
+ (params: protocol.UnregistrationParams) => {
102
+ params.unregisterations.forEach(
103
+ (capabilityUnregistration: protocol.Unregistration) => {
104
+ this.serverCapabilities = unregisterServerCapability(
105
+ this.serverCapabilities,
106
+ capabilityUnregistration,
107
+ );
108
+ },
109
+ );
110
+ },
111
+ );
112
+ this._disposables.push(unregisterCapabilityDisposable);
113
+
114
+ const disposable = this.connection.onClose(() => {
115
+ this._isConnected = false;
116
+ });
117
+ this._disposables.push(disposable);
118
+ },
119
+ });
120
+ }
121
+
122
+ /**
123
+ * Close the connection
124
+ */
125
+ close() {
126
+ if (this.connection) {
127
+ this.connection.dispose();
128
+ }
129
+ this.openedUris.clear();
130
+ this.socket.close();
131
+ }
132
+
133
+ /**
134
+ * The initialize request telling the server which options the client supports
135
+ */
136
+ sendInitialize() {
137
+ if (!this._isConnected) {
138
+ return;
139
+ }
140
+
141
+ this.openedUris.clear();
142
+
143
+ const message: protocol.InitializeParams = this.initializeParams();
144
+
145
+ this.connection
146
+ .sendRequest<protocol.InitializeResult>('initialize', message)
147
+ .then(
148
+ (params) => {
149
+ this.onServerInitialized(params);
150
+ return;
151
+ },
152
+ (e) => {
153
+ console.warn('LSP websocket connection initialization failure', e);
154
+ },
155
+ )
156
+ .catch(console.error);
157
+ }
158
+
159
+ /**
160
+ * Inform the server that the document was opened
161
+ */
162
+ sendOpen(documentInfo: IDocumentInfo) {
163
+ const textDocumentMessage: protocol.DidOpenTextDocumentParams = {
164
+ textDocument: {
165
+ uri: documentInfo.uri,
166
+ languageId: documentInfo.languageId,
167
+ text: documentInfo.text,
168
+ version: documentInfo.version,
169
+ } as protocol.TextDocumentItem,
170
+ };
171
+ this.connection
172
+ .sendNotification('textDocument/didOpen', textDocumentMessage)
173
+ .catch(console.error);
174
+ this.openedUris.set(documentInfo.uri, true);
175
+ this.sendChange(documentInfo);
176
+ }
177
+
178
+ /**
179
+ * Sends the full text of the document to the server
180
+ */
181
+ sendChange(documentInfo: IDocumentInfo) {
182
+ if (!this.isReady) {
183
+ return;
184
+ }
185
+ if (!this.openedUris.get(documentInfo.uri)) {
186
+ this.sendOpen(documentInfo);
187
+ return;
188
+ }
189
+ const textDocumentChange: protocol.DidChangeTextDocumentParams = {
190
+ textDocument: {
191
+ uri: documentInfo.uri,
192
+ version: documentInfo.version,
193
+ } as protocol.VersionedTextDocumentIdentifier,
194
+ contentChanges: [{ text: documentInfo.text }],
195
+ };
196
+ this.connection
197
+ .sendNotification('textDocument/didChange', textDocumentChange)
198
+ .catch(console.error);
199
+ documentInfo.version++;
200
+ }
201
+
202
+ /**
203
+ * Send save notification to the server.
204
+ */
205
+ sendSaved(documentInfo: IDocumentInfo) {
206
+ if (!this.isReady) {
207
+ return;
208
+ }
209
+
210
+ const textDocumentChange: protocol.DidSaveTextDocumentParams = {
211
+ textDocument: {
212
+ uri: documentInfo.uri,
213
+ version: documentInfo.version,
214
+ } as protocol.VersionedTextDocumentIdentifier,
215
+ text: documentInfo.text,
216
+ };
217
+ this.connection
218
+ .sendNotification('textDocument/didSave', textDocumentChange)
219
+ .catch(console.error);
220
+ }
221
+
222
+ /**
223
+ * Send configuration change to the server.
224
+ */
225
+ sendConfigurationChange(settings: protocol.DidChangeConfigurationParams) {
226
+ if (!this.isReady) {
227
+ return;
228
+ }
229
+
230
+ this.connection
231
+ .sendNotification('workspace/didChangeConfiguration', settings)
232
+ .catch(console.error);
233
+ }
234
+
235
+ /**
236
+ * Dispose the connection.
237
+ */
238
+ dispose(): void {
239
+ if (this._isDisposed) {
240
+ return;
241
+ }
242
+ this._isDisposed = true;
243
+ this._disposables.forEach((disposable) => {
244
+ disposable.dispose();
245
+ });
246
+ this._disposed.fire();
247
+ }
248
+
249
+ /**
250
+ * The internal websocket connection to the LSP handler
251
+ */
252
+ protected socket: WebSocket;
253
+
254
+ /**
255
+ * The json-rpc wrapper over the internal websocket connection.
256
+ */
257
+ protected connection: MessageConnection;
258
+
259
+ /**
260
+ * Map to track opened virtual documents..
261
+ */
262
+ protected openedUris = new Map<string, boolean>();
263
+
264
+ /**
265
+ * Server capabilities provider.
266
+ */
267
+ protected serverCapabilities: protocol.ServerCapabilities;
268
+
269
+ /**
270
+ * The connection is connected?
271
+ */
272
+ protected _isConnected = false;
273
+
274
+ /**
275
+ * The connection is initialized?
276
+ */
277
+ protected _isInitialized = false;
278
+
279
+ /**
280
+ * Array of LSP callback disposables, it is used to
281
+ * clear the callbacks when the connection is disposed.
282
+ */
283
+ protected _disposables: protocol.Disposable[] = [];
284
+
285
+ /**
286
+ * Callback called when the server is initialized.
287
+ */
288
+ protected onServerInitialized(params: protocol.InitializeResult): void {
289
+ this._isInitialized = true;
290
+ this.serverCapabilities = params.capabilities;
291
+ this.connection.sendNotification('initialized', {}).catch(console.error);
292
+ this.connection
293
+ .sendNotification('workspace/didChangeConfiguration', {
294
+ settings: {},
295
+ })
296
+ .catch(console.error);
297
+ }
298
+
299
+ /**
300
+ * Initialization parameters to be sent to the language server.
301
+ * Subclasses should override this when adding more features.
302
+ */
303
+ protected initializeParams(): protocol.InitializeParams {
304
+ return {
305
+ capabilities: {} as protocol.ClientCapabilities,
306
+ processId: null,
307
+ rootUri: this._rootUri,
308
+ workspaceFolders: null,
309
+ };
310
+ }
311
+
312
+ /**
313
+ * URI of the LSP handler enpoint.
314
+ */
315
+ protected _rootUri: string;
316
+
317
+ protected _disposed = new Emitter<void>();
318
+
319
+ protected _isDisposed = false;
320
+ }