1
0
mirror of https://github.com/SpaceVim/SpaceVim.git synced 2025-04-13 12:30:40 +08:00

Improve flygrep (#1898)

* fix delay for fly grep in neovim

* escape for command

* Improve flygrep

* Improve logger

* Fix cmd

* Fix flygrep statusline

* Fix flygrep statusline number

* Improve rg support

* Fix ag support

* Fix ag support

* Escape shell command

* Fix rg support

* 🍺 improve flygep

* Indent prompt with flygrep windows

* Indent flygep prompt

* Fix win screenpos api

* Do not redraw screen

* Detect compatible api

* Log stderr for debug

* Fix vim8 support

* Add key bindings
This commit is contained in:
Wang Shidong 2018-07-09 17:16:38 +08:00 committed by GitHub
parent 945fb7cdab
commit d0c7c389ff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 164 additions and 26 deletions

View File

@ -24,6 +24,7 @@ main () {
_detect autoload/SpaceVim/api.vim
_detect autoload/SpaceVim/api/logger.vim
_detect autoload/SpaceVim/api/vim/buffer.vim
_detect autoload/SpaceVim/api/vim/compatible.vim
_detect autoload/SpaceVim/api/data/list.vim
_detect autoload/SpaceVim/api/prompt.vim
_detect autoload/SpaceVim/api/job.vim

View File

@ -23,6 +23,7 @@
" <
let s:self = {}
let s:self.__cmp = SpaceVim#api#import('vim#compatible')
let s:self._keys = {
@ -140,7 +141,8 @@ endf
func! s:self._build_prompt() abort
normal! :
echohl Comment | echon self._prompt.mpt
let ident = repeat(' ', self.__cmp.win_screenpos(0)[1])
echohl Comment | echon ident . self._prompt.mpt
echohl None | echon self._prompt.begin
echohl Wildmenu | echon self._prompt.cursor
echohl None | echon self._prompt.end

View File

@ -14,11 +14,43 @@ function! SpaceVim#api#vim#compatible#get() abort
\ 'has' : '',
\ 'globpath' : '',
\ 'matchaddpos' : '',
\ 'win_screenpos' : '',
\ },
\ "function('s:' . v:key)"
\ )
endfunction
if has('patch-8.0.1364')
function! s:win_screenpos(nr) abort
return win_screenpos(a:nr)
endfunction
elseif has('python')
function! s:win_screenpos(nr) abort
if winnr('$') < a:nr || a:nr < 0
return [0, 0]
elseif a:nr == 0
return [pyeval('vim.current.window.row'),
\ pyeval('vim.current.window.col')]
endif
return [pyeval('vim.windows[' . a:nr . '].row'),
\ pyeval('vim.windows[' . a:nr . '].col')]
endfunction
elseif has('python3')
function! s:win_screenpos(nr) abort
if winnr('$') < a:nr || a:nr < 0
return [0, 0]
elseif a:nr == 0
return [py3eval('vim.current.window.row'),
\ py3eval('vim.current.window.col')]
endif
return [py3eval('vim.windows[' . a:nr . '].row'),
\ py3eval('vim.windows[' . a:nr . '].col')]
endfunction
else
function! s:win_screenpos(nr) abort
return [0, 0]
endfunction
endif
if exists('*execute')
function! s:execute(cmd, ...) abort

View File

@ -365,7 +365,7 @@ function! SpaceVim#layers#core#statusline#get(...) abort
return '%#SpaceVim_statusline_a_bold# FlyGrep %#SpaceVim_statusline_a_SpaceVim_statusline_b#' . s:lsep
\ . '%#SpaceVim_statusline_b# %{SpaceVim#plugins#flygrep#mode()} %#SpaceVim_statusline_b_SpaceVim_statusline_c#' . s:lsep
\ . '%#SpaceVim_statusline_c# %{getcwd()} %#SpaceVim_statusline_c_SpaceVim_statusline_b#' . s:lsep
\ . '%#SpaceVim_statusline_b# %{SpaceVim#plugins#flygrep#lineNr()} %#SpaceVim_statusline_b_SpaceVim_statusline_z#' . s:lsep
\ . '%#SpaceVim_statusline_b# %{SpaceVim#plugins#flygrep#lineNr()} %#SpaceVim_statusline_b_SpaceVim_statusline_z#' . s:lsep . ' '
elseif &filetype ==# 'TransientState'
return '%#SpaceVim_statusline_a# Transient State %#SpaceVim_statusline_a_SpaceVim_statusline_b#'
elseif &filetype ==# 'vimcalc'

View File

@ -18,7 +18,7 @@ let s:search_tools.a = {}
let s:search_tools.a.command = 'ag'
let s:search_tools.a.default_opts =
\ [
\ '-i', '--vimgrep', '--hidden', '--ignore',
\ '-i', '--column', '--hidden', '--ignore',
\ '.hg', '--ignore', '.svn', '--ignore', '.git', '--ignore', '.bzr',
\ ]
let s:search_tools.a.recursive_opt = []
@ -40,11 +40,14 @@ let s:search_tools.t.ignore_case = ['-i']
let s:search_tools.r = {}
let s:search_tools.r.command = 'rg'
let s:search_tools.r.default_opts = ['--hidden', '--no-heading', '--vimgrep']
let s:search_tools.r.default_opts = [
\ '--hidden', '--no-heading', '--color=never', '--with-filename', '--line-number', '--column',
\ '-g', '!.git'
\ ]
let s:search_tools.r.recursive_opt = []
let s:search_tools.r.expr_opt = ['-e']
let s:search_tools.r.fixed_string_opt = ['-F']
let s:search_tools.r.default_fopts = ['--hidden', '-N']
let s:search_tools.r.default_fopts = ['-N']
let s:search_tools.r.smart_case = ['-S']
let s:search_tools.r.ignore_case = ['-i']
@ -68,10 +71,13 @@ let s:search_tools.g.default_fopts = []
let s:search_tools.g.smart_case = []
let s:search_tools.g.ignore_case = ['-i']
function! SpaceVim#mapping#search#grep(key, scope)
function! SpaceVim#mapping#search#grep(key, scope) abort
let cmd = s:search_tools[a:key]['command']
let opt = s:search_tools[a:key]['default_opts']
let ropt = s:search_tools[a:key]['recursive_opt']
let ignore = s:search_tools[a:key]['ignore_case']
let smart = s:search_tools[a:key]['smart_case']
let expr = s:search_tools[a:key]['expr_opt']
if a:scope ==# 'b'
call SpaceVim#plugins#flygrep#open({
\ 'input' : input('grep pattern:'),
@ -79,6 +85,9 @@ function! SpaceVim#mapping#search#grep(key, scope)
\ 'cmd' : cmd,
\ 'opt' : opt,
\ 'ropt' : ropt,
\ 'ignore_case' : ignore,
\ 'smart_case' : smart,
\ 'expr_opt' : expr,
\ })
elseif a:scope ==# 'B'
call SpaceVim#plugins#flygrep#open({
@ -87,6 +96,9 @@ function! SpaceVim#mapping#search#grep(key, scope)
\ 'cmd' : cmd,
\ 'opt' : opt,
\ 'ropt' : ropt,
\ 'ignore_case' : ignore,
\ 'smart_case' : smart,
\ 'expr_opt' : expr,
\ })
elseif a:scope ==# 'p'
call SpaceVim#plugins#flygrep#open({
@ -94,6 +106,9 @@ function! SpaceVim#mapping#search#grep(key, scope)
\ 'cmd' : cmd,
\ 'opt' : opt,
\ 'ropt' : ropt,
\ 'ignore_case' : ignore,
\ 'smart_case' : smart,
\ 'expr_opt' : expr,
\ })
elseif a:scope ==# 'P'
call SpaceVim#plugins#flygrep#open({
@ -101,22 +116,31 @@ function! SpaceVim#mapping#search#grep(key, scope)
\ 'cmd' : cmd,
\ 'opt' : opt,
\ 'ropt' : ropt,
\ 'ignore_case' : ignore,
\ 'smart_case' : smart,
\ 'expr_opt' : expr,
\ })
elseif a:scope ==# 'd'
call SpaceVim#plugins#flygrep#open({
\ 'input' : input('grep pattern:'),
\ 'dir' : fnamemodify(expand("%"), ":p:h"),
\ 'dir' : fnamemodify(expand('%'), ':p:h'),
\ 'cmd' : cmd,
\ 'opt' : opt,
\ 'ropt' : ropt,
\ 'ignore_case' : ignore,
\ 'smart_case' : smart,
\ 'expr_opt' : expr,
\ })
elseif a:scope ==# 'D'
call SpaceVim#plugins#flygrep#open({
\ 'input' : expand('<cword>'),
\ 'dir' : fnamemodify(expand("%"), ":p:h"),
\ 'dir' : fnamemodify(expand('%'), ':p:h'),
\ 'cmd' : cmd,
\ 'opt' : opt,
\ 'ropt' : ropt,
\ 'ignore_case' : ignore,
\ 'smart_case' : smart,
\ 'expr_opt' : expr,
\ })
elseif a:scope ==# 'f'
call SpaceVim#plugins#flygrep#open({
@ -125,6 +149,9 @@ function! SpaceVim#mapping#search#grep(key, scope)
\ 'cmd' : cmd,
\ 'opt' : opt,
\ 'ropt' : ropt,
\ 'ignore_case' : ignore,
\ 'smart_case' : smart,
\ 'expr_opt' : expr,
\ })
elseif a:scope ==# 'F'
call SpaceVim#plugins#flygrep#open({
@ -133,6 +160,9 @@ function! SpaceVim#mapping#search#grep(key, scope)
\ 'cmd' : cmd,
\ 'opt' : opt,
\ 'ropt' : ropt,
\ 'ignore_case' : ignore,
\ 'smart_case' : smart,
\ 'expr_opt' : expr,
\ })
endif
endfunction

View File

@ -23,8 +23,8 @@ let [
\ s:grep_default_ropt,
\ s:grep_default_expr_opt,
\ s:grep_default_fix_string_opt,
\ s:grep_ignore_case,
\ s:grep_smart_case
\ s:grep_default_ignore_case,
\ s:grep_default_smart_case
\ ] = SpaceVim#mapping#search#default_tool()
let s:grep_timer_id = 0
let s:grepid = 0
@ -41,6 +41,7 @@ function! s:grep_timer(timer) abort
call SpaceVim#logger#info('grep cmd: ' . string(cmd))
let s:grepid = s:JOB.start(cmd, {
\ 'on_stdout' : function('s:grep_stdout'),
\ 'on_stderr' : function('s:grep_stderr'),
\ 'in_io' : 'null',
\ 'on_exit' : function('s:grep_exit'),
\ })
@ -57,16 +58,24 @@ function! s:get_search_cmd(expr) abort
if s:grep_mode ==# 'string'
let cmd += s:grep_default_fix_string_opt
endif
let cmd += s:grep_default_expr_opt
let cmd += s:grep_expr_opt
if !empty(s:grep_files) && type(s:grep_files) == 3
return cmd + [a:expr] + s:grep_files
let cmd += [a:expr] + s:grep_files
elseif !empty(s:grep_files) && type(s:grep_files) == 1
return cmd + [a:expr] + [s:grep_files]
let cmd += [a:expr] + [s:grep_files]
elseif !empty(s:grep_dir)
return cmd + [a:expr] + [s:grep_dir]
let cmd += [a:expr] + [s:grep_dir]
else
return cmd + [a:expr] + s:grep_ropt
let cmd += [a:expr] + s:grep_ropt
endif
" let cmd = map(cmd, 'shellescape(v:val)')
" if has('win32')
" let cmd += ['|', 'select', '-first', '3000']
" else
" let cmd += ['|', 'head', '-3000']
" endif
" let cmd = join(cmd, ' ')
return cmd
endfunction
function! s:flygrep(expr) abort
@ -202,7 +211,7 @@ endfunction
" @vimlint(EVL103, 1, a:event)
function! s:grep_stdout(id, data, event) abort
let datas =filter(a:data, '!empty(v:val)')
let datas = s:LIST.uniq_by_func(datas, function('s:file_line'))
" let datas = s:LIST.uniq_by_func(datas, function('s:file_line'))
if bufnr('%') == s:flygrep_buffer_id
if getline(1) ==# ''
call setline(1, datas)
@ -212,6 +221,10 @@ function! s:grep_stdout(id, data, event) abort
endif
endfunction
function! s:grep_stderr(id, data, event) abort
call SpaceVim#logger#error(' flygerp stderr: ' . string(a:data))
endfunction
function! s:grep_exit(id, data, event) abort
redrawstatus
let s:grepid = 0
@ -233,6 +246,47 @@ function! s:next_item() abort
endif
redraw
call s:MPT._build_prompt()
redrawstatus
endfunction
function! s:page_up() abort
exe "normal! \<PageUp>"
if s:preview_able == 1
call s:preview()
endif
redraw
call s:MPT._build_prompt()
redrawstatus
endfunction
function! s:page_down() abort
exe "normal! \<PageDown>"
if s:preview_able == 1
call s:preview()
endif
redraw
call s:MPT._build_prompt()
redrawstatus
endfunction
function! s:page_home() abort
normal! gg
if s:preview_able == 1
call s:preview()
endif
redraw
call s:MPT._build_prompt()
redrawstatus
endfunction
function! s:page_end() abort
normal! G
if s:preview_able == 1
call s:preview()
endif
redraw
call s:MPT._build_prompt()
redrawstatus
endfunction
function! s:previous_item() abort
@ -246,6 +300,7 @@ function! s:previous_item() abort
endif
redraw
call s:MPT._build_prompt()
redrawstatus
endfunction
function! s:open_item() abort
@ -264,7 +319,7 @@ function! s:open_item() abort
noautocmd q
exe 'e ' . filename
call cursor(linenr, colum)
redraw!
noautocmd normal! :
endif
endfunction
@ -309,7 +364,7 @@ function! s:toggle_preview() abort
pclose
let s:preview_able = 0
endif
redraw!
redraw
call s:MPT._build_prompt()
endfunction
@ -363,14 +418,14 @@ function! s:next_match_history() abort
endfunction
function! s:complete_input_history(str,num) abort
let results = filter(copy(s:grep_history), "v:val =~# '^' . a:str")
if len(results) > 0
call add(results, a:str)
let index = ((len(results) - 1) - a:num[0] + a:num[1]) % len(results)
return results[index]
else
return a:str
endif
let results = filter(copy(s:grep_history), "v:val =~# '^' . a:str")
if len(results) > 0
call add(results, a:str)
let index = ((len(results) - 1) - a:num[0] + a:num[1]) % len(results)
return results[index]
else
return a:str
endif
endfunction
let s:MPT._function_key = {
\ "\<Tab>" : function('s:next_item'),
@ -388,6 +443,10 @@ let s:MPT._function_key = {
\ "\<C-e>" : function('s:toggle_expr_mode'),
\ "\<Up>" : function('s:previous_match_history'),
\ "\<Down>" : function('s:next_match_history'),
\ "\<PageDown>" : function('s:page_down'),
\ "\<PageUp>" : function('s:page_up'),
\ "\<C-End>" : function('s:page_end'),
\ "\<C-Home>" : function('s:page_home'),
\ }
if has('nvim')
@ -440,7 +499,20 @@ function! SpaceVim#plugins#flygrep#open(agrv) abort
let s:grep_exe = get(a:agrv, 'cmd', s:grep_default_exe)
let s:grep_opt = get(a:agrv, 'opt', s:grep_default_opt)
let s:grep_ropt = get(a:agrv, 'ropt', s:grep_default_ropt)
let s:grep_ignore_case = get(a:agrv, 'ignore_case', s:grep_default_ignore_case)
let s:grep_smart_case = get(a:agrv, 'smart_case', s:grep_default_smart_case)
let s:grep_expr_opt = get(a:agrv, 'expr_opt', s:grep_default_expr_opt)
call SpaceVim#logger#info('FlyGrep startting ===========================')
call SpaceVim#logger#info(' executable : ' . s:grep_exe)
call SpaceVim#logger#info(' option : ' . string(s:grep_opt))
call SpaceVim#logger#info(' r_option : ' . string(s:grep_ropt))
call SpaceVim#logger#info(' files : ' . string(s:grep_files))
call SpaceVim#logger#info(' dir : ' . string(s:grep_dir))
call SpaceVim#logger#info(' ignore_case : ' . string(s:grep_ignore_case))
call SpaceVim#logger#info(' smart_case : ' . string(s:grep_smart_case))
call SpaceVim#logger#info(' expr opt : ' . string(s:grep_expr_opt))
call s:MPT.open()
call SpaceVim#logger#info('FlyGrep ending ===========================')
let &t_ve = save_tve
endfunction
" }}}

View File

@ -18,6 +18,7 @@ The next release is v0.9.0.
### Improvement
- Improve tab manager ([#1887](https://github.com/SpaceVim/SpaceVim/pull/1887))
- Improve flygep ([#1898](https://github.com/SpaceVim/SpaceVim/pull/1898))
### Changed