1
0
Fork 0
mirror of synced 2024-11-16 05:55:35 -05:00
ultimate-vim/sources_non_forked/vim-go/autoload/ctrlp/decls.vim

156 lines
3.8 KiB
VimL
Raw Normal View History

2016-03-20 14:01:44 -04:00
let s:go_decls_var = {
2016-06-26 07:12:36 -04:00
\ 'init': 'ctrlp#decls#init()',
\ 'exit': 'ctrlp#decls#exit()',
\ 'enter': 'ctrlp#decls#enter()',
\ 'accept': 'ctrlp#decls#accept',
\ 'lname': 'declarations',
\ 'sname': 'decls',
\ 'type': 'tabs',
\}
2016-03-20 14:01:44 -04:00
if exists('g:ctrlp_ext_vars') && !empty(g:ctrlp_ext_vars)
2016-06-26 07:12:36 -04:00
let g:ctrlp_ext_vars = add(g:ctrlp_ext_vars, s:go_decls_var)
2016-03-20 14:01:44 -04:00
else
2016-06-26 07:12:36 -04:00
let g:ctrlp_ext_vars = [s:go_decls_var]
2016-03-20 14:01:44 -04:00
endif
2016-12-27 09:46:49 -05:00
function! ctrlp#decls#init() abort
2016-06-26 07:12:36 -04:00
cal s:enable_syntax()
return s:decls
2016-03-20 14:01:44 -04:00
endfunction
2016-12-27 09:46:49 -05:00
function! ctrlp#decls#exit() abort
2016-06-26 07:12:36 -04:00
unlet! s:decls s:current_dir s:target
2016-03-20 14:01:44 -04:00
endfunction
" The action to perform on the selected string
" Arguments:
" a:mode the mode that has been chosen by pressing <cr> <c-v> <c-t> or <c-x>
" the values are 'e', 'v', 't' and 'h', respectively
" a:str the selected string
2016-12-27 09:46:49 -05:00
function! ctrlp#decls#accept(mode, str) abort
2016-06-26 07:12:36 -04:00
let cd = exists('*haslocaldir') && haslocaldir() ? 'lcd ' : 'cd '
let dir = getcwd()
try
" we jump to the file directory so we can get the fullpath via fnamemodify
" below
execute cd . s:current_dir
let vals = matchlist(a:str, '|\(.\{-}\):\(\d\+\):\(\d\+\)\s*\(.*\)|')
" i.e: main.go
2017-03-07 12:04:28 -05:00
let filename = vals[1]
2016-06-26 07:12:36 -04:00
let line = vals[2]
let col = vals[3]
" i.e: /Users/fatih/vim-go/main.go
let filepath = fnamemodify(filename, ":p")
" acceptile is a very versatile method,
call ctrlp#acceptfile(a:mode, filepath)
call cursor(line, col)
silent! norm! zvzz
finally
"jump back to old dir
execute cd . fnameescape(dir)
endtry
2016-03-20 14:01:44 -04:00
endfunction
2016-12-27 09:46:49 -05:00
function! ctrlp#decls#enter() abort
2016-06-26 07:12:36 -04:00
let s:current_dir = fnameescape(expand('%:p:h'))
let s:decls = []
let bin_path = go#path#CheckBinPath('motion')
if empty(bin_path)
return
endif
let command = printf("%s -format vim -mode decls", bin_path)
let command .= " -include ". get(g:, "go_decls_includes", "func,type")
call go#cmd#autowrite()
if s:mode == 0
" current file mode
let fname = expand("%:p")
if exists('s:target')
let fname = s:target
endif
let command .= printf(" -file %s", fname)
else
" all functions mode
let dir = expand("%:p:h")
if exists('s:target')
let dir = s:target
endif
let command .= printf(" -dir %s", dir)
endif
let out = go#util#System(command)
if go#util#ShellError() != 0
call go#util#EchoError(out)
return
endif
let result = eval(out)
if type(result) != 4 || !has_key(result, 'decls')
return
endif
let decls = result.decls
" find the maximum function name
let max_len = 0
for decl in decls
if len(decl.ident)> max_len
let max_len = len(decl.ident)
endif
endfor
for decl in decls
" paddings
let space = " "
for i in range(max_len - len(decl.ident))
let space .= " "
endfor
2017-03-07 12:04:28 -05:00
call add(s:decls, printf("%s\t%s |%s:%s:%s|\t%s",
2016-06-26 07:12:36 -04:00
\ decl.ident . space,
\ decl.keyword,
\ fnamemodify(decl.filename, ":t"),
\ decl.line,
\ decl.col,
\ decl.full,
\))
endfor
2016-03-20 14:01:44 -04:00
endfunc
2016-12-27 09:46:49 -05:00
function! s:enable_syntax() abort
2016-06-26 07:12:36 -04:00
if !(has('syntax') && exists('g:syntax_on'))
return
endif
2017-03-07 12:04:28 -05:00
syntax match CtrlPIdent '\zs\h\+\ze\s'
2016-06-26 07:12:36 -04:00
syntax match CtrlPKeyword '\zs[^\t|]\+\ze|[^|]\+:\d\+:\d\+|'
syntax match CtrlPFilename '|\zs[^|]\+:\d\+:\d\+\ze|'
syntax match CtrlPSignature '\zs\t.*\ze$' contains=CtrlPKeyWord,CtrlPFilename
highlight link CtrlPIdent Function
highlight link CtrlPKeyword Keyword
highlight link CtrlPFilename SpecialComment
highlight link CtrlPSignature Comment
2016-03-20 14:01:44 -04:00
endfunction
let s:id = g:ctrlp_builtins + len(g:ctrlp_ext_vars)
2016-12-27 09:46:49 -05:00
function! ctrlp#decls#cmd(mode, ...) abort
2016-06-26 07:12:36 -04:00
let s:mode = a:mode
if a:0 && !empty(a:1)
let s:target = a:1
endif
return s:id
2016-03-20 14:01:44 -04:00
endfunction
2016-06-26 07:12:36 -04:00
" vim: sw=2 ts=2 et