File tree 4 files changed +37
-7
lines changed
Sources/SourceKitLSP/Swift 4 files changed +37
-7
lines changed Original file line number Diff line number Diff line change @@ -29,17 +29,17 @@ struct MacroExpansion: RefactoringResponse {
29
29
/// The resulting array of `RefactoringEdit` of a semantic refactoring request
30
30
var edits : [ RefactoringEdit ]
31
31
32
- init ( title: String , uri : DocumentURI , refactoringEdits: [ RefactoringEdit ] ) {
32
+ init ( title: String , snapshot : DocumentSnapshot , refactoringEdits: [ RefactoringEdit ] ) {
33
33
self . title = title
34
- self . uri = uri
34
+ self . uri = snapshot . uri
35
35
self . edits = refactoringEdits. compactMap { refactoringEdit in
36
36
if refactoringEdit. bufferName == nil && !refactoringEdit. newText. isEmpty {
37
37
logger. fault ( " Unable to retrieve some parts of the expansion " )
38
38
return nil
39
39
}
40
40
41
41
return refactoringEdit
42
- }
42
+ } . concurrent ( to : snapshot )
43
43
}
44
44
}
45
45
Original file line number Diff line number Diff line change @@ -15,7 +15,7 @@ import LanguageServerProtocol
15
15
import SourceKitD
16
16
17
17
protocol RefactoringResponse {
18
- init ( title: String , uri : DocumentURI , refactoringEdits: [ RefactoringEdit ] )
18
+ init ( title: String , snapshot : DocumentSnapshot , refactoringEdits: [ RefactoringEdit ] )
19
19
}
20
20
21
21
extension RefactoringResponse {
@@ -81,7 +81,7 @@ extension RefactoringResponse {
81
81
return nil
82
82
}
83
83
84
- self . init ( title: title, uri : snapshot. uri , refactoringEdits: refactoringEdits)
84
+ self . init ( title: title, snapshot : snapshot, refactoringEdits: refactoringEdits)
85
85
}
86
86
}
87
87
Original file line number Diff line number Diff line change 12
12
13
13
import LanguageServerProtocol
14
14
import SourceKitD
15
+ import SwiftParser
16
+ import SwiftSyntax
15
17
16
18
/// Represents an edit from semantic refactor response. Notionally, a subclass of `TextEdit`
17
19
@_spi ( Testing) public struct RefactoringEdit : Hashable , Sendable , Codable {
@@ -72,3 +74,31 @@ extension RefactoringEdit: LSPAnyCodable {
72
74
] )
73
75
}
74
76
}
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 {
31
31
self . edit = edit
32
32
}
33
33
34
- init ( title: String , uri : DocumentURI , refactoringEdits: [ RefactoringEdit ] ) {
34
+ init ( title: String , snapshot : DocumentSnapshot , refactoringEdits: [ RefactoringEdit ] ) {
35
35
self . title = title
36
36
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) }
38
38
] )
39
39
}
40
40
}
You can’t perform that action at this time.
0 commit comments