Skip to content

Commit

Permalink
Improve VirtualDocument
Browse files Browse the repository at this point in the history
  • Loading branch information
trungleduc committed Oct 12, 2023
1 parent b9168b5 commit c01c013
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 14 deletions.
27 changes: 19 additions & 8 deletions packages/lsp/src/virtual/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -461,9 +461,6 @@ export class VirtualDocument implements IDisposable {
* Clear the virtual document and all related stuffs
*/
clear(): void {
this.unusedStandaloneDocuments.forEach(item =>
item.forEach(doc => doc.dispose())
);
this.unusedStandaloneDocuments.clear();

for (let document of this.foreignDocuments.values()) {
Expand Down Expand Up @@ -745,7 +742,7 @@ export class VirtualDocument implements IDisposable {
);
continue;
}
let foreignDocument = this.chooseForeignDocument(extractor);
let foreignDocument = this._chooseForeignDocument(extractor);
foreignDocumentsMap.set(result.range, {
virtualLine: foreignDocument.lastVirtualLine,
virtualDocument: foreignDocument,
Expand Down Expand Up @@ -800,8 +797,6 @@ export class VirtualDocument implements IDisposable {
* Close all foreign documents.
*/
closeAllForeignDocuments(): void {
console.log('closing');

for (let document of this.foreignDocuments.values()) {
this.closeForeign(document);
}
Expand Down Expand Up @@ -896,6 +891,19 @@ export class VirtualDocument implements IDisposable {
} as ISourcePosition;
}

/**
* Compute the position in root document from the position of
* a virtual document.
*/
transformVirtualToRoot(position: IVirtualPosition): IRootPosition | null {
const editor = this.virtualLines.get(position.line)?.editor;
const editorPosition = this.transformVirtualToEditor(position);
if (!editor || !editorPosition) {
return null;
}
return this.root.transformFromEditorToRoot(editor, editorPosition);
}

/**
* Get the corresponding editor of the virtual line.
*/
Expand Down Expand Up @@ -973,7 +981,7 @@ export class VirtualDocument implements IDisposable {
/**
* Get the foreign document that can be opened with the input extractor.
*/
private chooseForeignDocument(
private _chooseForeignDocument(
extractor: IForeignCodeExtractor
): VirtualDocument {
let foreignDocument: VirtualDocument;
Expand Down Expand Up @@ -1012,13 +1020,16 @@ export class VirtualDocument implements IDisposable {
standalone: boolean,
fileExtension: string
): VirtualDocument {
let document = new VirtualDocument({
let document = new (this.constructor as new (
...args: ConstructorParameters<typeof VirtualDocument>
) => VirtualDocument)({
...this.options,
parent: this,
standalone: standalone,
fileExtension: fileExtension,
language: language
});

const context: Document.IForeignContext = {
foreignDocument: document,
parentHost: this
Expand Down
12 changes: 6 additions & 6 deletions packages/lsp/test/document.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,16 +225,16 @@ describe('@jupyterlab/lsp', () => {
expect(foreignDocumentsMap.size).toEqual(1);
});
});
describe('#chooseForeignDocument', () => {
describe('#_chooseForeignDocument', () => {
it('should select the foreign document for markdown cell', () => {
const md: VirtualDocument = document['chooseForeignDocument'](
const md: VirtualDocument = document['_chooseForeignDocument'](
markdownCellExtractor
);
expect(md.uri).toBe('test.ipynb.python-markdown.md');
});
it('should select the foreign document for raw cell', () => {
const md: VirtualDocument =
document['chooseForeignDocument'](rawCellExtractor);
document['_chooseForeignDocument'](rawCellExtractor);
expect(md.uri).toBe('test.ipynb.python-text.txt');
});
});
Expand Down Expand Up @@ -282,14 +282,14 @@ describe('@jupyterlab/lsp', () => {
it('should emit the `foreignDocumentClosed` signal', () => {
const cb = jest.fn();
document.foreignDocumentClosed.connect(cb);
const md: VirtualDocument = document['chooseForeignDocument'](
const md: VirtualDocument = document['_chooseForeignDocument'](
markdownCellExtractor
);
document.closeForeign(md);
expect(cb).toHaveBeenCalled();
});
it('should close correctly foreign documents', () => {
const md: VirtualDocument = document['chooseForeignDocument'](
const md: VirtualDocument = document['_chooseForeignDocument'](
markdownCellExtractor
);
md.closeAllForeignDocuments = jest.fn();
Expand All @@ -312,7 +312,7 @@ describe('@jupyterlab/lsp', () => {
);
});
it('should get the markdown content of the document', () => {
const md = document['chooseForeignDocument'](markdownCellExtractor);
const md = document['_chooseForeignDocument'](markdownCellExtractor);

expect(md.value).toContain(
'test line in markdown 1\ntest line in markdown 2'
Expand Down

0 comments on commit c01c013

Please sign in to comment.