fish.vim 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. " Vim indent file
  2. " Language: fish
  3. " Maintainer: Nicholas Boyle (github.com/nickeb96)
  4. " Repository: https://github.com/nickeb96/fish.vim
  5. " Last Change: February 4, 2023
  6. " 2023 Aug 28 by Vim Project (undo_indent)
  7. if exists("b:did_indent")
  8. finish
  9. endif
  10. let b:did_indent = 1
  11. setlocal indentexpr=GetFishIndent(v:lnum)
  12. setlocal indentkeys+==end,=else,=case
  13. let b:undo_indent = "setlocal indentexpr< indentkeys<"
  14. function s:PrevCmdStart(linenum)
  15. let l:linenum = a:linenum
  16. " look for the first line that isn't a line continuation
  17. while l:linenum > 1 && getline(l:linenum - 1) =~# '\\$'
  18. let l:linenum = l:linenum - 1
  19. endwhile
  20. return l:linenum
  21. endfunction
  22. function GetFishIndent(lnum)
  23. let l:shiftwidth = shiftwidth()
  24. let l:prevlnum = prevnonblank(a:lnum - 1)
  25. if l:prevlnum ==# 0
  26. return 0
  27. endif
  28. " if the previous line ended with a line continuation
  29. if getline(a:lnum - 1) =~# '\\$'
  30. if a:lnum ==# 0 || getline(a:lnum - 2) !~# '\\$'
  31. " this is the first line continuation in a chain, so indent it
  32. return indent(a:lnum - 1) + l:shiftwidth
  33. else
  34. " use the same indentation as the previous continued line
  35. return indent(a:lnum - 1)
  36. endif
  37. endif
  38. let l:prevlnum = s:PrevCmdStart(l:prevlnum)
  39. let l:prevline = getline(l:prevlnum)
  40. if l:prevline =~# '^\s*\(begin\|if\|else\|while\|for\|function\|case\|switch\)\>'
  41. let l:indent = l:shiftwidth
  42. else
  43. let l:indent = 0
  44. endif
  45. let l:line = getline(a:lnum)
  46. if l:line =~# '^\s*end\>'
  47. " find end's matching start
  48. let l:depth = 1
  49. let l:currentlnum = a:lnum
  50. while l:depth > 0 && l:currentlnum > 0
  51. let l:currentlnum = s:PrevCmdStart(prevnonblank(l:currentlnum - 1))
  52. let l:currentline = getline(l:currentlnum)
  53. if l:currentline =~# '^\s*end\>'
  54. let l:depth = l:depth + 1
  55. elseif l:currentline =~# '^\s*\(begin\|if\|while\|for\|function\|switch\)\>'
  56. let l:depth = l:depth - 1
  57. endif
  58. endwhile
  59. if l:currentline =~# '^\s*switch\>'
  60. return indent(l:currentlnum)
  61. else
  62. return indent(l:prevlnum) + l:indent - l:shiftwidth
  63. endif
  64. elseif l:line =~# '^\s*else\>'
  65. return indent(l:prevlnum) + l:indent - l:shiftwidth
  66. elseif l:line =~# '^\s*case\>'
  67. if getline(l:prevlnum) =~# '^\s*switch\>'
  68. return indent(l:prevlnum) + l:indent
  69. else
  70. return indent(l:prevlnum) + l:indent - l:shiftwidth
  71. endif
  72. else
  73. return indent(l:prevlnum) + l:indent
  74. endif
  75. endfunction