@kongyo2/ts-comment-scanner 1.21.0 → 1.23.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +240 -52
- package/dist/astgrep.d.ts +4 -2
- package/dist/astgrep.d.ts.map +1 -1
- package/dist/astgrep.js +7 -4
- package/dist/astgrep.js.map +1 -1
- package/dist/containers.d.ts +48 -0
- package/dist/containers.d.ts.map +1 -0
- package/dist/containers.js +82 -0
- package/dist/containers.js.map +1 -0
- package/dist/coverage.d.ts +45 -0
- package/dist/coverage.d.ts.map +1 -0
- package/dist/coverage.js +92 -0
- package/dist/coverage.js.map +1 -0
- package/dist/directives.d.ts +13 -3
- package/dist/directives.d.ts.map +1 -1
- package/dist/directives.js +31 -11
- package/dist/directives.js.map +1 -1
- package/dist/files.d.ts +5 -0
- package/dist/files.d.ts.map +1 -1
- package/dist/files.js +103 -41
- package/dist/files.js.map +1 -1
- package/dist/imports.d.ts +12 -5
- package/dist/imports.d.ts.map +1 -1
- package/dist/imports.js +33 -10
- package/dist/imports.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/lines.d.ts +11 -0
- package/dist/lines.d.ts.map +1 -1
- package/dist/lines.js +30 -0
- package/dist/lines.js.map +1 -1
- package/dist/remove.d.ts +16 -12
- package/dist/remove.d.ts.map +1 -1
- package/dist/remove.js +51 -638
- package/dist/remove.js.map +1 -1
- package/dist/run.d.ts.map +1 -1
- package/dist/run.js +11 -10
- package/dist/run.js.map +1 -1
- package/dist/scanner.d.ts +29 -0
- package/dist/scanner.d.ts.map +1 -1
- package/dist/scanner.js +122 -5
- package/dist/scanner.js.map +1 -1
- package/dist/shields.d.ts +77 -0
- package/dist/shields.d.ts.map +1 -0
- package/dist/shields.js +758 -0
- package/dist/shields.js.map +1 -0
- package/dist/splice.d.ts +40 -15
- package/dist/splice.d.ts.map +1 -1
- package/dist/splice.js +185 -69
- package/dist/splice.js.map +1 -1
- package/package.json +6 -5
- package/src/astgrep.ts +7 -5
- package/src/containers.ts +128 -0
- package/src/coverage.ts +110 -0
- package/src/directives.ts +45 -11
- package/src/files.ts +147 -53
- package/src/imports.ts +42 -17
- package/src/index.ts +1 -0
- package/src/lines.ts +33 -1
- package/src/remove.ts +58 -748
- package/src/run.ts +11 -10
- package/src/scanner.ts +160 -5
- package/src/shields.ts +852 -0
- package/src/splice.ts +216 -75
package/src/splice.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { LINE_BREAK, LINE_TERMINATOR, isBlank, isHorizontalSpace, lineStartOffsets } from "./lines.js";
|
|
1
|
+
import { LINE_BREAK, LINE_TERMINATOR, isBlank, isHorizontalSpace, lineIndexAt, lineStartOffsets } from "./lines.js";
|
|
2
2
|
import { firstIndexAtOrAfter, rangeAt, type Range } from "./ranges.js";
|
|
3
3
|
|
|
4
4
|
/** One change to the source: a range to delete (tidying its line) or to replace verbatim. */
|
|
@@ -18,12 +18,26 @@ export interface Edit extends Range {
|
|
|
18
18
|
* the empty line.
|
|
19
19
|
*/
|
|
20
20
|
blankLine?: boolean;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* The lines (1-based, numbered as lineStartOffsets numbers them) that
|
|
25
|
+
* deleting ranges has to leave in place, each a line of its own, because a
|
|
26
|
+
* directive that stays covers them.
|
|
27
|
+
*/
|
|
28
|
+
export interface ShieldedLines {
|
|
21
29
|
/**
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
30
|
+
* The lines a directive covers. None is joined to another: with code on
|
|
31
|
+
* both sides of a deleted range that spans lines, one of them shielded, a
|
|
32
|
+
* line break of the range stays between them, wherever the range stands.
|
|
25
33
|
*/
|
|
26
|
-
|
|
34
|
+
shielded: ReadonlySet<number>;
|
|
35
|
+
/**
|
|
36
|
+
* Those of them the deletions empty that still have something below them
|
|
37
|
+
* (see heldLines). Each stays, as an empty line, so that what is below it
|
|
38
|
+
* moves up no line.
|
|
39
|
+
*/
|
|
40
|
+
held: ReadonlySet<number>;
|
|
27
41
|
}
|
|
28
42
|
|
|
29
43
|
// Brackets never merge with what follows them, nor punctuation with what
|
|
@@ -38,20 +52,30 @@ function needsSpace(before: string, after: string): boolean {
|
|
|
38
52
|
return !OPENING_BRACKETS.includes(before) && !CLOSING_PUNCTUATION.includes(after);
|
|
39
53
|
}
|
|
40
54
|
|
|
55
|
+
// Every line break of a text, CRLF as one.
|
|
56
|
+
const ALL_LINE_BREAKS = new RegExp(LINE_BREAK.source, "g");
|
|
57
|
+
|
|
41
58
|
/**
|
|
42
|
-
* The line
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
*
|
|
46
|
-
*
|
|
47
|
-
*
|
|
59
|
+
* The `count` line breaks that stay of those a deletion could take along —
|
|
60
|
+
* the ones inside the deleted range, and the ones around the lines it
|
|
61
|
+
* empties. Those ending in a line feed stay first: they end a line for
|
|
62
|
+
* every tool, where a lone CR, U+2028 or U+2029 ends one for tsc and ESLint
|
|
63
|
+
* but none for the tools that only count line feeds (tree-sitter, oxlint,
|
|
64
|
+
* Semgrep, c8), so the code on both sides stays on rows apart wherever a
|
|
65
|
+
* line feed held it apart. They stay in the order the source has them.
|
|
48
66
|
*/
|
|
49
|
-
function
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
67
|
+
function stayingBreaks(breaks: readonly string[], count: number): string[] {
|
|
68
|
+
const staying = new Set<number>();
|
|
69
|
+
for (const lineFeed of [true, false]) {
|
|
70
|
+
for (const [index, lineBreak] of breaks.entries()) {
|
|
71
|
+
if (staying.size < count && lineBreak.endsWith("\n") === lineFeed) staying.add(index);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return breaks.filter((_, index) => staying.has(index));
|
|
53
75
|
}
|
|
54
76
|
|
|
77
|
+
const NO_SHIELDED_LINES: ShieldedLines = { shielded: new Set(), held: new Set() };
|
|
78
|
+
|
|
55
79
|
// A global copy of the line-break matcher, so exec() can resume from a removal's end offset.
|
|
56
80
|
const NEXT_LINE_BREAK = new RegExp(LINE_BREAK.source, "g");
|
|
57
81
|
|
|
@@ -65,19 +89,39 @@ function lastLineBreakEnd(chunk: string): number {
|
|
|
65
89
|
|
|
66
90
|
/**
|
|
67
91
|
* Applies sorted, non-overlapping edits to the source, tidying the lines of
|
|
68
|
-
* deleted ranges
|
|
69
|
-
* line-state tracking, so the work
|
|
70
|
-
* comment-dense files.
|
|
92
|
+
* deleted ranges and leaving the shielded lines in place. Output is
|
|
93
|
+
* accumulated as segments with incremental line-state tracking, so the work
|
|
94
|
+
* stays linear in the source size even for comment-dense files.
|
|
95
|
+
*
|
|
96
|
+
* A deleted range takes line breaks along: its own, and where it empties
|
|
97
|
+
* lines, the ones around them. As many of them stay as there are lines left
|
|
98
|
+
* to end — the line of the code before the range, a line break that kept two
|
|
99
|
+
* tokens apart, the held lines — and which ones is stayingBreaks' choice.
|
|
71
100
|
*/
|
|
72
|
-
export function applyEdits(source: string, edits: readonly Edit[]): string {
|
|
101
|
+
export function applyEdits(source: string, edits: readonly Edit[], lines: ShieldedLines = NO_SHIELDED_LINES): string {
|
|
73
102
|
const segments: string[] = [];
|
|
74
103
|
let cursor = 0;
|
|
104
|
+
const lineStarts = lines.shielded.size > 0 ? lineStartOffsets(source) : [];
|
|
105
|
+
/** The (1-based) line holding the offset; 0 where no line is shielded. */
|
|
106
|
+
const lineOf = (offset: number): number => (lineStarts.length === 0 ? 0 : lineIndexAt(lineStarts, offset) + 1);
|
|
107
|
+
/** How many of the lines `from` to `to` (inclusive) are held. */
|
|
108
|
+
const heldAmong = (from: number, to: number): number => {
|
|
109
|
+
let held = 0;
|
|
110
|
+
for (let line = from; line <= to; line += 1) {
|
|
111
|
+
if (lines.held.has(line)) held += 1;
|
|
112
|
+
}
|
|
113
|
+
return held;
|
|
114
|
+
};
|
|
75
115
|
/** Whether the output's current (unterminated) line is all whitespace so far. */
|
|
76
116
|
let lineIsBlank = true;
|
|
77
117
|
/** Last character of the output so far ("" while empty). */
|
|
78
118
|
let lastChar = "";
|
|
79
119
|
|
|
80
|
-
|
|
120
|
+
/** The character the output ends in ("" while empty), read off the segments where some were taken off its end. */
|
|
121
|
+
const outputEnd = (): string => segments.at(-1)?.slice(-1) ?? "";
|
|
122
|
+
|
|
123
|
+
/** Appends the chunk as it is. */
|
|
124
|
+
const append = (chunk: string): void => {
|
|
81
125
|
if (chunk === "") return;
|
|
82
126
|
segments.push(chunk);
|
|
83
127
|
const breakEnd = lastLineBreakEnd(chunk);
|
|
@@ -86,27 +130,59 @@ export function applyEdits(source: string, edits: readonly Edit[]): string {
|
|
|
86
130
|
};
|
|
87
131
|
|
|
88
132
|
/**
|
|
89
|
-
*
|
|
90
|
-
*
|
|
91
|
-
*
|
|
133
|
+
* Appends the chunk, keeping the line breaks on both sides of the seam
|
|
134
|
+
* apart: a lone CR that a deletion leaves right in front of a line feed
|
|
135
|
+
* would read as one CRLF with it, a line break short, so the line feeds
|
|
136
|
+
* go in front of the CRs the output ends in.
|
|
92
137
|
*/
|
|
93
|
-
const
|
|
138
|
+
const push = (chunk: string): void => {
|
|
139
|
+
let loneReturns = "";
|
|
140
|
+
while (lastChar === "\r" && chunk.startsWith("\n")) {
|
|
141
|
+
const last = segments.pop() as string;
|
|
142
|
+
if (last.length > 1) segments.push(last.slice(0, -1));
|
|
143
|
+
lastChar = outputEnd();
|
|
144
|
+
loneReturns += "\r";
|
|
145
|
+
}
|
|
146
|
+
const rest = loneReturns === "" ? chunk : chunk.replace(/^\n+/, "");
|
|
147
|
+
append(chunk.slice(0, chunk.length - rest.length) + loneReturns + rest);
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
/** Takes everything the output holds after its last line break out of it, and returns it. */
|
|
151
|
+
const takeCurrentLine = (): string => {
|
|
152
|
+
let taken = "";
|
|
94
153
|
while (segments.length > 0) {
|
|
95
154
|
const last = segments[segments.length - 1] as string;
|
|
96
155
|
const breakEnd = lastLineBreakEnd(last);
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
156
|
+
taken = last.slice(breakEnd === -1 ? 0 : breakEnd) + taken;
|
|
157
|
+
if (breakEnd !== -1) {
|
|
158
|
+
segments[segments.length - 1] = last.slice(0, breakEnd);
|
|
159
|
+
break;
|
|
100
160
|
}
|
|
101
|
-
|
|
102
|
-
segments[segments.length - 1] = last.slice(0, tookBreak ? breakEnd - 1 : breakEnd);
|
|
103
|
-
lineIsBlank = !tookBreak;
|
|
104
|
-
lastChar = last.charAt(breakEnd - (tookBreak ? 2 : 1));
|
|
105
|
-
return tookBreak;
|
|
161
|
+
segments.pop();
|
|
106
162
|
}
|
|
107
163
|
lineIsBlank = true;
|
|
108
|
-
lastChar =
|
|
109
|
-
return
|
|
164
|
+
lastChar = outputEnd();
|
|
165
|
+
return taken;
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Takes the line break the output ends in out of it, and returns it — none
|
|
170
|
+
* at the start of the output. Only called right after takeCurrentLine, and
|
|
171
|
+
* followed by a line break going back in, which sets the line state again.
|
|
172
|
+
*/
|
|
173
|
+
const takeBreak = (): string[] => {
|
|
174
|
+
const last = segments.pop();
|
|
175
|
+
if (last === undefined) return [];
|
|
176
|
+
const lineBreak = last.endsWith("\r\n") ? "\r\n" : last.slice(-1);
|
|
177
|
+
const rest = last.slice(0, -lineBreak.length);
|
|
178
|
+
if (rest !== "") segments.push(rest);
|
|
179
|
+
lastChar = outputEnd();
|
|
180
|
+
return [lineBreak];
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
/** Puts `count` of the line breaks back in (see stayingBreaks). */
|
|
184
|
+
const keep = (breaks: readonly string[], count: number): void => {
|
|
185
|
+
for (const lineBreak of stayingBreaks(breaks, count)) push(lineBreak);
|
|
110
186
|
};
|
|
111
187
|
|
|
112
188
|
/**
|
|
@@ -155,44 +231,54 @@ export function applyEdits(source: string, edits: readonly Edit[]): string {
|
|
|
155
231
|
// goes in.
|
|
156
232
|
const lineFeed = source.slice(edit.start, lineEnd).includes("\r\n") ? "\r\n" : "\n";
|
|
157
233
|
if (lineIsBlank) {
|
|
158
|
-
|
|
159
|
-
if (lastChar !== "\n")
|
|
234
|
+
takeCurrentLine();
|
|
235
|
+
if (lastChar !== "\n") append(lineFeed);
|
|
160
236
|
} else {
|
|
161
237
|
trimLineEnd();
|
|
162
|
-
|
|
238
|
+
append(lineFeed);
|
|
163
239
|
}
|
|
164
|
-
|
|
240
|
+
append(lineFeed);
|
|
165
241
|
cursor = isBlank(restContent) ? lineEnd : skipSpaces(cursor);
|
|
166
242
|
continue;
|
|
167
243
|
}
|
|
168
244
|
|
|
245
|
+
// The line breaks the range holds, the one after its last line, and the
|
|
246
|
+
// lines it starts and ends on.
|
|
247
|
+
const own: readonly string[] = deleted.match(ALL_LINE_BREAKS) ?? [];
|
|
248
|
+
const after = lineBreak === null ? [] : [lineBreak[0]];
|
|
249
|
+
const first = lineOf(edit.start);
|
|
250
|
+
const last = lineOf(Math.max(edit.start, edit.end - 1));
|
|
251
|
+
|
|
169
252
|
if (lineIsBlank && isBlank(restContent)) {
|
|
170
|
-
// The range occupied whole line(s):
|
|
171
|
-
//
|
|
172
|
-
//
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
cursor =
|
|
253
|
+
// The range occupied whole line(s): they go, with their line breaks,
|
|
254
|
+
// but for the held ones, which stay as empty lines. One line break
|
|
255
|
+
// still ends the line in front of them.
|
|
256
|
+
takeCurrentLine();
|
|
257
|
+
const before = takeBreak();
|
|
258
|
+
keep([...before, ...own, ...after], before.length + heldAmong(first, last));
|
|
259
|
+
cursor = lineEnd;
|
|
177
260
|
} else if (isBlank(restContent)) {
|
|
178
|
-
// Trailing after code: trim the gap
|
|
261
|
+
// Trailing after code: trim the gap; one line break ends the code's
|
|
262
|
+
// line. The lines the range empties below the code go, but for the
|
|
263
|
+
// held ones.
|
|
179
264
|
trimLineEnd();
|
|
180
|
-
|
|
265
|
+
keep([...own, ...after], after.length + heldAmong(first + 1, last));
|
|
181
266
|
cursor = lineEnd;
|
|
182
267
|
} else if (
|
|
183
268
|
!lineIsBlank &&
|
|
184
|
-
|
|
185
|
-
(
|
|
269
|
+
own.length > 0 &&
|
|
270
|
+
(isApart(first, last) || needsSpace(lastChar, source.charAt(cursor)))
|
|
186
271
|
) {
|
|
187
272
|
// Code on both sides of a range that spans lines. Per ECMA-262 a
|
|
188
273
|
// multi-line comment acts as a line terminator for semicolon
|
|
189
|
-
// insertion, so
|
|
190
|
-
//
|
|
191
|
-
//
|
|
192
|
-
//
|
|
193
|
-
//
|
|
274
|
+
// insertion, so one of its line breaks takes its place instead of
|
|
275
|
+
// merging the lines. (Right after an opening bracket or before closing
|
|
276
|
+
// punctuation a line terminator changes nothing, so a multi-line
|
|
277
|
+
// comment there goes without one.) Where one of the two lines is
|
|
278
|
+
// shielded, a line break stays between them wherever the range stands,
|
|
279
|
+
// and the held lines between them stay as well.
|
|
194
280
|
trimLineEnd();
|
|
195
|
-
|
|
281
|
+
keep(own, heldAmong(first + 1, last - 1) + 1);
|
|
196
282
|
} else if (lastChar !== "" && !isBlank(lastChar)) {
|
|
197
283
|
// Code right before the range, more on the same line after it.
|
|
198
284
|
if (edit.jsx === true) continue;
|
|
@@ -206,7 +292,15 @@ export function applyEdits(source: string, edits: readonly Edit[]): string {
|
|
|
206
292
|
}
|
|
207
293
|
} else {
|
|
208
294
|
// Whitespace (or the line start) right before the range: the
|
|
209
|
-
// whitespace after it collapses into that.
|
|
295
|
+
// whitespace after it collapses into that. Where the range opened its
|
|
296
|
+
// line, the lines it empties above the code after it go, but for the
|
|
297
|
+
// held ones, and one line break still ends the line in front of them.
|
|
298
|
+
if (lineIsBlank && own.length > 0) {
|
|
299
|
+
const indentation = takeCurrentLine();
|
|
300
|
+
const before = takeBreak();
|
|
301
|
+
keep([...before, ...own], before.length + heldAmong(first, last - 1));
|
|
302
|
+
push(indentation);
|
|
303
|
+
}
|
|
210
304
|
cursor = skipSpaces(cursor);
|
|
211
305
|
if (edit.jsx !== true && !lineIsBlank && CLOSING_PUNCTUATION.includes(source.charAt(cursor))) {
|
|
212
306
|
// `a /* x */)` — the space before only held the comment off the code.
|
|
@@ -217,6 +311,11 @@ export function applyEdits(source: string, edits: readonly Edit[]): string {
|
|
|
217
311
|
|
|
218
312
|
segments.push(source.slice(cursor));
|
|
219
313
|
return segments.join("");
|
|
314
|
+
|
|
315
|
+
/** Whether the lines a range starts and ends on have to stay apart: one is shielded, or held lines lie between. */
|
|
316
|
+
function isApart(first: number, last: number): boolean {
|
|
317
|
+
return lines.shielded.has(first) || lines.shielded.has(last) || heldAmong(first + 1, last - 1) > 0;
|
|
318
|
+
}
|
|
220
319
|
}
|
|
221
320
|
|
|
222
321
|
/** Where blocks open and close besides brackets: JSX tags, whose children are laid out like a block's. */
|
|
@@ -229,6 +328,56 @@ export interface BlockTags {
|
|
|
229
328
|
|
|
230
329
|
type LineKind = "vanishing" | "blank" | "code";
|
|
231
330
|
|
|
331
|
+
/** Start offset and kind of every line of the source, a final line break ending the last line. */
|
|
332
|
+
interface LineKinds {
|
|
333
|
+
offsets: number[];
|
|
334
|
+
kinds: LineKind[];
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
/**
|
|
338
|
+
* What becomes of each line once the (sorted) `deletions` are made: a line
|
|
339
|
+
* vanishes when every non-whitespace character on it is deleted; a blank
|
|
340
|
+
* line holds nothing but spaces and tabs (a lone vertical tab or no-break
|
|
341
|
+
* space is content to tsc 7) and lies outside every comment; any other line
|
|
342
|
+
* holds code.
|
|
343
|
+
*/
|
|
344
|
+
function lineKinds(source: string, deletions: readonly Range[], comments: readonly Range[]): LineKinds {
|
|
345
|
+
const offsets = lineStartOffsets(source);
|
|
346
|
+
// A final line break ends the last line rather than opening an empty one.
|
|
347
|
+
const lineCount =
|
|
348
|
+
offsets.length > 1 && offsets[offsets.length - 1] === source.length ? offsets.length - 1 : offsets.length;
|
|
349
|
+
const kinds: LineKind[] = [];
|
|
350
|
+
for (let line = 0; line < lineCount; line += 1) {
|
|
351
|
+
const start = offsets[line] as number;
|
|
352
|
+
const end = offsets[line + 1] ?? source.length;
|
|
353
|
+
kinds.push(lineKind(source, start, end, endOfContent(source, start, end), deletions, comments));
|
|
354
|
+
}
|
|
355
|
+
return { offsets, kinds };
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
/**
|
|
359
|
+
* The shielded lines (1-based) that the deletions empty and that still have
|
|
360
|
+
* code, or a comment that stays, somewhere below them. Dropping such a line
|
|
361
|
+
* would move what is below it up into the directive's reach, so it stays, as
|
|
362
|
+
* an empty line (see ShieldedLines.held); below the last line that keeps
|
|
363
|
+
* anything there is nothing to move.
|
|
364
|
+
*/
|
|
365
|
+
export function heldLines(
|
|
366
|
+
source: string,
|
|
367
|
+
deletions: readonly Range[],
|
|
368
|
+
comments: readonly Range[],
|
|
369
|
+
shieldedLines: ReadonlySet<number>,
|
|
370
|
+
): Set<number> {
|
|
371
|
+
const held = new Set<number>();
|
|
372
|
+
if (shieldedLines.size === 0) return held;
|
|
373
|
+
const { kinds } = lineKinds(source, deletions, comments);
|
|
374
|
+
const lastKept = kinds.lastIndexOf("code");
|
|
375
|
+
for (const line of shieldedLines) {
|
|
376
|
+
if (line <= lastKept && kinds[line - 1] === "vanishing") held.add(line);
|
|
377
|
+
}
|
|
378
|
+
return held;
|
|
379
|
+
}
|
|
380
|
+
|
|
232
381
|
/**
|
|
233
382
|
* Deletions of the blank lines that removing whole comment lines would pile
|
|
234
383
|
* up. Where comment-only lines vanish, the blank lines around them close
|
|
@@ -242,35 +391,25 @@ type LineKind = "vanishing" | "blank" | "code";
|
|
|
242
391
|
* against that edge stay: the ones on the far side of the comment merely
|
|
243
392
|
* separated the comment from the code.
|
|
244
393
|
*
|
|
245
|
-
* `deletions` are the ranges about to be deleted, sorted
|
|
246
|
-
*
|
|
247
|
-
*
|
|
248
|
-
*
|
|
249
|
-
*
|
|
250
|
-
* among them stay, so no code moves into the directive's reach.
|
|
394
|
+
* `deletions` are the ranges about to be deleted, sorted. `shieldedLines`
|
|
395
|
+
* are the lines a surviving directive reaches (1-based): the blank lines
|
|
396
|
+
* among them stay, so no code moves into the directive's reach, and so do
|
|
397
|
+
* the `held` ones, which the deletions leave empty (see heldLines) — a held
|
|
398
|
+
* line counts among the blank lines its run is trimmed to.
|
|
251
399
|
*/
|
|
252
400
|
export function blankLineEdits(
|
|
253
401
|
source: string,
|
|
254
402
|
deletions: readonly Range[],
|
|
255
403
|
comments: readonly Range[],
|
|
256
|
-
|
|
404
|
+
{ shielded, held }: ShieldedLines,
|
|
257
405
|
tags: BlockTags,
|
|
258
406
|
): Edit[] {
|
|
259
|
-
const offsets =
|
|
260
|
-
|
|
261
|
-
const lineCount =
|
|
262
|
-
offsets.length > 1 && offsets[offsets.length - 1] === source.length ? offsets.length - 1 : offsets.length;
|
|
407
|
+
const { offsets, kinds } = lineKinds(source, deletions, comments);
|
|
408
|
+
const lineCount = kinds.length;
|
|
263
409
|
const lineStart = (line: number): number => offsets[line] as number;
|
|
264
410
|
const lineContentEnd = (line: number): number =>
|
|
265
411
|
endOfContent(source, lineStart(line), offsets[line + 1] ?? source.length);
|
|
266
412
|
|
|
267
|
-
const kinds: LineKind[] = [];
|
|
268
|
-
for (let line = 0; line < lineCount; line += 1) {
|
|
269
|
-
kinds.push(
|
|
270
|
-
lineKind(source, lineStart(line), offsets[line + 1] ?? source.length, lineContentEnd(line), deletions, comments),
|
|
271
|
-
);
|
|
272
|
-
}
|
|
273
|
-
|
|
274
413
|
/** Offset of the first (or last) character on the line that survives the deletions and is not whitespace. */
|
|
275
414
|
const survivingEdge = (line: number, fromEnd: boolean): number | undefined => {
|
|
276
415
|
const start = lineStart(line);
|
|
@@ -304,12 +443,14 @@ export function blankLineEdits(
|
|
|
304
443
|
continue;
|
|
305
444
|
}
|
|
306
445
|
const first = line;
|
|
446
|
+
// The lines of the run that are blank once the deletions are made.
|
|
307
447
|
const blanks: number[] = [];
|
|
308
448
|
// Blank lines between consecutive vanishing lines, from before the first to after the last.
|
|
309
449
|
const gaps = [0];
|
|
310
450
|
for (; line < lineCount && kinds[line] !== "code"; line += 1) {
|
|
311
451
|
if (kinds[line] === "vanishing") {
|
|
312
452
|
gaps.push(0);
|
|
453
|
+
if (held.has(line + 1)) blanks.push(line);
|
|
313
454
|
} else {
|
|
314
455
|
blanks.push(line);
|
|
315
456
|
gaps[gaps.length - 1] = (gaps[gaps.length - 1] as number) + 1;
|
|
@@ -333,7 +474,7 @@ export function blankLineEdits(
|
|
|
333
474
|
else keep = gaps.reduce((widest, gap) => Math.max(widest, gap));
|
|
334
475
|
}
|
|
335
476
|
for (const [position, blank] of blanks.entries()) {
|
|
336
|
-
if (
|
|
477
|
+
if (shielded.has(blank + 1)) keep = Math.max(keep, position + 1);
|
|
337
478
|
}
|
|
338
479
|
for (const blank of blanks.slice(keep)) {
|
|
339
480
|
edits.push({ start: lineStart(blank), end: lineContentEnd(blank) });
|