Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add quickfix plugin #930

Merged
merged 16 commits into from Oct 28, 2017
2 changes: 1 addition & 1 deletion autoload/SpaceVim.vim
Expand Up @@ -376,7 +376,7 @@ let g:spacevim_enable_vimfiler_gitstatus = 0
" Enable/Disable filetypeicon column in vimfiler buffer, default is 0.
let g:spacevim_enable_vimfiler_filetypeicon = 0
let g:spacevim_smartcloseignorewin = ['__Tagbar__' , 'vimfiler:default']
let g:spacevim_smartcloseignoreft = ['help', 'tagbar', 'vimfiler', 'SpaceVimRunner']
let g:spacevim_smartcloseignoreft = ['help', 'tagbar', 'vimfiler', 'SpaceVimRunner', 'SpaceVimQuickFix']
let g:spacevim_altmoveignoreft = ['Tagbar' , 'vimfiler']
let g:spacevim_enable_javacomplete2_py = 0
let g:spacevim_src_root = 'E:\sources\'
Expand Down
2 changes: 2 additions & 0 deletions autoload/SpaceVim/layers/core/statusline.vim
Expand Up @@ -257,6 +257,8 @@ function! SpaceVim#layers#core#statusline#get(...) abort
return '%#SpaceVim_statusline_a# Runner %#SpaceVim_statusline_a_SpaceVim_statusline_b# %{SpaceVim#plugins#runner#status()}'
elseif &filetype ==# 'VimMailClient'
return '%#SpaceVim_statusline_a# VimMail %#SpaceVim_statusline_a_SpaceVim_statusline_b# %{mail#client#win#status().dir}'
elseif &filetype ==# 'SpaceVimQuickFix'
return '%#SpaceVim_statusline_a# SpaceVimQuickFix %#SpaceVim_statusline_a_SpaceVim_statusline_b#'
endif
if a:0 > 0
return s:active()
Expand Down
50 changes: 33 additions & 17 deletions autoload/SpaceVim/plugins/pmd.vim
Expand Up @@ -28,11 +28,11 @@ let s:options = {
\ }

if !exists('Pmd_Cmd')
let g:Pmd_Cmd = ['pmd']
let g:Pmd_Cmd = ['pmd']
endif

if !exists('Pmd_Rulesets')
let g:Pmd_Rulesets = ["-R", "java-basic,java-design", "-property", "xsltFilename=my-own.xs"]
let g:Pmd_Rulesets = ["-R", "java-basic,java-design", "-property", "xsltFilename=my-own.xs"]
endif

if !exists('Pmd_silent_stderr')
Expand All @@ -43,26 +43,36 @@ endif

let s:JOB = SpaceVim#api#import('job')
let s:CMD = SpaceVim#api#import('vim#command')
let s:STRING = SpaceVim#api#import('data#string')

" set APIs

let s:CMD.options = s:options

let s:rst = []


let s:parserdir = ''
" /home/wsdjeg/sources/Mysql.vim/libs/mysqlvim/src/main/java/com/wsdjeg/mysqlvim/MysqlVi.java:18:^IDocument empty method body
" @vimlint(EVL103, 1, a:id)
" @vimlint(EVL103, 1, a:data)
" @vimlint(EVL103, 1, a:event)
function! s:on_pmd_stdout(id, data, event) abort
for data in a:data
let info = split(data, '\:\d\+\:')
if len(info) == 2
let [fname, text] = info
let lnum = matchstr(data, '\:\d\+\:')[1:-2]
call add(s:rst, {
\ 'filename' : fnamemodify(fname, ':p'),
\ 'lnum' : lnum,
\ 'text' : text,
\ })
endif
endfor
for data in a:data
let info = split(data, '\:\d\+\:')
if len(info) == 2
let [fname, text] = info
let text = s:STRING.trim(text)
let lnum = matchstr(data, '\:\d\+\:')[1:-2]
call add(s:rst, {
\ 'filename' : fnamemodify(fname, ':p'),
\ 'abbr' : substitute(fname, s:parserdir, '', 'g'),
\ 'lnum' : lnum,
\ 'col' : 0,
\ 'text' : text,
\ })
endif
endfor
endfunction

function! s:on_pmd_stderr(id, data, event) abort
Expand All @@ -73,11 +83,14 @@ function! s:on_pmd_stderr(id, data, event) abort
endfunction

function! s:on_pmd_exit(id, data, event) abort
call setqflist(s:rst)
let s:rst = []
copen
call SpaceVim#plugins#quickfix#setqflist(s:rst)
call SpaceVim#plugins#quickfix#openwin()
endfunction

" @vimlint(EVL103, 0, a:id)
" @vimlint(EVL103, 0, a:data)
" @vimlint(EVL103, 0, a:event)

function! SpaceVim#plugins#pmd#run(...)
let argv = g:Pmd_Cmd + a:000
if index(a:000, '-R') == -1
Expand All @@ -86,7 +99,10 @@ function! SpaceVim#plugins#pmd#run(...)
if index(argv, '-d') == -1
echohl ErrorMsg | echo 'you need to run PMD with -d option!'
return
else
let s:parserdir = fnamemodify(argv[index(argv, '-d') + 1], ':p')
endif

call s:JOB.start(argv,
\ {
\ 'on_stdout' : function('s:on_pmd_stdout'),
Expand Down
164 changes: 164 additions & 0 deletions autoload/SpaceVim/plugins/quickfix.vim
@@ -0,0 +1,164 @@
let s:qflist = []

let s:qf_title = ''

let s:filestack = []

let s:qf_index = 0

let s:qf_bufnr = -1

" like setqflist()


function! SpaceVim#plugins#quickfix#setqflist(list, ...)
let action = get(a:000, 0, ' ')
if action ==# 'a'
call extend(s:qflist, a:list)
elseif action ==# 'r'
let s:qflist = a:list
elseif empty(action) || action ==# ' '
let s:qflist = a:list
else
echohl Error
echo 'wrong args for SpaceVim setqflist: ' . action
echohl NONE
endif
let what = get(a:000, 1, {})
if has_key(what, 'title')
let s:qf_title = what.title
endif
endfunction


function! SpaceVim#plugins#quickfix#getqflist()

return s:qflist

endfunction


function! SpaceVim#plugins#quickfix#next()

let s:qf_index += 1
let file = get(s:filestack, s:qf_index, {})
if !empty(file)
wincmd p
exe 'e' file.name
exe file.lnum
endif

endfunction


function! SpaceVim#plugins#quickfix#pre()

let s:qf_index -= 1
let file = get(s:filestack, s:qf_index, {})
if !empty(file)
wincmd p
exe 'e' file.name
exe file.lnum
endif

endfunction


function! SpaceVim#plugins#quickfix#enter()
let s:qf_index = line('.') - 1
let file = get(s:filestack, s:qf_index, {})
if !empty(file)
wincmd p
exe 'e' file.name
exe file.lnum
endif
endfunction

let s:BUFFER = SpaceVim#api#import('vim#buffer')
function! SpaceVim#plugins#quickfix#openwin()
call s:BUFFER.open({
\ 'bufname' : '__quickfix__',
\ 'cmd' : 'setl buftype=nofile bufhidden=wipe filetype=SpaceVimQuickFix nomodifiable nowrap nobuflisted',
\ 'mode' : 'rightbelow split ',
\ })
let s:qf_bufnr = bufnr('%')
call s:BUFFER.resize(10, '')
call s:mappings()
call s:update_stack()
let lines = []
for file in s:qflist
let line = ''
if has_key(file, 'abbr')
let line .= file.abbr
elseif has_key(file, 'filename')
let line .= file.name
elseif has_key(file, 'bufnr')
let line .= bufname(file.bufnr)
endif
let line .= ' '
if has_key(file, 'type')
let line .= '|' . file.type . '| '
endif
let line .= file.text
call add(lines, line)
endfor
call setbufvar(bufnr('%'),'&ma', 1)
call s:BUFFER.buf_set_lines(bufnr('%'), 0, len(lines) - 1, 0, lines)
call setbufvar(bufnr('%'),'&ma', 0)
endfunction

function! s:mappings() abort
nnoremap <buffer><silent> <cr> :call SpaceVim#plugins#quickfix#enter()<cr>
nnoremap <buffer><silent> q :close<cr>
endfunction

function! s:update_stack() abort
let s:filestack = []
for item in s:qflist
let file = {}
if has_key(item, 'bufnr') && bufexists(item.bufnr)
let file.name = bufname(item.bufnr)
elseif has_key(item, 'bufname')
let file.name = item.bufname
elseif has_key(item, 'filename')
let file.name = item.filename
else
let file.name = ''
endif
let file.lnum = item.lnum
let file.col = item.col
call add(s:filestack, file)
endfor
endfunction

function! SpaceVim#plugins#quickfix#swapqf() abort
try
cclose
catch
endtry
call SpaceVim#plugins#quickfix#setqflist(getqflist())
call SpaceVim#plugins#quickfix#openwin()
endfunction

" :cclose only close quickfix windows on current tab, this func can close all
" qucikfix windows in all tabs when pass an argv to this func.

function! SpaceVim#plugins#quickfix#closewin(...) abort
let close_all = get(a:000, 1, 0)
let has_qf = bufexists('__quickfix__')
if !has_qf
return
endif
if close_all
else
call s:close_qfwin()
endif
endfunction

function! s:close_qfwin() abort
let wr = bufwinnr(s:qf_bufnr)
if wr > -1
exe wr . 'wincmd w'
close
endif
endfunction
5 changes: 2 additions & 3 deletions doc/SpaceVim.txt
Expand Up @@ -983,9 +983,8 @@ TMUX *SpaceVim-layer-tmux*

Adds integration between tmux and vim panes. Switch between panes seamlessly.
This layer is not added by default. To include it, add
`SpaceVim#layers#load('tmux')` to your `~/.SpaceVim.d/init.vim`. This layer
currently overwrites some SpaceVim keybinds including multiple cursors. If you
are having issues with <C-h> in a neovim buffer, see
`SpaceVim#layers#load('tmux')` to your `~/.SpaceVim.d/init.vim`. If you are
having issues with <C-h> in a neovim buffer, see
`https://github.com/neovim/neovim/issues/2048#issuecomment-78045837`

MAPPINGS
Expand Down
8 changes: 8 additions & 0 deletions syntax/SpaceVimQuickFix.vim
@@ -0,0 +1,8 @@
if exists("b:current_syntax")
finish
endif
let b:current_syntax = "SpaceVimQuickFix"
syntax case ignore
syn match FileName /^[^ ]*/

hi def link FileName String
13 changes: 13 additions & 0 deletions test/plugin/quickfix.vader
@@ -0,0 +1,13 @@
Execute ( SpaceVim plugin: quickfix ):
let qflist = [
\ {
\ 'abbr' : 'test-abbr',
\ 'filename' : '~/test/Foo.java',
\ 'lnum' : 1,
\ 'col' : 1,
\ 'type' : 'E',
\ 'text' : 'test-text'
\ }
\ ]
call SpaceVim#plugins#quickfix#setqflist(qflist)
AssertEqual SpaceVim#plugins#quickfix#getqflist()[0].abbr, 'test-abbr'