12
12
13
13
import LanguageServerProtocol
14
14
import SourceKitD
15
+ import LSPLogging
15
16
16
17
protocol RefactoringResponse {
17
-
18
18
init ( title: String , uri: DocumentURI , refactoringEdits: [ RefactoringEdit ] )
19
-
20
- /// Create an instance of `RefactoringResponse` from a sourcekitd semantic
21
- /// refactoring response dictionary, if possible. Passes the response to
22
- /// `init(title: String, uri: DocumentURI, refactoringEdits:
23
- /// [RefactoringEdit])` in a neat manner
24
- init ? ( _ title: String , _ dict: SKDResponseDictionary , _ snapshot: DocumentSnapshot , _ keys: sourcekitd_api_keys )
25
19
}
26
20
27
- typealias RefactoringEdit = ( startPosition: Position , endPosition: Position , newText: String , bufferName: String ? )
28
-
29
21
extension RefactoringResponse {
30
-
31
- /// Create an instance of `RefactoringResponse` from a sourcekitd semantic
32
- /// refactoring response dictionary, if possible. Passes the response to
33
- /// `init(title: String, uri: DocumentURI, refactoringEdits:
34
- /// [RefactoringEdit])` in a neat manner
22
+ /// Create an instance of `RefactoringResponse` from a sourcekitd semantic
23
+ /// refactoring response dictionary, if possible.
35
24
///
36
25
/// - Parameters:
37
26
/// - title: The title of the refactoring action.
38
27
/// - dict: Response dictionary to extract information from.
39
- /// - snapshot: The snapshot that triggered the `semantic_refactoring`
40
- /// request.
28
+ /// - snapshot: The snapshot that triggered the `semantic_refactoring` request.
41
29
/// - keys: The sourcekitd key set to use for looking up into `dict`.
42
30
init ? ( _ title: String , _ dict: SKDResponseDictionary , _ snapshot: DocumentSnapshot , _ keys: sourcekitd_api_keys ) {
43
31
guard let categorizedEdits: SKDResponseArray = dict [ keys. categorizedEdits] else {
32
+ logger. error ( " categorizedEdits doesn't exist in response dictionary " )
44
33
return nil
45
34
}
46
35
47
36
var refactoringEdits = [ RefactoringEdit] ( )
48
37
49
- categorizedEdits. forEach { _, value in
50
- guard let edits: SKDResponseArray = value [ keys. edits] else {
51
- return false
38
+ categorizedEdits. forEach { _, categorizedEdit in
39
+ guard let edits: SKDResponseArray = categorizedEdit [ keys. edits] else {
40
+ logger. error ( " edits doesn't exist in categorizedEdit dictionary " )
41
+ return true
52
42
}
53
- edits. forEach { _, value in
43
+ edits. forEach { _, edit in
54
44
// The LSP is zero based, but semantic_refactoring is one based.
55
- guard let startLine: Int = value [ keys. line] ,
56
- let startColumn: Int = value [ keys. column] ,
57
- let endLine: Int = value [ keys. endLine] ,
58
- let endColumn: Int = value [ keys. endColumn] ,
59
- let text: String = value [ keys. text]
45
+ guard let startLine: Int = edit [ keys. line] ,
46
+ let startColumn: Int = edit [ keys. column] ,
47
+ let endLine: Int = edit [ keys. endLine] ,
48
+ let endColumn: Int = edit [ keys. endColumn] ,
49
+ let text: String = edit [ keys. text]
60
50
else {
51
+ logger. error ( " Failed to deserialise edit dictionary containing values: \( edit) " )
61
52
return true // continue
62
53
}
63
54
@@ -74,17 +65,15 @@ extension RefactoringResponse {
74
65
// can't be represented in the editor properly.
75
66
let textWithSnippets = rewriteSourceKitPlaceholders ( in: text, clientSupportsSnippets: false )
76
67
refactoringEdits. append (
77
- (
78
- startPosition: startPosition, endPosition: endPosition, newText: textWithSnippets,
79
- bufferName: value [ keys. bufferName]
80
- )
68
+ RefactoringEdit ( range: startPosition..< endPosition, newText: textWithSnippets, bufferName: edit [ keys. bufferName] ?? " " )
81
69
)
82
70
return true
83
71
}
84
72
return true
85
73
}
86
74
87
- guard refactoringEdits. isEmpty == false else {
75
+ guard !refactoringEdits. isEmpty else {
76
+ logger. error ( " No refactors found " )
88
77
return nil
89
78
}
90
79
@@ -100,13 +89,12 @@ extension SwiftLanguageService {
100
89
/// request, such as the necessary edits and placeholder locations.
101
90
///
102
91
/// - Parameters:
103
- /// - command: The semantic `RefactorCommand` that triggered this request.
104
- /// - responseType: The response type `T.Type` of the particular command
92
+ /// - refactorCommand: The semantic `RefactorCommand` that triggered this request.
105
93
/// - Returns:
106
94
/// - The `RefactoringResponse` as `T`
107
- func refactoring< T: RefactorCommand > ( _ refactorCommand : T ) async throws
108
- -> T . Response
109
- {
95
+ func refactoring< T: RefactorCommand > (
96
+ _ refactorCommand : T
97
+ ) async throws -> T . Response {
110
98
let keys = self . keys
111
99
112
100
let uri = refactorCommand. textDocument. uri
0 commit comments