Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1 | " Vim indent file |
| 2 | " Language: Vim script |
| 3 | " Maintainer: Bram Moolenaar <Bram@vim.org> |
Bram Moolenaar | 1588bc8 | 2022-03-08 21:35:07 +0000 | [diff] [blame] | 4 | " Last Change: 2022 Mar 01 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5 | |
| 6 | " Only load this indent file when no other was loaded. |
| 7 | if exists("b:did_indent") |
| 8 | finish |
| 9 | endif |
| 10 | let b:did_indent = 1 |
| 11 | |
| 12 | setlocal indentexpr=GetVimIndent() |
Bram Moolenaar | c51cf03 | 2022-02-26 12:25:45 +0000 | [diff] [blame] | 13 | setlocal indentkeys+==endif,=enddef,=endfu,=endfor,=endwh,=endtry,=},=else,=cat,=finall,=END,0\\,0=\"\\\ |
Bram Moolenaar | 2547aa9 | 2020-07-26 17:00:44 +0200 | [diff] [blame] | 14 | setlocal indentkeys-=0# |
Bram Moolenaar | 4700398 | 2021-12-05 21:54:04 +0000 | [diff] [blame] | 15 | setlocal indentkeys-=: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 16 | |
Bram Moolenaar | c873442 | 2012-06-01 22:38:45 +0200 | [diff] [blame] | 17 | let b:undo_indent = "setl indentkeys< indentexpr<" |
| 18 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 19 | " Only define the function once. |
| 20 | if exists("*GetVimIndent") |
| 21 | finish |
| 22 | endif |
Bram Moolenaar | 8e52a59 | 2012-05-18 21:49:28 +0200 | [diff] [blame] | 23 | let s:keepcpo= &cpo |
| 24 | set cpo&vim |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 25 | |
| 26 | function GetVimIndent() |
Bram Moolenaar | 9b45125 | 2012-08-15 17:43:31 +0200 | [diff] [blame] | 27 | let ignorecase_save = &ignorecase |
| 28 | try |
| 29 | let &ignorecase = 0 |
| 30 | return GetVimIndentIntern() |
| 31 | finally |
| 32 | let &ignorecase = ignorecase_save |
| 33 | endtry |
| 34 | endfunc |
| 35 | |
Bram Moolenaar | 67f8ab8 | 2018-09-11 22:37:29 +0200 | [diff] [blame] | 36 | let s:lineContPat = '^\s*\(\\\|"\\ \)' |
| 37 | |
Bram Moolenaar | 9b45125 | 2012-08-15 17:43:31 +0200 | [diff] [blame] | 38 | function GetVimIndentIntern() |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 39 | " Find a non-blank line above the current line. |
| 40 | let lnum = prevnonblank(v:lnum - 1) |
| 41 | |
Bram Moolenaar | e0e3917 | 2021-01-25 21:14:57 +0100 | [diff] [blame] | 42 | " The previous line, ignoring line continuation |
| 43 | let prev_text_end = lnum > 0 ? getline(lnum) : '' |
| 44 | |
Bram Moolenaar | 67f8ab8 | 2018-09-11 22:37:29 +0200 | [diff] [blame] | 45 | " If the current line doesn't start with '\' or '"\ ' and below a line that |
| 46 | " starts with '\' or '"\ ', use the indent of the line above it. |
Bram Moolenaar | 91e15e1 | 2014-09-19 22:38:48 +0200 | [diff] [blame] | 47 | let cur_text = getline(v:lnum) |
Bram Moolenaar | 67f8ab8 | 2018-09-11 22:37:29 +0200 | [diff] [blame] | 48 | if cur_text !~ s:lineContPat |
| 49 | while lnum > 0 && getline(lnum) =~ s:lineContPat |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 50 | let lnum = lnum - 1 |
| 51 | endwhile |
| 52 | endif |
| 53 | |
| 54 | " At the start of the file use zero indent. |
| 55 | if lnum == 0 |
| 56 | return 0 |
| 57 | endif |
Bram Moolenaar | e0e3917 | 2021-01-25 21:14:57 +0100 | [diff] [blame] | 58 | |
| 59 | " the start of the previous line, skipping over line continuation |
Bram Moolenaar | 91e15e1 | 2014-09-19 22:38:48 +0200 | [diff] [blame] | 60 | let prev_text = getline(lnum) |
Bram Moolenaar | 82be484 | 2021-01-11 19:40:15 +0100 | [diff] [blame] | 61 | let found_cont = 0 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 62 | |
| 63 | " Add a 'shiftwidth' after :if, :while, :try, :catch, :finally, :function |
Bram Moolenaar | 67f8ab8 | 2018-09-11 22:37:29 +0200 | [diff] [blame] | 64 | " and :else. Add it three times for a line that starts with '\' or '"\ ' |
| 65 | " after a line that doesn't (or g:vim_indent_cont if it exists). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 66 | let ind = indent(lnum) |
Bram Moolenaar | 1ff14ba | 2019-11-02 14:09:23 +0100 | [diff] [blame] | 67 | |
| 68 | " In heredoc indenting works completely differently. |
| 69 | if has('syntax_items') |
| 70 | let syn_here = synIDattr(synID(v:lnum, 1, 1), "name") |
| 71 | if syn_here =~ 'vimLetHereDocStop' |
| 72 | " End of heredoc: use indent of matching start line |
| 73 | let lnum = v:lnum - 1 |
| 74 | while lnum > 0 |
Bram Moolenaar | 11e3c5b | 2021-04-21 18:09:37 +0200 | [diff] [blame] | 75 | let attr = synIDattr(synID(lnum, 1, 1), "name") |
| 76 | if attr != '' && attr !~ 'vimLetHereDoc' |
Bram Moolenaar | 1ff14ba | 2019-11-02 14:09:23 +0100 | [diff] [blame] | 77 | return indent(lnum) |
| 78 | endif |
| 79 | let lnum -= 1 |
| 80 | endwhile |
| 81 | return 0 |
| 82 | endif |
| 83 | if syn_here =~ 'vimLetHereDoc' |
| 84 | if synIDattr(synID(lnum, 1, 1), "name") !~ 'vimLetHereDoc' |
| 85 | " First line in heredoc: increase indent |
| 86 | return ind + shiftwidth() |
| 87 | endif |
| 88 | " Heredoc continues: no change in indent |
| 89 | return ind |
| 90 | endif |
| 91 | endif |
| 92 | |
Bram Moolenaar | 67f8ab8 | 2018-09-11 22:37:29 +0200 | [diff] [blame] | 93 | if cur_text =~ s:lineContPat && v:lnum > 1 && prev_text !~ s:lineContPat |
Bram Moolenaar | 82be484 | 2021-01-11 19:40:15 +0100 | [diff] [blame] | 94 | let found_cont = 1 |
Bram Moolenaar | d4755bb | 2004-09-02 19:12:26 +0000 | [diff] [blame] | 95 | if exists("g:vim_indent_cont") |
| 96 | let ind = ind + g:vim_indent_cont |
| 97 | else |
Bram Moolenaar | 705ada1 | 2016-01-24 17:56:50 +0100 | [diff] [blame] | 98 | let ind = ind + shiftwidth() * 3 |
Bram Moolenaar | d4755bb | 2004-09-02 19:12:26 +0000 | [diff] [blame] | 99 | endif |
Bram Moolenaar | e18dbe8 | 2016-07-02 21:42:23 +0200 | [diff] [blame] | 100 | elseif prev_text =~ '^\s*aug\%[roup]\s\+' && prev_text !~ '^\s*aug\%[roup]\s\+[eE][nN][dD]\>' |
Bram Moolenaar | 705ada1 | 2016-01-24 17:56:50 +0100 | [diff] [blame] | 101 | let ind = ind + shiftwidth() |
Bram Moolenaar | cab49df | 2011-03-22 17:40:10 +0100 | [diff] [blame] | 102 | else |
Bram Moolenaar | 91e15e1 | 2014-09-19 22:38:48 +0200 | [diff] [blame] | 103 | " A line starting with :au does not increment/decrement indent. |
Bram Moolenaar | 942db23 | 2021-02-13 18:14:48 +0100 | [diff] [blame] | 104 | " A { may start a block or a dict. Assume that when a } follows it's a |
| 105 | " terminated dict. |
Bram Moolenaar | c51cf03 | 2022-02-26 12:25:45 +0000 | [diff] [blame] | 106 | " ":function" starts a block but "function(" doesn't. |
Bram Moolenaar | 942db23 | 2021-02-13 18:14:48 +0100 | [diff] [blame] | 107 | if prev_text !~ '^\s*au\%[tocmd]' && prev_text !~ '^\s*{.*}' |
Bram Moolenaar | 1588bc8 | 2022-03-08 21:35:07 +0000 | [diff] [blame] | 108 | let i = match(prev_text, '\(^\||\)\s*\(export\s\+\)\?\({\|\(if\|wh\%[ile]\|for\|try\|cat\%[ch]\|fina\|finall\%[y]\|def\|el\%[seif]\)\>\|fu\%[nction][! ]\)') |
Bram Moolenaar | 91e15e1 | 2014-09-19 22:38:48 +0200 | [diff] [blame] | 109 | if i >= 0 |
Bram Moolenaar | 705ada1 | 2016-01-24 17:56:50 +0100 | [diff] [blame] | 110 | let ind += shiftwidth() |
Bram Moolenaar | 91e15e1 | 2014-09-19 22:38:48 +0200 | [diff] [blame] | 111 | if strpart(prev_text, i, 1) == '|' && has('syntax_items') |
Bram Moolenaar | 113cb51 | 2021-11-07 20:27:04 +0000 | [diff] [blame] | 112 | \ && synIDattr(synID(lnum, i, 1), "name") =~ '\(Comment\|String\|PatSep\)$' |
Bram Moolenaar | 705ada1 | 2016-01-24 17:56:50 +0100 | [diff] [blame] | 113 | let ind -= shiftwidth() |
Bram Moolenaar | 91e15e1 | 2014-09-19 22:38:48 +0200 | [diff] [blame] | 114 | endif |
Bram Moolenaar | cab49df | 2011-03-22 17:40:10 +0100 | [diff] [blame] | 115 | endif |
| 116 | endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 117 | endif |
| 118 | |
| 119 | " If the previous line contains an "end" after a pipe, but not in an ":au" |
Bram Moolenaar | 520470a | 2005-06-16 21:59:56 +0000 | [diff] [blame] | 120 | " command. And not when there is a backslash before the pipe. |
Bram Moolenaar | dc27ac1 | 2005-07-06 22:35:45 +0000 | [diff] [blame] | 121 | " And when syntax HL is enabled avoid a match inside a string. |
Bram Moolenaar | 91e15e1 | 2014-09-19 22:38:48 +0200 | [diff] [blame] | 122 | let i = match(prev_text, '[^\\]|\s*\(ene\@!\)') |
| 123 | if i > 0 && prev_text !~ '^\s*au\%[tocmd]' |
Bram Moolenaar | dc27ac1 | 2005-07-06 22:35:45 +0000 | [diff] [blame] | 124 | if !has('syntax_items') || synIDattr(synID(lnum, i + 2, 1), "name") !~ '\(Comment\|String\)$' |
Bram Moolenaar | 705ada1 | 2016-01-24 17:56:50 +0100 | [diff] [blame] | 125 | let ind = ind - shiftwidth() |
Bram Moolenaar | dc27ac1 | 2005-07-06 22:35:45 +0000 | [diff] [blame] | 126 | endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 127 | endif |
| 128 | |
Bram Moolenaar | 82be484 | 2021-01-11 19:40:15 +0100 | [diff] [blame] | 129 | " For a line starting with "}" find the matching "{". If it is at the start |
| 130 | " of the line align with it, probably end of a block. |
| 131 | " Use the mapped "%" from matchit to find the match, otherwise we may match |
| 132 | " a { inside a comment or string. |
| 133 | if cur_text =~ '^\s*}' |
| 134 | if maparg('%') != '' |
| 135 | exe v:lnum |
| 136 | silent! normal % |
| 137 | if line('.') < v:lnum && getline('.') =~ '^\s*{' |
| 138 | let ind = indent('.') |
| 139 | endif |
| 140 | else |
| 141 | " todo: use searchpair() to find a match |
| 142 | endif |
| 143 | endif |
| 144 | |
| 145 | " Below a line starting with "}" find the matching "{". If it is at the |
| 146 | " end of the line we must be below the end of a dictionary. |
| 147 | if prev_text =~ '^\s*}' |
| 148 | if maparg('%') != '' |
| 149 | exe lnum |
| 150 | silent! normal % |
| 151 | if line('.') == lnum || getline('.') !~ '^\s*{' |
| 152 | let ind = ind - shiftwidth() |
| 153 | endif |
| 154 | else |
| 155 | " todo: use searchpair() to find a match |
| 156 | endif |
| 157 | endif |
| 158 | |
| 159 | " Below a line starting with "]" we must be below the end of a list. |
Bram Moolenaar | 942db23 | 2021-02-13 18:14:48 +0100 | [diff] [blame] | 160 | " Include a "}" and "},} in case a dictionary ends too. |
| 161 | if prev_text_end =~ '^\s*\(},\=\s*\)\=]' |
Bram Moolenaar | 82be484 | 2021-01-11 19:40:15 +0100 | [diff] [blame] | 162 | let ind = ind - shiftwidth() |
| 163 | endif |
| 164 | |
Bram Moolenaar | 942db23 | 2021-02-13 18:14:48 +0100 | [diff] [blame] | 165 | let ends_in_comment = has('syntax_items') |
Bram Moolenaar | 9faec4e | 2021-02-27 16:38:07 +0100 | [diff] [blame] | 166 | \ && synIDattr(synID(lnum, len(getline(lnum)), 1), "name") =~ '\(Comment\|String\)$' |
Bram Moolenaar | 942db23 | 2021-02-13 18:14:48 +0100 | [diff] [blame] | 167 | |
Bram Moolenaar | 9faec4e | 2021-02-27 16:38:07 +0100 | [diff] [blame] | 168 | " A line ending in "{" or "[" is most likely the start of a dict/list literal, |
Bram Moolenaar | 942db23 | 2021-02-13 18:14:48 +0100 | [diff] [blame] | 169 | " indent the next line more. Not for a continuation line or {{{. |
| 170 | if !ends_in_comment && prev_text_end =~ '\s[{[]\s*$' && !found_cont |
Bram Moolenaar | 82be484 | 2021-01-11 19:40:15 +0100 | [diff] [blame] | 171 | let ind = ind + shiftwidth() |
| 172 | endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 173 | |
Bram Moolenaar | c51cf03 | 2022-02-26 12:25:45 +0000 | [diff] [blame] | 174 | " Subtract a 'shiftwidth' on a :endif, :endwhile, :endfor, :catch, :finally, |
| 175 | " :endtry, :endfun, :enddef, :else and :augroup END. |
| 176 | " Although ":en" would be enough only match short command names as in |
| 177 | " 'indentkeys'. |
| 178 | if cur_text =~ '^\s*\(endif\|endwh\|endfor\|endtry\|endfu\|enddef\|cat\|finall\|else\|aug\%[roup]\s\+[eE][nN][dD]\)' |
Bram Moolenaar | 705ada1 | 2016-01-24 17:56:50 +0100 | [diff] [blame] | 179 | let ind = ind - shiftwidth() |
Bram Moolenaar | c51cf03 | 2022-02-26 12:25:45 +0000 | [diff] [blame] | 180 | if ind < 0 |
| 181 | let ind = 0 |
| 182 | endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 183 | endif |
| 184 | |
| 185 | return ind |
| 186 | endfunction |
| 187 | |
Bram Moolenaar | 8e52a59 | 2012-05-18 21:49:28 +0200 | [diff] [blame] | 188 | let &cpo = s:keepcpo |
| 189 | unlet s:keepcpo |
| 190 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 191 | " vim:sw=2 |