@@ -44,22 +44,10 @@ extension CodeAction {
44
44
if let fromNote = fromNote {
45
45
title = fromNote
46
46
} else {
47
- guard let startIndex = snapshot. index ( of: edits [ 0 ] . range. lowerBound) ,
48
- let endIndex = snapshot. index ( of: edits [ 0 ] . range. upperBound) ,
49
- startIndex <= endIndex,
50
- snapshot. text. indices. contains ( startIndex) ,
51
- endIndex <= snapshot. text. endIndex
52
- else {
53
- logger. fault ( " position mapped, but indices failed for edit range \( String ( reflecting: edits [ 0 ] ) ) " )
47
+ guard let generatedTitle = Self . title ( for: edits, in: snapshot) else {
54
48
return nil
55
49
}
56
- let oldText = String ( snapshot. text [ startIndex..< endIndex] )
57
- let description = Self . fixitTitle ( replace: oldText, with: edits [ 0 ] . newText)
58
- if edits. count == 1 {
59
- title = description
60
- } else {
61
- title = description + " ... "
62
- }
50
+ title = generatedTitle
63
51
}
64
52
65
53
self . init (
@@ -71,10 +59,43 @@ extension CodeAction {
71
59
}
72
60
73
61
init ? ( _ fixIt: FixIt , in snapshot: DocumentSnapshot ) {
74
- // FIXME: Once https://github.com/apple/swift-syntax/pull/2226 is merged and
75
- // FixItApplier is public, use it to compute the edits that should be
76
- // applied to the source.
77
- return nil
62
+ var textEdits = [ TextEdit] ( )
63
+ for edit in fixIt. edits {
64
+ guard let startPosition = snapshot. position ( of: edit. range. lowerBound) ,
65
+ let endPosition = snapshot. position ( of: edit. range. upperBound)
66
+ else {
67
+ continue
68
+ }
69
+ textEdits. append ( TextEdit ( range: startPosition..< endPosition, newText: edit. replacement) )
70
+ }
71
+
72
+ self . init (
73
+ title: fixIt. message. message. withFirstLetterUppercased ( ) ,
74
+ kind: . quickFix,
75
+ diagnostics: nil ,
76
+ edit: WorkspaceEdit ( changes: [ snapshot. uri: textEdits] )
77
+ )
78
+ }
79
+
80
+ private static func title( for edits: [ TextEdit ] , in snapshot: DocumentSnapshot ) -> String ? {
81
+ if edits. isEmpty {
82
+ return nil
83
+ }
84
+ guard let startIndex = snapshot. index ( of: edits [ 0 ] . range. lowerBound) ,
85
+ let endIndex = snapshot. index ( of: edits [ 0 ] . range. upperBound) ,
86
+ startIndex <= endIndex,
87
+ snapshot. text. indices. contains ( startIndex) ,
88
+ endIndex <= snapshot. text. endIndex
89
+ else {
90
+ logger. fault ( " position mapped, but indices failed for edit range \( String ( reflecting: edits [ 0 ] ) ) " )
91
+ return nil
92
+ }
93
+ let oldText = String ( snapshot. text [ startIndex..< endIndex] )
94
+ let description = Self . fixitTitle ( replace: oldText, with: edits [ 0 ] . newText)
95
+ if edits. count == 1 {
96
+ return description
97
+ }
98
+ return description + " ... "
78
99
}
79
100
80
101
/// Describe a fixit's edit briefly.
0 commit comments