From 8848ab1c5fbef89055ad35118e735b0797d81023 Mon Sep 17 00:00:00 2001 From: Simon Holthausen Date: Mon, 11 Jan 2021 15:06:47 +0100 Subject: [PATCH] (fix) add prefix/suffix to rename #755 --- .../typescript/features/RenameProvider.ts | 29 ++++++--- .../features/RenameProvider.test.ts | 65 ++++++++++++++++++- .../testfiles/{ => rename}/rename.svelte | 0 .../testfiles/{ => rename}/rename2.svelte | 0 .../testfiles/{ => rename}/rename3.svelte | 0 .../testfiles/{ => rename}/rename4.svelte | 0 .../testfiles/{ => rename}/rename5.svelte | 0 .../testfiles/rename/rename6.svelte | 8 +++ 8 files changed, 92 insertions(+), 10 deletions(-) rename packages/language-server/test/plugins/typescript/testfiles/{ => rename}/rename.svelte (100%) rename packages/language-server/test/plugins/typescript/testfiles/{ => rename}/rename2.svelte (100%) rename packages/language-server/test/plugins/typescript/testfiles/{ => rename}/rename3.svelte (100%) rename packages/language-server/test/plugins/typescript/testfiles/{ => rename}/rename4.svelte (100%) rename packages/language-server/test/plugins/typescript/testfiles/{ => rename}/rename5.svelte (100%) create mode 100644 packages/language-server/test/plugins/typescript/testfiles/rename/rename6.svelte diff --git a/packages/language-server/src/plugins/typescript/features/RenameProvider.ts b/packages/language-server/src/plugins/typescript/features/RenameProvider.ts index 5b3106853..2295b4f07 100644 --- a/packages/language-server/src/plugins/typescript/features/RenameProvider.ts +++ b/packages/language-server/src/plugins/typescript/features/RenameProvider.ts @@ -50,15 +50,23 @@ export class RenameProviderImpl implements RenameProvider { return null; } - const renameLocations = lang.findRenameLocations(tsDoc.filePath, offset, false, false); + const renameLocations = lang.findRenameLocations( + tsDoc.filePath, + offset, + false, + false, + true + ); if (!renameLocations) { return null; } const docs = new Map([[tsDoc.filePath, fragment]]); - let convertedRenameLocations: Array = await this.mapAndFilterRenameLocations(renameLocations, docs); + let convertedRenameLocations: Array< + ts.RenameLocation & { + range: Range; + } + > = await this.mapAndFilterRenameLocations(renameLocations, docs); // eslint-disable-next-line max-len const additionalRenameForPropRenameInsideComponentWithProp = await this.getAdditionLocationsForRenameOfPropInsideComponentWithProp( document, @@ -94,7 +102,10 @@ export class RenameProviderImpl implements RenameProvider { if (!acc.changes[uri]) { acc.changes[uri] = []; } - acc.changes[uri].push({ newText: newName, range: loc.range }); + acc.changes[uri].push({ + newText: (loc.prefixText || '') + newName + (loc.suffixText || ''), + range: loc.range + }); return acc; }, >>{ changes: {} } @@ -137,7 +148,8 @@ export class RenameProviderImpl implements RenameProvider { /** * If user renames prop of component A inside component A, * we need to handle the rename of the prop of A ourselves. - * Reason: the rename will do {oldPropName: newPropName}, we have to handle + * Reason: the rename will do {oldPropName: newPropName}, meaning + * the rename will not propagate further, so we have to handle * the conversion to {newPropName: newPropName} ourselves. */ private async getAdditionLocationsForRenameOfPropInsideComponentWithProp( @@ -196,8 +208,9 @@ export class RenameProviderImpl implements RenameProvider { * If user renames prop of component A inside component B, * we need to handle the rename of the prop of A ourselves. * Reason: the rename will rename the prop in the computed svelte2tsx code, - * but not the `export let X` code in the original. This additional logic - * is done in this method. + * but not the `export let X` code in the original because the + * rename does not propagate further than the prop. + * This additional logic/propagation is done in this method. */ private async getAdditionalLocationsForRenameOfPropInsideOtherComponent( convertedRenameLocations: Array, diff --git a/packages/language-server/test/plugins/typescript/features/RenameProvider.test.ts b/packages/language-server/test/plugins/typescript/features/RenameProvider.test.ts index ffa327ef0..3b811e268 100644 --- a/packages/language-server/test/plugins/typescript/features/RenameProvider.test.ts +++ b/packages/language-server/test/plugins/typescript/features/RenameProvider.test.ts @@ -12,7 +12,7 @@ const testDir = path.join(__dirname, '..'); describe('RenameProvider', () => { function getFullPath(filename: string) { - return path.join(testDir, 'testfiles', filename); + return path.join(testDir, 'testfiles', 'rename', filename); } function getUri(filename: string) { @@ -34,7 +34,17 @@ describe('RenameProvider', () => { const renameDoc3 = await openDoc('rename3.svelte'); const renameDoc4 = await openDoc('rename4.svelte'); const renameDoc5 = await openDoc('rename5.svelte'); - return { provider, renameDoc1, renameDoc2, renameDoc3, renameDoc4, renameDoc5, docManager }; + const renameDoc6 = await openDoc('rename6.svelte'); + return { + provider, + renameDoc1, + renameDoc2, + renameDoc3, + renameDoc4, + renameDoc5, + renameDoc6, + docManager + }; async function openDoc(filename: string) { const filePath = getFullPath(filename); @@ -408,4 +418,55 @@ describe('RenameProvider', () => { assert.deepStrictEqual(result, null); }); + + it('should rename with prefix', async () => { + const { provider, renameDoc6 } = await setup(); + const result = await provider.rename(renameDoc6, Position.create(3, 9), 'newName'); + + assert.deepStrictEqual(result, { + changes: { + [getUri('rename6.svelte')]: [ + { + newText: 'newName', + range: { + start: { + character: 8, + line: 3 + }, + end: { + character: 11, + line: 3 + } + } + }, + { + newText: 'foo: newName', + range: { + start: { + character: 16, + line: 4 + }, + end: { + character: 19, + line: 4 + } + } + }, + { + newText: 'foo: newName', + range: { + start: { + character: 18, + line: 7 + }, + end: { + character: 21, + line: 7 + } + } + } + ] + } + }); + }); }); diff --git a/packages/language-server/test/plugins/typescript/testfiles/rename.svelte b/packages/language-server/test/plugins/typescript/testfiles/rename/rename.svelte similarity index 100% rename from packages/language-server/test/plugins/typescript/testfiles/rename.svelte rename to packages/language-server/test/plugins/typescript/testfiles/rename/rename.svelte diff --git a/packages/language-server/test/plugins/typescript/testfiles/rename2.svelte b/packages/language-server/test/plugins/typescript/testfiles/rename/rename2.svelte similarity index 100% rename from packages/language-server/test/plugins/typescript/testfiles/rename2.svelte rename to packages/language-server/test/plugins/typescript/testfiles/rename/rename2.svelte diff --git a/packages/language-server/test/plugins/typescript/testfiles/rename3.svelte b/packages/language-server/test/plugins/typescript/testfiles/rename/rename3.svelte similarity index 100% rename from packages/language-server/test/plugins/typescript/testfiles/rename3.svelte rename to packages/language-server/test/plugins/typescript/testfiles/rename/rename3.svelte diff --git a/packages/language-server/test/plugins/typescript/testfiles/rename4.svelte b/packages/language-server/test/plugins/typescript/testfiles/rename/rename4.svelte similarity index 100% rename from packages/language-server/test/plugins/typescript/testfiles/rename4.svelte rename to packages/language-server/test/plugins/typescript/testfiles/rename/rename4.svelte diff --git a/packages/language-server/test/plugins/typescript/testfiles/rename5.svelte b/packages/language-server/test/plugins/typescript/testfiles/rename/rename5.svelte similarity index 100% rename from packages/language-server/test/plugins/typescript/testfiles/rename5.svelte rename to packages/language-server/test/plugins/typescript/testfiles/rename/rename5.svelte diff --git a/packages/language-server/test/plugins/typescript/testfiles/rename/rename6.svelte b/packages/language-server/test/plugins/typescript/testfiles/rename/rename6.svelte new file mode 100644 index 000000000..a7b941af8 --- /dev/null +++ b/packages/language-server/test/plugins/typescript/testfiles/rename/rename6.svelte @@ -0,0 +1,8 @@ + + +