Question

Nemerle is a C-like language and mostly works very well with cindent. However, its construct analogous to switch is called match:

match (x)                // switch (x)
{                        // {
| "Hello World" => ...   // case "Hello World": ...
| _ => ...               // default: ...
}                        // }

Is it possible to get the cinoptions for switch statements to apply to this construct, instead? Maybe there is a regular expression I can set somewhere. If not, can I get the vertical bars to align with the braces another way?


Update

Here is what I came up with:

" Vim indent file
" Language:   Nemerle
" Maintainer: Alexey Badalov

" Only load this indent file when no other was loaded.
if exists("b:did_indent")
   finish
endif
let b:did_indent = 1

" Nemerle is C-like, but without switch statements or labels.
setlocal cindent cinoptions=L0

" Enable '|', disable ':'.
setlocal indentkeys=0{,0},0),0#,0\|,!^F,o,O,e

setlocal indentexpr=GetNemerleIndent()

let b:undo_indent = "setl cin< cino< indentkeys< indentexpr<"

function! GetNemerleIndent()
    " Nemerle is C-like; use built-in C indentation as a basis.
    let indent = cindent(v:lnum)

    " Set alignment for lines starting with '|' in line with the opening
    " brace. Use default indentation outside of blocks.
    if getline(v:lnum) =~ '^\s*|'
        call cursor(v:lnum, 1)
        silent! normal [{
        if line('.') == v:lnum
            return indent
        endif
        return indent(line('.'))
    endif

    return indent
endfunction
Was it helpful?

Solution

See :h indent-expression to get a foothold in the Vim documentation. Basically I think you will want to write your own "indent file" for your filetype, which will return an indentexpr with appropriate spaces for your match structure, and otherwise (assuming that's only change) return the usual cindent() value. It involves a little more than just setting a regular expression, the indent file will have Vim commands and structures to evaluate lines and return correct value. As documentation says, best way to learn how they work is to look at some of the indent files for other languages. . . . (C doesn't have an indent file for you to look at because it's all integrated into Vim's own c source code, but most other languages have indent files using Vimscript.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top