Wednesday, March 10, 2021

Help with indenting code

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.

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/s2arau%24t75%241%40ciao.gmane.io.

No comments: