1
0
mirror of https://github.com/SpaceVim/SpaceVim.git synced 2025-04-14 07:09:11 +08:00

chore(bundle): use bundle vim-lookup

This commit is contained in:
wsdjeg 2022-03-22 14:33:27 +08:00
parent 592e0ec9ef
commit 538fbac440
14 changed files with 516 additions and 1 deletions

View File

@ -70,7 +70,7 @@ function! SpaceVim#layers#lang#vim#plugins() abort
\ ['todesking/vint-syntastic', { 'on_ft' : 'vim'}],
\ ]
call add(plugins,['tweekmonster/exception.vim', {'merged' : 0}])
call add(plugins,['wsdjeg/vim-lookup', {'merged' : 0}])
call add(plugins,[g:_spacevim_root_dir . 'bundle/vim-lookup', {'merged' : 0}])
if !SpaceVim#layers#lsp#check_server('vimls') && !SpaceVim#layers#lsp#check_filetype('vim')
call add(plugins,['Shougo/neco-vim', { 'on_event' : 'InsertEnter', 'loadconf_before' : 1}])
if g:spacevim_autocomplete_method ==# 'asyncomplete'

View File

@ -18,6 +18,8 @@ let s:STRING = SpaceVim#api#import('data#string')
let s:CMP = SpaceVim#api#import('vim#compatible')
let s:VIM = SpaceVim#api#import('vim')
let s:LOGGER =SpaceVim#logger#derive('iedit')
let s:cursor_stack = []
let s:iedit_hi_info = [

2
bundle/vim-lookup/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
/test/vader.vim
/test/vim

View File

@ -0,0 +1,10 @@
dist: trusty
sudo: false
cache:
directories:
- test/vim
- test/vader.vim
script:
- test/run

View File

@ -0,0 +1,52 @@
[![Build Status](https://travis-ci.org/mhinz/vim-lookup.svg?branch=master)](https://travis-ci.org/mhinz/vim-lookup)
# vim-lookup
This plugin is meant for VimL programmers. It jumps to definitions of variables,
functions, and commands as if tags were used, without needing a tags file. It simply
uses your [runtimepath](https://neovim.io/doc/user/options.html#'rtp').
- [x] `s:var`
- [x] `s:func()`
- [x] `<sid>func()`
- [x] `autoload#foo#var`
- [x] `autoload#foo#func()`
- [x] `'autoload#foo#func'`
- [x] `Command`
Sometimes a function `foo#func()` is not found in `autoload/foo.vim` but
`plugin/foo.vim`. This case is handled as well.
It also works for global functions if they're defined or found in the current
file:
- [x] `GlobalFunc()`
- [x] `g:GlobalFunc()`
### Usage
- Use `lookup#lookup()` to jump to the defintion of the identifier under the
cursor.
- Use `lookup#pop()` (or the default mapping
[`<c-o>`](https://github.com/mhinz/vim-galore/#changelist-jumplist)) to jump
back.
### Configuration
```viml
autocmd FileType vim nnoremap <buffer><silent> <cr> :call lookup#lookup()<cr>
```
Alternatively, you can replace the default mappings Vim uses for
[tagstack](https://neovim.io/doc/user/tagsrch.html#tag-stack) navigation:
```viml
autocmd FileType vim nnoremap <buffer><silent> <c-]> :call lookup#lookup()<cr>
autocmd FileType vim nnoremap <buffer><silent> <c-t> :call lookup#pop()<cr>
```
### Other useful VimL plugins
- [exception.vim](https://github.com/tweekmonster/exception.vim)
- [helpful.vim](https://github.com/tweekmonster/helpful.vim)
- [vim-scriptease](https://github.com/tpope/vim-scriptease)

View File

@ -0,0 +1,198 @@
" lookup#lookup() {{{1
"
" Entry point. Map this function to your favourite keys.
"
" autocmd FileType vim nnoremap <buffer><silent> <cr> :call lookup#lookup()<cr>
"
function! lookup#lookup() abort
let dispatch = [
\ [function('s:find_local_var_def'), function('s:find_local_func_def')],
\ [function('s:find_autoload_var_def'), function('s:find_autoload_func_def')]]
let isk = &iskeyword
setlocal iskeyword+=:,<,>,#
let name = matchstr(getline('.'), '\k*\%'.col('.').'c\k*[("'']\?')
let plug = matchstr(getline('.'), '\c<plug>\k*\%'.col('.').'c\k*[("'']\?')
let &iskeyword = isk
let is_func = name =~ '($' ? 1 : 0
let could_be_funcref = name =~ '[''"]$' ? 1 : 0
let is_cmd = name =~# '\v^\u\w*>'
let name = matchstr(name, '\c\v^%(s:|\<sid\>)?\zs.{-}\ze[\("'']?$')
let is_auto = name =~ '#' ? 1 : 0
let position = s:getcurpos()
try
if is_cmd && s:find_local_cmd_def(name)
" Found command.
elseif !dispatch[is_auto][is_func](name) && !is_func && could_be_funcref
let is_func = 1
call dispatch[is_auto][is_func](name)
elseif !empty(plug) && s:find_plug_map_def(plug)
" Found plug.
endif
catch /^Vim\%((\a\+)\)\=:/
echohl ErrorMsg
" Strip off the :edit command prefix to make it look like a normal vim
" error message.
echomsg substitute(v:exception, "^[^:]*:", "", "")
echohl NONE
return 0
endtry
let didmove = position != s:getcurpos() ? 1 : 0
if didmove
call s:push(position, name)
else
echo 'No match'
return 0
endif
normal! zv
return didmove
endfunction
" lookup#pop() {{{1
function! lookup#pop()
if !has_key(w:, 'lookup_stack') || empty(w:lookup_stack)
echohl ErrorMsg
echo "lookup stack empty"
echohl NONE
return
endif
let pos = remove(w:lookup_stack, 0)
execute 'silent!' (bufexists(pos[0]) ? 'buffer' : 'edit') fnameescape(pos[0])
call cursor(pos[2:])
endfunction
" s:find_local_func_def() {{{1
function! s:find_local_func_def(funcname) abort
if search('\c\v<fu%[nction]!?\s+%(s:|\<sid\>)\zs\V'.a:funcname.'\>', 'bsw') != 0
return
endif
call s:jump_to_file_defining('function', a:funcname)
let fn = substitute(a:funcname, '^g:', '', '')
return search('\c\v<fu%[nction]!?\s+%(g:)?\zs\V'.fn.'\>', 'bsw')
endfunction
" s:find_local_cmd_def() {{{1
function! s:find_local_cmd_def(cmdname) abort
let pattern = '\c\v<com%[mand]!?\s+(-\w+.{-}\s+)*\zs\V'.a:cmdname.'\>'
if search(pattern, 'bsw') != 0
return
endif
call s:jump_to_file_defining('command', a:cmdname)
return search(pattern, 'bsw')
endfunction
" s:find_plug_map_def() {{{1
function! s:find_plug_map_def(plugname) abort
let pattern = '\c\v<[nvxsoilct]?(nore)?m%[ap]\s*(\<[bnseu]\w+\>\s*)*\s+\zs\V'.a:plugname.'\>'
if search(pattern, 'bsw') != 0
return
endif
call s:jump_to_file_defining('map', a:plugname)
return search(pattern, 'bsw')
endfunction
" s:jump_to_file_defining() {{{1
" Expects symbol_type = 'command' or 'function'
function! s:jump_to_file_defining(symbol_type, symbol_name) abort
let lang = v:lang
language message C
redir => location
silent! execute 'verbose ' a:symbol_type a:symbol_name
redir END
let failed = 0
if a:symbol_type == 'command'
let failed = location =~# 'No user-defined commands found'
endif
silent! execute 'language message' lang
if failed || location =~# 'E\d\{2,3}:'
return
endif
let matches = matchlist(location, '\v.*Last set from (.*) line (\d+)>')
execute 'silent edit +'. matches[2] matches[1]
endfunction
" s:find_local_var_def() {{{1
function! s:find_local_var_def(name) abort
return search('\c\v<let\s+s:\zs\V'.a:name.'\>', 'bsw')
endfunction
" s:find_autoload_func_def() {{{1
function! s:find_autoload_func_def(name) abort
let [path, func] = split(a:name, '.*\zs#')
let pattern = '\c\v<fu%[nction]!?\s+\zs\V'. path .'#'. func .'\>'
return s:find_autoload_def(path, pattern)
endfunction
" s:find_autoload_var_def() {{{1
function! s:find_autoload_var_def(name) abort
let [path, var] = split(a:name, '.*\zs#')
let pattern = '\c\v<let\s+\zs\V'. path .'#'. var .'\>'
return s:find_autoload_def(path, pattern)
endfunction
" s:find_autoload_def() {{{1
function! s:find_autoload_def(name, pattern) abort
for dir in ['autoload', 'plugin']
let path = printf('%s/%s.vim', dir, substitute(a:name, '#', '/', 'g'))
let aufiles = globpath(&runtimepath, path, '', 1)
if empty(aufiles) && exists('b:git_dir')
let aufiles = [fnamemodify(b:git_dir, ':h') .'/'. path]
endif
if empty(aufiles)
return search(a:pattern)
else
for file in aufiles
if !filereadable(file)
continue
endif
let lnum = match(readfile(file), a:pattern)
if lnum > -1
execute 'silent edit +'. (lnum+1) fnameescape(file)
call search(a:pattern)
return 1
break
endif
endfor
endif
endfor
return 0
endfunction
" s:push() {{{1
function! s:push(position, tagname) abort
call s:pushtagstack(a:position[1:], a:tagname)
if !has_key(w:, 'lookup_stack') || empty(w:lookup_stack)
let w:lookup_stack = [a:position]
return
endif
if w:lookup_stack[0] != a:position
call insert(w:lookup_stack, a:position)
endif
endfunction
" s:pushtagstack() {{{1
function! s:pushtagstack(curpos, tagname) abort
if !exists('*gettagstack') || !exists('*settagstack') || !has('patch-8.2.0077') " patch that adds 't' argument
" do nothing
return
endif
let item = {'bufnr': a:curpos[0], 'from': a:curpos, 'tagname': a:tagname}
let winid = win_getid()
let stack = gettagstack(winid)
let stack['items'] = [item]
call settagstack(winid, stack, 't')
endfunction
" s:getcurpos() {{{1
function! s:getcurpos() abort
let pos = getcurpos()
" getcurpos always returns bufnr 0.
let pos[0] = bufnr('%')
return [expand('%:p')] + pos
endfunction

View File

@ -0,0 +1,5 @@
function! auto#foo#func(...)
return 'func'
endfunction
let auto#foo#var = 'var'

View File

@ -0,0 +1,10 @@
function! Foo(...)
endfunction
call Foo(auto#foo#var,v:lang)
silent! echomsg auto#foo#var
call Foo(auto#foo#func(1,2))
silent! echomsg auto#foo#func()
let Bar = function('auto#foo#func')

View File

@ -0,0 +1,30 @@
#!/usr/bin/env bash
set -e
vim="$(command -v vim)"
# Change to the directory of this script.
cd "$( dirname "${BASH_SOURCE[0]}" )" || exit 1
if [[ "$TRAVIS" = true ]] || [[ -z $vim ]]; then
# The "test/vim" directory is created and cached by Travis CI.
if [[ ! -d vim/src ]]; then
git clone --depth=1 https://github.com/vim/vim.git
fi
# Build vim executable if needed.
if [[ ! -x vim/src/vim ]]; then
(cd vim; ./configure && make)
fi
vim=vim/src/vim
fi
# The "test/vader" directory is created and cached by Travis CI.
if [[ ! -d vader.vim/plugin ]]; then
git clone --depth=1 https://github.com/junegunn/vader.vim.git
fi
$vim -Nnu vimrc -i NONE +'Vader! tests/*.vader'
echo -e "\nExecutable used: $vim\n"

View File

@ -0,0 +1,34 @@
After:
bwipeout
Execute (:Lookup jumps to autoload variable):
edit fixture/plugin/auto.vim
normal! 5G^3w
call lookup#lookup()
AssertEqual 'fixture/autoload/auto/foo.vim', expand('%')
AssertEqual [5, 5], [line('.'), col('.')]
call lookup#pop()
normal! k
AssertEqual 'fixture/plugin/auto.vim', expand('%')
call lookup#lookup()
AssertEqual 'fixture/autoload/auto/foo.vim', expand('%')
AssertEqual [5, 5], [line('.'), col('.')]
Execute (:Lookup jumps to autoload function):
edit fixture/plugin/auto.vim
normal! 8G^3w
call lookup#lookup()
AssertEqual 'fixture/autoload/auto/foo.vim', expand('%')
AssertEqual [1, 11], [line('.'), col('.')]
call lookup#pop()
normal! k
AssertEqual 'fixture/plugin/auto.vim', expand('%')
call lookup#lookup()
AssertEqual 'fixture/autoload/auto/foo.vim', expand('%')
AssertEqual [1, 11], [line('.'), col('.')]
call lookup#pop()
normal! 10G23|
AssertEqual 'fixture/plugin/auto.vim', expand('%')
call lookup#lookup()
AssertEqual 'fixture/autoload/auto/foo.vim', expand('%')
AssertEqual [1, 11], [line('.'), col('.')]

View File

@ -0,0 +1,22 @@
Given vim:
command! -nargs=+ TestIt call s:func(<f-args>)
execute "command! -nargs=0 TestFlip let s:var = 'hello'"
execute 'let s:var = "bar"'
echomsg s:var
function! s:nested(a, b)
TestIt
TestFlip
endfunc
Execute (:call lookup#lookup() to find definition of commands):
" on TestIt
normal! 6G
AssertEqual [6, 3], [line('.'), col('.')]
call lookup#lookup()
AssertEqual [1, 22], [line('.'), col('.')]
" on TestFlip
normal! 7G
call lookup#lookup()
AssertEqual [2, 31], [line('.'), col('.')]

View File

@ -0,0 +1,38 @@
Given vim:
function! s:func(a, b)
endfunc
let s:var = 'foo'
execute 'let s:var = "bar"'
echomsg s:var
let s:var = 'foo'
function! s:func(a, b)
endfunc
let foo = function('s:func')
let foo = function("s:func")
Execute (:call lookup#lookup() cycles through script-local variables):
normal! 3GwE
AssertEqual [3, 9], [line('.'), col('.')]
call lookup#lookup()
AssertEqual [3, 7], [line('.'), col('.')]
call lookup#lookup()
AssertEqual [6, 7], [line('.'), col('.')]
call lookup#lookup()
AssertEqual [4, 16], [line('.'), col('.')]
call lookup#lookup()
AssertEqual [3, 7], [line('.'), col('.')]
Execute (:call lookup#lookup() cycles through script-local functions):
normal! 9G7w
AssertEqual [9, 23], [line('.'), col('.')]
call lookup#lookup()
AssertEqual [7, 13], [line('.'), col('.')]
call lookup#pop()
normal! j
AssertEqual [10, 23], [line('.'), col('.')]
call lookup#lookup()
AssertEqual [7, 13], [line('.'), col('.')]
call lookup#lookup()
AssertEqual [1, 13], [line('.'), col('.')]
call lookup#lookup()
AssertEqual [7, 13], [line('.'), col('.')]

View File

@ -0,0 +1,108 @@
Given vim:
function! s:func(a, b)
return s:nested(a, b)
endfunc
execute 'let s:var = "bar"'
let s:const = "foo"
echomsg s:var
function! s:nested(a, b)
return [s:var + a:a, s:const + a:b]
endfunc
Execute (lookup#lookup() a couple levels deep and C-t to go back):
" on s:nested
normal! 2G4e
AssertEqual [2, 17], [line('.'), col('.')]
call lookup#lookup()
AssertEqual [7, 13], [line('.'), col('.')]
" on s:var
normal! j
AssertEqual [8, 13], [line('.'), col('.')]
call lookup#lookup()
AssertEqual [4, 16], [line('.'), col('.')]
exec "normal! \<C-t>"
AssertEqual [8, 13], [line('.'), col('.')]
" on s:const
normal! fc
AssertEqual [8, 26], [line('.'), col('.')]
call lookup#lookup()
AssertEqual [5, 7], [line('.'), col('.')]
exec "normal! \<C-t>"
AssertEqual [8, 26], [line('.'), col('.')]
exec "normal! \<C-t>"
AssertEqual [2, 17], [line('.'), col('.')]
Execute (lookup#lookup() a couple levels deep and lookup#pop to go back):
" on s:nested
normal! 2G4e
AssertEqual [2, 17], [line('.'), col('.')]
call lookup#lookup()
AssertEqual [7, 13], [line('.'), col('.')]
" on s:var
normal! j
AssertEqual [8, 13], [line('.'), col('.')]
call lookup#lookup()
AssertEqual [4, 16], [line('.'), col('.')]
call lookup#pop()
AssertEqual [8, 13], [line('.'), col('.')]
" on s:const
normal! fc
AssertEqual [8, 26], [line('.'), col('.')]
call lookup#lookup()
AssertEqual [5, 7], [line('.'), col('.')]
call lookup#pop()
AssertEqual [8, 26], [line('.'), col('.')]
call lookup#pop()
AssertEqual [2, 17], [line('.'), col('.')]
Execute (lookup#lookup() across files and C-t to go back):
edit fixture/plugin/auto.vim
normal! 5G^3w
AssertEqual [5, 17], [line('.'), col('.')]
call lookup#lookup()
AssertEqual expand('fixture/autoload/auto/foo.vim'), expand('%')
AssertEqual [5, 5], [line('.'), col('.')]
exec "normal! \<C-t>"
AssertEqual expand('fixture/plugin/auto.vim'), expand('%')
AssertEqual [5, 17], [line('.'), col('.')]
normal! k
AssertEqual expand('fixture/plugin/auto.vim'), expand('%')
AssertEqual [4, 17], [line('.'), col('.')]
call lookup#lookup()
AssertEqual expand('fixture/autoload/auto/foo.vim'), expand('%')
AssertEqual [5, 5], [line('.'), col('.')]
exec "normal! \<C-t>"
AssertEqual expand('fixture/plugin/auto.vim'), expand('%')
AssertEqual [4, 17], [line('.'), col('.')]
Execute (lookup#lookup() across files and lookup#pop to go back):
edit fixture/plugin/auto.vim
normal! 5G^3w
AssertEqual [5, 17], [line('.'), col('.')]
call lookup#lookup()
AssertEqual expand('fixture/autoload/auto/foo.vim'), expand('%')
AssertEqual [5, 5], [line('.'), col('.')]
call lookup#pop()
AssertEqual expand('fixture/plugin/auto.vim'), expand('%')
AssertEqual [5, 17], [line('.'), col('.')]
normal! k
AssertEqual expand('fixture/plugin/auto.vim'), expand('%')
AssertEqual [4, 17], [line('.'), col('.')]
call lookup#lookup()
AssertEqual expand('fixture/autoload/auto/foo.vim'), expand('%')
AssertEqual [5, 5], [line('.'), col('.')]
call lookup#pop()
AssertEqual expand('fixture/plugin/auto.vim'), expand('%')
AssertEqual [4, 17], [line('.'), col('.')]

View File

@ -0,0 +1,4 @@
set runtimepath+=vader.vim
set runtimepath+=fixture
set runtimepath+=..
set hidden