dmd.vim 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. "============================================================================
  2. "File: d.vim
  3. "Description: Syntax checking plugin for syntastic
  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. let buf = bufnr('')
  41. if !exists('g:syntastic_d_include_dirs')
  42. let g:syntastic_d_include_dirs = s:GetIncludes(self, fnamemodify(bufname(buf), ':p:h'))
  43. endif
  44. return syntastic#c#GetLocList('d', 'dmd', {
  45. \ 'errorformat':
  46. \ '%-G%f:%s:,%f(%l): %m,' .
  47. \ '%f:%l: %m',
  48. \ 'main_flags': '-c -of' . syntastic#util#DevNull(),
  49. \ 'header_names': '\m\.di$' })
  50. endfunction " }}}1
  51. " Utilities {{{1
  52. function! s:GetIncludes(checker, base) " {{{2
  53. let includes = []
  54. if g:syntastic_d_use_dub && !exists('s:dub_ok')
  55. let s:dub_ok = s:ValidateDub(a:checker)
  56. endif
  57. if g:syntastic_d_use_dub && s:dub_ok
  58. let where = escape(a:base, ' ') . ';'
  59. let old_suffixesadd = &suffixesadd
  60. let dirs = syntastic#util#unique(map(filter(
  61. \ findfile('dub.json', where, -1) +
  62. \ findfile('dub.sdl', where, -1) +
  63. \ findfile('package.json', where, -1),
  64. \ 'filereadable(v:val)'), 'fnamemodify(v:val, ":h")'))
  65. let &suffixesadd = old_suffixesadd
  66. call a:checker.log('using dub: looking for includes in', dirs)
  67. for dir in dirs
  68. try
  69. execute 'silent lcd ' . fnameescape(dir)
  70. let paths = split(syntastic#util#system(syntastic#util#shescape(g:syntastic_d_dub_exec) . ' describe --import-paths'), "\n")
  71. silent lcd -
  72. if v:shell_error == 0
  73. call extend(includes, paths)
  74. call a:checker.log('using dub: found includes', paths)
  75. endif
  76. catch /\m^Vim\%((\a\+)\)\=:E472/
  77. " evil directory is evil
  78. endtry
  79. endfor
  80. endif
  81. if empty(includes)
  82. let includes = filter(glob($HOME . '/.dub/packages/*', 1, 1), 'isdirectory(v:val)')
  83. call map(includes, 'isdirectory(v:val . "/source") ? v:val . "/source" : v:val')
  84. call add(includes, './source')
  85. endif
  86. return syntastic#util#unique(includes)
  87. endfunction " }}}2
  88. function! s:ValidateDub(checker) " {{{2
  89. let ok = 0
  90. if executable(g:syntastic_d_dub_exec)
  91. let command = syntastic#util#shescape(g:syntastic_d_dub_exec) . ' --version'
  92. let version_output = syntastic#util#system(command)
  93. call a:checker.log('getVersion: ' . string(command) . ': ' .
  94. \ string(split(version_output, "\n", 1)) .
  95. \ (v:shell_error ? ' (exit code ' . v:shell_error . ')' : '') )
  96. let parsed_ver = syntastic#util#parseVersion(version_output)
  97. call a:checker.log(g:syntastic_d_dub_exec . ' version =', parsed_ver)
  98. if len(parsed_ver)
  99. let ok = syntastic#util#versionIsAtLeast(parsed_ver, [0, 9, 24])
  100. endif
  101. endif
  102. return ok
  103. endfunction " }}}2
  104. " }}}1
  105. call g:SyntasticRegistry.CreateAndRegisterChecker({
  106. \ 'filetype': 'd',
  107. \ 'name': 'dmd' })
  108. let &cpo = s:save_cpo
  109. unlet s:save_cpo
  110. " vim: set sw=4 sts=4 et fdm=marker: