Bram Moolenaar | 7a32991 | 2010-05-21 12:05:36 +0200 | [diff] [blame] | 1 | " Vim indent file |
| 2 | " Language: Liquid |
| 3 | " Maintainer: Tim Pope <vimNOSPAM@tpope.org> |
Bram Moolenaar | 47c532e | 2022-03-19 15:18:53 +0000 | [diff] [blame] | 4 | " Last Change: 2022 Mar 15 |
Bram Moolenaar | 7a32991 | 2010-05-21 12:05:36 +0200 | [diff] [blame] | 5 | |
| 6 | if exists('b:did_indent') |
| 7 | finish |
| 8 | endif |
| 9 | |
| 10 | set indentexpr= |
| 11 | if exists('b:liquid_subtype') |
| 12 | exe 'runtime! indent/'.b:liquid_subtype.'.vim' |
| 13 | else |
| 14 | runtime! indent/html.vim |
| 15 | endif |
| 16 | unlet! b:did_indent |
| 17 | |
| 18 | if &l:indentexpr == '' |
| 19 | if &l:cindent |
| 20 | let &l:indentexpr = 'cindent(v:lnum)' |
| 21 | else |
| 22 | let &l:indentexpr = 'indent(prevnonblank(v:lnum-1))' |
| 23 | endif |
| 24 | endif |
| 25 | let b:liquid_subtype_indentexpr = &l:indentexpr |
| 26 | |
| 27 | let b:did_indent = 1 |
| 28 | |
| 29 | setlocal indentexpr=GetLiquidIndent() |
| 30 | setlocal indentkeys=o,O,*<Return>,<>>,{,},0),0],o,O,!^F,=end,=endif,=endunless,=endifchanged,=endcase,=endfor,=endtablerow,=endcapture,=else,=elsif,=when,=empty |
| 31 | |
Bram Moolenaar | 47c532e | 2022-03-19 15:18:53 +0000 | [diff] [blame] | 32 | let b:undo_indent = "setl inde< indk<" |
| 33 | |
Bram Moolenaar | 7a32991 | 2010-05-21 12:05:36 +0200 | [diff] [blame] | 34 | " Only define the function once. |
| 35 | if exists('*GetLiquidIndent') |
| 36 | finish |
| 37 | endif |
| 38 | |
Bram Moolenaar | 47c532e | 2022-03-19 15:18:53 +0000 | [diff] [blame] | 39 | function! s:count(string, pattern) abort |
Bram Moolenaar | 7a32991 | 2010-05-21 12:05:36 +0200 | [diff] [blame] | 40 | let string = substitute(a:string,'\C'.a:pattern,"\n",'g') |
| 41 | return strlen(substitute(string,"[^\n]",'','g')) |
| 42 | endfunction |
| 43 | |
Bram Moolenaar | 47c532e | 2022-03-19 15:18:53 +0000 | [diff] [blame] | 44 | function! GetLiquidIndent(...) abort |
Bram Moolenaar | 7a32991 | 2010-05-21 12:05:36 +0200 | [diff] [blame] | 45 | if a:0 && a:1 == '.' |
| 46 | let v:lnum = line('.') |
| 47 | elseif a:0 && a:1 =~ '^\d' |
| 48 | let v:lnum = a:1 |
| 49 | endif |
| 50 | let vcol = col('.') |
| 51 | call cursor(v:lnum,1) |
| 52 | exe "let ind = ".b:liquid_subtype_indentexpr |
| 53 | let lnum = prevnonblank(v:lnum-1) |
| 54 | let line = getline(lnum) |
| 55 | let cline = getline(v:lnum) |
Bram Moolenaar | 47c532e | 2022-03-19 15:18:53 +0000 | [diff] [blame] | 56 | let line = substitute(line,'\C^\%(\s*{%-\=\s*end\w*\s*-\=%}\)\+','','') |
| 57 | let line = substitute(line,'\C\%(\s*{%-\=\s*if.\+-\=%}.\+{%-\=\s*endif\s*-\=%}\)\+','','g') |
| 58 | let line .= matchstr(cline,'\C^\%(\s*{%-\=\s*end\w*\s*-\=%}\)\+') |
| 59 | let cline = substitute(cline,'\C^\%(\s*{%-\=\s*end\w*\s*-\=%}\)\+','','') |
Bram Moolenaar | 3ec574f | 2017-06-13 18:12:01 +0200 | [diff] [blame] | 60 | let sw = shiftwidth() |
Bram Moolenaar | 47c532e | 2022-03-19 15:18:53 +0000 | [diff] [blame] | 61 | let ind += sw * s:count(line,'{%-\=\s*\%(if\|elsif\|else\|unless\|ifchanged\|case\|when\|for\|empty\|tablerow\|capture\)\>') |
| 62 | let ind -= sw * s:count(line,'{%-\=\s*end\%(if\|unless\|ifchanged\|case\|for\|tablerow\|capture\)\>') |
| 63 | let ind -= sw * s:count(cline,'{%-\=\s*\%(elsif\|else\|when\|empty\)\>') |
| 64 | let ind -= sw * s:count(cline,'{%-\=\s*end\w*$') |
Bram Moolenaar | 7a32991 | 2010-05-21 12:05:36 +0200 | [diff] [blame] | 65 | return ind |
| 66 | endfunction |