Skip to content

Commit 7b66b9f

Browse files
committedJan 25, 2024
allows command to not always produce expected event
fix
1 parent c791f80 commit 7b66b9f

File tree

2 files changed

+57
-22
lines changed

2 files changed

+57
-22
lines changed
 

‎src/polyglot-notebooks-vscode-common/src/interactiveClient.ts

+44-15
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ export class InteractiveClient {
244244

245245
}
246246

247-
completion(kernelName: string, code: string, line: number, character: number): Promise<CompletionsProduced> {
247+
async completion(kernelName: string, code: string, line: number, character: number): Promise<CompletionsProduced> {
248248

249249
const command = new KernelCommandEnvelope(
250250
RequestCompletionsType,
@@ -257,10 +257,16 @@ export class InteractiveClient {
257257
targetKernelName: kernelName
258258
}
259259
);
260-
return this.submitCommandAndGetResult<CompletionsProduced>(command, CompletionsProducedType);
260+
let result = await this.submitCommandAndGetResult<CompletionsProduced>(command, CompletionsProducedType, true);
261+
if (result === undefined) {
262+
result = {
263+
completions: []
264+
};
265+
}
266+
return result!;
261267
}
262268

263-
hover(language: string, code: string, line: number, character: number): Promise<HoverTextProduced> {
269+
async hover(language: string, code: string, line: number, character: number): Promise<HoverTextProduced> {
264270
const command = new KernelCommandEnvelope(
265271
RequestHoverTextType,
266272
<RequestHoverText>{
@@ -272,10 +278,16 @@ export class InteractiveClient {
272278
targetKernelName: language
273279
}
274280
);
275-
return this.submitCommandAndGetResult<HoverTextProduced>(command, HoverTextProducedType);
281+
let result = await this.submitCommandAndGetResult<HoverTextProduced>(command, HoverTextProducedType, true);
282+
if (result === undefined) {
283+
result = {
284+
content: []
285+
};
286+
}
287+
return result!;
276288
}
277289

278-
signatureHelp(language: string, code: string, line: number, character: number): Promise<SignatureHelpProduced> {
290+
async signatureHelp(language: string, code: string, line: number, character: number): Promise<SignatureHelpProduced> {
279291
const command = new KernelCommandEnvelope(
280292
RequestSignatureHelpType,
281293
<RequestSignatureHelp>{
@@ -287,7 +299,15 @@ export class InteractiveClient {
287299
targetKernelName: language
288300
}
289301
);
290-
return this.submitCommandAndGetResult<SignatureHelpProduced>(command, SignatureHelpProducedType);
302+
let result = await this.submitCommandAndGetResult<SignatureHelpProduced>(command, SignatureHelpProducedType, true);
303+
if (result === undefined) {
304+
result = {
305+
activeParameterIndex: 0,
306+
activeSignatureIndex: 0,
307+
signatures: []
308+
};
309+
}
310+
return result!;
291311
}
292312

293313
async getDiagnostics(kernelName: string, code: string): Promise<Array<Diagnostic>> {
@@ -299,7 +319,10 @@ export class InteractiveClient {
299319
}
300320
);
301321

302-
const diagsProduced = await this.submitCommandAndGetResult<DiagnosticsProduced>(command, DiagnosticsProducedType);
322+
let diagsProduced = await this.submitCommandAndGetResult<DiagnosticsProduced>(command, DiagnosticsProducedType, true);
323+
if (diagsProduced === undefined) {
324+
return [];
325+
}
303326
return diagsProduced.diagnostics;
304327
}
305328

@@ -318,18 +341,19 @@ export class InteractiveClient {
318341
return disposable;
319342
}
320343

321-
requestValueInfos(kernelName: string): Promise<ValueInfosProduced> {
344+
async requestValueInfos(kernelName: string): Promise<ValueInfosProduced> {
322345
const command = new KernelCommandEnvelope(
323346
RequestValueInfosType,
324347
<RequestValueInfos>{
325348
targetKernelName: kernelName,
326349
mimeType: "text/plain+summary"
327350
}
328351
);
329-
return this.submitCommandAndGetResult(command, ValueInfosProducedType);
352+
const result = await (this.submitCommandAndGetResult<ValueInfosProduced>(command, ValueInfosProducedType));
353+
return result!;
330354
}
331355

332-
requestValue(valueName: string, kernelName: string): Promise<ValueProduced> {
356+
async requestValue(valueName: string, kernelName: string): Promise<ValueProduced> {
333357
const command = new KernelCommandEnvelope(
334358
RequestValueType,
335359
<RequestValue>{
@@ -338,7 +362,8 @@ export class InteractiveClient {
338362
targetKernelName: kernelName,
339363
}
340364
);
341-
return this.submitCommandAndGetResult(command, ValueProducedType);
365+
const result = await this.submitCommandAndGetResult<ValueProduced>(command, ValueProducedType);
366+
return result!;
342367
}
343368

344369
cancel(): Promise<void> {
@@ -364,12 +389,12 @@ export class InteractiveClient {
364389
this.disposables.push(disposable);
365390
}
366391

367-
private submitCommandAndGetResult<TEvent extends KernelEvent>(command: KernelCommandEnvelope, expectedEventType: KernelEventType): Promise<TEvent> {
368-
return new Promise<TEvent>(async (resolve, reject) => {
392+
private submitCommandAndGetResult<TEvent extends KernelEvent>(command: KernelCommandEnvelope, expectedEventType: KernelEventType, eventIsOptional = false): Promise<TEvent | undefined> {
393+
return new Promise<TEvent | undefined>(async (resolve, reject) => {
369394
let handled = false;
370395
const token = command.getOrCreateToken();
371396
let disposable = this.subscribeToKernelTokenEvents(token, eventEnvelope => {
372-
if (eventEnvelope.command?.hasSameRootCommandAs(command) && eventEnvelope.eventType === expectedEventType) {
397+
if (eventEnvelope.command?.hasSameRootCommandAs(command)) {
373398
switch (eventEnvelope.eventType) {
374399
case CommandFailedType:
375400
if (!handled) {
@@ -383,7 +408,11 @@ export class InteractiveClient {
383408
if (!handled) {
384409
handled = true;
385410
disposable.dispose();
386-
reject('Command was handled before reporting expected result.');
411+
if (eventIsOptional) {
412+
resolve(undefined);
413+
} else {
414+
reject('Command was handled before reporting expected result.');
415+
}
387416
}
388417
break;
389418
default:

‎src/polyglot-notebooks-vscode-common/src/languageServices/hover.ts

+13-7
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,19 @@ export function provideHover(clientMapper: ClientMapper, language: string, docum
1010
return debounceAndReject(`hover-${documentUri.toString()}`, languageServiceDelay, async () => {
1111
const client = await clientMapper.getOrAddClient(documentUri);
1212
const hoverText = await client.hover(language, documentText, position.line, position.character);
13-
const content = hoverText.content.sort((a, b) => mimeTypeToPriority(a.mimeType) - mimeTypeToPriority(b.mimeType))[0];
14-
const hoverResult = {
15-
contents: content.value,
16-
isMarkdown: content.mimeType === 'text/markdown' || content.mimeType === 'text/x-markdown',
17-
range: hoverText.linePositionSpan
18-
};
19-
return hoverResult;
13+
if (hoverText.content.length > 0) {
14+
const content = hoverText.content.sort((a, b) => mimeTypeToPriority(a.mimeType) - mimeTypeToPriority(b.mimeType))[0];
15+
const hoverResult = {
16+
contents: content.value,
17+
isMarkdown: content.mimeType === 'text/markdown' || content.mimeType === 'text/x-markdown',
18+
range: hoverText.linePositionSpan
19+
};
20+
return hoverResult;
21+
} return {
22+
contents: "",
23+
isMarkdown: false,
24+
range: undefined
25+
}
2026
});
2127
}
2228

0 commit comments

Comments
 (0)