Skip to content

Commit 6dd88b3

Browse files
authored
Merge pull request #21781 from amcasey/TextChangesReplace
Tidy up textChanges.replace*
2 parents 1b62088 + f77cefe commit 6dd88b3

File tree

11 files changed

+65
-46
lines changed

11 files changed

+65
-46
lines changed

src/services/refactors/extractSymbol.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -968,7 +968,7 @@ namespace ts.refactor.extractSymbol {
968968
}
969969

970970
if (isReadonlyArray(range.range)) {
971-
changeTracker.replaceNodesWithNodes(context.file, range.range, newNodes);
971+
changeTracker.replaceNodeRangeWithNodes(context.file, first(range.range), last(range.range), newNodes);
972972
}
973973
else {
974974
changeTracker.replaceNodeWithNodes(context.file, range.range, newNodes);

src/services/textChanges.ts

Lines changed: 34 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,11 @@ namespace ts.textChanges {
2828
}
2929

3030
export interface ConfigurableStart {
31+
/** True to use getStart() (NB, not getFullStart()) without adjustment. */
3132
useNonAdjustedStartPosition?: boolean;
3233
}
3334
export interface ConfigurableEnd {
35+
/** True to use getEnd() without adjustment. */
3436
useNonAdjustedEndPosition?: boolean;
3537
}
3638

@@ -132,7 +134,7 @@ namespace ts.textChanges {
132134

133135
export function getAdjustedStartPosition(sourceFile: SourceFile, node: Node, options: ConfigurableStart, position: Position) {
134136
if (options.useNonAdjustedStartPosition) {
135-
return node.getFullStart();
137+
return node.getStart();
136138
}
137139
const fullStart = node.getFullStart();
138140
const start = node.getStart(sourceFile);
@@ -280,51 +282,46 @@ namespace ts.textChanges {
280282
return this;
281283
}
282284

283-
public replaceRange(sourceFile: SourceFile, range: TextRange, newNode: Node, options: InsertNodeOptions = {}) {
285+
public replaceRange(sourceFile: SourceFile, range: TextRange, newNode: Node, options: ChangeNodeOptions = {}) {
284286
this.changes.push({ kind: ChangeKind.ReplaceWithSingleNode, sourceFile, range, options, node: newNode });
285287
return this;
286288
}
287289

288290
public replaceNode(sourceFile: SourceFile, oldNode: Node, newNode: Node, options: ChangeNodeOptions = {}) {
289-
const startPosition = getAdjustedStartPosition(sourceFile, oldNode, options, Position.Start);
290-
const endPosition = getAdjustedEndPosition(sourceFile, oldNode, options);
291-
return this.replaceWithSingle(sourceFile, startPosition, endPosition, newNode, options);
291+
const pos = getAdjustedStartPosition(sourceFile, oldNode, options, Position.Start);
292+
const end = getAdjustedEndPosition(sourceFile, oldNode, options);
293+
return this.replaceRange(sourceFile, { pos, end }, newNode, options);
292294
}
293295

294296
public replaceNodeRange(sourceFile: SourceFile, startNode: Node, endNode: Node, newNode: Node, options: ChangeNodeOptions = {}) {
295-
const startPosition = getAdjustedStartPosition(sourceFile, startNode, options, Position.Start);
296-
const endPosition = getAdjustedEndPosition(sourceFile, endNode, options);
297-
return this.replaceWithSingle(sourceFile, startPosition, endPosition, newNode, options);
297+
const pos = getAdjustedStartPosition(sourceFile, startNode, options, Position.Start);
298+
const end = getAdjustedEndPosition(sourceFile, endNode, options);
299+
return this.replaceRange(sourceFile, { pos, end }, newNode, options);
298300
}
299301

300-
private replaceWithSingle(sourceFile: SourceFile, startPosition: number, endPosition: number, newNode: Node, options: ChangeNodeOptions): this {
301-
this.changes.push({
302-
kind: ChangeKind.ReplaceWithSingleNode,
303-
sourceFile,
304-
options,
305-
node: newNode,
306-
range: { pos: startPosition, end: endPosition }
307-
});
308-
return this;
302+
private getDefaultChangeMultipleNodesOptions(): ChangeMultipleNodesOptions {
303+
return {
304+
nodeSeparator: this.newLineCharacter,
305+
useNonAdjustedStartPosition: true,
306+
useNonAdjustedEndPosition: true,
307+
};
309308
}
310309

311-
private replaceWithMultiple(sourceFile: SourceFile, startPosition: number, endPosition: number, newNodes: ReadonlyArray<Node>, options: ChangeMultipleNodesOptions): this {
312-
this.changes.push({
313-
kind: ChangeKind.ReplaceWithMultipleNodes,
314-
sourceFile,
315-
options,
316-
nodes: newNodes,
317-
range: { pos: startPosition, end: endPosition }
318-
});
310+
public replaceRangeWithNodes(sourceFile: SourceFile, range: TextRange, newNodes: ReadonlyArray<Node>, options: ChangeMultipleNodesOptions = this.getDefaultChangeMultipleNodesOptions()) {
311+
this.changes.push({ kind: ChangeKind.ReplaceWithMultipleNodes, sourceFile, range, options, nodes: newNodes });
319312
return this;
320313
}
321314

322-
public replaceNodeWithNodes(sourceFile: SourceFile, oldNode: Node, newNodes: ReadonlyArray<Node>): void {
323-
this.replaceWithMultiple(sourceFile, oldNode.getStart(sourceFile), oldNode.getEnd(), newNodes, { nodeSeparator: this.newLineCharacter });
315+
public replaceNodeWithNodes(sourceFile: SourceFile, oldNode: Node, newNodes: ReadonlyArray<Node>, options: ChangeMultipleNodesOptions = this.getDefaultChangeMultipleNodesOptions()) {
316+
const pos = getAdjustedStartPosition(sourceFile, oldNode, options, Position.Start);
317+
const end = getAdjustedEndPosition(sourceFile, oldNode, options);
318+
return this.replaceRangeWithNodes(sourceFile, { pos, end }, newNodes, options);
324319
}
325320

326-
public replaceNodesWithNodes(sourceFile: SourceFile, oldNodes: ReadonlyArray<Node>, newNodes: ReadonlyArray<Node>): void {
327-
this.replaceWithMultiple(sourceFile, first(oldNodes).getStart(sourceFile), last(oldNodes).getEnd(), newNodes, { nodeSeparator: this.newLineCharacter });
321+
public replaceNodeRangeWithNodes(sourceFile: SourceFile, startNode: Node, endNode: Node, newNodes: ReadonlyArray<Node>, options: ChangeMultipleNodesOptions = this.getDefaultChangeMultipleNodesOptions()) {
322+
const pos = getAdjustedStartPosition(sourceFile, startNode, options, Position.Start);
323+
const end = getAdjustedEndPosition(sourceFile, endNode, options);
324+
return this.replaceRangeWithNodes(sourceFile, { pos, end }, newNodes, options);
328325
}
329326

330327
private insertNodeAt(sourceFile: SourceFile, pos: number, newNode: Node, options: InsertNodeOptions = {}) {
@@ -341,18 +338,18 @@ namespace ts.textChanges {
341338
}
342339

343340
public insertNodeBefore(sourceFile: SourceFile, before: Node, newNode: Node, blankLineBetween = false) {
344-
const startPosition = getAdjustedStartPosition(sourceFile, before, {}, Position.Start);
345-
return this.replaceWithSingle(sourceFile, startPosition, startPosition, newNode, this.getOptionsForInsertNodeBefore(before, blankLineBetween));
341+
const pos = getAdjustedStartPosition(sourceFile, before, {}, Position.Start);
342+
return this.replaceRange(sourceFile, { pos, end: pos }, newNode, this.getOptionsForInsertNodeBefore(before, blankLineBetween));
346343
}
347344

348345
public insertModifierBefore(sourceFile: SourceFile, modifier: SyntaxKind, before: Node): void {
349346
const pos = before.getStart(sourceFile);
350-
this.replaceWithSingle(sourceFile, pos, pos, createToken(modifier), { suffix: " " });
347+
this.replaceRange(sourceFile, { pos, end: pos }, createToken(modifier), { suffix: " " });
351348
}
352349

353350
public changeIdentifierToPropertyAccess(sourceFile: SourceFile, prefix: string, node: Identifier): void {
354-
const startPosition = getAdjustedStartPosition(sourceFile, node, {}, Position.Start);
355-
this.replaceWithSingle(sourceFile, startPosition, startPosition, createPropertyAccess(createIdentifier(prefix), ""), {});
351+
const pos = getAdjustedStartPosition(sourceFile, node, {}, Position.Start);
352+
this.replaceRange(sourceFile, { pos, end: pos }, createPropertyAccess(createIdentifier(prefix), ""), {});
356353
}
357354

358355
private getOptionsForInsertNodeBefore(before: Node, doubleNewlines: boolean): ChangeNodeOptions {
@@ -390,8 +387,8 @@ namespace ts.textChanges {
390387
}
391388

392389
public insertNodeAtEndOfScope(sourceFile: SourceFile, scope: Node, newNode: Node): void {
393-
const startPosition = getAdjustedStartPosition(sourceFile, scope.getLastToken(), {}, Position.Start);
394-
this.replaceWithSingle(sourceFile, startPosition, startPosition, newNode, {
390+
const pos = getAdjustedStartPosition(sourceFile, scope.getLastToken(), {}, Position.Start);
391+
this.replaceRange(sourceFile, { pos, end: pos }, newNode, {
395392
prefix: isLineBreak(sourceFile.text.charCodeAt(scope.getLastToken().pos)) ? this.newLineCharacter : this.newLineCharacter + this.newLineCharacter,
396393
suffix: this.newLineCharacter
397394
});
@@ -433,7 +430,7 @@ namespace ts.textChanges {
433430
}
434431
}
435432
const endPosition = getAdjustedEndPosition(sourceFile, after, {});
436-
return this.replaceWithSingle(sourceFile, endPosition, endPosition, newNode, this.getInsertNodeAfterOptions(after));
433+
return this.replaceRange(sourceFile, { pos: endPosition, end: endPosition }, newNode, this.getInsertNodeAfterOptions(after));
437434
}
438435

439436
private getInsertNodeAfterOptions(node: Node): InsertNodeOptions {

tests/baselines/reference/textChanges/deleteNode2.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,8 @@ var z = 3; // comment 4
99

1010
===MODIFIED===
1111

12-
var x = 1;var z = 3; // comment 4
12+
var x = 1; // some comment - 1
13+
/**
14+
* comment 2
15+
*/
16+
var z = 3; // comment 4

tests/baselines/reference/textChanges/deleteNode4.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,9 @@ var z = 3; // comment 4
99

1010
===MODIFIED===
1111

12-
var x = 1; // comment 3
12+
var x = 1; // some comment - 1
13+
/**
14+
* comment 2
15+
*/
16+
// comment 3
1317
var z = 3; // comment 4

tests/baselines/reference/textChanges/deleteNodeRange2.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,7 @@ var a = 4; // comment 7
1111
===MODIFIED===
1212

1313
// comment 1
14-
var x = 1;// comment 6
14+
var x = 1; // comment 2
15+
// comment 3
16+
// comment 6
1517
var a = 4; // comment 7

tests/baselines/reference/textChanges/deleteNodeRange4.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ var a = 4; // comment 7
1111
===MODIFIED===
1212

1313
// comment 1
14-
var x = 1; // comment 5
14+
var x = 1; // comment 2
15+
// comment 3
16+
// comment 5
1517
// comment 6
1618
var a = 4; // comment 7

tests/baselines/reference/textChanges/replaceNode2.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ var a = 4; // comment 7
1010
===MODIFIED===
1111

1212
// comment 1
13-
var x = 1;
13+
var x = 1; // comment 2
14+
// comment 3
15+
1416
public class class1 implements interface1
1517
{
1618
property1: boolean;

tests/baselines/reference/textChanges/replaceNode4.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ var a = 4; // comment 7
1010
===MODIFIED===
1111

1212
// comment 1
13-
var x = 1;public class class1 implements interface1
13+
var x = 1; // comment 2
14+
// comment 3
15+
public class class1 implements interface1
1416
{
1517
property1: boolean;
1618
} // comment 4

tests/baselines/reference/textChanges/replaceNode5.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ var z = 3; // comment 5
88
// comment 6
99
var a = 4; // comment 7
1010
===MODIFIED===
11+
12+
// comment 1
1113
public class class1 implements interface1
1214
{
1315
property1: boolean;

tests/baselines/reference/textChanges/replaceNodeRange2.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ var a = 4; // comment 7
1010
===MODIFIED===
1111

1212
// comment 1
13-
var x = 1;
13+
var x = 1; // comment 2
14+
// comment 3
15+
1416
public class class1 implements interface1
1517
{
1618
property1: boolean;

0 commit comments

Comments
 (0)