elm.vim 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. " Elm indent plugin file
  2. " Language: Elm
  3. " Maintainer: Andreas Scharf <as@99n.de>
  4. " Original Author: Joseph Hager <ajhager@gmail.com>
  5. " Copyright: Joseph Hager <ajhager@gmail.com>
  6. " License: BSD3
  7. " Latest Revision: 2021-09-29
  8. " Only load this indent file when no other was loaded.
  9. if exists('b:did_indent')
  10. finish
  11. endif
  12. let b:did_indent = 1
  13. " Local defaults
  14. setlocal expandtab
  15. setlocal indentexpr=GetElmIndent()
  16. setlocal indentkeys+=0=else,0=if,0=of,0=import,0=then,0=type,0\|,0},0\],0),=-},0=in
  17. setlocal nolisp
  18. setlocal nosmartindent
  19. let b:undo_indent = "setl et< inde< indk< lisp< si<"
  20. " Only define the function once.
  21. if exists('*GetElmIndent')
  22. finish
  23. endif
  24. " Indent pairs
  25. function! s:FindPair(pstart, pmid, pend)
  26. "call search(a:pend, 'bW')
  27. return indent(searchpair(a:pstart, a:pmid, a:pend, 'bWn', 'synIDattr(synID(line("."), col("."), 0), "name") =~? "string\\|comment"'))
  28. endfunction
  29. function! GetElmIndent()
  30. let l:lnum = v:lnum - 1
  31. " Ident 0 if the first line of the file:
  32. if l:lnum == 0
  33. return 0
  34. endif
  35. let l:ind = indent(l:lnum)
  36. let l:lline = getline(l:lnum)
  37. let l:line = getline(v:lnum)
  38. " Indent if current line begins with '}':
  39. if l:line =~? '^\s*}'
  40. return s:FindPair('{', '', '}')
  41. " Indent if current line begins with 'else':
  42. elseif l:line =~# '^\s*else\>'
  43. if l:lline !~# '^\s*\(if\|then\)\>'
  44. return s:FindPair('\<if\>', '', '\<else\>')
  45. endif
  46. " Indent if current line begins with 'then':
  47. elseif l:line =~# '^\s*then\>'
  48. if l:lline !~# '^\s*\(if\|else\)\>'
  49. return s:FindPair('\<if\>', '', '\<then\>')
  50. endif
  51. " HACK: Indent lines in case with nearest case clause:
  52. elseif l:line =~# '->' && l:line !~# ':' && l:line !~# '\\'
  53. return indent(search('^\s*case', 'bWn')) + &shiftwidth
  54. " HACK: Don't change the indentation if the last line is a comment.
  55. elseif l:lline =~# '^\s*--'
  56. return l:ind
  57. " Align the end of block comments with the start
  58. elseif l:line =~# '^\s*-}'
  59. return indent(search('{-', 'bWn'))
  60. " Indent double shift after let with an empty rhs
  61. elseif l:lline =~# '\<let\>.*\s=$'
  62. return l:ind + 4 + &shiftwidth
  63. " Align 'in' with the parent let.
  64. elseif l:line =~# '^\s*in\>'
  65. return indent(search('^\s*let', 'bWn'))
  66. " Align bindings with the parent let.
  67. elseif l:lline =~# '\<let\>'
  68. return l:ind + 4
  69. " Align bindings with the parent in.
  70. elseif l:lline =~# '^\s*in\>'
  71. return l:ind
  72. endif
  73. " Add a 'shiftwidth' after lines ending with:
  74. if l:lline =~# '\(|\|=\|->\|<-\|(\|\[\|{\|\<\(of\|else\|if\|then\)\)\s*$'
  75. let l:ind = l:ind + &shiftwidth
  76. " Add a 'shiftwidth' after lines starting with type ending with '=':
  77. elseif l:lline =~# '^\s*type' && l:line =~# '^\s*='
  78. let l:ind = l:ind + &shiftwidth
  79. " Back to normal indent after comments:
  80. elseif l:lline =~# '-}\s*$'
  81. call search('-}', 'bW')
  82. let l:ind = indent(searchpair('{-', '', '-}', 'bWn', 'synIDattr(synID(line("."), col("."), 0), "name") =~? "string"'))
  83. " Ident some operators if there aren't any starting the last line.
  84. elseif l:line =~# '^\s*\(!\|&\|(\|`\|+\||\|{\|[\|,\)=' && l:lline !~# '^\s*\(!\|&\|(\|`\|+\||\|{\|[\|,\)=' && l:lline !~# '^\s*$'
  85. let l:ind = l:ind + &shiftwidth
  86. elseif l:lline ==# '' && getline(l:lnum - 1) !=# ''
  87. let l:ind = indent(search('^\s*\S+', 'bWn'))
  88. endif
  89. return l:ind
  90. endfunc