File tree 4 files changed +37
-14
lines changed
Sources/SourceKitLSP/Swift 4 files changed +37
-14
lines changed Original file line number Diff line number Diff line change @@ -29,17 +29,10 @@ 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
35
- self . edits = refactoringEdits. compactMap { refactoringEdit in
36
- if refactoringEdit. bufferName. isEmpty {
37
- logger. fault ( " Unable to retrieve some parts of the expansion " )
38
- return nil
39
- }
40
-
41
- return refactoringEdit
42
- }
34
+ self . uri = snapshot. uri
35
+ self . edits = refactoringEdits. concurrent ( to: snapshot)
43
36
}
44
37
}
45
38
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 {
@@ -59,3 +61,31 @@ extension RefactoringEdit: LSPAnyCodable {
59
61
] )
60
62
}
61
63
}
64
+
65
+ @_spi ( Testing) public extension Array < RefactoringEdit > {
66
+ func concurrent( to snapshot: DocumentSnapshot ) -> [ RefactoringEdit ] {
67
+ let sequentialEdits = self . map { refactoringEdit in
68
+
69
+ let absoluteStartPosition = snapshot. absolutePosition ( of: refactoringEdit. range. lowerBound)
70
+
71
+ let absoluteEndPosition = snapshot. absolutePosition ( of: refactoringEdit. range. upperBound)
72
+
73
+ return SourceEdit ( range: absoluteStartPosition..< absoluteEndPosition, replacement: refactoringEdit. newText)
74
+ }
75
+
76
+ let concurrentEdits = ConcurrentEdits ( fromSequential: sequentialEdits)
77
+
78
+ var refactoringEdits = [ RefactoringEdit] ( )
79
+ for (i, concurrentEdit) in concurrentEdits. edits. enumerated ( ) {
80
+ refactoringEdits. append (
81
+ RefactoringEdit (
82
+ range: snapshot. absolutePositionRange ( of: concurrentEdit. range) ,
83
+ newText: concurrentEdit. replacement,
84
+ bufferName: self [ i] . bufferName // Is there a better way to access bufferName after receiving concurrentEdits?
85
+ )
86
+ )
87
+ }
88
+
89
+ return refactoringEdits
90
+ }
91
+ }
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