julia.vim 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. " Vim filetype plugin file
  2. " Language: Julia
  3. " Maintainer: Carlo Baldassi <carlobaldassi@gmail.com>
  4. " Homepage: https://github.com/JuliaEditorSupport/julia-vim
  5. " Last Change: 2014 may 29
  6. " adapted from upstream 2021 Aug 4
  7. if exists("b:did_ftplugin")
  8. finish
  9. endif
  10. let b:did_ftplugin = 1
  11. let s:save_cpo = &cpo
  12. set cpo-=C
  13. setlocal include=^\\s*\\%(reload\\\|include\\)\\>
  14. setlocal suffixesadd=.jl
  15. setlocal comments=:#
  16. setlocal commentstring=#\ %s
  17. setlocal cinoptions+=#1
  18. setlocal define=^\\s*macro\\>
  19. setlocal fo-=t fo+=croql
  20. let b:julia_vim_loaded = 1
  21. let b:undo_ftplugin = "setlocal include< suffixesadd< comments< commentstring<"
  22. \ . " define< fo< shiftwidth< expandtab< indentexpr< indentkeys< cinoptions< completefunc<"
  23. \ . " | unlet! b:julia_vim_loaded"
  24. " MatchIt plugin support
  25. if exists("loaded_matchit")
  26. let b:match_ignorecase = 0
  27. " note: begin_keywords must contain all blocks, in order
  28. " for nested-structures-skipping to work properly
  29. " note: 'mutable struct' and 'struct' are defined separately because
  30. " using \? puts the cursor on 'struct' instead of 'mutable' for some reason
  31. let b:julia_begin_keywords = '\%(\.\s*\|@\)\@<!\<\%(function\|macro\|begin\|mutable\s\+struct\|\%(mutable\s\+\)\@<!struct\|\%(abstract\|primitive\)\s\+type\|let\|do\|\%(bare\)\?module\|quote\|if\|for\|while\|try\)\>'
  32. " note: the following regex not only recognizes macros, but also local/global keywords.
  33. " the purpose is recognizing things like `@inline myfunction()`
  34. " or `global myfunction(...)` etc, for matchit and block movement functionality
  35. let s:macro_regex = '\%(@\%([#(]\@!\S\)\+\|\<\%(local\|global\)\)\s\+'
  36. let s:nomacro = '\%(' . s:macro_regex . '\)\@<!'
  37. let s:yesmacro = s:nomacro . '\%('. s:macro_regex . '\)\+'
  38. let b:julia_begin_keywordsm = '\%(' . s:yesmacro . b:julia_begin_keywords . '\)\|'
  39. \ . '\%(' . s:nomacro . b:julia_begin_keywords . '\)'
  40. let b:julia_end_keywords = '\<end\>'
  41. " note: this function relies heavily on the syntax file
  42. function! JuliaGetMatchWords()
  43. let [l,c] = [line('.'),col('.')]
  44. let attr = synIDattr(synID(l, c, 1),"name")
  45. let c1 = c
  46. while attr == 'juliaMacro' || expand('<cword>') =~# '\<\%(global\|local\)\>'
  47. normal! W
  48. if line('.') > l || col('.') == c1
  49. call cursor(l, c)
  50. return ''
  51. endif
  52. let attr = synIDattr(synID(l, col('.'), 1),"name")
  53. let c1 = col('.')
  54. endwhile
  55. call cursor(l, c)
  56. if attr == 'juliaConditional'
  57. return b:julia_begin_keywordsm . ':\<\%(elseif\|else\)\>:' . b:julia_end_keywords
  58. elseif attr =~# '\<\%(juliaRepeat\|juliaRepKeyword\)\>'
  59. return b:julia_begin_keywordsm . ':\<\%(break\|continue\)\>:' . b:julia_end_keywords
  60. elseif attr == 'juliaBlKeyword'
  61. return b:julia_begin_keywordsm . ':' . b:julia_end_keywords
  62. elseif attr == 'juliaException'
  63. return b:julia_begin_keywordsm . ':\<\%(catch\|finally\)\>:' . b:julia_end_keywords
  64. endif
  65. return '\<\>:\<\>'
  66. endfunction
  67. let b:match_words = 'JuliaGetMatchWords()'
  68. " we need to skip everything within comments, strings and
  69. " the 'begin' and 'end' keywords when they are used as a range rather than as
  70. " the delimiter of a block
  71. let b:match_skip = 'synIDattr(synID(line("."),col("."),0),"name") =~# '
  72. \ . '"\\<julia\\%(Comprehension\\%(For\\|If\\)\\|RangeKeyword\\|Comment\\%([LM]\\|Delim\\)\\|\\%([bs]\\|Shell\\|Printf\\|Doc\\)\\?String\\|StringPrefixed\\|DocStringM\\(Raw\\)\\?\\|RegEx\\|SymbolS\\?\\|Dotted\\)\\>"'
  73. let b:undo_ftplugin = b:undo_ftplugin
  74. \ . " | unlet! b:match_words b:match_skip b:match_ignorecase"
  75. \ . " | unlet! b:julia_begin_keywords b:julia_end_keywords"
  76. \ . " | delfunction JuliaGetMatchWords"
  77. endif
  78. let &cpo = s:save_cpo
  79. unlet s:save_cpo