From faa0a92a4abd83ea8eb107b770cc3bf58897313a Mon Sep 17 00:00:00 2001 From: Wang Shidong Date: Sat, 15 Jun 2019 13:23:05 +0800 Subject: [PATCH] Improve searching histroy completion in flygrep (#2888) * Improve searching histroy completion in flygrep * Cache flygrep history * Update history function --- autoload/SpaceVim/plugins/flygrep.vim | 36 +++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/autoload/SpaceVim/plugins/flygrep.vim b/autoload/SpaceVim/plugins/flygrep.vim index e1179ea8c..ca1a38628 100644 --- a/autoload/SpaceVim/plugins/flygrep.vim +++ b/autoload/SpaceVim/plugins/flygrep.vim @@ -15,6 +15,7 @@ let s:BUFFER = SpaceVim#api#import('vim#buffer') let s:LIST = SpaceVim#api#import('data#list') let s:HI = SpaceVim#api#import('vim#highlight') let s:FLOATING = SpaceVim#api#import('neovim#floating') +let s:JSON = SpaceVim#api#import('data#json') " }}} let s:grepid = 0 @@ -33,7 +34,26 @@ let [ let s:grep_timer_id = -1 let s:preview_timer_id = -1 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] " }}} @@ -48,6 +68,7 @@ function! s:grep_timer(timer) abort endif let cmd = s:get_search_cmd(s:current_grep_pattern) call SpaceVim#logger#info('grep cmd: ' . string(cmd)) + call s:update_history() let s:grepid = s:JOB.start(cmd, { \ 'on_stdout' : function('s:grep_stdout'), \ '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:preview_timer_id) normal! "_ggdG + let s:complete_input_history_num = [0,0] endfunction let s:MPT._oninputpro = function('s:close_grep_job') @@ -390,7 +412,6 @@ function! s:previous_item() abort endfunction function! s:open_item() abort - call add(s:grep_history, s:grep_expr) let s:MPT._handle_fly = function('s:flygrep') if getline('.') !=# '' if s:grepid != 0 @@ -522,7 +543,6 @@ function! s:previous_match_history() abort endfunction function! s:next_match_history() abort - if s:complete_input_history_num == [0,0] let s:complete_input_history_base = s:MPT._prompt.begin let s:MPT._prompt.cursor = '' @@ -538,14 +558,20 @@ 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) + if a:num[0] - a:num[1] == 0 + return a:str + elseif len(results) > 0 let index = ((len(results) - 1) - a:num[0] + a:num[1]) % len(results) return results[index] else return a:str endif endfunction + +function! Testflygrep(a, b) abort + return s:complete_input_history(a:a, a:b) +endfunction + let s:MPT._function_key = { \ "\" : function('s:next_item'), \ "\" : function('s:next_item'),