aboutsummaryrefslogtreecommitdiff
path: root/runtime/indent/vim.vim
diff options
context:
space:
mode:
Diffstat (limited to 'runtime/indent/vim.vim')
-rw-r--r--runtime/indent/vim.vim90
1 files changed, 85 insertions, 5 deletions
diff --git a/runtime/indent/vim.vim b/runtime/indent/vim.vim
index cd735c3a3c..ee3d39490d 100644
--- a/runtime/indent/vim.vim
+++ b/runtime/indent/vim.vim
@@ -1,7 +1,7 @@
" Vim indent file
" Language: Vim script
" Maintainer: Bram Moolenaar <Bram@vim.org>
-" Last Change: 2016 Jun 27
+" Last Change: 2021 Apr 18
" Only load this indent file when no other was loaded.
if exists("b:did_indent")
@@ -10,7 +10,8 @@ endif
let b:did_indent = 1
setlocal indentexpr=GetVimIndent()
-setlocal indentkeys+==end,=else,=cat,=fina,=END,0\\,0=\"\\\
+setlocal indentkeys+==end,=},=else,=cat,=finall,=END,0\\,0=\"\\\
+setlocal indentkeys-=0#
let b:undo_indent = "setl indentkeys< indentexpr<"
@@ -37,6 +38,9 @@ function GetVimIndentIntern()
" Find a non-blank line above the current line.
let lnum = prevnonblank(v:lnum - 1)
+ " The previous line, ignoring line continuation
+ let prev_text_end = lnum > 0 ? getline(lnum) : ''
+
" If the current line doesn't start with '\' or '"\ ' and below a line that
" starts with '\' or '"\ ', use the indent of the line above it.
let cur_text = getline(v:lnum)
@@ -50,13 +54,43 @@ function GetVimIndentIntern()
if lnum == 0
return 0
endif
+
+ " the start of the previous line, skipping over line continuation
let prev_text = getline(lnum)
+ let found_cont = 0
" Add a 'shiftwidth' after :if, :while, :try, :catch, :finally, :function
" and :else. Add it three times for a line that starts with '\' or '"\ '
" after a line that doesn't (or g:vim_indent_cont if it exists).
let ind = indent(lnum)
+
+ " In heredoc indenting works completely differently.
+ if has('syntax_items')
+ let syn_here = synIDattr(synID(v:lnum, 1, 1), "name")
+ if syn_here =~ 'vimLetHereDocStop'
+ " End of heredoc: use indent of matching start line
+ let lnum = v:lnum - 1
+ while lnum > 0
+ let attr = synIDattr(synID(lnum, 1, 1), "name")
+ if attr != '' && attr !~ 'vimLetHereDoc'
+ return indent(lnum)
+ endif
+ let lnum -= 1
+ endwhile
+ return 0
+ endif
+ if syn_here =~ 'vimLetHereDoc'
+ if synIDattr(synID(lnum, 1, 1), "name") !~ 'vimLetHereDoc'
+ " First line in heredoc: increase indent
+ return ind + shiftwidth()
+ endif
+ " Heredoc continues: no change in indent
+ return ind
+ endif
+ endif
+
if cur_text =~ s:lineContPat && v:lnum > 1 && prev_text !~ s:lineContPat
+ let found_cont = 1
if exists("g:vim_indent_cont")
let ind = ind + g:vim_indent_cont
else
@@ -66,8 +100,10 @@ function GetVimIndentIntern()
let ind = ind + shiftwidth()
else
" A line starting with :au does not increment/decrement indent.
- if prev_text !~ '^\s*au\%[tocmd]'
- let i = match(prev_text, '\(^\||\)\s*\(if\|wh\%[ile]\|for\|try\|cat\%[ch]\|fina\%[lly]\|fu\%[nction]\|el\%[seif]\)\>')
+ " A { may start a block or a dict. Assume that when a } follows it's a
+ " terminated dict.
+ if prev_text !~ '^\s*au\%[tocmd]' && prev_text !~ '^\s*{.*}'
+ let i = match(prev_text, '\(^\||\)\s*\(export\s\+\)\?\({\|\(if\|wh\%[ile]\|for\|try\|cat\%[ch]\|fina\|finall\%[y]\|fu\%[nction]\|def\|el\%[seif]\)\>\)')
if i >= 0
let ind += shiftwidth()
if strpart(prev_text, i, 1) == '|' && has('syntax_items')
@@ -88,10 +124,54 @@ function GetVimIndentIntern()
endif
endif
+ " For a line starting with "}" find the matching "{". If it is at the start
+ " of the line align with it, probably end of a block.
+ " Use the mapped "%" from matchit to find the match, otherwise we may match
+ " a { inside a comment or string.
+ if cur_text =~ '^\s*}'
+ if maparg('%') != ''
+ exe v:lnum
+ silent! normal %
+ if line('.') < v:lnum && getline('.') =~ '^\s*{'
+ let ind = indent('.')
+ endif
+ else
+ " todo: use searchpair() to find a match
+ endif
+ endif
+
+ " Below a line starting with "}" find the matching "{". If it is at the
+ " end of the line we must be below the end of a dictionary.
+ if prev_text =~ '^\s*}'
+ if maparg('%') != ''
+ exe lnum
+ silent! normal %
+ if line('.') == lnum || getline('.') !~ '^\s*{'
+ let ind = ind - shiftwidth()
+ endif
+ else
+ " todo: use searchpair() to find a match
+ endif
+ endif
+
+ " Below a line starting with "]" we must be below the end of a list.
+ " Include a "}" and "},} in case a dictionary ends too.
+ if prev_text_end =~ '^\s*\(},\=\s*\)\=]'
+ let ind = ind - shiftwidth()
+ endif
+
+ let ends_in_comment = has('syntax_items')
+ \ && synIDattr(synID(lnum, len(getline(lnum)), 1), "name") =~ '\(Comment\|String\)$'
+
+ " A line ending in "{" or "[" is most likely the start of a dict/list literal,
+ " indent the next line more. Not for a continuation line or {{{.
+ if !ends_in_comment && prev_text_end =~ '\s[{[]\s*$' && !found_cont
+ let ind = ind + shiftwidth()
+ endif
" Subtract a 'shiftwidth' on a :endif, :endwhile, :catch, :finally, :endtry,
" :endfun, :else and :augroup END.
- if cur_text =~ '^\s*\(ene\@!\|cat\|fina\|el\|aug\%[roup]\s\+[eE][nN][dD]\)'
+ if cur_text =~ '^\s*\(ene\@!\|cat\|finall\|el\|aug\%[roup]\s\+[eE][nN][dD]\)'
let ind = ind - shiftwidth()
endif