Skip to content

Commit a92b68a

Browse files
committed
Generalize SyntaxRefactoringCodeActionProvider to work with EditRefactoringProvider
Rather than only adapt refactoring actions that conform to SyntaxRefactoringProvider, which takes a syntax node and produces a syntax node, adapt to the less-constraining EditRefactoringProvider, which takes a syntax node and produces edits. We can map edits over just as effectively.
1 parent 1dd429b commit a92b68a

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

Sources/SourceKitLSP/Swift/CodeActions/SyntaxRefactoringCodeActionProvider.swift

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import SwiftSyntax
1616

1717
/// Protocol that adapts a SyntaxRefactoringProvider (that comes from
1818
/// swift-syntax) into a SyntaxCodeActionProvider.
19-
protocol SyntaxRefactoringCodeActionProvider: SyntaxCodeActionProvider, SyntaxRefactoringProvider {
19+
protocol SyntaxRefactoringCodeActionProvider: SyntaxCodeActionProvider, EditRefactoringProvider {
2020
static var title: String { get }
2121
}
2222

@@ -31,20 +31,23 @@ extension SyntaxRefactoringCodeActionProvider where Self.Context == Void {
3131
return []
3232
}
3333

34-
guard let refactored = Self.refactor(syntax: node) else {
34+
let sourceEdits = Self.textRefactor(syntax: node)
35+
if sourceEdits.isEmpty {
3536
return []
3637
}
3738

38-
let edit = TextEdit(
39-
range: scope.snapshot.range(of: node),
40-
newText: refactored.description
41-
)
39+
let textEdits = sourceEdits.map { edit in
40+
TextEdit(
41+
range: scope.snapshot.range(of: edit.range),
42+
newText: edit.replacement
43+
)
44+
}
4245

4346
return [
4447
CodeAction(
4548
title: Self.title,
4649
kind: .refactorInline,
47-
edit: WorkspaceEdit(changes: [scope.snapshot.uri: [edit]])
50+
edit: WorkspaceEdit(changes: [scope.snapshot.uri: textEdits])
4851
)
4952
]
5053
}

0 commit comments

Comments
 (0)