1
0
mirror of https://github.com/SpaceVim/SpaceVim.git synced 2025-03-12 17:55:41 +08:00

Improve leaderf layer (#3114)

This commit is contained in:
Wang Shidong 2019-10-18 00:05:29 +08:00 committed by GitHub
parent ba3c55d64a
commit cd1fe9ab7d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 507 additions and 13 deletions

View File

@ -6,6 +6,7 @@
" License: GPLv3
"=============================================================================
let s:CMP = SpaceVim#api#import('vim#compatible')
function! SpaceVim#layers#leaderf#plugins() abort
let plugins = []
@ -15,39 +16,452 @@ function! SpaceVim#layers#leaderf#plugins() abort
\ 'loadconf' : 1,
\ 'merged' : 0,
\ }])
call add(plugins, ['Shougo/neomru.vim', {'merged' : 0}])
call add(plugins, ['Shougo/neoyank.vim', {'merged' : 0}])
return plugins
endfunction
let g:Lf_Extensions = get(g:, 'Lf_Extensions', {})
let s:filename = expand('<sfile>:~')
let s:lnum = expand('<slnum>') + 2
function! SpaceVim#layers#leaderf#config() abort
call SpaceVim#mapping#space#def('nnoremap', ['b', 'b'], 'LeaderfBuffer', 'buffer list', 1)
call SpaceVim#mapping#space#def('nnoremap', ['f', 'r'], 'LeaderfMru', 'open-recent-file', 1)
call SpaceVim#mapping#space#def('nnoremap', ['j', 'i'], 'Denite outline', 'jump to a definition in buffer', 1)
nnoremap <silent> <C-p> :LeaderfFile<cr>
call SpaceVim#mapping#space#def('nnoremap', ['T', 's'], 'LeaderfColorscheme', 'fuzzy find colorschemes', 1)
let g:_spacevim_mappings.f = {'name' : '+Fuzzy Finder'}
call s:defind_fuzzy_finder()
let g:Lf_Extensions.menu =
\ {
\ "source": string(s:_function('s:menu', 1))[10:-3],
\ "arguments": [
\ { "name": ["--name"], "nargs": 1, "help": "Use leaderf show unite menu"},
\ ],
\ "accept": string(s:_function('s:accept', 1))[10:-3],
\ }
let g:Lf_Extensions.register =
\ {
\ "source": string(s:_function('s:register', 1))[10:-3],
\ "accept": string(s:_function('s:register_acp', 1))[10:-3],
\ "highlights_def": {
\ "Lf_register_name": '^".',
\ "Lf_register_content": '\s\+.*',
\ },
\ "highlights_cmd": [
\ "hi def link Lf_register_name ModeMsg",
\ "hi def link Lf_register_content Normal",
\ ],
\ 'after_enter' : string(s:_function('s:init_leaderf_win', 1))[10:-3]
\ }
let g:Lf_Extensions.jumplist =
\ {
\ "source": string(s:_function('s:jumplist', 1))[10:-3],
\ "accept": string(s:_function('s:jumplist_acp', 1))[10:-3],
\ "highlights_def": {
\ "Lf_register_name": '^".',
\ "Lf_register_content": '\s\+.*',
\ },
\ "highlights_cmd": [
\ "hi def link Lf_register_name ModeMsg",
\ "hi def link Lf_register_content Normal",
\ ],
\ 'after_enter' : string(s:_function('s:init_leaderf_win', 1))[10:-3]
\ }
let g:Lf_Extensions.message =
\ {
\ "source": string(s:_function('s:message', 1))[10:-3],
\ "accept": string(s:_function('s:message_acp', 1))[10:-3],
\ "highlights_def": {
\ "Lf_register_name": '^".',
\ "Lf_register_content": '\s\+.*',
\ },
\ "highlights_cmd": [
\ "hi def link Lf_register_name ModeMsg",
\ "hi def link Lf_register_content Normal",
\ ],
\ 'after_enter' : string(s:_function('s:init_leaderf_win', 1))[10:-3]
\ }
let g:Lf_Extensions.neoyank =
\ {
\ "source": string(s:_function('s:neoyank', 1))[10:-3],
\ "accept": string(s:_function('s:neoyank_acp', 1))[10:-3],
\ "highlights_def": {
\ "Lf_register_name": '^".',
\ "Lf_register_content": '\s\+.*',
\ },
\ "highlights_cmd": [
\ "hi def link Lf_register_name ModeMsg",
\ "hi def link Lf_register_content Normal",
\ ],
\ 'after_enter' : string(s:_function('s:init_leaderf_win', 1))[10:-3]
\ }
let g:Lf_Extensions.quickfix =
\ {
\ "source": string(s:_function('s:quickfix', 1))[10:-3],
\ "accept": string(s:_function('s:quickfix_acp', 1))[10:-3],
\ "highlights_def": {
\ "Lf_register_name": '^".',
\ "Lf_register_content": '\s\+.*',
\ },
\ "highlights_cmd": [
\ "hi def link Lf_register_name ModeMsg",
\ "hi def link Lf_register_content Normal",
\ ],
\ 'after_enter' : string(s:_function('s:init_leaderf_win', 1))[10:-3]
\ }
let g:Lf_Extensions.locationlist =
\ {
\ "source": string(s:_function('s:locationlist', 1))[10:-3],
\ "accept": string(s:_function('s:locationlist_acp', 1))[10:-3],
\ "highlights_def": {
\ "Lf_register_name": '^".',
\ "Lf_register_content": '\s\+.*',
\ },
\ "highlights_cmd": [
\ "hi def link Lf_register_name ModeMsg",
\ "hi def link Lf_register_content Normal",
\ ],
\ 'after_enter' : string(s:_function('s:init_leaderf_win', 1))[10:-3]
\ }
let lnum = expand('<slnum>') + s:lnum - 1
call SpaceVim#mapping#space#def('nnoremap', ['f', 'f'],
\ "exe 'LeaderfFile ' . fnamemodify(bufname('%'), ':h')",
call SpaceVim#mapping#space#def('nnoremap', ['?'], 'call call('
\ . string(s:_function('s:warp_denite')) . ', ["Leaderf menu --name CustomKeyMaps --input [SPC]"])',
\ ['show-mappings',
\ [
\ 'SPC ? is to show mappings',
\ '',
\ 'Definition: ' . s:filename . ':' . lnum,
\ ]
\ ],
\ 1)
let lnum = expand('<slnum>') + s:lnum - 1
call SpaceVim#mapping#space#def('nnoremap', ['h', '[SPC]'], 'Leaderf help --input=SpaceVim',
\ ['find-SpaceVim-help',
\ [
\ 'SPC h SPC is to find SpaceVim help',
\ '',
\ 'Definition: ' . s:filename . ':' . lnum,
\ ]
\ ],
\ 1)
" @fixme SPC h SPC make vim flick
nmap <Space>h<Space> [SPC]h[SPC]
let lnum = expand('<slnum>') + s:lnum - 1
call SpaceVim#mapping#space#def('nnoremap', ['b', 'b'], 'Leaderf buffer',
\ ['buffer-list',
\ [
\ 'SPC b b is to open buffer list',
\ '',
\ 'Definition: ' . s:filename . ':' . lnum,
\ ]
\ ],
\ 1)
let lnum = expand('<slnum>') + s:lnum - 1
" let g:Lf_Extensions = get(g:, 'Lf_Extensions', {})
" let g:Lf_Extensions = {
" \ "neomru": {
" \ "source": function("neomru#_gather_file_candidates()"),
" \ "accept": function("s:accept_mru"),
" \ "supports_name_only": 1,
" \ "supports_multi": 0,
" \ },
" \}
call SpaceVim#mapping#space#def('nnoremap', ['f', 'r'], 'Leaderf neomru',
\ ['open-recent-file',
\ [
\ 'SPC f r is to open recent file list',
\ '',
\ 'Definition: ' . s:filename . ':' . lnum,
\ ]
\ ],
\ 1)
let lnum = expand('<slnum>') + s:lnum - 1
call SpaceVim#mapping#space#def('nnoremap', ['j', 'i'], 'Leaderf function',
\ ['jump to a definition in buffer',
\ [
\ 'SPC j i is to jump to a definition in buffer',
\ '',
\ 'Definition: ' . s:filename . ':' . lnum,
\ ]
\ ],
\ 1)
let lnum = expand('<slnum>') + s:lnum - 1
call SpaceVim#mapping#space#def('nnoremap', ['r', 'l'], 'call call('
\ . string(s:_function('s:warp_denite')) . ', ["Leaderf --recall"])',
\ ['resume fuzzy finder windows',
\ [
\ 'SPC r l is to resume fuzzy finder windows',
\ '',
\ 'Definition: ' . s:filename . ':' . lnum,
\ ]
\ ],
\ 1)
let lnum = expand('<slnum>') + s:lnum - 1
call SpaceVim#mapping#space#def('nnoremap', ['T', 's'], 'Leaderf colorscheme',
\ ['fuzzy find colorschemes',
\ [
\ 'SPC T s is to fuzzy find colorschemes',
\ '',
\ 'Definition: ' . s:filename . ':' . lnum,
\ ]
\ ],
\ 1)
let lnum = expand('<slnum>') + s:lnum - 1
call SpaceVim#mapping#space#def('nnoremap', ['f', 'f'], 'exe "Leaderf file " . expand("%:p:h")',
\ ['Find files in the directory of the current buffer',
\ [
\ '[SPC f f] is to find files in the directory of the current buffer',
\ '',
\ 'Definition: ' . s:filename . ':' . lnum,
\ ]
\ ],
\ 1)
let lnum = expand('<slnum>') + s:lnum - 1
call SpaceVim#mapping#space#def('nnoremap', ['p', 'f'], 'Leaderf file .',
\ ['find files in current project',
\ [
\ '[SPC p f] is to find files in the root of the current project',
\ '',
\ 'Definition: ' . s:filename . ':' . lnum,
\ ]
\ , 1)
\ ],
\ 1)
nnoremap <silent> <C-p> :<C-u>Leaderf file .<cr>
let lnum = expand('<slnum>') + s:lnum - 1
call SpaceVim#mapping#space#def('nnoremap', ['h', 'i'], 'LeaderfHelpCword',
\ ['get help with the symbol at point',
\ [
\ '[SPC h i] is to get help with the symbol at point',
\ '',
\ 'Definition: ' . s:filename . ':' . lnum,
\ ]
\ ],
\ 1)
let g:_spacevim_mappings.f = {'name' : '+Fuzzy Finder'}
call s:defind_fuzzy_finder()
endfunction
function! s:init_leaderf_win(...)
setlocal nonumber
setlocal nowrap
endfunction
function! s:register(...)
return split(s:CMP.execute('registers'), '\n')[1:]
endfunction
function! s:register_acp(line, args)
let @" = a:line
echohl ModeMsg
echon 'Yanked!'
echohl None
endfunction
function! s:jumplist(...) abort
return split(s:CMP.execute('jumps'), '\n')[1:]
endfunction
function! s:jumplist_acp(line, args) abort
let list = split(a:line)
if len(list) < 4
return
endif
let [linenr, col, file_text] = [list[1], list[2]+1, join(list[3:])]
let lines = getbufline(file_text, linenr)
let path = file_text
if empty(lines)
if stridx(join(split(getline(linenr))), file_text) == 0
let lines = [file_text]
let path = bufname('%')
elseif filereadable(path)
let lines = ['buffer unloaded']
else
" Skip.
return
endif
endif
exe 'e ' . path
call cursor(linenr, col)
endfunction
function! s:message(...) abort
return split(s:CMP.execute('message'), '\n')
endfunction
function! s:message_acp(line, args) abort
let @" = a:line
echohl ModeMsg
echo 'Yanked'
echohl None
endfunction
func! s:neoyank(...)
let yank = []
for text in neoyank#_get_yank_histories()['"']
call add(yank, '": ' . join(split(text[0], "\n"), '\n'))
endfor
return yank
endfunction
function! s:neoyank_acp(line, args) abort
let line = a:line[3:]
call append(0, split(line, '\\n'))
endfunction
function! s:menu(name)
let s:menu_action = {}
let menu = get(g:unite_source_menu_menus, a:name['--name'][0], {})
if has_key(menu, 'command_candidates')
let rt = []
for item in menu.command_candidates
call add(rt, item[0])
call extend(s:menu_action, {item[0] : item[1]}, 'force')
endfor
return rt
else
return []
endif
endfunction
function! s:accept(line, args)
let action = get(s:menu_action, a:line, '')
exe action
endfunction
function! s:quickfix_to_grep(v) abort
return bufname(a:v.bufnr) . ':' . a:v.lnum . ':' . a:v.col . ':' . a:v.text
endfunction
function! s:quickfix(...) abort
return map(getqflist(), 's:quickfix_to_grep(v:val)')
endfunction
function! s:quickfix_acp(line, args) abort
let line = a:line
let filename = fnameescape(split(line, ':\d\+:')[0])
let linenr = matchstr(line, ':\d\+:')[1:-2]
let colum = matchstr(line, '\(:\d\+\)\@<=:\d\+:')[1:-2]
exe 'e ' . filename
call cursor(linenr, colum)
endfunction
function! s:location_list_to_grep(v) abort
return bufname(a:v.bufnr) . ':' . a:v.lnum . ':' . a:v.col . ':' . a:v.text
endfunction
function! s:locationlist(...) abort
return map(getloclist(0), 's:location_list_to_grep(v:val)')
endfunction
function! s:locationlist_acp(line, args) abort
let line = a:line
let filename = fnameescape(split(line, ':\d\+:')[0])
let linenr = matchstr(line, ':\d\+:')[1:-2]
let colum = matchstr(line, '\(:\d\+\)\@<=:\d\+:')[1:-2]
exe 'e ' . filename
call cursor(linenr, colum)
endfunction
let s:file = expand('<sfile>:~')
let s:unite_lnum = expand('<slnum>') + 3
function! s:defind_fuzzy_finder() abort
nnoremap <silent> <Leader>fo :<C-u>LeaderfFunction<CR>
nnoremap <silent> <Leader>fr
\ :<C-u>Leaderf --recall<CR>
let lnum = expand('<slnum>') + s:unite_lnum - 4
let g:_spacevim_mappings.f.o = ['LeaderfFunction',
let g:_spacevim_mappings.f.r = ['Leaderf --recall',
\ 'resume fuzzy finder window',
\ [
\ '[Leader f r ] is to resume fuzzy finder window',
\ '',
\ 'Definition: ' . s:file . ':' . lnum,
\ ]
\ ]
nnoremap <silent> <Leader>fe
\ :<C-u>Leaderf register<CR>
let lnum = expand('<slnum>') + s:unite_lnum - 4
let g:_spacevim_mappings.f.e = ['Leaderf register',
\ 'fuzzy find registers',
\ [
\ '[Leader f r ] is to fuzzy find registers',
\ '',
\ 'Definition: ' . s:file . ':' . lnum,
\ ]
\ ]
nnoremap <silent> <Leader>fh
\ :<C-u>Leaderf neoyank<CR>
let lnum = expand('<slnum>') + s:unite_lnum - 4
let g:_spacevim_mappings.f.h = ['Leaderf neoyank',
\ 'fuzzy find yank history',
\ [
\ '[Leader f h] is to fuzzy find history and yank content',
\ '',
\ 'Definition: ' . s:file . ':' . lnum,
\ ]
\ ]
nnoremap <silent> <Leader>fj
\ :<C-u>Leaderf jumplist<CR>
let lnum = expand('<slnum>') + s:unite_lnum - 4
let g:_spacevim_mappings.f.j = ['Leaderf jumplist',
\ 'fuzzy find jump list',
\ [
\ '[Leader f j] is to fuzzy find jump list',
\ '',
\ 'Definition: ' . s:file . ':' . lnum,
\ ]
\ ]
nnoremap <silent> <Leader>fl
\ :<C-u>Leaderf locationlist<CR>
let lnum = expand('<slnum>') + s:unite_lnum - 4
let g:_spacevim_mappings.f.l = ['Leaderf locationlist',
\ 'fuzzy find location list',
\ [
\ '[Leader f l] is to fuzzy find location list',
\ '',
\ 'Definition: ' . s:file . ':' . lnum,
\ ]
\ ]
nnoremap <silent> <Leader>fm
\ :<C-u>Leaderf message<CR>
let lnum = expand('<slnum>') + s:unite_lnum - 4
let g:_spacevim_mappings.f.m = ['Leaderf message',
\ 'fuzzy find message',
\ [
\ '[Leader f m] is to fuzzy find message',
\ '',
\ 'Definition: ' . s:file . ':' . lnum,
\ ]
\ ]
nnoremap <silent> <Leader>fq
\ :<C-u>Leaderf quickfix<CR>
let lnum = expand('<slnum>') + s:unite_lnum - 4
let g:_spacevim_mappings.f.q = ['Leaderf quickfix',
\ 'fuzzy find quickfix list',
\ [
\ '[Leader f q] is to fuzzy find quickfix list',
\ '',
\ 'Definition: ' . s:file . ':' . lnum,
\ ]
\ ]
nnoremap <silent> <Leader>fo :<C-u>Leaderf function<CR>
let lnum = expand('<slnum>') + s:unite_lnum - 4
let g:_spacevim_mappings.f.o = ['Leaderf function',
\ 'fuzzy find outline',
\ [
\ '[Leader f o] is to fuzzy find outline',
@ -55,4 +469,56 @@ function! s:defind_fuzzy_finder() abort
\ 'Definition: ' . s:file . ':' . lnum,
\ ]
\ ]
nnoremap <silent> <Leader>f<Space> :<C-u>Leaderf menu --name CustomKeyMaps<CR>
let g:_spacevim_mappings.f['[SPC]'] = ['Leaderf menu --name CustomKeyMaps',
\ 'fuzzy find custom key bindings',
\ [
\ '[Leader f SPC] is to fuzzy find custom key bindings',
\ '',
\ 'Definition: ' . s:file . ':' . lnum,
\ ]
\ ]
nnoremap <silent> <Leader>fp :<C-u>Leaderf menu --name AddedPlugins<CR>
let lnum = expand('<slnum>') + s:unite_lnum - 4
let g:_spacevim_mappings.f.p = ['Leaderf menu --name AddedPlugins',
\ 'fuzzy find vim packages',
\ [
\ '[Leader f p] is to fuzzy find vim packages installed in SpaceVim',
\ '',
\ 'Definition: ' . s:file . ':' . lnum,
\ ]
\ ]
endfunction
function! s:accept_mru(line) abort
exe 'e ' . line
endfunction
function! s:warp_denite(cmd) abort
exe a:cmd
doautocmd WinEnter
endfunction
" function() wrapper
if v:version > 703 || v:version == 703 && has('patch1170')
function! s:_SID() abort
return matchstr(expand('<sfile>'), '<SNR>\zs\d\+\ze__SID$')
endfunction
let s:_s = '<SNR>' . s:_SID() . '_'
function! s:_function(fstr, ...) abort
if a:0 > 1
return function(substitute(a:fstr, 's:', s:_s, 'g'))
else
return function(a:fstr)
endif
endfunction
else
function! s:_SID() abort
return matchstr(expand('<sfile>'), '<SNR>\zs\d\+\ze__SID$')
endfunction
let s:_s = '<SNR>' . s:_SID() . '_'
function! s:_function(fstr) abort
return function(substitute(a:fstr, 's:', s:_s, 'g'))
endfunction
endif
" vim:set et sw=2 cc=80:

View File

@ -1,3 +1,31 @@
scriptencoding utf-8
let g:Lf_StlSeparator = get(g:, 'Lf_StlSeparator', { 'left': '', 'right': '' })
let g:Lf_StlColorscheme = get(g:, 'Lf_StlColorscheme', 'spacevim')
let g:Lf_StlColorscheme = g:spacevim_colorscheme
" disable default mru
"
augroup LeaderF_Mru
autocmd!
autocmd FileType leaderf setlocal nonumber
augroup END
let g:Lf_PreviewResult = {
\ 'File': 0,
\ 'Buffer': 0,
\ 'Mru': 0,
\ 'Tag': 0,
\ 'BufTag': 0,
\ 'Function': 0,
\ 'Line': 0,
\ 'Colorscheme': 0
\}
let g:Lf_CommandMap = {
\ '<C-J>' : ['<Tab>'],
\ '<C-K>' : ['<S-Tab>'],
\ '<C-R>' : ['<C-E>'],
\ '<C-X>' : ['<C-S>'],
\ '<C-]>' : ['<C-V>'],
\ '<C-F>' : ['<C-D>'],
\ '<Tab>' : ['<Esc>'],
\ }
let g:Lf_HideHelp = 1