Skip to content

Commit 52c8ab1

Browse files
committed
Fix code action scope after main update
1 parent e31adb4 commit 52c8ab1

3 files changed

Lines changed: 27 additions & 2 deletions

File tree

Sources/SwiftLanguageService/CodeActions/ConvertCommentToDocComment.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ struct ConvertCommentToDocComment: SyntaxRefactoringProvider {
3838
extension ConvertCommentToDocComment: SyntaxRefactoringCodeActionProvider {
3939
static let title = "Convert Comment to Doc Comment"
4040

41-
static func nodeToRefactor(in scope: SyntaxCodeActionScope) -> DeclSyntax? {
41+
static func nodeToRefactor(in scope: CodeActionScope) -> DeclSyntax? {
4242
let cursorPosition = scope.snapshot.absolutePosition(of: scope.request.range.lowerBound)
4343
guard let token = scope.file.token(at: cursorPosition) else {
4444
return nil

Sources/SwiftLanguageService/CodeActions/ForEachToForInCodeAction.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ struct ForEachToForInCodeAction: SyntaxCodeActionProvider {
7979
replacement.leadingTrivia = []
8080

8181
let edit = TextEdit(
82-
range: scope.snapshot.range(of: match.callExpr),
82+
range: scope.snapshot.absolutePositionRange(of: match.callExpr.trimmedRange),
8383
newText: replacement.description
8484
)
8585

Tests/SourceKitLSPTests/ForEachToForInCodeActionTests.swift

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,31 @@ final class ForEachToForInCodeActionTests: SourceKitLSPTestCase {
101101
)
102102
}
103103

104+
func testReplacementRangePreservesSurroundingTrivia() async throws {
105+
let (project, uri, positions) = try await makeProject(
106+
"""
107+
func test() {
108+
let array = [1, 2, 3]
109+
// Keep this comment.
110+
1️⃣array.2️⃣forEach { item in
111+
print(item)
112+
}3️⃣ // Keep this trailing comment.
113+
}
114+
"""
115+
)
116+
let action = try await forEachAction(in: project, uri: uri, at: positions["2️⃣"])
117+
let change = try XCTUnwrap(action?.edit?.changes?[uri]?.first)
118+
XCTAssertEqual(change.range, positions["1️⃣"]..<positions["3️⃣"])
119+
XCTAssertEqual(
120+
change.newText,
121+
"""
122+
for item in array {
123+
print(item)
124+
}
125+
"""
126+
)
127+
}
128+
104129
func testRejectsCustomForEach() async throws {
105130
let (project, uri, positions) = try await makeProject(
106131
"""

0 commit comments

Comments
 (0)