dmd.vim 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. "============================================================================
  2. "File: d.vim
  3. "Description: Syntax checking plugin for syntastic.vim
  4. "Maintainer: Alfredo Di Napoli <alfredo dot dinapoli at gmail dot com>
  5. "License: Based on the original work of Gregor Uhlenheuer and his
  6. " cpp.vim checker so credits are dued.
  7. " THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  8. " EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  9. " OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  10. " NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  11. " HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  12. " WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  13. " FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  14. " OTHER DEALINGS IN THE SOFTWARE.
  15. "
  16. "============================================================================
  17. if exists('g:loaded_syntastic_d_dmd_checker')
  18. finish
  19. endif
  20. let g:loaded_syntastic_d_dmd_checker = 1
  21. if !exists('g:syntastic_d_compiler_options')
  22. let g:syntastic_d_compiler_options = ''
  23. endif
  24. if !exists('g:syntastic_d_use_dub')
  25. let g:syntastic_d_use_dub = 1
  26. endif
  27. if !exists('g:syntastic_d_dub_exec')
  28. let g:syntastic_d_dub_exec = 'dub'
  29. endif
  30. let s:save_cpo = &cpo
  31. set cpo&vim
  32. function! SyntaxCheckers_d_dmd_IsAvailable() dict " {{{1
  33. if !exists('g:syntastic_d_compiler')
  34. let g:syntastic_d_compiler = self.getExec()
  35. endif
  36. call self.log('g:syntastic_d_compiler =', g:syntastic_d_compiler)
  37. return executable(expand(g:syntastic_d_compiler, 1))
  38. endfunction " }}}1
  39. function! SyntaxCheckers_d_dmd_GetLocList() dict " {{{1
  40. if !exists('g:syntastic_d_include_dirs')
  41. let g:syntastic_d_include_dirs = s:GetIncludes(self, expand('%:p:h'))
  42. endif
  43. return syntastic#c#GetLocList('d', 'dmd', {
  44. \ 'errorformat':
  45. \ '%-G%f:%s:,%f(%l): %m,' .
  46. \ '%f:%l: %m',
  47. \ 'main_flags': '-c -of' . syntastic#util#DevNull(),
  48. \ 'header_names': '\m\.di$' })
  49. endfunction " }}}1
  50. " Utilities {{{1
  51. function! s:GetIncludes(checker, base) " {{{2
  52. let includes = []
  53. if g:syntastic_d_use_dub && !exists('s:dub_ok')
  54. let s:dub_ok = s:ValidateDub(a:checker)
  55. endif
  56. if g:syntastic_d_use_dub && s:dub_ok
  57. let where = escape(a:base, ' ') . ';'
  58. let old_suffixesadd = &suffixesadd
  59. let dirs = syntastic#util#unique(map(filter(
  60. \ findfile('dub.json', where, -1) +
  61. \ findfile('dub.sdl', where, -1) +
  62. \ findfile('package.json', where, -1),
  63. \ 'filereadable(v:val)'), 'fnamemodify(v:val, ":h")'))
  64. let &suffixesadd = old_suffixesadd
  65. call a:checker.log('using dub: looking for includes in', dirs)
  66. for dir in dirs
  67. try
  68. execute 'silent lcd ' . fnameescape(dir)
  69. let paths = split(syntastic#util#system(syntastic#util#shescape(g:syntastic_d_dub_exec) . ' describe --import-paths'), "\n")
  70. silent lcd -
  71. if v:shell_error == 0
  72. call extend(includes, paths)
  73. call a:checker.log('using dub: found includes', paths)
  74. endif
  75. catch /\m^Vim\%((\a\+)\)\=:E472/
  76. " evil directory is evil
  77. endtry
  78. endfor
  79. endif
  80. if empty(includes)
  81. let includes = filter(glob($HOME . '/.dub/packages/*', 1, 1), 'isdirectory(v:val)')
  82. call map(includes, 'isdirectory(v:val . "/source") ? v:val . "/source" : v:val')
  83. call add(includes, './source')
  84. endif
  85. return syntastic#util#unique(includes)
  86. endfunction " }}}2
  87. function! s:ValidateDub(checker) " {{{2
  88. let ok = 0
  89. if executable(g:syntastic_d_dub_exec)
  90. let command = syntastic#util#shescape(g:syntastic_d_dub_exec) . ' --version'
  91. let version_output = syntastic#util#system(command)
  92. call a:checker.log('getVersion: ' . string(command) . ': ' .
  93. \ string(split(version_output, "\n", 1)) .
  94. \ (v:shell_error ? ' (exit code ' . v:shell_error . ')' : '') )
  95. let parsed_ver = syntastic#util#parseVersion(version_output)
  96. call a:checker.log(g:syntastic_d_dub_exec . ' version =', parsed_ver)
  97. if len(parsed_ver)
  98. let ok = syntastic#util#versionIsAtLeast(parsed_ver, [0, 9, 24])
  99. endif
  100. endif
  101. return ok
  102. endfunction " }}}2
  103. " }}}1
  104. call g:SyntasticRegistry.CreateAndRegisterChecker({
  105. \ 'filetype': 'd',
  106. \ 'name': 'dmd' })
  107. let &cpo = s:save_cpo
  108. unlet s:save_cpo
  109. " vim: set sw=4 sts=4 et fdm=marker: