Skip to content

Commit 4cd3a4b

Browse files
committed
Make vim respect the tab width settings
Somehow the dune filetype in neovim ends up with the "lisp" option set despite this not being explicitly configured. Without this setting indentation of dune files does not happen at all, as it's needed for vim to indent lines based on parentheses. Unfortunately when "lisp" is set, it also causes the softtabstop and shiftwidth settings to be ignored, presumably to allow for indentation conventions in lisp where elements of lists are aligned to the first element of the list, regardless of its indentation. The default behaviour with "lisp" enabled also effectively uses a softtabstop of 2, and this can't be overriden by setting the softtabstop variable. This makes it frustrating to edit dune files in vim, as the indentation inserted by the editor won't match the 1-space indentation commonly found in dune files. The workaround is to to set lispoptions=expr:1 which allows vim to use a custom indentexpr when "lisp" is set, and to supply a custom indentexpr function which changes the indentation based on unmatched parentheses, which is an approximation of the behaviour of `dune format-dune-file`. Signed-off-by: Stephen Sherratt <[email protected]>
1 parent 81be9d0 commit 4cd3a4b

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

indent/dune.vim

+22-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,28 @@ if exists("b:did_indent")
99
endif
1010
let b:did_indent = 1
1111

12+
" A rough approximation of the indentation behaviour implemented by
13+
" `dune format-dune-file`.
14+
function! DuneIndent()
15+
let prev_line = getline(v:lnum - 1)
16+
let prev_indent = indent(v:lnum - 1)
17+
let current_indent = prev_indent
18+
for i in range(len(prev_line))
19+
if prev_line[i] == '('
20+
let current_indent += &shiftwidth
21+
endif
22+
if prev_line[i] == ')'
23+
let current_indent -= &shiftwidth
24+
endif
25+
endfor
26+
return current_indent
27+
endfunction
28+
1229
" dune format-dune-file uses 1 space to indent
13-
setlocal softtabstop=1 shiftwidth=1 expandtab
30+
" Explicitly set "lisp" since without this setting vim will not auto indent
31+
" sexp files like dune files at all. When "lisp" is enabled, vim doesn't
32+
" respect softtabstop or shiftwidth by default, so a custom indentexpr is
33+
" needed.
34+
setlocal softtabstop=1 shiftwidth=1 expandtab lisp indentexpr=DuneIndent() lispoptions=expr:1
1435

1536
let b:undo_indent = "setl et< sts< sw<"

0 commit comments

Comments
 (0)