Skip to content

Commit 0c1f2b9

Browse files
committed
Fix registration options, diagnostics (one slot!), migrate hover
1 parent 2914ff2 commit 0c1f2b9

File tree

4 files changed

+85
-16
lines changed

4 files changed

+85
-16
lines changed

packages/jupyterlab-lsp/src/connection.ts

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ import {
1111
IPosition,
1212
LspWsConnection
1313
} from 'lsp-ws-connection';
14+
import {
15+
registerServerCapability,
16+
unregisterServerCapability
17+
} from 'lsp-ws-connection/lib/server-capability-registration';
1418
import type * as rpc from 'vscode-jsonrpc';
1519
import type * as lsp from 'vscode-languageserver-protocol';
1620
import type { MessageConnection } from 'vscode-ws-jsonrpc';
@@ -196,7 +200,7 @@ class ServerRequestHandler<
196200
protected emitter: LSPConnection
197201
) {
198202
// on request accepts "thenable"
199-
this.connection.onRequest(method, this.handle);
203+
this.connection.onRequest(method, this.handle.bind(this));
200204
this._handler = null;
201205
}
202206

@@ -319,12 +323,21 @@ export class LSPConnection extends LspWsConnection {
319323
}
320324
}
321325

326+
sendInitialize() {
327+
super.sendInitialize();
328+
}
329+
322330
protected onServerInitialized(params: lsp.InitializeResult) {
323331
super.onServerInitialized(params);
324332
while (this.documentsToOpen.length) {
325333
this.sendOpen(this.documentsToOpen.pop());
326334
}
335+
// TODO: move to send Initialize after disabling overwrites in ws-connection
336+
// or maybe move the code there? How to handle logging without bringing in lumino signals?
337+
this.afterInitialized();
338+
}
327339

340+
protected afterInitialized() {
328341
for (const method of Object.values(
329342
Method.ServerNotification
330343
) as (keyof ServerNotifications)[]) {
@@ -349,6 +362,40 @@ export class LSPConnection extends LspWsConnection {
349362
this.serverRequests = this.constructServerRequestHandler<ServerRequests>(
350363
Method.ServerRequests
351364
);
365+
366+
this.serverRequests['client/registerCapability'].setHandler(
367+
async (params: lsp.RegistrationParams) => {
368+
params.registrations.forEach(
369+
(capabilityRegistration: lsp.Registration) => {
370+
try {
371+
this.serverCapabilities = registerServerCapability(
372+
this.serverCapabilities,
373+
capabilityRegistration
374+
);
375+
} catch (err) {
376+
console.error(err);
377+
}
378+
}
379+
);
380+
381+
// TODO log event
382+
}
383+
);
384+
385+
this.serverRequests['client/unregisterCapability'].setHandler(
386+
async (params: lsp.UnregistrationParams) => {
387+
params.unregisterations.forEach(
388+
(capabilityUnregistration: lsp.Unregistration) => {
389+
this.serverCapabilities = unregisterServerCapability(
390+
this.serverCapabilities,
391+
capabilityUnregistration
392+
);
393+
}
394+
);
395+
396+
// TODO log event
397+
}
398+
);
352399
}
353400

354401
public sendSelectiveChange(

packages/jupyterlab-lsp/src/features/diagnostics/diagnostics.spec.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ describe('Diagnostics', () => {
7777
markers = env.ce_editor.editor.getDoc().getAllMarks();
7878
expect(markers.length).to.equal(0);
7979

80-
feature.handleDiagnostic({
80+
feature.handleDiagnostic(null, {
8181
uri: env.document_options.path,
8282
diagnostics: diagnostics
8383
});
@@ -99,7 +99,7 @@ describe('Diagnostics', () => {
9999
env.ce_editor.model.value.text = text;
100100
await env.adapter.update_documents();
101101

102-
feature.handleDiagnostic({
102+
feature.handleDiagnostic(null, {
103103
uri: env.document_options.path,
104104
diagnostics: diagnostics
105105
});
@@ -124,7 +124,7 @@ describe('Diagnostics', () => {
124124
env.ce_editor.model.value.text = text;
125125
await env.adapter.update_documents();
126126

127-
feature.handleDiagnostic({
127+
feature.handleDiagnostic(null, {
128128
uri: env.document_options.path,
129129
diagnostics: diagnostics
130130
});
@@ -167,7 +167,7 @@ describe('Diagnostics', () => {
167167
let document = env.virtual_editor.virtual_document;
168168
let uri = env.virtual_editor.virtual_document.uri;
169169

170-
feature.handleDiagnostic({
170+
feature.handleDiagnostic(null, {
171171
uri: uri,
172172
diagnostics: [
173173
{
@@ -285,7 +285,7 @@ describe('Diagnostics', () => {
285285
} as lsProtocol.PublishDiagnosticsParams;
286286

287287
// test guards against wrongly propagated responses:
288-
feature.handleDiagnostic(response);
288+
feature.handleDiagnostic(null, response);
289289
let cm_editors = env.adapter.editors.map(
290290
ce_editor => (ce_editor as CodeMirrorEditor).editor
291291
);
@@ -297,7 +297,7 @@ describe('Diagnostics', () => {
297297
expect(marks_cell_2.length).to.equal(0);
298298

299299
// correct propagation
300-
foreign_feature.handleDiagnostic(response);
300+
foreign_feature.handleDiagnostic(null, response);
301301

302302
marks_cell_1 = cm_editors[0].getDoc().getAllMarks();
303303
marks_cell_2 = cm_editors[1].getDoc().getAllMarks();
@@ -314,7 +314,7 @@ describe('Diagnostics', () => {
314314
expect(is_equal(mark_position.to, { line: 2, ch: 1 })).to.be.true;
315315

316316
// the silenced diagnostic for the %%python magic should be ignored
317-
feature.handleDiagnostic({
317+
feature.handleDiagnostic(null, {
318318
uri: document.uri,
319319
diagnostics: [
320320
{

packages/jupyterlab-lsp/src/features/diagnostics/diagnostics.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import type * as lsProtocol from 'vscode-languageserver-protocol';
88

99
import diagnosticsSvg from '../../../style/icons/diagnostics.svg';
1010
import { CodeDiagnostics as LSPDiagnosticsSettings } from '../../_diagnostics';
11+
import { LSPConnection } from '../../connection';
1112
import { PositionConverter } from '../../converter';
1213
import { CodeMirrorIntegration } from '../../editor_integration/codemirror';
1314
import { FeatureSettings } from '../../feature';
@@ -288,7 +289,12 @@ export class DiagnosticsCM extends CodeMirrorIntegration {
288289
}
289290

290291
register(): void {
291-
this.connection_handlers.set('diagnostic', this.handleDiagnostic);
292+
// this.connection_handlers.set('diagnostic', this.handleDiagnostic);
293+
// TODO: unregister
294+
this.connection.serverNotifications[
295+
'textDocument/publishDiagnostics'
296+
].connect(this.handleDiagnostic);
297+
292298
this.wrapper_handlers.set('focusin', this.switchDiagnosticsPanelSource);
293299
this.unique_editor_ids = new DefaultMap(() => this.unique_editor_ids.size);
294300
this.settings.changed.connect(this.refreshDiagnostics, this);
@@ -606,7 +612,10 @@ export class DiagnosticsCM extends CodeMirrorIntegration {
606612
this.diagnostics_db.set(this.virtual_document, diagnostics_list);
607613
}
608614

609-
public handleDiagnostic = (response: lsProtocol.PublishDiagnosticsParams) => {
615+
public handleDiagnostic = (
616+
connection: LSPConnection,
617+
response: lsProtocol.PublishDiagnosticsParams
618+
) => {
610619
if (!uris_equal(response.uri, this.virtual_document.document_info.uri)) {
611620
return;
612621
}

packages/jupyterlab-lsp/src/features/hover.ts

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -218,12 +218,25 @@ export class HoverCM extends CodeMirrorIntegration {
218218
}
219219

220220
protected on_hover = async () => {
221-
return await this.connection.getHoverTooltip(
222-
this.virtual_position,
223-
// this might be wrong - should not it be using the specific virtual document?
224-
this.virtual_document.document_info,
225-
false
226-
);
221+
if (
222+
!(
223+
this.connection.isReady &&
224+
this.connection.serverCapabilities?.hoverProvider
225+
)
226+
) {
227+
return;
228+
}
229+
let position = this.virtual_position;
230+
return await this.connection.clientRequests['textDocument/hover'].request({
231+
textDocument: {
232+
// this might be wrong - should not it be using the specific virtual document?
233+
uri: this.virtual_document.document_info.uri
234+
},
235+
position: {
236+
line: position.line,
237+
character: position.ch
238+
}
239+
});
227240
};
228241

229242
protected static get_markup_for_hover(

0 commit comments

Comments
 (0)