mirror of
https://github.com/SpaceVim/SpaceVim.git
synced 2025-03-16 03:55:41 +08:00
Improve searching histroy completion in flygrep (#2888)
* Improve searching histroy completion in flygrep * Cache flygrep history * Update history function
This commit is contained in:
parent
9457fc8af0
commit
faa0a92a4a
@ -15,6 +15,7 @@ let s:BUFFER = SpaceVim#api#import('vim#buffer')
|
|||||||
let s:LIST = SpaceVim#api#import('data#list')
|
let s:LIST = SpaceVim#api#import('data#list')
|
||||||
let s:HI = SpaceVim#api#import('vim#highlight')
|
let s:HI = SpaceVim#api#import('vim#highlight')
|
||||||
let s:FLOATING = SpaceVim#api#import('neovim#floating')
|
let s:FLOATING = SpaceVim#api#import('neovim#floating')
|
||||||
|
let s:JSON = SpaceVim#api#import('data#json')
|
||||||
" }}}
|
" }}}
|
||||||
|
|
||||||
let s:grepid = 0
|
let s:grepid = 0
|
||||||
@ -33,7 +34,26 @@ let [
|
|||||||
let s:grep_timer_id = -1
|
let s:grep_timer_id = -1
|
||||||
let s:preview_timer_id = -1
|
let s:preview_timer_id = -1
|
||||||
let s:grepid = 0
|
let s:grepid = 0
|
||||||
let s:grep_history = []
|
function! s:read_histroy() abort
|
||||||
|
if filereadable(expand('~/.cache/SpaceVim/flygrep_history'))
|
||||||
|
let _his = s:JSON.json_decode(join(readfile(expand('~/.cache/SpaceVim/flygrep_history'), ''), ''))
|
||||||
|
if type(_his) ==# type([])
|
||||||
|
return _his
|
||||||
|
else
|
||||||
|
return []
|
||||||
|
endif
|
||||||
|
else
|
||||||
|
return []
|
||||||
|
endif
|
||||||
|
endfunction
|
||||||
|
function! s:update_history() abort
|
||||||
|
if index(s:grep_history, s:grep_expr) >= 0
|
||||||
|
call remove(s:grep_history, index(s:grep_history, s:grep_expr))
|
||||||
|
endif
|
||||||
|
call add(s:grep_history, s:grep_expr)
|
||||||
|
call writefile([s:JSON.json_encode(s:grep_history)], expand('~/.cache/SpaceVim/flygrep_history'))
|
||||||
|
endfunction
|
||||||
|
let s:grep_history = s:read_histroy()
|
||||||
let s:complete_input_history_num = [0,0]
|
let s:complete_input_history_num = [0,0]
|
||||||
" }}}
|
" }}}
|
||||||
|
|
||||||
@ -48,6 +68,7 @@ function! s:grep_timer(timer) abort
|
|||||||
endif
|
endif
|
||||||
let cmd = s:get_search_cmd(s:current_grep_pattern)
|
let cmd = s:get_search_cmd(s:current_grep_pattern)
|
||||||
call SpaceVim#logger#info('grep cmd: ' . string(cmd))
|
call SpaceVim#logger#info('grep cmd: ' . string(cmd))
|
||||||
|
call s:update_history()
|
||||||
let s:grepid = s:JOB.start(cmd, {
|
let s:grepid = s:JOB.start(cmd, {
|
||||||
\ 'on_stdout' : function('s:grep_stdout'),
|
\ 'on_stdout' : function('s:grep_stdout'),
|
||||||
\ 'on_stderr' : function('s:grep_stderr'),
|
\ 'on_stderr' : function('s:grep_stderr'),
|
||||||
@ -266,6 +287,7 @@ function! s:close_grep_job() abort
|
|||||||
call timer_stop(s:grep_timer_id)
|
call timer_stop(s:grep_timer_id)
|
||||||
call timer_stop(s:preview_timer_id)
|
call timer_stop(s:preview_timer_id)
|
||||||
normal! "_ggdG
|
normal! "_ggdG
|
||||||
|
let s:complete_input_history_num = [0,0]
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
let s:MPT._oninputpro = function('s:close_grep_job')
|
let s:MPT._oninputpro = function('s:close_grep_job')
|
||||||
@ -390,7 +412,6 @@ function! s:previous_item() abort
|
|||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
function! s:open_item() abort
|
function! s:open_item() abort
|
||||||
call add(s:grep_history, s:grep_expr)
|
|
||||||
let s:MPT._handle_fly = function('s:flygrep')
|
let s:MPT._handle_fly = function('s:flygrep')
|
||||||
if getline('.') !=# ''
|
if getline('.') !=# ''
|
||||||
if s:grepid != 0
|
if s:grepid != 0
|
||||||
@ -522,7 +543,6 @@ function! s:previous_match_history() abort
|
|||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
function! s:next_match_history() abort
|
function! s:next_match_history() abort
|
||||||
|
|
||||||
if s:complete_input_history_num == [0,0]
|
if s:complete_input_history_num == [0,0]
|
||||||
let s:complete_input_history_base = s:MPT._prompt.begin
|
let s:complete_input_history_base = s:MPT._prompt.begin
|
||||||
let s:MPT._prompt.cursor = ''
|
let s:MPT._prompt.cursor = ''
|
||||||
@ -538,14 +558,20 @@ endfunction
|
|||||||
|
|
||||||
function! s:complete_input_history(str,num) abort
|
function! s:complete_input_history(str,num) abort
|
||||||
let results = filter(copy(s:grep_history), "v:val =~# '^' . a:str")
|
let results = filter(copy(s:grep_history), "v:val =~# '^' . a:str")
|
||||||
if len(results) > 0
|
if a:num[0] - a:num[1] == 0
|
||||||
call add(results, a:str)
|
return a:str
|
||||||
|
elseif len(results) > 0
|
||||||
let index = ((len(results) - 1) - a:num[0] + a:num[1]) % len(results)
|
let index = ((len(results) - 1) - a:num[0] + a:num[1]) % len(results)
|
||||||
return results[index]
|
return results[index]
|
||||||
else
|
else
|
||||||
return a:str
|
return a:str
|
||||||
endif
|
endif
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
|
function! Testflygrep(a, b) abort
|
||||||
|
return s:complete_input_history(a:a, a:b)
|
||||||
|
endfunction
|
||||||
|
|
||||||
let s:MPT._function_key = {
|
let s:MPT._function_key = {
|
||||||
\ "\<Tab>" : function('s:next_item'),
|
\ "\<Tab>" : function('s:next_item'),
|
||||||
\ "\<C-j>" : function('s:next_item'),
|
\ "\<C-j>" : function('s:next_item'),
|
||||||
|
Loading…
x
Reference in New Issue
Block a user