@@ -105,14 +105,14 @@ Refer to `TryThis.Suggestion`; this extends that structure with a `span?` field,
105105hint to suggest modifications at different locations. If `span?` is not specified, then the syntax
106106reference provided to `MessageData.hint` will be used.
107107-/
108- structure Suggestion extends TryThis.Suggestion where
108+ structure Suggestion extends toTryThisSuggestion : TryThis.Suggestion where
109109 span? : Option Syntax := none
110110
111111instance : Coe TryThis.SuggestionText Suggestion where
112112 coe t := { suggestion := t }
113113
114114instance : ToMessageData Suggestion where
115- toMessageData s := toMessageData s.toSuggestion
115+ toMessageData s := toMessageData s.toTryThisSuggestion
116116
117117/--
118118Produces a diff that splits either on characters, tokens, or not at all, depending on the edit
@@ -124,12 +124,12 @@ Guarantees that all actions in the output will be maximally grouped; that is, in
124124def readableDiff (s s' : String) : Array (Diff.Action × String) :=
125125 let minLength := min s.length s'.length
126126 -- Given that `Diff.diff` returns a minimal diff, any length-≤3 diff can only have edits at the
127- -- front and back, or at a single interior point. This will always be fairly readable, so we can
128- -- return it without further analysis:
127+ -- front and back, or at a single interior point. This will always be fairly readable (and
128+ -- splitting by a larger unit would likely only be worse), so return it without further analysis:
129129 if charDiff.size ≤ 3 then
130130 charDiff
131131 else
132- -- The coefficients on these two constants can be tuned:
132+ -- The coefficients on these two values can be tuned:
133133 let charDiffDistance := minLength / 5
134134 let wordDiffDistance := minLength
135135 if let some editDistance := EditDistance.levenshtein s s' wordDiffDistance then
@@ -140,12 +140,6 @@ def readableDiff (s s' : String) : Array (Diff.Action × String) :=
140140 else
141141 maxDiff
142142where
143- splitChars (s : String) :=
144- s.toList.toArray
145-
146- splitWords (s : String) :=
147- s.splitOn " " |>.intersperse " " |>.toArray
148-
149143 charDiff :=
150144 Diff.diff (splitChars s) (splitChars s')
151145 |> joinEdits
@@ -159,6 +153,12 @@ where
159153 maxDiff :=
160154 #[(.delete, s), (.insert, s')]
161155
156+ splitChars (s : String) :=
157+ s.toList.toArray
158+
159+ splitWords (s : String) :=
160+ s.splitOn " " |>.intersperse " " |>.toArray
161+
162162 joinEdits {α} (ds : Array (Diff.Action × α)) : Array (Diff.Action × Array α) :=
163163 ds.foldl (init := #[]) fun acc (act, c) =>
164164 if h : acc.isEmpty then
@@ -183,8 +183,10 @@ def mkSuggestionsMessage (suggestions : Array Suggestion)
183183 for suggestion in suggestions do
184184 if let some range := (suggestion.span?.getD ref).getRange? then
185185 let { info, suggestions := suggestionArr, range := lspRange } ←
186- processSuggestions ref range #[suggestion.toSuggestion ] codeActionPrefix?
186+ processSuggestions ref range #[suggestion.toTryThisSuggestion ] codeActionPrefix?
187187 pushInfoLeaf info
188+ -- The following access is safe because
189+ -- `suggestionsArr = #[suggestion.toTryThisSuggestion].map ...` (see `processSuggestions`)
188190 let suggestionText := suggestionArr[0 ]!.2 .1
189191 let map ← getFileMap
190192 let rangeContents := Substring.mk map.source range.start range.stop |>.toString
@@ -204,9 +206,9 @@ def mkSuggestionsMessage (suggestions : Array Suggestion)
204206 } (suggestion.messageData?.getD (mkDiffString edits))
205207 let widgetMsg := m!"{preInfo}{widget}{postInfo}"
206208 let suggestionMsg := if suggestions.size == 1 then
207- MessageData.ofFormat " \n " ++ m!"{widgetMsg}"
209+ m!"\n {widgetMsg}"
208210 else
209- MessageData.ofFormat "\n " ++ MessageData.nest 2 m!"• {widgetMsg}"
211+ m! "\n " ++ MessageData.nest 2 m!"• {widgetMsg}"
210212 msg := msg ++ MessageData.nestD suggestionMsg
211213 return msg
212214
0 commit comments