src/vs/editor/common/model/mirrorTextModel.ts
197 LOC · 188 covered · 9 uncovered · 32 ranges · 150 concepts · 13 introducers · 108 tests
File neighbourhood
The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.
Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file
In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.
Graph controls are ready.
Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.
/*---------------------------------------------------------------------------------------------
mirrorTextModel.ts ×10
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { splitLines } from '../../../base/common/strings.js';
import { URI } from '../../../base/common/uri.js';
import { Position } from '../core/position.js';
import { IRange } from '../core/range.js';
import { PrefixSumComputer } from './prefixSumComputer.js';
export interface IModelContentChange {
/**
* The old range that got replaced.
*/
readonly range: IRange;
/**
* The offset of the range that got replaced.
*/
readonly rangeOffset: number;
/**
* The length of the range that got replaced.
*/
readonly rangeLength: number;
/**
* The new text for the range.
*/
readonly text: string;
}
export interface IModelChangedEvent {
/**
* The actual changes.
*/
readonly changes: IModelContentChange[];
/**
* The (new) end-of-line character.
*/
readonly eol: string;
/**
* The new version id the model has transitioned to.
*/
readonly versionId: number;
/**
* Flag that indicates that this event was generated while undoing.
*/
readonly isUndoing: boolean;
/**
* Flag that indicates that this event was generated while redoing.
*/
readonly isRedoing: boolean;
}
export interface IMirrorTextModel {
readonly version: number;
}
export class MirrorTextModel implements IMirrorTextModel {
protected _uri: URI;
protected _lines: string[];
protected _eol: string;
protected _versionId: number;
protected _lineStarts: PrefixSumComputer | null;
private _cachedTextValue: string | null;
constructor(uri: URI, lines: string[], eol: string, versionId: number) {
this._lines = lines;
this._eol = eol;
this._versionId = versionId;
this._lineStarts = null;
this._cachedTextValue = null;
}
dispose(): void {
}
get version(): number {
}
getText(): string {
this._cachedTextValue = this._lines.join(this._eol);
}
return this._cachedTextValue;
}
onEvents(e: IModelChangedEvent): void {
this._lineStarts = null;
}
// Update my lines
const changes = e.changes;
for (const change of changes) {
this._acceptDeleteRange(change.range);
this._acceptInsertText(new Position(change.range.startLineNumber, change.range.startColumn), change.text);
}
this._versionId = e.versionId;
this._cachedTextValue = null;
}
protected _ensureLineStarts(): void {
const eolLength = this._eol.length;
const linesLength = this._lines.length;
const lineStartValues = new Uint32Array(linesLength);
for (let i = 0; i < linesLength; i++) {
lineStartValues[i] = this._lines[i].length + eolLength;
}
this._lineStarts = new PrefixSumComputer(lineStartValues);
}
}
/**
* All changes to a line's text go through this method
*/
private _setLineText(lineIndex: number, newValue: string): void {
if (this._lineStarts) {
// update prefix sum
this._lineStarts.setValue(lineIndex, this._lines[lineIndex].length + this._eol.length);
}
private _acceptDeleteRange(range: IRange): void {
if (range.startLineNumber === range.endLineNumber) {
return;
}
this._setLineText(range.startLineNumber - 1,
this._lines[range.startLineNumber - 1].substring(0, range.startColumn - 1)
+ this._lines[range.startLineNumber - 1].substring(range.endColumn - 1)
);
return;
}
// Take remaining text on last line and append it to remaining text on first line
this._setLineText(range.startLineNumber - 1,
this._lines[range.startLineNumber - 1].substring(0, range.startColumn - 1)
+ this._lines[range.endLineNumber - 1].substring(range.endColumn - 1)
);
// Delete middle lines
this._lines.splice(range.startLineNumber, range.endLineNumber - range.startLineNumber);
if (this._lineStarts) {
// update prefix sum
this._lineStarts.removeValues(range.startLineNumber, range.endLineNumber - range.startLineNumber);
}
private _acceptInsertText(position: Position, insertText: string): void {
return;
}
if (insertLines.length === 1) {
this._setLineText(position.lineNumber - 1,
this._lines[position.lineNumber - 1].substring(0, position.column - 1)
+ insertLines[0]
+ this._lines[position.lineNumber - 1].substring(position.column - 1)
);
return;
}
// Append overflowing text from first line to the end of text to insert
insertLines[insertLines.length - 1] += this._lines[position.lineNumber - 1].substring(position.column - 1);
// Delete overflowing text from first line and insert text on first line
this._setLineText(position.lineNumber - 1,
this._lines[position.lineNumber - 1].substring(0, position.column - 1)
+ insertLines[0]
);
// Insert new lines & store lengths
const newLengths = new Uint32Array(insertLines.length - 1);
for (let i = 1; i < insertLines.length; i++) {
this._lines.splice(position.lineNumber + i - 1, 0, insertLines[i]);
newLengths[i - 1] = insertLines[i].length + this._eol.length;
}
if (this._lineStarts) {
// update prefix sum
this._lineStarts.insertValues(position.lineNumber, newLengths);
}