forked from Silverquark/dance
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lines.ts
214 lines (174 loc) · 5.47 KB
/
lines.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import * as vscode from "vscode";
import { Context } from "./context";
/**
* Returns the 0-based number of the first visible line in the current editor.
*/
export function firstVisibleLine(editor = Context.current.editor) {
return editor.visibleRanges[0].start.line;
}
/**
* Returns the 0-based number of the middle visible line in the current editor.
*/
export function middleVisibleLine(editor = Context.current.editor) {
const range = editor.visibleRanges[0];
return ((range.start.line + range.end.line) / 2) | 0;
}
/**
* Returns the 0-based number of the last visible line in the current editor.
*/
export function lastVisibleLine(editor = Context.current.editor) {
return editor.visibleRanges[0].end.line;
}
/**
* Returns the text contents of the given line.
*/
export function text(line: number, document = Context.current.document) {
return document.lineAt(line).text;
}
/**
* Returns the length of the given line.
*/
export function length(line: number, document = Context.current.document) {
return document.lineAt(line).text.length;
}
/**
* Returns whether the given line is empty.
*/
export function isEmpty(line: number, document = Context.current.document) {
return length(line, document) === 0;
}
/**
* Returns the given line number, possibly modified to fit in the current
* document.
*/
export function clamp(line: number, document?: vscode.TextDocument) {
if (line < 0) {
return 0;
}
const lastLine = (document ?? Context.current.document).lineCount - 1;
if (line > lastLine) {
return lastLine;
}
return line;
}
function diffAddedByTabs(text: string, editor: Pick<vscode.TextEditor, "options">) {
const tabSize = editor.options.tabSize as number;
let total = 0;
for (const ch of text) {
if (ch === "\t") {
total += tabSize - 1;
}
}
return total;
}
function getCharacter(
text: string,
column: number,
editor: Pick<vscode.TextEditor, "options">,
roundUp: boolean,
) {
const tabSize = editor.options.tabSize as number;
let character = 0;
for (const ch of text) {
if (column <= 0) {
break;
}
if (ch === "\t") {
column -= tabSize;
if (!roundUp && column < 0) {
// Handle negative values here to escape loop before incrementing
// `character`.
break;
}
} else {
column--;
}
character++;
}
return character;
}
/**
* Returns the position corresponding to the character at the given position,
* taking into account tab characters that precede it.
*/
export function column(
position: vscode.Position,
editor?: Pick<vscode.TextEditor, "document" | "options">,
): vscode.Position;
/**
* Returns the render column corresponding to the specified character in the
* specified line, taking into account tab characters that precede it.
*/
export function column(
line: number,
character: number,
editor?: Pick<vscode.TextEditor, "document" | "options">,
): number;
export function column(
line: number | vscode.Position,
character?: number | Pick<vscode.TextEditor, "document" | "options">,
editor?: Pick<vscode.TextEditor, "document" | "options">,
) {
if (typeof line === "number") {
editor ??= Context.current.editor;
const text = editor.document.lineAt(line).text.slice(0, character as number);
return text.length + diffAddedByTabs(text, editor);
}
editor ??= Context.current.editor;
const text = editor.document.lineAt(line.line).text.slice(0, line.character);
return new vscode.Position(line.line, text.length + diffAddedByTabs(text, editor));
}
/**
* Returns the {@link vscode.Position}-compatible position for the given
* position. Reverses the diff added by {@link column}.
*/
export function character(
position: vscode.Position,
editor?: Pick<vscode.TextEditor, "document" | "options">,
roundUp?: boolean,
): vscode.Position;
/**
* Returns the {@link vscode.Position}-compatible character for the given
* column. Reverses the diff added by {@link column}.
*/
export function character(
line: number,
character: number,
editor?: Pick<vscode.TextEditor, "document" | "options">,
roundUp?: boolean,
): number;
export function character(
lineOrPosition: number | vscode.Position,
characterOrEditor?: number | Pick<vscode.TextEditor, "document" | "options">,
editorOrRoundUp?: Pick<vscode.TextEditor, "document" | "options"> | boolean,
roundUp?: boolean,
) {
if (typeof lineOrPosition === "number") {
// Second overload.
const line = lineOrPosition,
character = characterOrEditor as number,
editor = editorOrRoundUp as vscode.TextEditor ?? Context.current.editor;
return getCharacter(editor.document.lineAt(line).text, character, editor, roundUp ?? false);
}
// First overload.
const position = lineOrPosition,
editor = characterOrEditor as vscode.TextEditor ?? Context.current.editor;
roundUp = editorOrRoundUp as boolean ?? false;
const text = editor.document.lineAt(position.line).text;
return new vscode.Position(
position.line, getCharacter(text, position.character, editor, roundUp));
}
/**
* Same as {@link length}, but also increases the count according to tab
* characters so that the result matches the rendered view.
*/
export function columns(
line: number | vscode.Position,
editor: Pick<vscode.TextEditor, "document" | "options"> = Context.current.editor,
): number {
if (typeof line !== "number") {
line = line.line;
}
const text = editor.document.lineAt(line).text;
return text.length + diffAddedByTabs(text, editor);
}