forked from Silverquark/dance
-
Notifications
You must be signed in to change notification settings - Fork 0
/
positions.ts
178 lines (149 loc) · 4.33 KB
/
positions.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
import * as vscode from "vscode";
import { Context } from "./context";
import { Direction } from "./types";
/**
* Returns the position right after the given position, or `undefined` if
* `position` is the last position of the document.
*/
export function next(position: vscode.Position, document?: vscode.TextDocument) {
document ??= Context.current.document;
const line = position.line,
character = position.character,
textLineLen = document.lineAt(line).text.length;
if (character < textLineLen) {
return new vscode.Position(line, character + 1);
}
if (line === document.lineCount - 1) {
return undefined;
}
return new vscode.Position(line + 1, 0);
}
/**
* Returns the position right before the given position, or `undefined` if
* `position` is the first position of the document.
*/
export function previous(position: vscode.Position, document?: vscode.TextDocument) {
const line = position.line,
character = position.character;
if (character > 0) {
return new vscode.Position(line, character - 1);
}
if (line === 0) {
return undefined;
}
return new vscode.Position(
line - 1,
(document ?? Context.current.document).lineAt(line - 1).text.length,
);
}
/**
* Returns the position at a given (possibly negative) offset from the given
* position, or `undefined` if such a position would go out of the bounds of the
* document.
*/
export function offset(
position: vscode.Position,
by: number,
document?: vscode.TextDocument,
) {
if (by === 0) {
return position;
}
if (by === 1) {
return next(position, document);
}
if (by === -1) {
return previous(position, document);
}
document ??= Context.current.document;
const offset = document.offsetAt(position) + by;
if (offset === -1) {
return undefined;
}
return document.positionAt(document.offsetAt(position) + by);
}
/**
* Same as {@link offset}, but clamps to document edges.
*/
export function offsetOrEdge(
position: vscode.Position,
by: number,
document?: vscode.TextDocument,
) {
const result = offset(position, by, document);
if (result === undefined) {
return by < 0 ? zero : last(document);
}
return result;
}
/**
* The (0, 0) position.
*/
export const zero = new vscode.Position(0, 0);
/**
* Returns the last position of the given document.
*/
export function last(document = Context.current.document) {
return document.lineAt(document.lineCount - 1).rangeIncludingLineBreak.end;
}
/**
* Returns the position at the given line and character.
*/
export function at(line: number, character: number) {
return new vscode.Position(line, character);
}
/**
* Returns the position at the start of the given line.
*/
export function lineStart(line: number) {
return new vscode.Position(line, 0);
}
/**
* Returns the position at the first non-blank character of the given line, or
* the end of the line if the line is fully blank.
*/
export function nonBlankLineStart(line: number, document = Context.current.document) {
return new vscode.Position(line, document.lineAt(line).firstNonWhitespaceCharacterIndex);
}
/**
* Returns the position at the end of the given line.
*/
export function lineEnd(line: number, document = Context.current.document) {
return new vscode.Position(line, document.lineAt(line).text.length);
}
/**
* Returns the position after the end of the given line, i.e. the first
* position of the next line.
*/
export function lineBreak(line: number, document = Context.current.document) {
return line + 1 === document.lineCount ? lineEnd(line, document) : lineStart(line + 1);
}
/**
* Returns the last position of the current document when going in the given
* direction. If {@link Direction.Backward}, this is {@link zero}. If
* {@link Direction.Forward}, this is {@link last(document)}.
*/
export function edge(direction: Direction, document?: vscode.TextDocument) {
return direction === Direction.Backward ? zero : last(document);
}
/**
* Returns a string representation of the position.
*
* ### Example
*
* ```js
* expect(Positions.toString(Selections.nth(0)!.active), "to be", "1:1");
* expect(Positions.toString(Selections.nth(1)!.active), "to be", "2:3");
* ```
*
* With:
* ```
* abc
* | 0
* def
* | 1
* ```
*/
export function toString(position: vscode.Position) {
return `${position.line + 1}:${position.character + 1}` as const;
}