@kongyo2/ts-comment-scanner 1.20.0 → 1.22.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/args.d.ts.map +1 -1
- package/dist/args.js +23 -19
- package/dist/args.js.map +1 -1
- package/dist/astgrep.d.ts +5 -3
- package/dist/astgrep.d.ts.map +1 -1
- package/dist/astgrep.js +10 -12
- 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 +21 -3
- package/dist/directives.d.ts.map +1 -1
- package/dist/directives.js +31 -23
- package/dist/directives.js.map +1 -1
- package/dist/files.d.ts +7 -3
- package/dist/files.d.ts.map +1 -1
- package/dist/files.js +129 -82
- package/dist/files.js.map +1 -1
- package/dist/imports.d.ts.map +1 -1
- package/dist/imports.js +14 -8
- 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/jsx.d.ts.map +1 -1
- package/dist/jsx.js +5 -6
- package/dist/jsx.js.map +1 -1
- package/dist/lines.d.ts +24 -5
- package/dist/lines.d.ts.map +1 -1
- package/dist/lines.js +50 -16
- package/dist/lines.js.map +1 -1
- package/dist/ranges.d.ts +8 -0
- package/dist/ranges.d.ts.map +1 -1
- package/dist/ranges.js +12 -0
- package/dist/ranges.js.map +1 -1
- package/dist/remove.d.ts +18 -14
- package/dist/remove.d.ts.map +1 -1
- package/dist/remove.js +69 -630
- package/dist/remove.js.map +1 -1
- package/dist/run.d.ts.map +1 -1
- package/dist/run.js +21 -28
- package/dist/run.js.map +1 -1
- package/dist/scanner.d.ts +34 -0
- package/dist/scanner.d.ts.map +1 -1
- package/dist/scanner.js +137 -31
- package/dist/scanner.js.map +1 -1
- package/dist/shields.d.ts +72 -0
- package/dist/shields.d.ts.map +1 -0
- package/dist/shields.js +624 -0
- package/dist/shields.js.map +1 -0
- package/dist/splice.d.ts +42 -11
- package/dist/splice.d.ts.map +1 -1
- package/dist/splice.js +189 -56
- package/dist/splice.js.map +1 -1
- package/package.json +2 -1
- package/src/args.ts +21 -21
- package/src/astgrep.ts +11 -13
- package/src/containers.ts +128 -0
- package/src/coverage.ts +110 -0
- package/src/directives.ts +38 -20
- package/src/files.ts +169 -91
- package/src/imports.ts +15 -8
- package/src/index.ts +1 -0
- package/src/jsx.ts +5 -6
- package/src/lines.ts +55 -14
- package/src/ranges.ts +17 -0
- package/src/remove.ts +82 -730
- package/src/run.ts +22 -34
- package/src/scanner.ts +180 -32
- package/src/shields.ts +701 -0
- package/src/splice.ts +224 -60
package/src/splice.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { LINE_BREAK, LINE_TERMINATOR, isBlank, 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. */
|
|
@@ -20,7 +20,25 @@ export interface Edit extends Range {
|
|
|
20
20
|
blankLine?: boolean;
|
|
21
21
|
}
|
|
22
22
|
|
|
23
|
-
|
|
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 {
|
|
29
|
+
/**
|
|
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.
|
|
33
|
+
*/
|
|
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>;
|
|
41
|
+
}
|
|
24
42
|
|
|
25
43
|
// Brackets never merge with what follows them, nor punctuation with what
|
|
26
44
|
// precedes it, so removing a comment between them needs no space to keep
|
|
@@ -34,6 +52,30 @@ function needsSpace(before: string, after: string): boolean {
|
|
|
34
52
|
return !OPENING_BRACKETS.includes(before) && !CLOSING_PUNCTUATION.includes(after);
|
|
35
53
|
}
|
|
36
54
|
|
|
55
|
+
// Every line break of a text, CRLF as one.
|
|
56
|
+
const ALL_LINE_BREAKS = new RegExp(LINE_BREAK.source, "g");
|
|
57
|
+
|
|
58
|
+
/**
|
|
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.
|
|
66
|
+
*/
|
|
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));
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const NO_SHIELDED_LINES: ShieldedLines = { shielded: new Set(), held: new Set() };
|
|
78
|
+
|
|
37
79
|
// A global copy of the line-break matcher, so exec() can resume from a removal's end offset.
|
|
38
80
|
const NEXT_LINE_BREAK = new RegExp(LINE_BREAK.source, "g");
|
|
39
81
|
|
|
@@ -47,19 +89,39 @@ function lastLineBreakEnd(chunk: string): number {
|
|
|
47
89
|
|
|
48
90
|
/**
|
|
49
91
|
* Applies sorted, non-overlapping edits to the source, tidying the lines of
|
|
50
|
-
* deleted ranges
|
|
51
|
-
* line-state tracking, so the work
|
|
52
|
-
* 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.
|
|
53
100
|
*/
|
|
54
|
-
export function applyEdits(source: string, edits: readonly Edit[]): string {
|
|
101
|
+
export function applyEdits(source: string, edits: readonly Edit[], lines: ShieldedLines = NO_SHIELDED_LINES): string {
|
|
55
102
|
const segments: string[] = [];
|
|
56
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
|
+
};
|
|
57
115
|
/** Whether the output's current (unterminated) line is all whitespace so far. */
|
|
58
116
|
let lineIsBlank = true;
|
|
59
117
|
/** Last character of the output so far ("" while empty). */
|
|
60
118
|
let lastChar = "";
|
|
61
119
|
|
|
62
|
-
|
|
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 => {
|
|
63
125
|
if (chunk === "") return;
|
|
64
126
|
segments.push(chunk);
|
|
65
127
|
const breakEnd = lastLineBreakEnd(chunk);
|
|
@@ -68,27 +130,59 @@ export function applyEdits(source: string, edits: readonly Edit[]): string {
|
|
|
68
130
|
};
|
|
69
131
|
|
|
70
132
|
/**
|
|
71
|
-
*
|
|
72
|
-
*
|
|
73
|
-
*
|
|
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.
|
|
74
137
|
*/
|
|
75
|
-
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 = "";
|
|
76
153
|
while (segments.length > 0) {
|
|
77
154
|
const last = segments[segments.length - 1] as string;
|
|
78
155
|
const breakEnd = lastLineBreakEnd(last);
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
156
|
+
taken = last.slice(breakEnd === -1 ? 0 : breakEnd) + taken;
|
|
157
|
+
if (breakEnd !== -1) {
|
|
158
|
+
segments[segments.length - 1] = last.slice(0, breakEnd);
|
|
159
|
+
break;
|
|
82
160
|
}
|
|
83
|
-
|
|
84
|
-
segments[segments.length - 1] = last.slice(0, tookBreak ? breakEnd - 1 : breakEnd);
|
|
85
|
-
lineIsBlank = !tookBreak;
|
|
86
|
-
lastChar = last.charAt(breakEnd - (tookBreak ? 2 : 1));
|
|
87
|
-
return tookBreak;
|
|
161
|
+
segments.pop();
|
|
88
162
|
}
|
|
89
163
|
lineIsBlank = true;
|
|
90
|
-
lastChar =
|
|
91
|
-
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);
|
|
92
186
|
};
|
|
93
187
|
|
|
94
188
|
/**
|
|
@@ -137,39 +231,54 @@ export function applyEdits(source: string, edits: readonly Edit[]): string {
|
|
|
137
231
|
// goes in.
|
|
138
232
|
const lineFeed = source.slice(edit.start, lineEnd).includes("\r\n") ? "\r\n" : "\n";
|
|
139
233
|
if (lineIsBlank) {
|
|
140
|
-
|
|
141
|
-
if (lastChar !== "\n")
|
|
234
|
+
takeCurrentLine();
|
|
235
|
+
if (lastChar !== "\n") append(lineFeed);
|
|
142
236
|
} else {
|
|
143
237
|
trimLineEnd();
|
|
144
|
-
|
|
238
|
+
append(lineFeed);
|
|
145
239
|
}
|
|
146
|
-
|
|
240
|
+
append(lineFeed);
|
|
147
241
|
cursor = isBlank(restContent) ? lineEnd : skipSpaces(cursor);
|
|
148
242
|
continue;
|
|
149
243
|
}
|
|
150
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
|
+
|
|
151
252
|
if (lineIsBlank && isBlank(restContent)) {
|
|
152
|
-
// The range occupied whole line(s):
|
|
153
|
-
//
|
|
154
|
-
//
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
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;
|
|
159
260
|
} else if (isBlank(restContent)) {
|
|
160
|
-
// 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.
|
|
161
264
|
trimLineEnd();
|
|
162
|
-
|
|
265
|
+
keep([...own, ...after], after.length + heldAmong(first + 1, last));
|
|
163
266
|
cursor = lineEnd;
|
|
164
|
-
} else if (
|
|
267
|
+
} else if (
|
|
268
|
+
!lineIsBlank &&
|
|
269
|
+
own.length > 0 &&
|
|
270
|
+
(isApart(first, last) || needsSpace(lastChar, source.charAt(cursor)))
|
|
271
|
+
) {
|
|
165
272
|
// Code on both sides of a range that spans lines. Per ECMA-262 a
|
|
166
273
|
// multi-line comment acts as a line terminator for semicolon
|
|
167
|
-
// insertion, so
|
|
168
|
-
//
|
|
169
|
-
//
|
|
170
|
-
//
|
|
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.
|
|
171
280
|
trimLineEnd();
|
|
172
|
-
|
|
281
|
+
keep(own, heldAmong(first + 1, last - 1) + 1);
|
|
173
282
|
} else if (lastChar !== "" && !isBlank(lastChar)) {
|
|
174
283
|
// Code right before the range, more on the same line after it.
|
|
175
284
|
if (edit.jsx === true) continue;
|
|
@@ -183,7 +292,15 @@ export function applyEdits(source: string, edits: readonly Edit[]): string {
|
|
|
183
292
|
}
|
|
184
293
|
} else {
|
|
185
294
|
// Whitespace (or the line start) right before the range: the
|
|
186
|
-
// 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
|
+
}
|
|
187
304
|
cursor = skipSpaces(cursor);
|
|
188
305
|
if (edit.jsx !== true && !lineIsBlank && CLOSING_PUNCTUATION.includes(source.charAt(cursor))) {
|
|
189
306
|
// `a /* x */)` — the space before only held the comment off the code.
|
|
@@ -194,6 +311,11 @@ export function applyEdits(source: string, edits: readonly Edit[]): string {
|
|
|
194
311
|
|
|
195
312
|
segments.push(source.slice(cursor));
|
|
196
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
|
+
}
|
|
197
319
|
}
|
|
198
320
|
|
|
199
321
|
/** Where blocks open and close besides brackets: JSX tags, whose children are laid out like a block's. */
|
|
@@ -206,6 +328,56 @@ export interface BlockTags {
|
|
|
206
328
|
|
|
207
329
|
type LineKind = "vanishing" | "blank" | "code";
|
|
208
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
|
+
|
|
209
381
|
/**
|
|
210
382
|
* Deletions of the blank lines that removing whole comment lines would pile
|
|
211
383
|
* up. Where comment-only lines vanish, the blank lines around them close
|
|
@@ -219,35 +391,25 @@ type LineKind = "vanishing" | "blank" | "code";
|
|
|
219
391
|
* against that edge stay: the ones on the far side of the comment merely
|
|
220
392
|
* separated the comment from the code.
|
|
221
393
|
*
|
|
222
|
-
* `deletions` are the ranges about to be deleted, sorted
|
|
223
|
-
*
|
|
224
|
-
*
|
|
225
|
-
*
|
|
226
|
-
*
|
|
227
|
-
* 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.
|
|
228
399
|
*/
|
|
229
400
|
export function blankLineEdits(
|
|
230
401
|
source: string,
|
|
231
402
|
deletions: readonly Range[],
|
|
232
403
|
comments: readonly Range[],
|
|
233
|
-
|
|
404
|
+
{ shielded, held }: ShieldedLines,
|
|
234
405
|
tags: BlockTags,
|
|
235
406
|
): Edit[] {
|
|
236
|
-
const offsets =
|
|
237
|
-
|
|
238
|
-
const lineCount =
|
|
239
|
-
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;
|
|
240
409
|
const lineStart = (line: number): number => offsets[line] as number;
|
|
241
410
|
const lineContentEnd = (line: number): number =>
|
|
242
411
|
endOfContent(source, lineStart(line), offsets[line + 1] ?? source.length);
|
|
243
412
|
|
|
244
|
-
const kinds: LineKind[] = [];
|
|
245
|
-
for (let line = 0; line < lineCount; line += 1) {
|
|
246
|
-
kinds.push(
|
|
247
|
-
lineKind(source, lineStart(line), offsets[line + 1] ?? source.length, lineContentEnd(line), deletions, comments),
|
|
248
|
-
);
|
|
249
|
-
}
|
|
250
|
-
|
|
251
413
|
/** Offset of the first (or last) character on the line that survives the deletions and is not whitespace. */
|
|
252
414
|
const survivingEdge = (line: number, fromEnd: boolean): number | undefined => {
|
|
253
415
|
const start = lineStart(line);
|
|
@@ -281,12 +443,14 @@ export function blankLineEdits(
|
|
|
281
443
|
continue;
|
|
282
444
|
}
|
|
283
445
|
const first = line;
|
|
446
|
+
// The lines of the run that are blank once the deletions are made.
|
|
284
447
|
const blanks: number[] = [];
|
|
285
448
|
// Blank lines between consecutive vanishing lines, from before the first to after the last.
|
|
286
449
|
const gaps = [0];
|
|
287
450
|
for (; line < lineCount && kinds[line] !== "code"; line += 1) {
|
|
288
451
|
if (kinds[line] === "vanishing") {
|
|
289
452
|
gaps.push(0);
|
|
453
|
+
if (held.has(line + 1)) blanks.push(line);
|
|
290
454
|
} else {
|
|
291
455
|
blanks.push(line);
|
|
292
456
|
gaps[gaps.length - 1] = (gaps[gaps.length - 1] as number) + 1;
|
|
@@ -310,7 +474,7 @@ export function blankLineEdits(
|
|
|
310
474
|
else keep = gaps.reduce((widest, gap) => Math.max(widest, gap));
|
|
311
475
|
}
|
|
312
476
|
for (const [position, blank] of blanks.entries()) {
|
|
313
|
-
if (
|
|
477
|
+
if (shielded.has(blank + 1)) keep = Math.max(keep, position + 1);
|
|
314
478
|
}
|
|
315
479
|
for (const blank of blanks.slice(keep)) {
|
|
316
480
|
edits.push({ start: lineStart(blank), end: lineContentEnd(blank) });
|