Skip to content

Commit 866d115

Browse files
committed
Add support for Lean 4.24's try this widget.
Even though it's about to change again in leanprover/lean4#10524...
1 parent dd3b681 commit 866d115

File tree

3 files changed

+65
-34
lines changed

3 files changed

+65
-34
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
---@brief [[
2+
--- The `Try This` widget on versions of Lean v4.24+.
3+
---@brief ]]
4+
5+
local Element = require('lean.tui').Element
6+
7+
---@alias SuggestionText string
8+
9+
---@class TryThis.Suggestion
10+
---@field suggestion SuggestionText Text to be used as a replacement via a code action.
11+
---@field preInfo? string Optional info to be printed immediately before replacement text in a widget.
12+
---@field postInfo? string Optional info to be printed immediately after replacement text in a widget.
13+
14+
---A code action suggestion associated with a hint in a message.
15+
---
16+
---Refer to `TryThis.Suggestion`. This extends that structure with several fields specific to inline
17+
---hints.
18+
---@class Suggestion: TryThis.Suggestion
19+
20+
---@class TryThisParams
21+
---@field diff { type: 'insertion' | 'deletion' | 'unchanged', text: string }[]
22+
---@field range lsp.Range
23+
---@field suggestion string
24+
25+
---@param ctx RenderContext
26+
---@param props TryThisParams
27+
return function(ctx, props)
28+
return Element:new {
29+
text = props.suggestion,
30+
highlightable = true,
31+
hlgroup = 'widgetLink',
32+
events = {
33+
click = function()
34+
ctx:apply_edits {
35+
{ range = props.range, newText = props.suggestion },
36+
}
37+
end,
38+
},
39+
}
40+
end

lua/lean/widgets/Lean/Meta/Tactic/TryThis/tryThisWidget.lua

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
1-
local Element = require('lean.tui').Element
2-
3-
---@alias SuggestionText string
1+
---@brief [[
2+
--- The `Try This` widget on old versions of Lean pre-v4.23.
3+
---@brief ]]
44

5-
---@class Suggestion
6-
---@field suggestion SuggestionText Text to be used as a replacement via a code action.
7-
---@field preInfo? string Optional info to be printed immediately before replacement text in a widget.
8-
---@field postInfo? string Optional info to be printed immediately after replacement text in a widget.
5+
local Element = require('lean.tui').Element
96

10-
---@class TryThisParams
11-
---@field suggestions Suggestion[]
7+
---@class LegacyTryThisParams
8+
---@field suggestions TryThis.Suggestion[]
129
---@field range lsp.Range
1310
---@field header string
1411
---@field isInline boolean
1512
---@field style any
1613

1714
---@param ctx RenderContext
18-
---@param props TryThisParams
15+
---@param props LegacyTryThisParams
1916
return function(ctx, props)
2017
local blocks = vim.iter(ipairs(props.suggestions)):map(function(i, each)
2118
local children = {

spec/widgets/core_spec.lua

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@
22
--- Tests for widgets from Lean core.
33
---@brief ]]
44

5+
local Window = require 'std.nvim.window'
6+
57
local helpers = require 'spec.helpers'
68
local infoview = require 'lean.infoview'
79

810
require('lean').setup {}
911

1012
describe('Lean core widgets', function()
13+
local lean_window = Window:current()
14+
1115
it(
1216
'supports error description widgets',
1317
helpers.clean_buffer('def f := g', function()
@@ -35,18 +39,17 @@ describe('Lean core widgets', function()
3539
3640
⊢ 2 = 2
3741
38-
▼ suggestion:
39-
exact rfl
40-
4142
▼ 2:3-2:9: information:
42-
Try this: exact rfl
43+
Try this:
44+
exact rfl
4345
]]
4446

4547
infoview.go_to()
46-
helpers.move_cursor { to = { 6, 1 } }
48+
helpers.search 'exact '
4749
helpers.feed '<CR>'
4850

4951
-- the buffer contents have changed but we also jumped to the lean win
52+
assert.current_window.is(lean_window)
5053
assert.contents.are [[
5154
example : 2 = 2 := by
5255
exact rfl
@@ -66,17 +69,16 @@ describe('Lean core widgets', function()
6669
assert.infoview_contents.are [[
6770
Goals accomplished 🎉
6871
69-
▼ suggestion:
70-
exact rfl
71-
7272
▼ 1:62-1:68: information:
73-
Try this: exact rfl
73+
Try this:
74+
exact rfl
7475
]]
7576

7677
infoview.go_to()
77-
helpers.move_cursor { to = { 4, 1 } }
78+
helpers.search 'exact '
7879
helpers.feed '<CR>'
7980

81+
assert.current_window.is(lean_window)
8082
assert.contents.are [[
8183
example {𝔽 : Type} (x : 𝔽) (_ : 𝔽) (_ : 𝔽) : x = x := by exact rfl
8284
]]
@@ -105,14 +107,10 @@ describe('Lean core widgets', function()
105107
assert.infoview_contents.are [[
106108
⊢ True
107109
108-
▼ suggestion:
109-
trivial
110-
sorry
111-
112110
▼ 11:3-11:6: information:
113111
Try these:
114-
• trivial
115-
• sorry
112+
• trivial
113+
• sorry
116114
]]
117115
end
118116
)
@@ -140,17 +138,13 @@ describe('Lean core widgets', function()
140138
assert.infoview_contents.are [[
141139
⊢ 37 = 37
142140
143-
▼ suggestion:
144-
trivial
145-
146-
▼ suggestion:
147-
rfl
148-
149141
▼ 10:25-10:39: information:
150-
Try this: trivial
142+
Try this:
143+
trivial
151144
152145
▼ 10:25-10:39: information:
153-
Try this: rfl
146+
Try this:
147+
rfl
154148
155149
▼ 10:22-10:39: error:
156150
unsolved goals

0 commit comments

Comments
 (0)