src/vs/editor/common/diff/defaultLinesDiffComputer/linesSliceCharSequence.ts
246 LOC · 210 covered · 36 uncovered · 70 ranges · 310 concepts · 28 introducers · 138 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.
/*---------------------------------------------------------------------------------------------
diffAlgorithm.ts ×23
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { findLastIdxMonotonous, findLastMonotonous, findFirstMonotonous } from '../../../../base/common/arraysFind.js';
import { CharCode } from '../../../../base/common/charCode.js';
import { OffsetRange } from '../../core/ranges/offsetRange.js';
import { Position } from '../../core/position.js';
import { Range } from '../../core/range.js';
import { ISequence } from './algorithms/diffAlgorithm.js';
import { isSpace } from './utils.js';
export class LinesSliceCharSequence implements ISequence {
private readonly elements: number[] = [];
private readonly firstElementOffsetByLineIdx: number[] = [];
private readonly lineStartOffsets: number[] = [];
private readonly trimmedWsLengthsByLineIdx: number[] = [];
constructor(public readonly lines: string[], private readonly range: Range, public readonly considerWhitespaceChanges: boolean) {
for (let lineNumber = this.range.startLineNumber; lineNumber <= this.range.endLineNumber; lineNumber++) {
let line = lines[lineNumber - 1];
let lineStartOffset = 0;
if (lineNumber === this.range.startLineNumber && this.range.startColumn > 1) {
line = line.substring(lineStartOffset);
}
let trimmedWsLength = 0;
if (!considerWhitespaceChanges) {
trimmedWsLength = line.length - trimmedStartLine.length;
line = trimmedStartLine.trimEnd();
}
const lineLength = lineNumber === this.range.endLineNumber ? Math.min(this.range.endColumn - 1 - lineStartOffset - trimmedWsLength, line.length) : line.length;
for (let i = 0; i < lineLength; i++) {
this.elements.push(line.charCodeAt(i));
}
if (lineNumber < this.range.endLineNumber) {
this.firstElementOffsetByLineIdx.push(this.elements.length);
}
}
toString() {
return `Slice: "${this.text}"`;
}
get text(): string {
return this.getText(new OffsetRange(0, this.length));
}
getText(range: OffsetRange): string {
return this.elements.slice(range.start, range.endExclusive).map(e => String.fromCharCode(e)).join('');
linesSliceCharSequence.ts ×2
}
getElement(offset: number): number {
}
get length(): number {
}
public getBoundaryScore(length: number): number {
// 11 0 0 12 15 6 13 0 0 11
const prevCategory = getCategory(length > 0 ? this.elements[length - 1] : -1);
const nextCategory = getCategory(length < this.elements.length ? this.elements[length] : -1);
if (prevCategory === CharBoundaryCategory.LineBreakCR && nextCategory === CharBoundaryCategory.LineBreakLF) {
// don't break between \r and \n
return 0;
}
return 150;
}
let score = 0;
if (prevCategory !== nextCategory) {
score += 10;
if (prevCategory === CharBoundaryCategory.WordLower && nextCategory === CharBoundaryCategory.WordUpper) {
}
score += getCategoryBoundaryScore(prevCategory);
score += getCategoryBoundaryScore(nextCategory);
return score;
}
public translateOffset(offset: number, preference: 'left' | 'right' = 'right'): Position {
// find smallest i, so that lineBreakOffsets[i] <= offset using binary search
linesSliceCharSequence.ts ×1
const i = findLastIdxMonotonous(this.firstElementOffsetByLineIdx, (value) => value <= offset);
const lineOffset = offset - this.firstElementOffsetByLineIdx[i];
return new Position(
this.range.startLineNumber + i,
1 + this.lineStartOffsets[i] + lineOffset + ((lineOffset === 0 && preference === 'left') ? 0 : this.trimmedWsLengthsByLineIdx[i])
);
}
public translateRange(range: OffsetRange): Range {
const pos1 = this.translateOffset(range.start, 'right');
heuristicSequenceOptimizations.ts ×26
const pos2 = this.translateOffset(range.endExclusive, 'left');
if (pos2.isBefore(pos1)) {
}
}
/**
* Finds the word that contains the character at the given offset
*/
public findWordContaining(offset: number): OffsetRange | undefined {
return undefined;
}
if (!isWordChar(this.elements[offset])) {
return undefined;
}
// find start
let start = offset;
}
// find end
let end = offset;
while (end < this.elements.length && isWordChar(this.elements[end])) {
linesSliceCharSequence.ts ×7
}
return new OffsetRange(start, end);
/** fooBar has the two sub-words foo and bar */
public findSubWordContaining(offset: number): OffsetRange | undefined {
if (offset < 0 || offset >= this.elements.length) {
return undefined;
}
if (!isWordChar(this.elements[offset])) {
return undefined;
}
// find start
let start = offset;
while (start > 0 && isWordChar(this.elements[start - 1]) && !isUpperCase(this.elements[start])) {
start--;
}
// find end
let end = offset;
while (end < this.elements.length && isWordChar(this.elements[end]) && !isUpperCase(this.elements[end])) {
end++;
}
return new OffsetRange(start, end);
}
public countLinesIn(range: OffsetRange): number {
return this.translateOffset(range.endExclusive).lineNumber - this.translateOffset(range.start).lineNumber;
heuristicSequenceOptimizations.ts ×5
}
public isStronglyEqual(offset1: number, offset2: number): boolean {
}
public extendToFullLines(range: OffsetRange): OffsetRange {
const start = findLastMonotonous(this.firstElementOffsetByLineIdx, x => x <= range.start) ?? 0;
linesSliceCharSequence.ts ×2
const end = findFirstMonotonous(this.firstElementOffsetByLineIdx, x => range.endExclusive <= x) ?? this.elements.length;
return new OffsetRange(start, end);
}
return charCode >= CharCode.a && charCode <= CharCode.z
|| charCode >= CharCode.A && charCode <= CharCode.Z
|| charCode >= CharCode.Digit0 && charCode <= CharCode.Digit9;
}
function isUpperCase(charCode: number): boolean {
return charCode >= CharCode.A && charCode <= CharCode.Z;
}
const enum CharBoundaryCategory {
WordLower,
WordUpper,
WordNumber,
End,
Other,
Separator,
Space,
LineBreakCR,
LineBreakLF,
}
const score: Record<CharBoundaryCategory, number> = {
[CharBoundaryCategory.WordLower]: 0,
[CharBoundaryCategory.WordUpper]: 0,
[CharBoundaryCategory.WordNumber]: 0,
[CharBoundaryCategory.End]: 10,
[CharBoundaryCategory.Other]: 2,
[CharBoundaryCategory.Separator]: 30,
[CharBoundaryCategory.Space]: 3,
[CharBoundaryCategory.LineBreakCR]: 10,
[CharBoundaryCategory.LineBreakLF]: 10,
};
function getCategoryBoundaryScore(category: CharBoundaryCategory): number {
linesSliceCharSequence.ts ×12
return score[category];
}
if (charCode === CharCode.LineFeed) {
return CharBoundaryCategory.LineBreakCR;
} else if (charCode >= CharCode.Digit0 && charCode <= CharCode.Digit9) {
linesSliceCharSequence.ts ×12
} else if (charCode === CharCode.Comma || charCode === CharCode.Semicolon) {
linesSliceCharSequence.ts ×2
}