From a50b8510c29489826a5855177494c6e3716a215b Mon Sep 17 00:00:00 2001 From: PaddiM8 Date: Sun, 22 Dec 2024 01:23:29 +0100 Subject: [PATCH] editors: Vim indentation --- editors/vim/indent/elk.vim | 75 ++++++++++++++++++++++++++++++++++++++ editors/vim/syntax/elk.vim | 4 +- 2 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 editors/vim/indent/elk.vim diff --git a/editors/vim/indent/elk.vim b/editors/vim/indent/elk.vim new file mode 100644 index 00000000..ba302310 --- /dev/null +++ b/editors/vim/indent/elk.vim @@ -0,0 +1,75 @@ +if exists('b:did_indent') + finish +endif +let b:did_indent = 1 + +setlocal cinoptions& cinoptions+=j1,L0 +setlocal indentexpr=ElkIndent() +setlocal indentkeys=0},0),!^F,o,O,e, + +function! ElkIndent() + if v:lnum == 0 + return 0 + endif + + let prev_num = prevnonblank(v:lnum - 1) + let prev = getline(prev_num) + let prev_prev_num = prevnonblank(prev_num - 1) + let prev_prev = getline(prev_prev_num) + let prev_indent = indent(prev_num) + let cur = getline(v:lnum) + + let prev_open_paren = prev =~ '^.*(\s*$' + let cur_close_paren = cur =~ '^\s*).*$' + let prev_close_paren = prev =~ '^\s*).*$' + + let prev_open_brace = prev =~ '^.*{\s*$' + let cur_close_brace = cur =~ '^\s*}.*$' + let prev_close_brace = prev =~ '^\s*}.*$' + + if (prev_close_brace && !prev_open_brace) || (prev_close_paren && !prev_open_paren) + " find the start of the block + let i = v:lnum - 2 + while i >= 0 && indent(i) > prev_indent && getline(i) != "" + let i -= 1 + endwhile + + " ...and check if it starts with a pipe + " if it does, we need to account for the pipe's + " additional indentation and remove that too + if getline(i) =~ "^\\s*\|" + let prev_indent -= shiftwidth() + endif + endif + + if prev =~ "^\\s*\|" + let prev_indent -= shiftwidth() + endif + + if cur =~ "^\\s*\|" + return prev_indent + shiftwidth() + endif + + let trailing_keyword_re = '\(\s\)\+\(or\|and\|\\\)\s*$' + if prev_prev =~# trailing_keyword_re + let prev_indent -= shiftwidth() + endif + + if prev =~# trailing_keyword_re + return prev_indent + shiftwidth() + endif + + if (prev_open_paren || prev_open_brace) && prev =~ "^\\s*\|" + let prev_indent += shiftwidth() + endif + + if prev_open_paren && !cur_close_paren || prev_open_brace && !cur_close_brace + return prev_indent + shiftwidth() + endif + + if cur_close_paren && !prev_open_paren || cur_close_brace && !prev_open_brace + return prev_indent - shiftwidth() + endif + + return prev_indent +endfunction diff --git a/editors/vim/syntax/elk.vim b/editors/vim/syntax/elk.vim index 351a6068..216b5fa2 100644 --- a/editors/vim/syntax/elk.vim +++ b/editors/vim/syntax/elk.vim @@ -23,7 +23,7 @@ syn match elkNumber /\v<[+-]=(\d+\.)=\d+>/ syn match elkBraces "[\[\]]" syn region elkStringInterp matchgroup=elkBraces start=/\${/ end=/}/ contained contains=ALL -syn region elkString start=/"/ skip=/\\\\\|\\"/ end=/"/ contains=elkStringInterp +syn region elkString start=/["']/ skip=/\\\\\|\\"|\\'/ end=/["']/ contains=elkStringInterp hi def link elkBraces Delimiter hi def link elkKeyword Keyword @@ -37,6 +37,6 @@ hi def link elkOperator Operator hi def link elkString String hi def link elkNumber Number hi def link elkBoolean Boolean -hi def link elkControl Exception +hi def link elkControl Keyword let b:current_syntax = 'elk'