Merge pull request #632 from mbedna/master

Fix to enable using syntastic plugin with java projects using maven in cygwin
This commit is contained in:
LCD 47 2013-05-06 05:52:45 -07:00
commit d261751c77
2 changed files with 83 additions and 10 deletions

View File

@ -27,19 +27,44 @@ function! SyntaxCheckers_java_checkstyle_IsAvailable()
return executable('java') return executable('java')
endfunction endfunction
function! s:CygwinPath(path)
return substitute(system("cygpath -m ".a:path), '\%x00', '', 'g')
endfunction
function! s:RemoveCarriageReturn(line)
return substitute(a:line, '\r', '', 'g')
endfunction
function! s:RemoveCarriageReturns(errors)
for error in a:errors
let error['text'] = s:RemoveCarriageReturn(error['text'])
endfor
endfunction
function! SyntaxCheckers_java_checkstyle_GetLocList() function! SyntaxCheckers_java_checkstyle_GetLocList()
let fname = fnameescape(expand ( '%:p:h' ) . '/' . expand ( '%:t' ))
if has('win32unix')
let fname = s:CygwinPath(fname)
endif
let makeprg = syntastic#makeprg#build({ let makeprg = syntastic#makeprg#build({
\ 'exe': 'java', \ 'exe': 'java',
\ 'args': '-cp ' . g:syntastic_java_checkstyle_classpath . ' com.puppycrawl.tools.checkstyle.Main -c ' . g:syntastic_java_checkstyle_conf_file, \ 'args': '-cp ' . g:syntastic_java_checkstyle_classpath . ' com.puppycrawl.tools.checkstyle.Main -c ' . g:syntastic_java_checkstyle_conf_file,
\ 'fname': expand ( '%:p:h' ) . '/' . expand ( '%:t' ), \ 'fname': fname,
\ 'tail': '2>&1', \ 'tail': '2>&1',
\ 'subchecker': 'checkstyle' }) \ 'subchecker': 'checkstyle' })
" check style format " check style format
let errorformat = '%f:%l:%c:\ %m,%f:%l:\ %m' let errorformat = '%f:%l:%c:\ %m,%f:%l:\ %m'
let errors = SyntasticMake({ 'makeprg': makeprg, 'errorformat': errorformat })
return SyntasticMake({ 'makeprg': makeprg, 'errorformat': errorformat }) if has('win32unix')
call s:RemoveCarriageReturns(errors)
endif
return errors
endfunction endfunction
call g:SyntasticRegistry.CreateAndRegisterChecker({ call g:SyntasticRegistry.CreateAndRegisterChecker({

View File

@ -37,9 +37,15 @@ if !exists("g:syntastic_java_javac_delete_output")
let g:syntastic_java_javac_delete_output = 1 let g:syntastic_java_javac_delete_output = 1
endif endif
function! s:CygwinPath(path)
return substitute(system("cygpath -m ".a:path), '\%x00', '', 'g')
endfunction
if !exists("g:syntastic_java_javac_temp_dir") if !exists("g:syntastic_java_javac_temp_dir")
if has('win32') || has('win64') if has('win32') || has('win64')
let g:syntastic_java_javac_temp_dir = $TEMP."\\vim-syntastic-javac" let g:syntastic_java_javac_temp_dir = $TEMP."\\vim-syntastic-javac"
elseif has('win32unix')
let g:syntastic_java_javac_temp_dir=s:CygwinPath('/tmp/vim-syntastic-javac')
else else
let g:syntastic_java_javac_temp_dir = '/tmp/vim-syntastic-javac' let g:syntastic_java_javac_temp_dir = '/tmp/vim-syntastic-javac'
endif endif
@ -70,6 +76,16 @@ if !exists("g:syntastic_java_javac_maven_pom_classpath")
let g:syntastic_java_javac_maven_pom_classpath = '' let g:syntastic_java_javac_maven_pom_classpath = ''
endif endif
function! s:RemoveCarriageReturn(line)
return substitute(a:line, '\r', '', 'g')
endfunction
function! s:RemoveCarriageReturns(errors)
for error in a:errors
let error['text'] = s:RemoveCarriageReturn(error['text'])
endfor
endfunction
" recursively remove directory and all it's sub-directories " recursively remove directory and all it's sub-directories
function! s:RemoveDir(dir) function! s:RemoveDir(dir)
if isdirectory(a:dir) if isdirectory(a:dir)
@ -87,7 +103,7 @@ function! s:AddToClasspath(classpath,path)
return a:classpath return a:classpath
endif endif
if a:classpath != '' && a:path != '' if a:classpath != '' && a:path != ''
if has('win32') || has('win64') if has('win32') || has('win32unix') || has('win64')
return a:classpath . ";" . a:path return a:classpath . ";" . a:path
else else
return a:classpath . ":" . a:path return a:classpath . ":" . a:path
@ -150,7 +166,7 @@ function! s:GetMavenClasspath()
let class_path_next = 0 let class_path_next = 0
for line in mvn_classpath_output for line in mvn_classpath_output
if class_path_next == 1 if class_path_next == 1
let mvn_classpath = line let mvn_classpath = s:RemoveCarriageReturn(line)
break break
endif endif
if match(line,'Dependencies classpath:') >= 0 if match(line,'Dependencies classpath:') >= 0
@ -158,6 +174,7 @@ function! s:GetMavenClasspath()
endif endif
endfor endfor
let mvn_classpath = s:AddToClasspath(mvn_classpath,'target/classes') let mvn_classpath = s:AddToClasspath(mvn_classpath,'target/classes')
let mvn_classpath = s:AddToClasspath(mvn_classpath,'target/test-classes')
let g:syntastic_java_javac_maven_pom_cwd = getcwd() let g:syntastic_java_javac_maven_pom_cwd = getcwd()
let g:syntastic_java_javac_maven_pom_ftime = getftime('pom.xml') let g:syntastic_java_javac_maven_pom_ftime = getftime('pom.xml')
let g:syntastic_java_javac_maven_pom_classpath = mvn_classpath let g:syntastic_java_javac_maven_pom_classpath = mvn_classpath
@ -171,6 +188,23 @@ function! SyntaxCheckers_java_javac_IsAvailable()
return executable(g:syntastic_java_javac_executable) return executable(g:syntastic_java_javac_executable)
endfunction endfunction
function! s:MavenOutputDirectory()
if filereadable('pom.xml')
let output_dir = getcwd()
if match(expand( '%:p:h' ), "src.main.java") >= 0
let output_dir .= '/target/classes'
endif
if match(expand( '%:p:h' ), "src.test.java") >= 0
let output_dir .= '/target/test-classes'
endif
if has('win32unix')
let output_dir=s:CygwinPath(output_dir)
endif
return output_dir
endif
endfunction
function! SyntaxCheckers_java_javac_GetLocList() function! SyntaxCheckers_java_javac_GetLocList()
let javac_opts = g:syntastic_java_javac_options let javac_opts = g:syntastic_java_javac_options
@ -209,26 +243,35 @@ function! SyntaxCheckers_java_javac_GetLocList()
endfor endfor
if g:syntastic_java_javac_autoload_maven_classpath if g:syntastic_java_javac_autoload_maven_classpath
if !g:syntastic_java_javac_delete_output
let maven_output_dir = s:MavenOutputDirectory()
let javac_opts .= ' -d ' . maven_output_dir
endif
let maven_classpath = s:GetMavenClasspath() let maven_classpath = s:GetMavenClasspath()
let javac_classpath = s:AddToClasspath(javac_classpath,maven_classpath) let javac_classpath = s:AddToClasspath(javac_classpath,maven_classpath)
endif endif
if javac_classpath != '' if javac_classpath != ''
let javac_opts .= ' -cp ' . fnameescape(javac_classpath) let javac_opts .= ' -cp "' . fnameescape(javac_classpath) . '"'
endif endif
" path seperator " path seperator
if has('win32') || has('win64') if has('win32') || has('win32unix') || has('win64')
let sep = "\\" let sep = "\\"
else else
let sep = '/' let sep = '/'
endif endif
let fname = fnameescape(expand ( '%:p:h' ) . sep . expand ( '%:t' ))
if has('win32unix')
let fname = s:CygwinPath(fname)
endif
let makeprg = syntastic#makeprg#build({ let makeprg = syntastic#makeprg#build({
\ 'exe': g:syntastic_java_javac_executable, \ 'exe': g:syntastic_java_javac_executable,
\ 'args': javac_opts, \ 'args': javac_opts,
\ 'fname': fnameescape(expand ( '%:p:h' ) . sep . expand ( '%:t' )), \ 'fname': fname,
\ 'tail': '2>&1', \ 'tail': '2>&1',
\ 'subchecker': 'javac' }) \ 'subchecker': 'javac' })
@ -238,11 +281,16 @@ function! SyntaxCheckers_java_javac_GetLocList()
if g:syntastic_java_javac_delete_output if g:syntastic_java_javac_delete_output
silent! call mkdir(output_dir,'p') silent! call mkdir(output_dir,'p')
endif endif
let r = SyntasticMake({ 'makeprg': makeprg, 'errorformat': errorformat }) let errors = SyntasticMake({ 'makeprg': makeprg, 'errorformat': errorformat })
if has('win32unix')
call s:RemoveCarriageReturns(errors)
endif
if g:syntastic_java_javac_delete_output if g:syntastic_java_javac_delete_output
call s:RemoveDir(output_dir) call s:RemoveDir(output_dir)
endif endif
return r return errors
endfunction endfunction