jez/vim-better-sml

Adjust indent level for nested case expressions

Opened this issue · 0 comments

jez commented

It'd be nice if the indent level was context-sensitive, to the extent that Vim
syntax regex can make it. In particular, we we could solve the following edge
case. Consider the following snippet of SML:

datatype ord = Z | S of ord | Sup of (int -> ord)

fun toString n =
  case n
    of Z => "0"
     | S n' =>
         (case n'
            of Z => "1"
             | S _ => "1 < n < aleph"
             | _ => "malformed")
     | Sup f => ">= aleph"

Try copying this into Vim right now; the last line isn't indented properly.
Right now, the indent file indents lines like the last line under the most
recent case keyword, ignoring any nesting structure.

After thinking about how to do it, I'm confident that it's possible, but I don't
quite have the attention to devote to it right now. For posterity's sake, here's
what I think a potential strategy would be.

The core of the work would be reworking the default syntax/sml.vim file. We'd
add in a bunch of syntax highlighting regions; more than currently exist
there. We'd be matching things like let .. in .. end regions and parenthesized
regions. Then, in the indent expression defined in indent/sml.vim (of this
repo, now, not the default Vim one), we'd use synIDattr(), synstack(), and
related functions to look up the syntax highlighted group under the cursor and
indent appropriately. If we have the entire stack of highlighting regions under
the cursor, we can look up how many groups we just left in going from one line
to the next, and decrease the indent appropriately.

So all in all, very possible, just a bit of work.