-
-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathmockEditor.ts
169 lines (152 loc) · 4.46 KB
/
mockEditor.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import { URI } from "vscode-uri";
import {
EndOfLine,
Position,
Range,
Selection,
TextDocument,
TextEditor,
TextEditorOptions,
TextLine,
} from "..";
// See the TextLine, TextEditor, and TextDocument interfaces
// for documentation of these classes and their fields.
export class MockTextLine implements TextLine {
readonly lineNumber: number;
readonly text: string;
readonly range: Range;
readonly rangeIncludingLineBreak: Range;
readonly firstNonWhitespaceCharacterIndex: number;
readonly lastNonWhitespaceCharacterIndex: number;
readonly isEmptyOrWhitespace: boolean;
constructor(lineNumber: number, text: string) {
if (lineNumber < 0) {
throw new Error("lineNumber must be non-negative");
}
this.lineNumber = lineNumber;
// capture any trailing \r\n or \n as eol (possibly neither is present)
const eol = text.match(/(\r?\n)$/)?.[1] ?? "";
if (eol.length > 0) {
this.text = text.slice(0, -eol.length);
} else {
this.text = text;
}
this.range = new Range(
this.lineNumber,
0,
this.lineNumber,
this.text.length,
);
this.rangeIncludingLineBreak = new Range(
this.lineNumber,
0,
this.lineNumber,
this.text.length + eol.length,
);
const first = this.text.search(/\S/);
this.firstNonWhitespaceCharacterIndex =
first === -1 ? this.text.length : first;
const all = this.text.match(/\S/g);
this.lastNonWhitespaceCharacterIndex = all
? this.text.lastIndexOf(all[all.length - 1])
: 0;
this.isEmptyOrWhitespace =
this.firstNonWhitespaceCharacterIndex === this.text.length;
}
}
export class MockTextDocument implements TextDocument {
readonly uri: URI;
readonly languageId: string;
readonly version: number;
readonly range: Range;
readonly eol: EndOfLine;
private lines: MockTextLine[];
private contents: string;
constructor(filename: string, languageId: string, contents: string) {
this.uri = URI.file(filename);
this.languageId = languageId;
this.version = 1;
this.contents = contents;
const rawLines: string[] = contents.match(/[^\n]*\n|[^\n]+/g) ?? [];
this.lines = rawLines.map((line, i) => {
return new MockTextLine(i, line);
});
if (this.lines.length === 0) {
this.range = new Range(0, 0, 0, 0);
} else {
const lastLineRange = this.lines[this.lines.length - 1].range;
this.range = new Range(
0,
0,
lastLineRange.end.line,
lastLineRange.end.character,
);
}
this.eol = "LF";
}
get lineCount(): number {
return this.lines.length;
}
public lineAt(x: number | Position): TextLine {
if (typeof x === "number") {
return this.lines[x];
}
return this.lines[x.line];
}
public offsetAt(position: Position): number {
let offset = 0;
for (let i = 0; i < position.line; i++) {
offset += this.lineAt(i).rangeIncludingLineBreak.end.character;
}
offset += position.character;
return offset;
}
public positionAt(offset: number): Position {
let line = 0;
while (offset >= this.lineAt(line).rangeIncludingLineBreak.end.character) {
offset -= this.lineAt(line).rangeIncludingLineBreak.end.character;
line++;
}
return new Position(line, offset);
}
public getText(range?: Range): string {
if (range === undefined) {
return this.contents;
}
const startOffset = this.offsetAt(range.start);
const endOffset = this.offsetAt(range.end);
return this.contents.slice(startOffset, endOffset);
}
}
export class MockTextEditor implements TextEditor {
public primarySelection: Selection;
readonly id: string;
readonly document: TextDocument;
readonly options: TextEditorOptions;
readonly isActive: boolean;
constructor(document: TextDocument, active: boolean) {
this.id = document.uri.toString();
this.document = document;
this.primarySelection = new Selection(0, 0, 0, 0);
this.options = new DefaultTextEditorOptions();
this.isActive = active;
// TODO: support visible ranges, multiple selections, options
}
get visibleRanges(): Range[] {
return [this.document.range];
}
get selections(): Selection[] {
return [this.primarySelection];
}
isEqual(other: TextEditor): boolean {
return this.id === other.id;
}
}
class DefaultTextEditorOptions implements TextEditorOptions {
get tabSize(): number | string {
return 4;
}
get insertSpaces(): boolean | string {
return true;
}
}