Skip to content

Commit 7b45cf9

Browse files
committed
Show refactors with code actions (only really works with intentions ui)
1 parent 882396a commit 7b45cf9

File tree

4 files changed

+33
-14
lines changed

4 files changed

+33
-14
lines changed

lib/main/atom-ide/codeActionsProvider.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export function getCodeActionsProvider(codefixProvider: CodefixProvider): CodeAc
1414
_diagnostics: Message[],
1515
): Promise<CodeAction[]> {
1616
return (await codefixProvider.runCodeFix(textEditor, range.start)).map((fix) => ({
17-
getTitle: async () => fix.description,
17+
getTitle: async () => ("description" in fix ? fix.description : fix.actionDescription),
1818
dispose: () => {},
1919
apply: async () => {
2020
await codefixProvider.applyFix(fix)

lib/main/atom/codefix/codefixProvider.ts

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@ import * as Atom from "atom"
22
import {ClientResolver, TSClient} from "../../../client"
33
import {ErrorPusher} from "../../errorPusher"
44
import {ApplyEdits} from "../../pluginManager"
5-
import {spanToRange} from "../utils"
5+
import {
6+
applyRefactors,
7+
getApplicableRefactorsActions,
8+
RefactorAction,
9+
} from "../commands/refactorCode"
10+
import {pointToLocation, spanToRange} from "../utils"
611

712
export class CodefixProvider {
813
private supportedFixes: WeakMap<TSClient, Set<number>> = new WeakMap()
@@ -30,7 +35,7 @@ export class CodefixProvider {
3035
public async runCodeFix(
3136
textEditor: Atom.TextEditor,
3237
bufferPosition: Atom.Point,
33-
): Promise<protocol.CodeAction[]> {
38+
): Promise<Array<protocol.CodeAction | RefactorAction>> {
3439
const filePath = textEditor.getPath()
3540

3641
if (filePath === undefined) return []
@@ -52,7 +57,7 @@ export class CodefixProvider {
5257
)
5358

5459
const fixes = await Promise.all(requests)
55-
const results: protocol.CodeAction[] = []
60+
const results: Array<protocol.CodeAction | RefactorAction> = []
5661

5762
for (const result of fixes) {
5863
if (result.body) {
@@ -62,11 +67,24 @@ export class CodefixProvider {
6267
}
6368
}
6469

70+
const refactors = await getApplicableRefactorsActions(client, {
71+
file: filePath,
72+
...pointToLocation(bufferPosition),
73+
})
74+
75+
results.push(...refactors)
76+
6577
return results
6678
}
6779

68-
public async applyFix(fix: protocol.CodeAction) {
69-
return this.applyEdits(fix.changes)
80+
public async applyFix(fix: protocol.CodeAction | RefactorAction) {
81+
if ("changes" in fix) return this.applyEdits(fix.changes)
82+
else {
83+
const client = await this.clientResolver.get(fix.refactorRange.file)
84+
return applyRefactors(fix, client, {
85+
applyEdits: this.applyEdits,
86+
})
87+
}
7088
}
7189

7290
public dispose() {

lib/main/atom/codefix/intentionsProvider.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export function getIntentionsProvider(
5252
async getIntentions({bufferPosition, textEditor}) {
5353
return (await codefixProvider.runCodeFix(textEditor, bufferPosition)).map((fix) => ({
5454
priority: 100,
55-
title: fix.description,
55+
title: "description" in fix ? fix.description : fix.actionDescription,
5656
selected: () => {
5757
handlePromise(codefixProvider.applyFix(fix))
5858
},

lib/main/atom/commands/refactorCode.tsx

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ import {HighlightComponent} from "../views/highlightComponent"
66
import {selectListView} from "../views/simpleSelectionView"
77
import {addCommand, Dependencies} from "./registry"
88

9-
interface RefactorAction {
9+
export interface RefactorAction {
1010
refactorName: string
1111
refactorDescription: string
12+
refactorRange: protocol.FileLocationOrRangeRequestArgs
1213
actionName: string
1314
actionDescription: string
1415
inlineable: boolean
@@ -56,11 +57,11 @@ addCommand("atom-text-editor", "typescript:refactor-selection", (deps) => ({
5657
})
5758

5859
if (selectedAction === undefined) return
59-
await applyRefactors(selectedAction, fileRange, client, deps)
60+
await applyRefactors(selectedAction, client, deps)
6061
},
6162
}))
6263

63-
async function getApplicableRefactorsActions(
64+
export async function getApplicableRefactorsActions(
6465
client: TSClient,
6566
pointOrRange: protocol.FileLocationOrRangeRequestArgs,
6667
) {
@@ -76,6 +77,7 @@ async function getApplicableRefactorsActions(
7677
actions.push({
7778
refactorName: refactor.name,
7879
refactorDescription: refactor.description,
80+
refactorRange: pointOrRange,
7981
actionName: action.name,
8082
actionDescription: action.description,
8183
inlineable: refactor.inlineable !== undefined ? refactor.inlineable : true,
@@ -100,14 +102,13 @@ async function getApplicabeRefactors(
100102
}
101103
}
102104

103-
async function applyRefactors(
105+
export async function applyRefactors(
104106
selectedAction: RefactorAction,
105-
range: protocol.FileLocationOrRangeRequestArgs,
106107
client: TSClient,
107-
deps: Dependencies,
108+
deps: Pick<Dependencies, "applyEdits">,
108109
) {
109110
const responseEdits = await client.execute("getEditsForRefactor", {
110-
...range,
111+
...selectedAction.refactorRange,
111112
refactor: selectedAction.refactorName,
112113
action: selectedAction.actionName,
113114
})

0 commit comments

Comments
 (0)