Skip to content

Commit 092e90c

Browse files
authored
(fix) add prefix/suffix to rename (#757)
#755
1 parent f9db434 commit 092e90c

File tree

8 files changed

+92
-10
lines changed

8 files changed

+92
-10
lines changed

packages/language-server/src/plugins/typescript/features/RenameProvider.ts

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,23 @@ export class RenameProviderImpl implements RenameProvider {
5050
return null;
5151
}
5252

53-
const renameLocations = lang.findRenameLocations(tsDoc.filePath, offset, false, false);
53+
const renameLocations = lang.findRenameLocations(
54+
tsDoc.filePath,
55+
offset,
56+
false,
57+
false,
58+
true
59+
);
5460
if (!renameLocations) {
5561
return null;
5662
}
5763

5864
const docs = new Map<string, SnapshotFragment>([[tsDoc.filePath, fragment]]);
59-
let convertedRenameLocations: Array<ts.RenameLocation & {
60-
range: Range;
61-
}> = await this.mapAndFilterRenameLocations(renameLocations, docs);
65+
let convertedRenameLocations: Array<
66+
ts.RenameLocation & {
67+
range: Range;
68+
}
69+
> = await this.mapAndFilterRenameLocations(renameLocations, docs);
6270
// eslint-disable-next-line max-len
6371
const additionalRenameForPropRenameInsideComponentWithProp = await this.getAdditionLocationsForRenameOfPropInsideComponentWithProp(
6472
document,
@@ -94,7 +102,10 @@ export class RenameProviderImpl implements RenameProvider {
94102
if (!acc.changes[uri]) {
95103
acc.changes[uri] = [];
96104
}
97-
acc.changes[uri].push({ newText: newName, range: loc.range });
105+
acc.changes[uri].push({
106+
newText: (loc.prefixText || '') + newName + (loc.suffixText || ''),
107+
range: loc.range
108+
});
98109
return acc;
99110
},
100111
<Required<Pick<WorkspaceEdit, 'changes'>>>{ changes: {} }
@@ -137,7 +148,8 @@ export class RenameProviderImpl implements RenameProvider {
137148
/**
138149
* If user renames prop of component A inside component A,
139150
* we need to handle the rename of the prop of A ourselves.
140-
* Reason: the rename will do {oldPropName: newPropName}, we have to handle
151+
* Reason: the rename will do {oldPropName: newPropName}, meaning
152+
* the rename will not propagate further, so we have to handle
141153
* the conversion to {newPropName: newPropName} ourselves.
142154
*/
143155
private async getAdditionLocationsForRenameOfPropInsideComponentWithProp(
@@ -196,8 +208,9 @@ export class RenameProviderImpl implements RenameProvider {
196208
* If user renames prop of component A inside component B,
197209
* we need to handle the rename of the prop of A ourselves.
198210
* Reason: the rename will rename the prop in the computed svelte2tsx code,
199-
* but not the `export let X` code in the original. This additional logic
200-
* is done in this method.
211+
* but not the `export let X` code in the original because the
212+
* rename does not propagate further than the prop.
213+
* This additional logic/propagation is done in this method.
201214
*/
202215
private async getAdditionalLocationsForRenameOfPropInsideOtherComponent(
203216
convertedRenameLocations: Array<ts.RenameLocation & { range: Range }>,

packages/language-server/test/plugins/typescript/features/RenameProvider.test.ts

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const testDir = path.join(__dirname, '..');
1212

1313
describe('RenameProvider', () => {
1414
function getFullPath(filename: string) {
15-
return path.join(testDir, 'testfiles', filename);
15+
return path.join(testDir, 'testfiles', 'rename', filename);
1616
}
1717

1818
function getUri(filename: string) {
@@ -34,7 +34,17 @@ describe('RenameProvider', () => {
3434
const renameDoc3 = await openDoc('rename3.svelte');
3535
const renameDoc4 = await openDoc('rename4.svelte');
3636
const renameDoc5 = await openDoc('rename5.svelte');
37-
return { provider, renameDoc1, renameDoc2, renameDoc3, renameDoc4, renameDoc5, docManager };
37+
const renameDoc6 = await openDoc('rename6.svelte');
38+
return {
39+
provider,
40+
renameDoc1,
41+
renameDoc2,
42+
renameDoc3,
43+
renameDoc4,
44+
renameDoc5,
45+
renameDoc6,
46+
docManager
47+
};
3848

3949
async function openDoc(filename: string) {
4050
const filePath = getFullPath(filename);
@@ -408,4 +418,55 @@ describe('RenameProvider', () => {
408418

409419
assert.deepStrictEqual(result, null);
410420
});
421+
422+
it('should rename with prefix', async () => {
423+
const { provider, renameDoc6 } = await setup();
424+
const result = await provider.rename(renameDoc6, Position.create(3, 9), 'newName');
425+
426+
assert.deepStrictEqual(result, {
427+
changes: {
428+
[getUri('rename6.svelte')]: [
429+
{
430+
newText: 'newName',
431+
range: {
432+
start: {
433+
character: 8,
434+
line: 3
435+
},
436+
end: {
437+
character: 11,
438+
line: 3
439+
}
440+
}
441+
},
442+
{
443+
newText: 'foo: newName',
444+
range: {
445+
start: {
446+
character: 16,
447+
line: 4
448+
},
449+
end: {
450+
character: 19,
451+
line: 4
452+
}
453+
}
454+
},
455+
{
456+
newText: 'foo: newName',
457+
range: {
458+
start: {
459+
character: 18,
460+
line: 7
461+
},
462+
end: {
463+
character: 21,
464+
line: 7
465+
}
466+
}
467+
}
468+
]
469+
}
470+
});
471+
});
411472
});
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<script lang="ts">
2+
function action(_: any, props: {foo: number}) {}
3+
4+
const foo = 1;
5+
action(null, {foo});
6+
</script>
7+
8+
<div use:action={{foo}} />

0 commit comments

Comments
 (0)