Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/editor/common/diff/defaultLinesDiffComputer/lineSequence.ts
3296 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { CharCode } from '../../../../base/common/charCode.js';
7
import { OffsetRange } from '../../core/ranges/offsetRange.js';
8
import { ISequence } from './algorithms/diffAlgorithm.js';
9
10
export class LineSequence implements ISequence {
11
constructor(
12
private readonly trimmedHash: number[],
13
private readonly lines: string[]
14
) { }
15
16
getElement(offset: number): number {
17
return this.trimmedHash[offset];
18
}
19
20
get length(): number {
21
return this.trimmedHash.length;
22
}
23
24
getBoundaryScore(length: number): number {
25
const indentationBefore = length === 0 ? 0 : getIndentation(this.lines[length - 1]);
26
const indentationAfter = length === this.lines.length ? 0 : getIndentation(this.lines[length]);
27
return 1000 - (indentationBefore + indentationAfter);
28
}
29
30
getText(range: OffsetRange): string {
31
return this.lines.slice(range.start, range.endExclusive).join('\n');
32
}
33
34
isStronglyEqual(offset1: number, offset2: number): boolean {
35
return this.lines[offset1] === this.lines[offset2];
36
}
37
}
38
39
function getIndentation(str: string): number {
40
let i = 0;
41
while (i < str.length && (str.charCodeAt(i) === CharCode.Space || str.charCodeAt(i) === CharCode.Tab)) {
42
i++;
43
}
44
return i;
45
}
46
47