Skip to content

Commit 17301c4

Browse files
committed
Restore reuse of unused standalone connections
1 parent d5782eb commit 17301c4

File tree

1 file changed

+38
-1
lines changed

1 file changed

+38
-1
lines changed

packages/jupyterlab-lsp/src/virtual/document.ts

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import type {
44
IVirtualPosition,
55
IRootPosition,
66
Document,
7-
ForeignDocumentsMap
7+
ForeignDocumentsMap,
8+
IForeignCodeExtractor
89
} from '@jupyterlab/lsp';
910
import { VirtualDocument as VirtualDocumentBase } from '@jupyterlab/lsp';
1011

@@ -33,6 +34,9 @@ export class VirtualDocument extends VirtualDocumentBase {
3334
this.lineMagicsOverrides = new ReversibleOverridesMap(
3435
overrides ? overrides.line : []
3536
);
37+
// override private `chooseForeignDocument` as a workaround for
38+
// https://github.com/jupyter-lsp/jupyterlab-lsp/issues/959
39+
this['chooseForeignDocument'] = this._chooseForeignDocument;
3640
}
3741

3842
// TODO: this could be moved out
@@ -202,4 +206,37 @@ export class VirtualDocument extends VirtualDocumentBase {
202206
}
203207
return [...maps.values()];
204208
}
209+
210+
/**
211+
* Get the foreign document that can be opened with the input extractor.
212+
*/
213+
private _chooseForeignDocument(
214+
extractor: IForeignCodeExtractor
215+
): VirtualDocumentBase {
216+
let foreignDocument: VirtualDocumentBase;
217+
// if not standalone, try to append to existing document
218+
let foreignExists = this.foreignDocuments.has(extractor.language);
219+
if (!extractor.standalone && foreignExists) {
220+
foreignDocument = this.foreignDocuments.get(extractor.language)!;
221+
} else {
222+
// if standalone, try to re-use existing connection to the server
223+
let unusedStandalone = this.unusedStandaloneDocuments.get(
224+
extractor.language
225+
);
226+
if (extractor.standalone && unusedStandalone.length > 0) {
227+
foreignDocument = unusedStandalone.pop()!;
228+
this.unusedDocuments.delete(foreignDocument);
229+
} else {
230+
// if (previous document does not exists) or (extractor produces standalone documents
231+
// and no old standalone document could be reused): create a new document
232+
// @ts-ignore
233+
foreignDocument = this.openForeign(
234+
extractor.language,
235+
extractor.standalone,
236+
extractor.fileExtension
237+
);
238+
}
239+
}
240+
return foreignDocument;
241+
}
205242
}

0 commit comments

Comments
 (0)