@@ -4,7 +4,8 @@ import type {
4
4
IVirtualPosition ,
5
5
IRootPosition ,
6
6
Document ,
7
- ForeignDocumentsMap
7
+ ForeignDocumentsMap ,
8
+ IForeignCodeExtractor
8
9
} from '@jupyterlab/lsp' ;
9
10
import { VirtualDocument as VirtualDocumentBase } from '@jupyterlab/lsp' ;
10
11
@@ -33,6 +34,9 @@ export class VirtualDocument extends VirtualDocumentBase {
33
34
this . lineMagicsOverrides = new ReversibleOverridesMap (
34
35
overrides ? overrides . line : [ ]
35
36
) ;
37
+ // override private `chooseForeignDocument` as a workaround for
38
+ // https://github.com/jupyter-lsp/jupyterlab-lsp/issues/959
39
+ this [ 'chooseForeignDocument' ] = this . _chooseForeignDocument ;
36
40
}
37
41
38
42
// TODO: this could be moved out
@@ -202,4 +206,38 @@ export class VirtualDocument extends VirtualDocumentBase {
202
206
}
203
207
return [ ...maps . values ( ) ] ;
204
208
}
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
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
233
+ // @ts -ignore
234
+ foreignDocument = this . openForeign (
235
+ extractor . language ,
236
+ extractor . standalone ,
237
+ extractor . fileExtension
238
+ ) ;
239
+ }
240
+ }
241
+ return foreignDocument ;
242
+ }
205
243
}
0 commit comments