@@ -28,21 +28,6 @@ open PrettyPrinter
2828
2929/-! # Code action information -/
3030
31- /-- A packet of information about a "Try this" suggestion
32- that we store in the infotree for the associated code action to retrieve. -/
33- structure TryThisInfo : Type where
34- /-- The textual range to be replaced by one of the suggestions. -/
35- range : Lsp.Range
36- /--
37- A list of suggestions for the user to choose from.
38- Each suggestion may optionally come with an override for the code action title.
39- -/
40- suggestionTexts : Array (String × Option String)
41- /-- The prefix to display before the code action for a "Try this" suggestion if no custom code
42- action title is provided. If not provided, `"Try this: "` is used. -/
43- codeActionPrefix? : Option String
44- deriving TypeName
45-
4631/-! # `Suggestion` data -/
4732
4833-- TODO: we could also support `Syntax` and `Format`
@@ -180,6 +165,23 @@ instance : ToMessageData Suggestion where
180165instance : Coe SuggestionText Suggestion where
181166 coe t := { suggestion := t }
182167
168+ /--
169+ A packet of information about a "Try this" suggestion
170+ that we store in the infotree for the associated code action to retrieve.
171+ -/
172+ structure TryThisInfo : Type where
173+ /-- Suggestion edit as it will be applied to the editor document. -/
174+ edit : Lsp.TextEdit
175+ /-- Title of the code action that is displayed in the code action selection dialog. -/
176+ codeActionTitle : String
177+ /--
178+ Original suggestion that this `TryThisInfo` was derived from.
179+ Stored so that meta-code downstream can extract the suggestions
180+ produced by a tactic from the `InfoTree`.
181+ -/
182+ suggestion : Suggestion
183+ deriving TypeName
184+
183185/-! # Formatting -/
184186
185187/-- Yields `(indent, column)` given a `FileMap` and a `String.Range`, where `indent` is the number
@@ -226,50 +228,16 @@ def prettyExtra (s : SuggestionText) (w : Option Nat := none)
226228
227229end SuggestionText
228230
229- /-- Converts a `Suggestion` to `Json` in `CoreM`. We need `CoreM` in order to pretty-print syntax.
230-
231- This also returns a `String × Option String` consisting of the pretty-printed text and any custom
232- code action title if `toCodeActionTitle?` is provided.
233-
234- If `w := none`, then `w := getInputWidth (← getOptions)` is used.
235- -/
236- def Suggestion.process (s : Suggestion) (w : Option Nat := none) (indent column : Nat := 0 ) :
237- CoreM (String × Option String) := do
238- let text ← s.suggestion.prettyExtra w indent column
239- return (text, s.toCodeActionTitle?.map (· text))
240-
241- /--
242- Represents processed data for a collection of suggestions that can be passed to a widget and pushed
243- in an info leaf.
244-
245- It contains the following data:
246- * `suggestions`: tuples of the form `(j, t, p)` where `j` is JSON containing a suggestion and its
247- pre- and post-info, `t` is the text to be inserted by the suggestion, and `p` is the code action
248- prefix thereof.
249- * `info`: the `TryThisInfo` data corresponding to a collection of suggestions
250- * `range`: the range at which the suggestion is to be applied.
251- -/
252- structure ProcessedSuggestions where
253- suggestions : Array String
254- info : Elab.Info
255- range : Lsp.Range
231+ def Suggestion.pretty (s : Suggestion) (w : Option Nat := none) (indent column : Nat := 0 ) :
232+ CoreM String := do
233+ s.suggestion.prettyExtra w indent column
256234
257- /--
258- Processes an array of `Suggestion`s into data that can be used to construct a code-action info leaf
259- and "try this" widget.
260- -/
261- def processSuggestions (ref : Syntax) (range : String.Range) (suggestions : Array Suggestion)
262- (codeActionPrefix? : Option String) : CoreM ProcessedSuggestions := do
235+ def Suggestion.processEdit (s : Suggestion) (range : String.Range) : CoreM Lsp.TextEdit := do
263236 let map ← getFileMap
264237 -- FIXME: this produces incorrect results when `by` is at the beginning of the line, i.e.
265238 -- replacing `tac` in `by tac`, because the next line will only be 2 space indented
266239 -- (less than `tac` which starts at column 3)
267240 let (indent, column) := getIndentAndColumn map range
268- let suggestions ← suggestions.mapM (·.process (indent := indent) (column := column))
269- let ref := Syntax.ofRange <| ref.getRange?.getD range
241+ let newText ← s.pretty (indent := indent) (column := column)
270242 let range := map.utf8RangeToLspRange range
271- let info := .ofCustomInfo {
272- stx := ref
273- value := Dynamic.mk { range, suggestionTexts := suggestions, codeActionPrefix? : TryThisInfo }
274- }
275- return { info, suggestions := suggestions.map (·.1 ), range }
243+ return { range, newText }
0 commit comments