forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtext.ts
More file actions
251 lines (228 loc) · 6.94 KB
/
text.ts
File metadata and controls
251 lines (228 loc) · 6.94 KB
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import { Position, Range, TextDocument } from 'vscode';
import { isNumber } from './sysTypes';
export function getWindowsLineEndingCount(document: TextDocument, offset: number): number {
// const eolPattern = new RegExp('\r\n', 'g');
const eolPattern = /\r\n/g;
const readBlock = 1024;
let count = 0;
let offsetDiff = offset.valueOf();
// In order to prevent the one-time loading of large files from taking up too much memory
for (let pos = 0; pos < offset; pos += readBlock) {
const startAt = document.positionAt(pos);
let endAt: Position;
if (offsetDiff >= readBlock) {
endAt = document.positionAt(pos + readBlock);
offsetDiff = offsetDiff - readBlock;
} else {
endAt = document.positionAt(pos + offsetDiff);
}
const text = document.getText(new Range(startAt, endAt!));
const cr = text.match(eolPattern);
count += cr ? cr.length : 0;
}
return count;
}
/**
* Return the range represented by the given string.
*
* If a number is provided then it is used as both lines and the
* character are set to 0.
*
* Examples:
* '1:5-3:5' -> Range(1, 5, 3, 5)
* '1-3' -> Range(1, 0, 3, 0)
* '1:3-1:5' -> Range(1, 3, 1, 5)
* '1-1' -> Range(1, 0, 1, 0)
* '1' -> Range(1, 0, 1, 0)
* '1:3-' -> Range(1, 3, 1, 0)
* '1:3' -> Range(1, 3, 1, 0)
* '' -> Range(0, 0, 0, 0)
* '3-1' -> Range(1, 0, 3, 0)
*/
export function parseRange(raw: string | number): Range {
if (isNumber(raw)) {
return new Range(raw, 0, raw, 0);
}
if (raw === '') {
return new Range(0, 0, 0, 0);
}
const parts = raw.split('-');
if (parts.length > 2) {
throw new Error(`invalid range ${raw}`);
}
const start = parsePosition(parts[0]);
let end = start;
if (parts.length === 2) {
end = parsePosition(parts[1]);
}
return new Range(start, end);
}
/**
* Return the line/column represented by the given string.
*
* If a number is provided then it is used as the line and the character
* is set to 0.
*
* Examples:
* '1:5' -> Position(1, 5)
* '1' -> Position(1, 0)
* '' -> Position(0, 0)
*/
export function parsePosition(raw: string | number): Position {
if (isNumber(raw)) {
return new Position(raw, 0);
}
if (raw === '') {
return new Position(0, 0);
}
const parts = raw.split(':');
if (parts.length > 2) {
throw new Error(`invalid position ${raw}`);
}
let line = 0;
if (parts[0] !== '') {
if (!/^\d+$/.test(parts[0])) {
throw new Error(`invalid position ${raw}`);
}
line = +parts[0];
}
let col = 0;
if (parts.length === 2 && parts[1] !== '') {
if (!/^\d+$/.test(parts[1])) {
throw new Error(`invalid position ${raw}`);
}
col = +parts[1];
}
return new Position(line, col);
}
/**
* Return the indentation part of the given line.
*/
export function getIndent(line: string): string {
const found = line.match(/^ */);
return found![0];
}
/**
* Return the dedented lines in the given text.
*
* This is used to represent text concisely and readably, which is
* particularly useful for declarative definitions (e.g. in tests).
*
* (inspired by Python's `textwrap.dedent()`)
*/
export function getDedentedLines(text: string): string[] {
const linesep = text.includes('\r') ? '\r\n' : '\n';
const lines = text.split(linesep);
if (!lines) {
return [text];
}
if (lines[0] !== '') {
throw Error('expected actual first line to be blank');
}
lines.shift();
if (lines.length === 0) {
return [];
}
if (lines[0] === '') {
throw Error('expected "first" line to not be blank');
}
const leading = getIndent(lines[0]).length;
for (let i = 0; i < lines.length; i += 1) {
const line = lines[i];
if (getIndent(line).length < leading) {
throw Error(`line ${i} has less indent than the "first" line`);
}
lines[i] = line.substring(leading);
}
return lines;
}
/**
* Extract a tree based on the given text.
*
* The tree is derived from the indent level of each line. The caller
* is responsible for applying any meaning to the text of each node
* in the tree.
*
* Blank lines and comments (with a leading `#`) are ignored. Also,
* the full text is automatically dedented until at least one line
* has no indent (i.e. is treated as a root).
*
* @returns - the list of nodes in the tree (pairs of text & parent index)
* (note that the parent index of roots is `-1`)
*
* Example:
*
* parseTree(`
* # This comment and the following blank line are ignored.
*
* this is a root
* the first branch
* a sub-branch # This comment is ignored.
* this is the first leaf node!
* another leaf node...
* middle
*
* the second main branch
* # indents do not have to be consistent across the full text.
* # ...and the indent of comments is not relevant.
* node 1
* node 2
*
* the last leaf node!
*
* another root
* nothing to see here!
*
* # this comment is ignored
* `.trim())
*
* would produce the following:
*
* [
* ['this is a root', -1],
* ['the first branch', 0],
* ['a sub-branch', 1],
* ['this is the first leaf node!', 2],
* ['another leaf node...', 1],
* ['middle', 1],
* ['the second main branch', 0],
* ['node 1', 6],
* ['node 2', 6],
* ['the last leaf node!', 0],
* ['another root', -1],
* ['nothing to see here!', 10],
* ]
*/
export function parseTree(text: string): [string, number][] {
const parsed: [string, number][] = [];
const parents: [string, number][] = [];
const lines = getDedentedLines(text)
.map((l) => l.split(' #')[0].split(' //')[0].trimEnd())
.filter((l) => l.trim() !== '');
lines.forEach((line) => {
const indent = getIndent(line);
const entry = line.trim();
let parentIndex: number;
if (indent === '') {
parentIndex = -1;
parents.push([indent, parsed.length]);
} else if (parsed.length === 0) {
throw Error(`expected non-indented line, got ${line}`);
} else {
let parentIndent: string;
[parentIndent, parentIndex] = parents[parents.length - 1];
while (indent.length <= parentIndent.length) {
parents.pop();
[parentIndent, parentIndex] = parents[parents.length - 1];
}
if (parentIndent.length < indent.length) {
parents.push([indent, parsed.length]);
}
}
parsed.push([entry, parentIndex!]);
});
return parsed;
}