@@ -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 . title ( for: edits, in: 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,43 @@ 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)
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 + " ... "
7899 }
79100
80101 /// Describe a fixit's edit briefly.
0 commit comments