liquid.vim 2.0 KB

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