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

155 lines
3.7 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 = []
2018-06-14 06:31:12 -04:00
let l:cmd = ['motion',
\ '-format', 'vim',
\ '-mode', 'decls',
\ '-include', go#config#DeclsIncludes(),
\ ]
2016-06-26 07:12:36 -04:00
call go#cmd#autowrite()
if s:mode == 0
" current file mode
2018-06-14 06:31:12 -04:00
let l:fname = expand("%:p")
2016-06-26 07:12:36 -04:00
if exists('s:target')
2018-06-14 06:31:12 -04:00
let l:fname = s:target
2016-06-26 07:12:36 -04:00
endif
2018-06-14 06:31:12 -04:00
let cmd += ['-file', l:fname]
2016-06-26 07:12:36 -04:00
else
" all functions mode
2018-06-14 06:31:12 -04:00
let l:dir = expand("%:p:h")
2016-06-26 07:12:36 -04:00
if exists('s:target')
2018-06-14 06:31:12 -04:00
let l:dir = s:target
2016-06-26 07:12:36 -04:00
endif
2018-06-14 06:31:12 -04:00
let cmd += ['-dir', l:dir]
2016-06-26 07:12:36 -04:00
endif
2018-06-14 06:31:12 -04:00
let [l:out, l:err] = go#util#Exec(l:cmd)
if l:err
call go#util#EchoError(l:out)
2016-06-26 07:12:36 -04:00
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,
2018-07-19 08:52:53 -04:00
\ fnamemodify(decl.filename, ":."),
2016-06-26 07:12:36 -04:00
\ 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