Skip to content

Commit c045dcd

Browse files
committed
chore: reorganization and naming improvement
1 parent 8c7d6c9 commit c045dcd

File tree

2 files changed

+23
-21
lines changed

2 files changed

+23
-21
lines changed

src/Lean/Meta/Hint.lean

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -105,14 +105,14 @@ Refer to `TryThis.Suggestion`; this extends that structure with a `span?` field,
105105
hint to suggest modifications at different locations. If `span?` is not specified, then the syntax
106106
reference 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

111111
instance : Coe TryThis.SuggestionText Suggestion where
112112
coe t := { suggestion := t }
113113

114114
instance : ToMessageData Suggestion where
115-
toMessageData s := toMessageData s.toSuggestion
115+
toMessageData s := toMessageData s.toTryThisSuggestion
116116

117117
/--
118118
Produces 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
124124
def 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
142142
where
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

tests/lean/run/hintSuggestionMessage.lean

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ Hint: Hint message
9393
̲B̲
9494
̲C̲
9595
-/
96-
#guard_msgs in
96+
#guard_msgs (whitespace := exact) in
9797
run_meta do
9898
let hint ← MessageData.hint m!"Hint message" #["A\nB\nC"]
9999
logInfo hint
@@ -109,7 +109,7 @@ Hint: Hint message
109109
̲ ̲ ̲E̲
110110
̲ ̲ ̲F̲
111111
-/
112-
#guard_msgs in
112+
#guard_msgs (whitespace := exact) in
113113
run_meta do
114114
let hint ← MessageData.hint m!"Hint message" #["A\nB\nC", "D\n E\n F"]
115115
logInfo hint
@@ -181,24 +181,24 @@ Hint: Use one of these instead
181181
• l̵o̵n̵g̵W̵o̵r̵d̵W̵i̵t̵h̵o̵u̵t̵A̵n̵y̵S̵paces
182182
• l̵o̵n̵g̵W̵o̵r̵d̵W̵i̵t̵h̵o̵u̵t̵A̵n̵y̵S̵p̵a̵c̵e̵s̵o̲r̲d̲S̲p̲a̲c̲e̲s̲
183183
-/
184-
#guard_msgs in
184+
#guard_msgs (whitespace := exact) in
185185
#check longWordWithoutAnySpaces
186186

187187
elab "first" "second" "third" "fourth" "fifth" : term => do
188188
let hint ← MessageData.hint m!"Use one of these instead"
189-
#["first second third", "second fourth fifth", "fisethfofi", "second"]
189+
#["first second thi", "second fourth fifth", "fisethfofi", "second"]
190190
throwError m!"Invalid" ++ hint
191191

192192
/--
193193
error: Invalid
194194
195195
Hint: Use one of these instead
196-
• first second third ̵f̵o̵u̵r̵t̵h̵ ̵f̵i̵f̵t̵h̵
196+
• first second thir̵d̵ ̵f̵o̵u̵r̵t̵h̵ ̵f̵i̵f̵t̵h̵
197197
• f̵i̵r̵s̵t̵ ̵second t̵h̵i̵r̵d̵ ̵fourth fifth
198198
• f̵i̵r̵s̵t̵ ̵s̵e̵c̵o̵n̵d̵ ̵t̵h̵i̵r̵d̵ ̵f̵o̵u̵r̵t̵h̵ ̵f̵i̵f̵t̵h̵f̲i̲s̲e̲t̲h̲f̲o̲f̲i̲
199199
• f̵i̵r̵s̵t̵ ̵s̵e̵c̵o̵n̵d̵ ̵t̵h̵i̵r̵d̵ ̵f̵o̵u̵r̵t̵h̵ ̵f̵i̵f̵t̵h̵s̲e̲c̲o̲n̲d̲
200200
-/
201-
#guard_msgs in
201+
#guard_msgs (whitespace := exact) in
202202
#check first second third fourth fifth
203203

204204
elab "suggest_replacement%" t:term : term => do
@@ -218,7 +218,7 @@ Hint: Try one of these
218218
• l̵e̵t̵ ̵x ̵:̵=̵ ̵4̵2̵
219219
̵ ̵ ̵2̵ ̵*̵ ̵x̵
220220
-/
221-
#guard_msgs in
221+
#guard_msgs (whitespace := exact) in
222222
#check suggest_replacement%
223223
let x := 42
224224
2 * x

0 commit comments

Comments
 (0)