@@ -44,22 +44,10 @@ extension CodeAction {
4444 if let fromNote = fromNote {
4545 title = fromNote
4646 } 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 . generateTitle ( with: edits, snapshot: snapshot) else {
5448 return nil
5549 }
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
6351 }
6452
6553 self . init (
@@ -71,10 +59,46 @@ extension CodeAction {
7159 }
7260
7361 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) else {
66+ continue
67+ }
68+ textEdits. append ( TextEdit ( range: startPosition ..< endPosition, newText: edit. replacement) )
69+ }
70+
71+ guard let generatedTitle = Self . generateTitle ( with: textEdits, snapshot: snapshot) else {
72+ return nil
73+ }
74+
75+ self . init (
76+ title: generatedTitle,
77+ kind: . quickFix,
78+ diagnostics: nil ,
79+ edit: WorkspaceEdit ( changes: [ snapshot. uri: textEdits] )
80+ )
81+ }
82+
83+ private static func generateTitle( with edits: [ TextEdit ] , snapshot: DocumentSnapshot ) -> String ? {
84+ if edits. isEmpty {
85+ return nil
86+ }
87+ guard let startIndex = snapshot. index ( of: edits [ 0 ] . range. lowerBound) ,
88+ let endIndex = snapshot. index ( of: edits [ 0 ] . range. upperBound) ,
89+ startIndex <= endIndex,
90+ snapshot. text. indices. contains ( startIndex) ,
91+ endIndex <= snapshot. text. endIndex
92+ else {
93+ logger. fault ( " position mapped, but indices failed for edit range \( String ( reflecting: edits [ 0 ] ) ) " )
94+ return nil
95+ }
96+ let oldText = String ( snapshot. text [ startIndex..< endIndex] )
97+ let description = Self . fixitTitle ( replace: oldText, with: edits [ 0 ] . newText)
98+ if edits. count == 1 {
99+ return description
100+ }
101+ return description + " ... "
78102 }
79103
80104 /// Describe a fixit's edit briefly.
0 commit comments