diff --git a/Sources/SourceKitLSP/Swift/MacroExpansion.swift b/Sources/SourceKitLSP/Swift/MacroExpansion.swift index fa6a95edf..da198b708 100644 --- a/Sources/SourceKitLSP/Swift/MacroExpansion.swift +++ b/Sources/SourceKitLSP/Swift/MacroExpansion.swift @@ -29,17 +29,10 @@ struct MacroExpansion: RefactoringResponse { /// The resulting array of `RefactoringEdit` of a semantic refactoring request var edits: [RefactoringEdit] - init(title: String, uri: DocumentURI, refactoringEdits: [RefactoringEdit]) { + init(title: String, snapshot: DocumentSnapshot, refactoringEdits: [RefactoringEdit]) { self.title = title - self.uri = uri - self.edits = refactoringEdits.compactMap { refactoringEdit in - if refactoringEdit.bufferName.isEmpty { - logger.fault("Unable to retrieve some parts of the expansion") - return nil - } - - return refactoringEdit - } + self.uri = snapshot.uri + self.edits = refactoringEdits.concurrent(to: snapshot) } } diff --git a/Sources/SourceKitLSP/Swift/Refactoring.swift b/Sources/SourceKitLSP/Swift/Refactoring.swift index 0b50bed86..5a407be63 100644 --- a/Sources/SourceKitLSP/Swift/Refactoring.swift +++ b/Sources/SourceKitLSP/Swift/Refactoring.swift @@ -15,7 +15,7 @@ import LanguageServerProtocol import SourceKitD protocol RefactoringResponse { - init(title: String, uri: DocumentURI, refactoringEdits: [RefactoringEdit]) + init(title: String, snapshot: DocumentSnapshot, refactoringEdits: [RefactoringEdit]) } extension RefactoringResponse { @@ -81,7 +81,7 @@ extension RefactoringResponse { return nil } - self.init(title: title, uri: snapshot.uri, refactoringEdits: refactoringEdits) + self.init(title: title, snapshot: snapshot, refactoringEdits: refactoringEdits) } } diff --git a/Sources/SourceKitLSP/Swift/RefactoringEdit.swift b/Sources/SourceKitLSP/Swift/RefactoringEdit.swift index 9fce22208..43eee5129 100644 --- a/Sources/SourceKitLSP/Swift/RefactoringEdit.swift +++ b/Sources/SourceKitLSP/Swift/RefactoringEdit.swift @@ -12,6 +12,8 @@ import LanguageServerProtocol import SourceKitD +import SwiftParser +import SwiftSyntax /// Represents an edit from semantic refactor response. Notionally, a subclass of `TextEdit` @_spi(Testing) public struct RefactoringEdit: Hashable, Sendable, Codable { @@ -59,3 +61,31 @@ extension RefactoringEdit: LSPAnyCodable { ]) } } + +@_spi(Testing) public extension Array { + func concurrent(to snapshot: DocumentSnapshot) -> [RefactoringEdit] { + let sequentialEdits = self.map { refactoringEdit in + + let absoluteStartPosition = snapshot.absolutePosition(of: refactoringEdit.range.lowerBound) + + let absoluteEndPosition = snapshot.absolutePosition(of: refactoringEdit.range.upperBound) + + return SourceEdit(range: absoluteStartPosition..