idris2.vim 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. " Vim indent file
  2. " Language: Idris 2
  3. " Maintainer: Idris Hackers (https://github.com/edwinb/idris2-vim), Serhii Khoma <srghma@gmail.com>
  4. " Author: raichoo <raichoo@googlemail.com>
  5. " Last Change: 2024 Nov 05
  6. " License: Vim (see :h license)
  7. " Repository: https://github.com/ShinKage/idris2-nvim
  8. "
  9. " indentation for idris (idris-lang.org)
  10. "
  11. " Based on haskell indentation by motemen <motemen@gmail.com>
  12. "
  13. " Indentation configuration variables:
  14. "
  15. " g:idris2_indent_if (default: 3)
  16. " Controls indentation after 'if' statements
  17. " Example:
  18. " if condition
  19. " >>>then expr
  20. " >>>else expr
  21. "
  22. " g:idris2_indent_case (default: 5)
  23. " Controls indentation of case expressions
  24. " Example:
  25. " case x of
  26. " >>>>>Left y => ...
  27. " >>>>>Right z => ...
  28. "
  29. " g:idris2_indent_let (default: 4)
  30. " Controls indentation after 'let' bindings
  31. " Example:
  32. " let x = expr in
  33. " >>>>body
  34. "
  35. " g:idris2_indent_rewrite (default: 8)
  36. " Controls indentation after 'rewrite' expressions
  37. " Example:
  38. " rewrite proof in
  39. " >>>>>>>>expr
  40. "
  41. " g:idris2_indent_where (default: 6)
  42. " Controls indentation of 'where' blocks
  43. " Example:
  44. " function args
  45. " >>>>>>where helper = expr
  46. "
  47. " g:idris2_indent_do (default: 3)
  48. " Controls indentation in 'do' blocks
  49. " Example:
  50. " do x <- action
  51. " >>>y <- action
  52. "
  53. " Example configuration in .vimrc:
  54. " let g:idris2_indent_if = 2
  55. if exists('b:did_indent')
  56. finish
  57. endif
  58. setlocal indentexpr=GetIdrisIndent()
  59. setlocal indentkeys=!^F,o,O,}
  60. let b:did_indent = 1
  61. let b:undo_indent = "setlocal indentexpr< indentkeys<"
  62. " we want to use line continuations (\) BEGINNING
  63. let s:cpo_save = &cpo
  64. set cpo&vim
  65. " Define defaults for indent configuration
  66. let s:indent_defaults = {
  67. \ 'idris2_indent_if': 3,
  68. \ 'idris2_indent_case': 5,
  69. \ 'idris2_indent_let': 4,
  70. \ 'idris2_indent_rewrite': 8,
  71. \ 'idris2_indent_where': 6,
  72. \ 'idris2_indent_do': 3
  73. \ }
  74. " we want to use line continuations (\) END
  75. let &cpo = s:cpo_save
  76. unlet s:cpo_save
  77. " Set up indent settings with user overrides
  78. for [key, default] in items(s:indent_defaults)
  79. let varname = 'g:' . key
  80. if !exists(varname)
  81. execute 'let' varname '=' default
  82. endif
  83. endfor
  84. if exists("*GetIdrisIndent")
  85. finish
  86. endif
  87. function! GetIdrisIndent()
  88. let prevline = getline(v:lnum - 1)
  89. if prevline =~ '\s\+(\s*.\+\s\+:\s\+.\+\s*)\s\+->\s*$'
  90. return match(prevline, '(')
  91. elseif prevline =~ '\s\+{\s*.\+\s\+:\s\+.\+\s*}\s\+->\s*$'
  92. return match(prevline, '{')
  93. endif
  94. if prevline =~ '[!#$%&*+./<>?@\\^|~-]\s*$'
  95. let s = match(prevline, '[:=]')
  96. if s > 0
  97. return s + 2
  98. else
  99. return match(prevline, '\S')
  100. endif
  101. endif
  102. if prevline =~ '[{([][^})\]]\+$'
  103. return match(prevline, '[{([]')
  104. endif
  105. if prevline =~ '\<let\>\s\+.\+\<in\>\s*$'
  106. return match(prevline, '\<let\>') + g:idris2_indent_let
  107. endif
  108. if prevline =~ '\<rewrite\>\s\+.\+\<in\>\s*$'
  109. return match(prevline, '\<rewrite\>') + g:idris2_indent_rewrite
  110. endif
  111. if prevline !~ '\<else\>'
  112. let s = match(prevline, '\<if\>.*\&.*\zs\<then\>')
  113. if s > 0
  114. return s
  115. endif
  116. let s = match(prevline, '\<if\>')
  117. if s > 0
  118. return s + g:idris2_indent_if
  119. endif
  120. endif
  121. if prevline =~ '\(\<where\>\|\<do\>\|=\|[{([]\)\s*$'
  122. return match(prevline, '\S') + &shiftwidth
  123. endif
  124. if prevline =~ '\<where\>\s\+\S\+.*$'
  125. return match(prevline, '\<where\>') + g:idris2_indent_where
  126. endif
  127. if prevline =~ '\<do\>\s\+\S\+.*$'
  128. return match(prevline, '\<do\>') + g:idris2_indent_do
  129. endif
  130. if prevline =~ '^\s*\<\(co\)\?data\>\s\+[^=]\+\s\+=\s\+\S\+.*$'
  131. return match(prevline, '=')
  132. endif
  133. if prevline =~ '\<with\>\s\+([^)]*)\s*$'
  134. return match(prevline, '\S') + &shiftwidth
  135. endif
  136. if prevline =~ '\<case\>\s\+.\+\<of\>\s*$'
  137. return match(prevline, '\<case\>') + g:idris2_indent_case
  138. endif
  139. if prevline =~ '^\s*\(\<namespace\>\|\<\(co\)\?data\>\)\s\+\S\+\s*$'
  140. return match(prevline, '\(\<namespace\>\|\<\(co\)\?data\>\)') + &shiftwidth
  141. endif
  142. if prevline =~ '^\s*\(\<using\>\|\<parameters\>\)\s*([^(]*)\s*$'
  143. return match(prevline, '\(\<using\>\|\<parameters\>\)') + &shiftwidth
  144. endif
  145. if prevline =~ '^\s*\<mutual\>\s*$'
  146. return match(prevline, '\<mutual\>') + &shiftwidth
  147. endif
  148. let line = getline(v:lnum)
  149. if (line =~ '^\s*}\s*' && prevline !~ '^\s*;')
  150. return match(prevline, '\S') - &shiftwidth
  151. endif
  152. return match(prevline, '\S')
  153. endfunction
  154. " vim:et:sw=2:sts=2