ocaml.vim 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. " Vim indent file
  2. " Language: OCaml
  3. " Maintainers: Jean-Francois Yuen <jfyuen@happycoders.org>
  4. " Mike Leary <leary@nwlink.com>
  5. " Markus Mottl <markus.mottl@gmail.com>
  6. " URL: https://github.com/ocaml/vim-ocaml
  7. " Last Change: 2017 Jun 13
  8. " 2005 Jun 25 - Fixed multiple bugs due to 'else\nreturn ind' working
  9. " 2005 May 09 - Added an option to not indent OCaml-indents specially (MM)
  10. " 2013 June - commented textwidth (Marc Weber)
  11. "
  12. " Marc Weber's comment: This file may contain a lot of (very custom) stuff
  13. " which eventually should be moved somewhere else ..
  14. " Only load this indent file when no other was loaded.
  15. if exists("b:did_indent")
  16. finish
  17. endif
  18. let b:did_indent = 1
  19. setlocal expandtab
  20. setlocal indentexpr=GetOCamlIndent()
  21. setlocal indentkeys+=0=and,0=class,0=constraint,0=done,0=else,0=end,0=exception,0=external,0=if,0=in,0=include,0=inherit,0=initializer,0=let,0=method,0=open,0=then,0=type,0=val,0=with,0;;,0>\],0\|\],0>},0\|,0},0\],0)
  22. setlocal nolisp
  23. setlocal nosmartindent
  24. " At least Marc Weber and Markus Mottl do not like this:
  25. " setlocal textwidth=80
  26. " Comment formatting
  27. if !exists("no_ocaml_comments")
  28. if (has("comments"))
  29. setlocal comments=sr:(*\ ,mb:\ ,ex:*)
  30. setlocal comments^=sr:(**,mb:\ \ ,ex:*)
  31. setlocal fo=cqort
  32. endif
  33. endif
  34. " Only define the function once.
  35. if exists("*GetOCamlIndent")
  36. finish
  37. endif
  38. " Define some patterns:
  39. let s:beflet = '^\s*\(initializer\|method\|try\)\|\(\<\(begin\|do\|else\|in\|then\|try\)\|->\|<-\|=\|;\|(\)\s*$'
  40. let s:letpat = '^\s*\(let\|type\|module\|class\|open\|exception\|val\|include\|external\)\>'
  41. let s:letlim = '\(\<\(sig\|struct\)\|;;\)\s*$'
  42. let s:lim = '^\s*\(exception\|external\|include\|let\|module\|open\|type\|val\)\>'
  43. let s:module = '\<\%(begin\|sig\|struct\|object\)\>'
  44. let s:obj = '^\s*\(constraint\|inherit\|initializer\|method\|val\)\>\|\<\(object\|object\s*(.*)\)\s*$'
  45. let s:type = '^\s*\%(class\|let\|type\)\>.*='
  46. " Skipping pattern, for comments
  47. function! s:GetLineWithoutFullComment(lnum)
  48. let lnum = prevnonblank(a:lnum - 1)
  49. let lline = substitute(getline(lnum), '(\*.*\*)\s*$', '', '')
  50. while lline =~ '^\s*$' && lnum > 0
  51. let lnum = prevnonblank(lnum - 1)
  52. let lline = substitute(getline(lnum), '(\*.*\*)\s*$', '', '')
  53. endwhile
  54. return lnum
  55. endfunction
  56. " Indent for ';;' to match multiple 'let'
  57. function! s:GetInd(lnum, pat, lim)
  58. let llet = search(a:pat, 'bW')
  59. let old = indent(a:lnum)
  60. while llet > 0
  61. let old = indent(llet)
  62. let nb = s:GetLineWithoutFullComment(llet)
  63. if getline(nb) =~ a:lim
  64. return old
  65. endif
  66. let llet = search(a:pat, 'bW')
  67. endwhile
  68. return old
  69. endfunction
  70. " Indent pairs
  71. function! s:FindPair(pstart, pmid, pend)
  72. call search(a:pend, 'bW')
  73. return indent(searchpair(a:pstart, a:pmid, a:pend, 'bWn', 'synIDattr(synID(line("."), col("."), 0), "name") =~? "string\\|comment"'))
  74. endfunction
  75. " Indent 'let'
  76. function! s:FindLet(pstart, pmid, pend)
  77. call search(a:pend, 'bW')
  78. return indent(searchpair(a:pstart, a:pmid, a:pend, 'bWn', 'synIDattr(synID(line("."), col("."), 0), "name") =~? "string\\|comment" || getline(".") =~ "^\\s*let\\>.*=.*\\<in\\s*$" || getline(prevnonblank(".") - 1) =~ s:beflet'))
  79. endfunction
  80. function! GetOCamlIndent()
  81. " Find a non-commented line above the current line.
  82. let lnum = s:GetLineWithoutFullComment(v:lnum)
  83. " At the start of the file use zero indent.
  84. if lnum == 0
  85. return 0
  86. endif
  87. let ind = indent(lnum)
  88. let lline = substitute(getline(lnum), '(\*.*\*)\s*$', '', '')
  89. " Return double 'shiftwidth' after lines matching:
  90. if lline =~ '^\s*|.*->\s*$'
  91. return ind + 2 * shiftwidth()
  92. endif
  93. let line = getline(v:lnum)
  94. " Indent if current line begins with 'end':
  95. if line =~ '^\s*end\>'
  96. return s:FindPair(s:module, '','\<end\>')
  97. " Indent if current line begins with 'done' for 'do':
  98. elseif line =~ '^\s*done\>'
  99. return s:FindPair('\<do\>', '','\<done\>')
  100. " Indent if current line begins with '}' or '>}':
  101. elseif line =~ '^\s*\(\|>\)}'
  102. return s:FindPair('{', '','}')
  103. " Indent if current line begins with ']', '|]' or '>]':
  104. elseif line =~ '^\s*\(\||\|>\)\]'
  105. return s:FindPair('\[', '','\]')
  106. " Indent if current line begins with ')':
  107. elseif line =~ '^\s*)'
  108. return s:FindPair('(', '',')')
  109. " Indent if current line begins with 'let':
  110. elseif line =~ '^\s*let\>'
  111. if lline !~ s:lim . '\|' . s:letlim . '\|' . s:beflet
  112. return s:FindLet(s:type, '','\<let\s*$')
  113. endif
  114. " Indent if current line begins with 'class' or 'type':
  115. elseif line =~ '^\s*\(class\|type\)\>'
  116. if lline !~ s:lim . '\|\<and\s*$\|' . s:letlim
  117. return s:FindLet(s:type, '','\<\(class\|type\)\s*$')
  118. endif
  119. " Indent for pattern matching:
  120. elseif line =~ '^\s*|'
  121. if lline !~ '^\s*\(|[^\]]\|\(match\|type\|with\)\>\)\|\<\(function\|parser\|private\|with\)\s*$'
  122. call search('|', 'bW')
  123. return indent(searchpair('^\s*\(match\|type\)\>\|\<\(function\|parser\|private\|with\)\s*$', '', '^\s*|', 'bWn', 'synIDattr(synID(line("."), col("."), 0), "name") =~? "string\\|comment" || getline(".") !~ "^\\s*|.*->"'))
  124. endif
  125. " Indent if current line begins with ';;':
  126. elseif line =~ '^\s*;;'
  127. if lline !~ ';;\s*$'
  128. return s:GetInd(v:lnum, s:letpat, s:letlim)
  129. endif
  130. " Indent if current line begins with 'in':
  131. elseif line =~ '^\s*in\>'
  132. if lline !~ '^\s*\(let\|and\)\>'
  133. return s:FindPair('\<let\>', '', '\<in\>')
  134. endif
  135. " Indent if current line begins with 'else':
  136. elseif line =~ '^\s*else\>'
  137. if lline !~ '^\s*\(if\|then\)\>'
  138. return s:FindPair('\<if\>', '', '\<else\>')
  139. endif
  140. " Indent if current line begins with 'then':
  141. elseif line =~ '^\s*then\>'
  142. if lline !~ '^\s*\(if\|else\)\>'
  143. return s:FindPair('\<if\>', '', '\<then\>')
  144. endif
  145. " Indent if current line begins with 'and':
  146. elseif line =~ '^\s*and\>'
  147. if lline !~ '^\s*\(and\|let\|type\)\>\|\<end\s*$'
  148. return ind - shiftwidth()
  149. endif
  150. " Indent if current line begins with 'with':
  151. elseif line =~ '^\s*with\>'
  152. if lline !~ '^\s*\(match\|try\)\>'
  153. return s:FindPair('\<\%(match\|try\)\>', '','\<with\>')
  154. endif
  155. " Indent if current line begins with 'exception', 'external', 'include' or
  156. " 'open':
  157. elseif line =~ '^\s*\(exception\|external\|include\|open\)\>'
  158. if lline !~ s:lim . '\|' . s:letlim
  159. call search(line)
  160. return indent(search('^\s*\(\(exception\|external\|include\|open\|type\)\>\|val\>.*:\)', 'bW'))
  161. endif
  162. " Indent if current line begins with 'val':
  163. elseif line =~ '^\s*val\>'
  164. if lline !~ '^\s*\(exception\|external\|include\|open\)\>\|' . s:obj . '\|' . s:letlim
  165. return indent(search('^\s*\(\(exception\|include\|initializer\|method\|open\|type\|val\)\>\|external\>.*:\)', 'bW'))
  166. endif
  167. " Indent if current line begins with 'constraint', 'inherit', 'initializer'
  168. " or 'method':
  169. elseif line =~ '^\s*\(constraint\|inherit\|initializer\|method\)\>'
  170. if lline !~ s:obj
  171. return indent(search('\<\(object\|object\s*(.*)\)\s*$', 'bW')) + shiftwidth()
  172. endif
  173. endif
  174. " Add a 'shiftwidth' after lines ending with:
  175. if lline =~ '\(:\|=\|->\|<-\|(\|\[\|{\|{<\|\[|\|\[<\|\<\(begin\|do\|else\|fun\|function\|functor\|if\|initializer\|object\|parser\|private\|sig\|struct\|then\|try\)\|\<object\s*(.*)\)\s*$'
  176. let ind = ind + shiftwidth()
  177. " Back to normal indent after lines ending with ';;':
  178. elseif lline =~ ';;\s*$' && lline !~ '^\s*;;'
  179. let ind = s:GetInd(v:lnum, s:letpat, s:letlim)
  180. " Back to normal indent after lines ending with 'end':
  181. elseif lline =~ '\<end\s*$'
  182. let ind = s:FindPair(s:module, '','\<end\>')
  183. " Back to normal indent after lines ending with 'in':
  184. elseif lline =~ '\<in\s*$' && lline !~ '^\s*in\>'
  185. let ind = s:FindPair('\<let\>', '', '\<in\>')
  186. " Back to normal indent after lines ending with 'done':
  187. elseif lline =~ '\<done\s*$'
  188. let ind = s:FindPair('\<do\>', '','\<done\>')
  189. " Back to normal indent after lines ending with '}' or '>}':
  190. elseif lline =~ '\(\|>\)}\s*$'
  191. let ind = s:FindPair('{', '','}')
  192. " Back to normal indent after lines ending with ']', '|]' or '>]':
  193. elseif lline =~ '\(\||\|>\)\]\s*$'
  194. let ind = s:FindPair('\[', '','\]')
  195. " Back to normal indent after comments:
  196. elseif lline =~ '\*)\s*$'
  197. call search('\*)', 'bW')
  198. let ind = indent(searchpair('(\*', '', '\*)', 'bWn', 'synIDattr(synID(line("."), col("."), 0), "name") =~? "string"'))
  199. " Back to normal indent after lines ending with ')':
  200. elseif lline =~ ')\s*$'
  201. let ind = s:FindPair('(', '',')')
  202. " If this is a multiline comment then align '*':
  203. elseif lline =~ '^\s*(\*' && line =~ '^\s*\*'
  204. let ind = ind + 1
  205. else
  206. " Don't change indentation of this line
  207. " for new lines (indent==0) use indentation of previous line
  208. " This is for preventing removing indentation of these args:
  209. " let f x =
  210. " let y = x + 1 in
  211. " Printf.printf
  212. " "o" << here
  213. " "oeuth" << don't touch indentation
  214. let i = indent(v:lnum)
  215. return i == 0 ? ind : i
  216. endif
  217. " Subtract a 'shiftwidth' after lines matching 'match ... with parser':
  218. if lline =~ '\<match\>.*\<with\>\s*\<parser\s*$'
  219. let ind = ind - shiftwidth()
  220. endif
  221. return ind
  222. endfunction
  223. " vim:sw=2