UNPKG

@jupyter-lsp/jupyterlab-lsp

Version:

Language Server Protocol integration for JupyterLab

114 lines 5.05 kB
import { VirtualDocument as VirtualDocumentBase } from '@jupyterlab/lsp'; import { ReversibleOverridesMap } from '../overrides/maps'; export class VirtualDocument extends VirtualDocumentBase { constructor(options) { super(options); const overrides = this.language in options.overridesRegistry ? options.overridesRegistry[this.language] : null; this.cellMagicsOverrides = new ReversibleOverridesMap(overrides ? overrides.cell : []); this.lineMagicsOverrides = new ReversibleOverridesMap(overrides ? overrides.line : []); } // TODO: this could be moved out decodeCodeBlock(rawCode) { // TODO: add back previously extracted foreign code const cellOverride = this.cellMagicsOverrides.reverse.overrideFor(rawCode); if (cellOverride != null) { return cellOverride; } else { let lines = this.lineMagicsOverrides.reverseReplaceAll(rawCode.split('\n')); return lines.join('\n'); } } /** * Extends parent method to hook cell magics overrides. */ prepareCodeBlock(block, editorShift = { line: 0, column: 0 }) { let lines; let skipInspect; let { cellCodeKept, foreignDocumentsMap } = this.extractForeignCode(block, editorShift); const cellCode = cellCodeKept; // cell magics are replaced if requested and matched const cellOverride = this.cellMagicsOverrides.overrideFor(cellCode); if (cellOverride != null) { lines = cellOverride.split('\n'); skipInspect = lines.map(_line => [this.idPath]); } else { // otherwise, we replace line magics - if any let result = this.lineMagicsOverrides.replaceAll(cellCode.split('\n')); lines = result.lines; skipInspect = result.skipInspect.map(skip => (skip ? [this.idPath] : [])); } return { lines, foreignDocumentsMap, skipInspect }; } appendCodeBlock(block, editorShift = { line: 0, column: 0 }, virtualShift) { let cellCode = block.value; let ceEditor = block.ceEditor; if (this.isDisposed) { console.warn('Cannot append code block: document disposed'); return; } let sourceCellLines = cellCode.split('\n'); let { lines, foreignDocumentsMap, skipInspect } = this.prepareCodeBlock(block, editorShift); for (let i = 0; i < lines.length; i++) { this.virtualLines.set(this.lastVirtualLine + i, { skipInspect: skipInspect[i], editor: ceEditor, // TODO this is incorrect, wont work if something was extracted sourceLine: this.lastSourceLine + i }); } for (let i = 0; i < sourceCellLines.length; i++) { this.sourceLines.set(this.lastSourceLine + i, { editorLine: i, editorShift: { line: editorShift.line - ((virtualShift === null || virtualShift === void 0 ? void 0 : virtualShift.line) || 0), column: i === 0 ? editorShift.column - ((virtualShift === null || virtualShift === void 0 ? void 0 : virtualShift.column) || 0) : 0 }, // TODO: move those to a new abstraction layer (DocumentBlock class) editor: ceEditor, foreignDocumentsMap, // TODO this is incorrect, wont work if something was extracted virtualLine: this.lastVirtualLine + i }); } this.lastVirtualLine += lines.length; // one empty line is necessary to separate code blocks, next 'n' lines are to silence linters; // the final cell does not get the additional lines (thanks to the use of join, see below) this.lineBlocks.push(lines.join('\n') + '\n'); // adding the virtual lines for the blank lines for (let i = 0; i < this.blankLinesBetweenCells; i++) { this.virtualLines.set(this.lastVirtualLine + i, { skipInspect: [this.idPath], editor: ceEditor, sourceLine: null }); } this.lastVirtualLine += this.blankLinesBetweenCells; this.lastSourceLine += sourceCellLines.length; } /** * @experimental */ transformVirtualToRoot(position) { let editor = this.virtualLines.get(position.line).editor; let editorPosition = this.transformVirtualToEditor(position); // only root holds the full editor mapping return this.root.transformFromEditorToRoot(editor, editorPosition); } /** * @experimental */ getForeignDocuments(editorAccessor) { let maps = new Set(); for (let line of this.sourceLines.values()) { if (line.editor === editorAccessor) { maps.add(line.foreignDocumentsMap); } } return [...maps.values()]; } } //# sourceMappingURL=document.js.map