From 68da57c6593d087cadc40cc0644bdec2dea6d189 Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Tue, 17 Oct 2017 14:55:41 -0700 Subject: [PATCH 1/2] Disambiguate same-named refactors using description --- src/harness/fourslash.ts | 7 ++++--- tests/cases/fourslash/extract-const1.ts | 15 +++++++++++++++ tests/cases/fourslash/fourslash.ts | 2 +- 3 files changed, 20 insertions(+), 4 deletions(-) create mode 100644 tests/cases/fourslash/extract-const1.ts diff --git a/src/harness/fourslash.ts b/src/harness/fourslash.ts index 493b0187b57ad..3203c3d805fbe 100644 --- a/src/harness/fourslash.ts +++ b/src/harness/fourslash.ts @@ -2846,17 +2846,17 @@ Actual: ${stringify(fullActual)}`); } } - public applyRefactor({ refactorName, actionName, actionDescription, newContent: newContentWithRenameMarker }: FourSlashInterface.ApplyRefactorOptions) { + public applyRefactor({ refactorName, description, actionName, actionDescription, newContent: newContentWithRenameMarker }: FourSlashInterface.ApplyRefactorOptions) { const range = this.getSelection(); const refactors = this.languageService.getApplicableRefactors(this.activeFile.fileName, range); - const refactor = refactors.find(r => r.name === refactorName); + const refactor = refactors.find(r => r.name === refactorName && description === undefined || description === r.description); if (!refactor) { this.raiseError(`The expected refactor: ${refactorName} is not available at the marker location.\nAvailable refactors: ${refactors.map(r => r.name)}`); } const action = refactor.actions.find(a => a.name === actionName); if (!action) { - this.raiseError(`The expected action: ${action} is not included in: ${refactor.actions.map(a => a.name)}`); + this.raiseError(`The expected action: ${actionName} is not included in: ${refactor.actions.map(a => a.name)}`); } if (action.description !== actionDescription) { this.raiseError(`Expected action description to be ${JSON.stringify(actionDescription)}, got: ${JSON.stringify(action.description)}`); @@ -4428,6 +4428,7 @@ namespace FourSlashInterface { export interface ApplyRefactorOptions { refactorName: string; + description?: string; actionName: string; actionDescription: string; newContent: string; diff --git a/tests/cases/fourslash/extract-const1.ts b/tests/cases/fourslash/extract-const1.ts new file mode 100644 index 0000000000000..c9ae6383d1e3a --- /dev/null +++ b/tests/cases/fourslash/extract-const1.ts @@ -0,0 +1,15 @@ +/// + +////const x = /*a*/0/*b*/; + +goTo.select("a", "b"); +edit.applyRefactor({ + refactorName: "Extract Symbol", + description: "Extract constant", + actionName: "constant_scope_0", + actionDescription: "Extract to constant in enclosing scope", + newContent: +`const newLocal = 0; + +const x = /*RENAME*/newLocal;` +}); diff --git a/tests/cases/fourslash/fourslash.ts b/tests/cases/fourslash/fourslash.ts index e1d9607de8aaf..0d4e0d89e5d91 100644 --- a/tests/cases/fourslash/fourslash.ts +++ b/tests/cases/fourslash/fourslash.ts @@ -339,7 +339,7 @@ declare namespace FourSlashInterface { enableFormatting(): void; disableFormatting(): void; - applyRefactor(options: { refactorName: string, actionName: string, actionDescription: string, newContent: string }): void; + applyRefactor(options: { refactorName: string, description?: string, actionName: string, actionDescription: string, newContent: string }): void; } class debug { printCurrentParameterHelp(): void; From ce7dc33ee4276784d7ce6c69e352f107acc9e98f Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Wed, 18 Oct 2017 08:44:36 -0700 Subject: [PATCH 2/2] Use only actionName to disambiguate --- src/harness/fourslash.ts | 11 +++++------ tests/cases/fourslash/extract-const1.ts | 1 - tests/cases/fourslash/fourslash.ts | 2 +- 3 files changed, 6 insertions(+), 8 deletions(-) diff --git a/src/harness/fourslash.ts b/src/harness/fourslash.ts index 3203c3d805fbe..7a2a76b44f325 100644 --- a/src/harness/fourslash.ts +++ b/src/harness/fourslash.ts @@ -2846,17 +2846,17 @@ Actual: ${stringify(fullActual)}`); } } - public applyRefactor({ refactorName, description, actionName, actionDescription, newContent: newContentWithRenameMarker }: FourSlashInterface.ApplyRefactorOptions) { + public applyRefactor({ refactorName, actionName, actionDescription, newContent: newContentWithRenameMarker }: FourSlashInterface.ApplyRefactorOptions) { const range = this.getSelection(); const refactors = this.languageService.getApplicableRefactors(this.activeFile.fileName, range); - const refactor = refactors.find(r => r.name === refactorName && description === undefined || description === r.description); - if (!refactor) { + const refactorsWithName = refactors.filter(r => r.name === refactorName); + if (refactorsWithName.length === 0) { this.raiseError(`The expected refactor: ${refactorName} is not available at the marker location.\nAvailable refactors: ${refactors.map(r => r.name)}`); } - const action = refactor.actions.find(a => a.name === actionName); + const action = ts.firstDefined(refactorsWithName, refactor => refactor.actions.find(a => a.name === actionName)); if (!action) { - this.raiseError(`The expected action: ${actionName} is not included in: ${refactor.actions.map(a => a.name)}`); + this.raiseError(`The expected action: ${actionName} is not included in: ${ts.flatMap(refactorsWithName, r => r.actions.map(a => a.name))}`); } if (action.description !== actionDescription) { this.raiseError(`Expected action description to be ${JSON.stringify(actionDescription)}, got: ${JSON.stringify(action.description)}`); @@ -4428,7 +4428,6 @@ namespace FourSlashInterface { export interface ApplyRefactorOptions { refactorName: string; - description?: string; actionName: string; actionDescription: string; newContent: string; diff --git a/tests/cases/fourslash/extract-const1.ts b/tests/cases/fourslash/extract-const1.ts index c9ae6383d1e3a..3ed9373f2bae1 100644 --- a/tests/cases/fourslash/extract-const1.ts +++ b/tests/cases/fourslash/extract-const1.ts @@ -5,7 +5,6 @@ goTo.select("a", "b"); edit.applyRefactor({ refactorName: "Extract Symbol", - description: "Extract constant", actionName: "constant_scope_0", actionDescription: "Extract to constant in enclosing scope", newContent: diff --git a/tests/cases/fourslash/fourslash.ts b/tests/cases/fourslash/fourslash.ts index 0d4e0d89e5d91..e1d9607de8aaf 100644 --- a/tests/cases/fourslash/fourslash.ts +++ b/tests/cases/fourslash/fourslash.ts @@ -339,7 +339,7 @@ declare namespace FourSlashInterface { enableFormatting(): void; disableFormatting(): void; - applyRefactor(options: { refactorName: string, description?: string, actionName: string, actionDescription: string, newContent: string }): void; + applyRefactor(options: { refactorName: string, actionName: string, actionDescription: string, newContent: string }): void; } class debug { printCurrentParameterHelp(): void;