Help with indenting code

9 views
Skip to first unread message

Lifepillar

unread,
Mar 10, 2021, 11:15:38 AM3/10/21
to vim...@googlegroups.com
I would like to indent code structured as follows:

if X then
(
if Y then
(
...
)
)
else
...

To make Python look like a language that is easy to indent, this
language has `if... then... [else]` blocks without `endif`.

I get can get it almost right, except that the last `else` above gets
aligned with `if Y then`. The relevant snippet from my indent script
currently looks as follows:

fun! s:find_pair(pstart, pmid, pend)
call cursor(v:lnum, 1)
call search(a:pend, 'Wc')
return indent(searchpair(a:pstart, a:pmid, a:pend, 'bWn'))
endf

" And in my indentation function:

if getline(v:lnum) =~# '\<else\>'
return s:find_pair('\<then\>', '', '\<else\>')
endif

if getline(v:lnum) =~# '^\s*)'
return s:find_pair('(', '', ')')
endif

Any idea how I could take into account the blocks in parentheses when
matching `else` with its corresponding `if... then`?

Thanks,
Life.

Reply all
Reply to author
Forward
0 new messages