File tree Expand file tree Collapse file tree 4 files changed +37
-7
lines changed
Sources/SourceKitLSP/Swift Expand file tree Collapse file tree 4 files changed +37
-7
lines changed Original file line number Diff line number Diff line change @@ -29,17 +29,17 @@ struct MacroExpansion: RefactoringResponse {
2929 /// The resulting array of `RefactoringEdit` of a semantic refactoring request
3030 var edits : [ RefactoringEdit ]
3131
32- init ( title: String , uri : DocumentURI , refactoringEdits: [ RefactoringEdit ] ) {
32+ init ( title: String , snapshot : DocumentSnapshot , refactoringEdits: [ RefactoringEdit ] ) {
3333 self . title = title
34- self . uri = uri
34+ self . uri = snapshot . uri
3535 self . edits = refactoringEdits. compactMap { refactoringEdit in
3636 if refactoringEdit. bufferName == nil && !refactoringEdit. newText. isEmpty {
3737 logger. fault ( " Unable to retrieve some parts of the expansion " )
3838 return nil
3939 }
4040
4141 return refactoringEdit
42- }
42+ } . concurrent ( to : snapshot )
4343 }
4444}
4545
Original file line number Diff line number Diff line change @@ -15,7 +15,7 @@ import LanguageServerProtocol
1515import SourceKitD
1616
1717protocol RefactoringResponse {
18- init ( title: String , uri : DocumentURI , refactoringEdits: [ RefactoringEdit ] )
18+ init ( title: String , snapshot : DocumentSnapshot , refactoringEdits: [ RefactoringEdit ] )
1919}
2020
2121extension RefactoringResponse {
@@ -81,7 +81,7 @@ extension RefactoringResponse {
8181 return nil
8282 }
8383
84- self . init ( title: title, uri : snapshot. uri , refactoringEdits: refactoringEdits)
84+ self . init ( title: title, snapshot : snapshot, refactoringEdits: refactoringEdits)
8585 }
8686}
8787
Original file line number Diff line number Diff line change 1212
1313import LanguageServerProtocol
1414import SourceKitD
15+ import SwiftParser
16+ import SwiftSyntax
1517
1618/// Represents an edit from semantic refactor response. Notionally, a subclass of `TextEdit`
1719@_spi ( Testing) public struct RefactoringEdit : Hashable , Sendable , Codable {
@@ -72,3 +74,31 @@ extension RefactoringEdit: LSPAnyCodable {
7274 ] )
7375 }
7476}
77+
78+ @_spi ( Testing) public extension Array < RefactoringEdit > {
79+ func concurrent( to snapshot: DocumentSnapshot ) -> [ RefactoringEdit ] {
80+ let sequentialEdits = self . map { refactoringEdit in
81+
82+ let absoluteStartPosition = snapshot. absolutePosition ( of: refactoringEdit. range. lowerBound)
83+
84+ let absoluteEndPosition = snapshot. absolutePosition ( of: refactoringEdit. range. upperBound)
85+
86+ return SourceEdit ( range: absoluteStartPosition..< absoluteEndPosition, replacement: refactoringEdit. newText)
87+ }
88+
89+ let concurrentEdits = ConcurrentEdits ( fromSequential: sequentialEdits)
90+
91+ var refactoringEdits = [ RefactoringEdit] ( )
92+ for (i, concurrentEdit) in concurrentEdits. edits. enumerated ( ) {
93+ refactoringEdits. append (
94+ RefactoringEdit (
95+ range: snapshot. absolutePositionRange ( of: concurrentEdit. range) ,
96+ newText: concurrentEdit. replacement,
97+ bufferName: self [ i] . bufferName // Is there a better way to access bufferName after receiving concurrentEdits?
98+ )
99+ )
100+ }
101+
102+ return refactoringEdits
103+ }
104+ }
Original file line number Diff line number Diff line change @@ -31,10 +31,10 @@ struct SemanticRefactoring: RefactoringResponse {
3131 self . edit = edit
3232 }
3333
34- init ( title: String , uri : DocumentURI , refactoringEdits: [ RefactoringEdit ] ) {
34+ init ( title: String , snapshot : DocumentSnapshot , refactoringEdits: [ RefactoringEdit ] ) {
3535 self . title = title
3636 self . edit = WorkspaceEdit ( changes: [
37- uri: refactoringEdits. map { TextEdit ( range: $0. range, newText: $0. newText) }
37+ snapshot . uri: refactoringEdits. map { TextEdit ( range: $0. range, newText: $0. newText) }
3838 ] )
3939 }
4040}
You can’t perform that action at this time.
0 commit comments