Skip to content

Commit

Permalink
Inside multiline delimiters redux.
Browse files Browse the repository at this point in the history
  • Loading branch information
Julian committed Dec 16, 2024
1 parent 4b443c9 commit 2b9f351
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 27 deletions.
20 changes: 12 additions & 8 deletions lua/lean/indent.lua
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,12 @@ end

---Is the given line within a Lean enclosed bracket?
---@param linenr number
---@param position integer
---@return boolean
local function is_enclosed(linenr)
local syntax = vim.inspect_pos(0, linenr, 0).syntax[1]
return syntax and syntax.hl_group == 'leanEncl'
local function is_enclosed(linenr, position)
local syntax = vim.inspect_pos(0, linenr, position).syntax[1]
local hlgroup = syntax and syntax.hl_group
return hlgroup == 'leanEncl' or hlgroup == 'leanAnonymousLiteral'
end

---Is the given line within a Lean comment (normal or block)?
Expand Down Expand Up @@ -110,16 +112,18 @@ function M.indentexpr(linenr)

local _, last_indent = last:find '^%s+'

if is_enclosed(linenr - 1, 0) then
if is_enclosed(linenr - 2, 0) then
return last_indent
end
return (last_indent or 0) + shiftwidth
end

if last_indent then
if focuses_at(last, last_indent + 1) then
return last_indent + #'·'
end

if is_enclosed(linenr - 1) then
local _, bracket = last:find '%['
return bracket or last_indent + shiftwidth
end

if not is_declaration_args(linenr - 2) then
local dedent_one = last_indent - shiftwidth

Expand Down
14 changes: 14 additions & 0 deletions spec/fixtures/indent/multiline_delimited.in.lean
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
def f := "foo bar
baz
quux"

def g := [1, 2,
3,
4,
5]

def h := [1, 2,
3]

def i := ["foo", "bar",
"baz"]
14 changes: 14 additions & 0 deletions spec/fixtures/indent/multiline_delimited.lean
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
def f := "foo bar
baz
quux"

def g := [1, 2,
3,
4,
5]

def h := [1, 2,
3]

def i := ["foo", "bar",
"baz"]
18 changes: 0 additions & 18 deletions spec/indent_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -135,24 +135,6 @@ describe('indent', function()
)
)

it(
'aligns with brackets',
helpers.clean_buffer(
[[
example : 37 = 37 ∧ 73 = 73 := by
simp [Nat.lt_or_gt,
]],
function()
helpers.feed 'GoNat.lt_or_gt]'
assert.contents.are [[
example : 37 = 37 ∧ 73 = 73 := by
simp [Nat.lt_or_gt,
Nat.lt_or_gt]
]]
end
)
)

for each in fixtures.indent() do
it(each.description, function()
vim.cmd.edit { each.unindented, bang = true }
Expand Down
2 changes: 1 addition & 1 deletion syntax/lean.vim
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ syn region leanEncl matchgroup=leanDelim start="#\[" end="\]" contains=TOP conta
syn region leanEncl matchgroup=leanDelim start="(" end=")" contains=TOP containedin=ALLBUT,leanFrenchQuote keepend
syn region leanEncl matchgroup=leanDelim start="\[" end="\]" contains=TOP containedin=ALLBUT,leanFrenchQuote keepend
syn region leanEncl matchgroup=leanDelim start="" end="" contains=TOP containedin=ALLBUT,leanFrenchQuote keepend
syn region leanEncl matchgroup=leanDelim start="" end="" contains=TOP containedin=ALLBUT,leanFrenchQuote keepend

syn region leanAnonymousLiteral matchgroup=leanDelim start="" end="" contains=TOP containedin=ALLBUT,leanFrenchQuote keepend
syn region leanStructureLiteral matchgroup=leanDelim start="{" end="}" contains=TOP containedin=ALLBUT,leanFrenchQuote keepend

" FIXME(gabriel): distinguish backquotes in notations from names
Expand Down

0 comments on commit 2b9f351

Please sign in to comment.