Skip to content

Commit

Permalink
Convert sourcekitd response into ConcurrentEdits
Browse files Browse the repository at this point in the history
  • Loading branch information
lokesh-tr committed Jun 13, 2024
1 parent 50d4290 commit be4204c
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 14 deletions.
13 changes: 3 additions & 10 deletions Sources/SourceKitLSP/Swift/MacroExpansion.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
4 changes: 2 additions & 2 deletions Sources/SourceKitLSP/Swift/Refactoring.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
}

Expand Down
30 changes: 30 additions & 0 deletions Sources/SourceKitLSP/Swift/RefactoringEdit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -59,3 +61,31 @@ extension RefactoringEdit: LSPAnyCodable {
])
}
}

@_spi(Testing) public extension Array<RefactoringEdit> {
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..<absoluteEndPosition, replacement: refactoringEdit.newText)
}

let concurrentEdits = ConcurrentEdits(fromSequential: sequentialEdits)

var refactoringEdits = [RefactoringEdit]()
for (i, concurrentEdit) in concurrentEdits.edits.enumerated() {
refactoringEdits.append(
RefactoringEdit(
range: snapshot.absolutePositionRange(of: concurrentEdit.range),
newText: concurrentEdit.replacement,
bufferName: self[i].bufferName // Is there a better way to access bufferName after receiving concurrentEdits?
)
)
}

return refactoringEdits
}
}
4 changes: 2 additions & 2 deletions Sources/SourceKitLSP/Swift/SemanticRefactoring.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ struct SemanticRefactoring: RefactoringResponse {
self.edit = edit
}

init(title: String, uri: DocumentURI, refactoringEdits: [RefactoringEdit]) {
init(title: String, snapshot: DocumentSnapshot, refactoringEdits: [RefactoringEdit]) {
self.title = title
self.edit = WorkspaceEdit(changes: [
uri: refactoringEdits.map { TextEdit(range: $0.range, newText: $0.newText) }
snapshot.uri: refactoringEdits.map { TextEdit(range: $0.range, newText: $0.newText) }
])
}
}
Expand Down

0 comments on commit be4204c

Please sign in to comment.