Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pick the dynamic filter from #353 #364

Merged
merged 55 commits into from
Apr 4, 2020
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
55 commits
Select commit Hold shift + click to select a range
1ba2b2c
Pick dyn filter code
liuchengxu Mar 29, 2020
4a15231
Replaced f64 scoring with i32.
ImmemorConsultrixContrarie Mar 25, 2020
a4bb575
Make it compile
liuchengxu Mar 29, 2020
12a8d47
Intergate with vim initially
liuchengxu Mar 29, 2020
6782304
Change ITEMS_TO_SHOW to 100
liuchengxu Mar 29, 2020
595daba
Split filter sync/async
liuchengxu Mar 29, 2020
1906bee
Move to filter/async/dyn.vim
liuchengxu Mar 29, 2020
e6c28fc
Split out print_top_items()
liuchengxu Mar 30, 2020
3393700
.
liuchengxu Mar 30, 2020
7d40877
Split out select_top_items_to_show()
liuchengxu Mar 30, 2020
894e9a5
Extract find_bast_score_idx() and try_notify_top_results()
liuchengxu Mar 30, 2020
7b7a2e1
Fix vint
liuchengxu Mar 30, 2020
d7bdfea
Pass enable_icon to try_notify_top_results()
liuchengxu Mar 30, 2020
f7bb807
Fix sorting glitch in top scores
liuchengxu Mar 30, 2020
f3b0300
Pass --enable-icon in vimscript
liuchengxu Mar 30, 2020
0522228
Update CHANGELOG
liuchengxu Mar 30, 2020
29b88b5
Support neovim too
liuchengxu Mar 30, 2020
7970cc1
Process truncated_map in vim
liuchengxu Mar 30, 2020
aece025
Introduce Source::Exec
liuchengxu Mar 31, 2020
b849ba1
Commit state.vim
liuchengxu Mar 31, 2020
a8fdd39
Fix vint
liuchengxu Mar 31, 2020
c6f5ba6
Split out clap#state#handle_message()
liuchengxu Mar 31, 2020
e1a2f2a
Pass cmd into clap#filter#async#start(cmd)
liuchengxu Mar 31, 2020
742732a
Try using dyn filter on grep provider
liuchengxu Mar 31, 2020
4aea5c7
.
liuchengxu Mar 31, 2020
7bd0eba
Nits
liuchengxu Mar 31, 2020
d817dde
Merge branch 'master' of https://github.com/liuchengxu/vim-clap into …
liuchengxu Mar 31, 2020
e4bb2c7
Use vim job's native api
liuchengxu Apr 1, 2020
eec7f1d
Merge branch 'master' of https://github.com/liuchengxu/vim-clap into …
liuchengxu Apr 1, 2020
5ba6eb7
Add enable_dyn to default-features in fuzzy_filter
liuchengxu Apr 1, 2020
72c1561
Merge branch 'master' of https://github.com/liuchengxu/vim-clap into …
liuchengxu Apr 1, 2020
1be30c8
Nits
liuchengxu Apr 2, 2020
0fb5979
Use systemlist()
liuchengxu Apr 2, 2020
6b14021
Merge branch 'master' of https://github.com/liuchengxu/vim-clap into …
liuchengxu Apr 2, 2020
cde7b0e
Merge branch 'master' of https://github.com/liuchengxu/vim-clap into …
liuchengxu Apr 2, 2020
d436afe
Only when using maple
liuchengxu Apr 2, 2020
6cf6197
Split out job/stdio.vim
liuchengxu Apr 2, 2020
820ac95
Clean up dyn.vim
liuchengxu Apr 2, 2020
5d8f729
Remove rpc.vim
liuchengxu Apr 2, 2020
c0cc692
Add job/stdio.vim
liuchengxu Apr 2, 2020
9938aa1
Fix switching between files and history provider
liuchengxu Apr 3, 2020
c0fae09
Merge branch 'master' of https://github.com/liuchengxu/vim-clap into …
liuchengxu Apr 3, 2020
a72ee23
Check if has g:__clap_forerunner_tempfile
liuchengxu Apr 3, 2020
c2e1d11
Clear g:__clap_provider_cwd on exit
liuchengxu Apr 3, 2020
c715aa4
Add clap#state#refresh_matches_count_on_forerunner_done()
liuchengxu Apr 3, 2020
0c940a7
Fix vint
liuchengxu Apr 3, 2020
5a57f0d
.
liuchengxu Apr 3, 2020
f8a3184
Make maple.vim run filter --sync
liuchengxu Apr 3, 2020
be5b05b
.
liuchengxu Apr 3, 2020
266ab2a
.
liuchengxu Apr 3, 2020
adbbd28
Reset g:__clap_provider_cwd on start
liuchengxu Apr 3, 2020
ac771aa
.
liuchengxu Apr 3, 2020
8525cd0
Put s:clear_state() before setting provider source_type
liuchengxu Apr 3, 2020
b47fbb1
Extract clap#state#clear_pre() and clap#state#clear_post()
liuchengxu Apr 4, 2020
bd77cba
.
liuchengxu Apr 4, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ CHANGELOG
- Print a note about Rust nightly is requred for building the Python dynamic module.
- Refine the syntax of `Clap lines` with `ClapLinesBufname` and `ClapLinesNumber` group added.
- [perf] Use const table instead of `lazy_static` for the icons, [more info](https://github.com/liuchengxu/vim-clap/pull/354#discussion_r395975392). Thanks to @ImmemorConsultrixContrarie .
- [perf] Major improvement :tada: support the filter dynamic support, contribution by @ImmemorConsultrixContrarie. ([#364](https://github.com/liuchengxu/vim-clap/pull/364))

### Fixed

Expand Down
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion autoload/clap.vim
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ function! s:inject_default_impl_is_ok(provider_info) abort
let provider_info.on_typed = function('clap#impl#on_typed')
endif
if !has_key(provider_info, 'filter')
let provider_info.filter = function('clap#filter#')
let provider_info.filter = function('clap#filter#sync')
endif
else
if !has_key(provider_info, 'on_typed')
Expand Down
16 changes: 10 additions & 6 deletions autoload/clap/api.vim
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,11 @@ if s:is_nvim
endfunction

function! s:_win_is_valid() dict abort
return nvim_win_is_valid(self.winid)
if self.winid == -1
return v:false
else
return nvim_win_is_valid(self.winid)
endif
endfunction
else
function! s:_get_lines() dict abort
Expand Down Expand Up @@ -239,7 +243,7 @@ function! s:init_display() abort
" Optional argument: pattern to match
" Default: input
function! display.add_highlight(...) abort
let pattern = a:0 > 0 ? a:1 : clap#filter#viml#matchadd_pattern()
let pattern = a:0 > 0 ? a:1 : clap#filter#sync#viml#matchadd_pattern()
if empty(pattern)
return
endif
Expand Down Expand Up @@ -417,7 +421,7 @@ function! s:init_provider() abort
" Currently only maple extension supports --input option, for the other
" external filter, use cat instead.
function! s:read_from_file_or_pipe(ext_filter_cmd, input_file) abort
if clap#filter#external#using_maple()
if clap#filter#async#external#using_maple()
if g:clap.provider.id ==# 'blines'
return printf('%s %s', a:ext_filter_cmd, a:input_file)
endif
Expand All @@ -430,7 +434,7 @@ function! s:init_provider() abort

" Pipe the source into the external filter
function! s:wrap_async_cmd(source_cmd) abort
let ext_filter_cmd = clap#filter#external#get_cmd_or_default()
let ext_filter_cmd = clap#filter#async#external#get_cmd_or_default()
if exists('g:__clap_forerunner_tempfile')
let cmd = s:read_from_file_or_pipe(ext_filter_cmd, g:__clap_forerunner_tempfile)
else
Expand Down Expand Up @@ -493,7 +497,7 @@ function! s:init_provider() abort
let lines = copy(Source())
endif

let ext_filter_cmd = clap#filter#external#get_cmd_or_default()
let ext_filter_cmd = clap#filter#async#external#get_cmd_or_default()

let tmp = s:into_source_tmp_file(lines)
let cmd = s:read_from_file_or_pipe(ext_filter_cmd, tmp)
Expand Down Expand Up @@ -566,7 +570,7 @@ function! s:init_provider() abort
function! provider.can_async() abort
" The default async implementation is not doable and the provider does not
" provide a source_async implementation explicitly.
if !clap#filter#external#has_default() && !has_key(self._(), 'source_async')
if !clap#filter#async#external#has_default() && !has_key(self._(), 'source_async')
return v:false
else
return !get(g:, 'clap_disable_optional_async', v:false)
Expand Down
2 changes: 1 addition & 1 deletion autoload/clap/debugging.vim
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ function! clap#debugging#info() abort
echohl Normal | echon has('python3') | echohl NONE

echohl Type | echo 'has py dynamic module: ' | echohl NONE
echohl Normal | echon clap#filter#python#has_dynamic_module() | echohl NONE
echohl Normal | echon clap#filter#sync#python#has_dynamic_module() | echohl NONE

echohl Type | echo ' Current FileType: ' | echohl NONE
echohl Normal | echon &filetype | echohl NONE
Expand Down
6 changes: 3 additions & 3 deletions autoload/clap/dispatcher.vim
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ if has('nvim')

function! s:set_matches_count() abort
let matches_count = s:loaded_size + s:dropped_size
call clap#impl#refresh_matches_count(string(matches_count))
call clap#state#refresh_matches_count(string(matches_count))
endfunction
else
function! s:handle_cache(to_cache) abort
Expand All @@ -52,7 +52,7 @@ if has('nvim')

function! s:set_matches_count() abort
let matches_count = s:loaded_size + len(g:clap.display.cache)
call clap#impl#refresh_matches_count(string(matches_count))
call clap#state#refresh_matches_count(string(matches_count))
endfunction
endif

Expand Down Expand Up @@ -168,7 +168,7 @@ else
let matches_count = g:clap.display.line_count()
endif

call clap#impl#refresh_matches_count(string(matches_count))
call clap#state#refresh_matches_count(string(matches_count))
endfunction

function! s:post_check() abort
Expand Down
18 changes: 9 additions & 9 deletions autoload/clap/filter.vim
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ let s:has_py_dynamic_module = v:false

if s:has_python
try
let s:has_py_dynamic_module = clap#filter#python#has_dynamic_module()
let s:has_py_dynamic_module = clap#filter#sync#python#has_dynamic_module()
let s:can_use_python = v:true
catch
call clap#helper#echo_error(v:exception)
Expand Down Expand Up @@ -43,17 +43,17 @@ if s:can_use_python
return clap#highlight#provider_has_offset() ? v:true : v:false
endfunction

function! clap#filter#(query, candidates) abort
function! clap#filter#sync(query, candidates) abort
try
return clap#filter#python#(a:query, a:candidates, winwidth(g:clap.display.winid), s:enable_icon())
return clap#filter#sync#python#(a:query, a:candidates, winwidth(g:clap.display.winid), s:enable_icon())
catch
call clap#helper#echo_error(v:exception)
return clap#filter#viml#(a:query, a:candidates)
return clap#filter#sync#viml#(a:query, a:candidates)
endtry
endfunction
else
function! clap#filter#(query, candidates) abort
return clap#filter#viml#(a:query, a:candidates)
function! clap#filter#sync(query, candidates) abort
return clap#filter#sync#viml#(a:query, a:candidates)
endfunction
endif

Expand All @@ -64,15 +64,15 @@ function! clap#filter#on_typed(FilterFn, query, candidates) abort
let l:lines = [g:clap_no_matches_msg]
let g:__clap_has_no_matches = v:true
call g:clap.display.set_lines_lazy(lines)
" In clap#impl#refresh_matches_count() we reset the sign to the first line,
" In clap#state#refresh_matches_count() we reset the sign to the first line,
" But the signs are seemingly removed when setting the lines, so we should
" postpone the sign update.
call clap#impl#refresh_matches_count('0')
call clap#state#refresh_matches_count('0')
call g:clap.preview.hide()
else
let g:__clap_has_no_matches = v:false
call g:clap.display.set_lines_lazy(lines)
call clap#impl#refresh_matches_count(string(len(l:lines)))
call clap#state#refresh_matches_count(string(len(l:lines)))
endif

call g:clap#display_win.shrink_if_undersize()
Expand Down
147 changes: 147 additions & 0 deletions autoload/clap/filter/async/dyn.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
" Author: liuchengxu <xuliuchengxlc@gmail.com>
" Description: Dynamic update version of maple filter.

let s:save_cpo = &cpoptions
set cpoptions&vim

let s:job_id = -1

if has('nvim')

let s:round_message = ''
let s:content_length = 0

function! s:handle_stdout(lines) abort
while !empty(a:lines)
let line = remove(a:lines, 0)

if line ==# ''
continue
elseif s:content_length == 0
if line =~# '^Content-length:'
let s:content_length = str2nr(matchstr(line, '\d\+$'))
else
call clap#helper#echo_error('This should not happen, unknown message:'.line)
endif
continue
endif

if s:content_length < strlen(l:line)
let s:round_message .= strpart(line, 0, s:content_length)
call insert(a:lines, strpart(line, s:content_length))
let s:content_length = 0
else
let s:round_message .= line
let s:content_length -= strlen(l:line)
endif

" The message for this round is still incomplete, contintue to read more.
if s:content_length > 0
continue
endif

try
call s:MessageHandler(trim(s:round_message))
catch
call clap#helper#echo_error('[dyn]Failed to handle message:'.v:exception.', throwpoint:'.v:throwpoint)
finally
let s:round_message = ''
endtry

endwhile
endfunction

function! s:on_event(job_id, data, event) abort
" We only process the job that was spawned last time.
if a:job_id == s:job_id
if a:event ==# 'stdout'
call s:handle_stdout(a:data)
elseif a:event ==# 'stderr'
if a:data == ['']
return
endif
" call clap#helper#echo_error('[dyn]on_event:'.string(a:data))
endif
endif
endfunction

function! s:start_dyn_filter_job(cmd) abort
let s:job_id = jobstart(a:cmd, {
\ 'on_exit': function('s:on_event'),
\ 'on_stdout': function('s:on_event'),
\ 'on_stderr': function('s:on_event'),
\ })
endfunction

else

function! s:out_cb(channel, message) abort
if s:job_id > 0 && clap#job#vim8_job_id_of(a:channel) == s:job_id
if a:message =~# '^Content-length:' || a:message ==# ''
return
endif
try
call s:MessageHandler(a:message)
catch
call clap#helper#echo_error('[dyn]Failed to handle message:'.a:message.', exception:'.v:exception.', '.v:throwpoint)
endtry
endif
endfunction

function! s:err_cb(channel, message) abort
if s:job_id > 0 && clap#job#vim8_job_id_of(a:channel) == s:job_id
call clap#helper#echo_error(a:message)
endif
endfunction

function! s:start_dyn_filter_job(cmd) abort
let job = job_start(clap#job#wrap_cmd(a:cmd), {
\ 'err_cb': function('s:err_cb'),
\ 'out_cb': function('s:out_cb'),
\ 'noblock': 1,
\ })
let s:job_id = clap#job#parse_vim8_job_id(string(job))
endfunction

endif

function! s:handle_message(msg) abort
if !g:clap.display.win_is_valid()
\ || g:clap.input.get() !=# s:last_query
return
endif

call clap#state#handle_message(a:msg)
endfunction

function! s:job_stop() abort
if s:job_id > 0
call clap#job#stop(s:job_id)
let s:job_id = -1
endif
endfunction

let s:MessageHandler = function('s:handle_message')

function! clap#filter#async#dyn#start(cmd) abort
call s:job_stop()

let s:last_query = g:clap.input.get()

let cmd_dir = clap#rooter#working_dir()
let filter_cmd = printf('%s --number 100 --winwidth %d filter "%s" --cmd "%s" --cmd-dir "%s"',
\ g:clap_enable_icon ? '--enable-icon' : '',
\ winwidth(g:clap.display.winid),
\ g:clap.input.get(),
\ a:cmd,
\ cmd_dir,
\ )

echom "cmd:".filter_cmd
liuchengxu marked this conversation as resolved.
Show resolved Hide resolved
let maple_cmd = clap#maple#build_cmd(filter_cmd)

call s:start_dyn_filter_job(maple_cmd)
endfunction

let &cpoptions = s:save_cpo
unlet s:save_cpo
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ function! s:cmd_of(ext_filter) abort
endif
endfunction

function! clap#filter#external#has_default() abort
function! clap#filter#async#external#has_default() abort
return s:default_ext_filter isnot v:null
endfunction

Expand All @@ -72,7 +72,7 @@ function! s:default_external_cmd() abort
endfunction

" Filter using the external tools given the current input.
function! clap#filter#external#get_cmd_or_default() abort
function! clap#filter#async#external#get_cmd_or_default() abort
let external_filter = s:get_external_filter()

if external_filter isnot v:null
Expand All @@ -82,7 +82,7 @@ function! clap#filter#external#get_cmd_or_default() abort
return s:default_external_cmd()
endfunction

function! clap#filter#external#using_maple() abort
function! clap#filter#async#external#using_maple() abort
return s:cur_ext_filter ==# 'maple'
endfunction

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ endif

execute s:py_exe 'from clap.fzy import' s:py_fn

function! clap#filter#python#has_dynamic_module() abort
function! clap#filter#sync#python#has_dynamic_module() abort
return s:has_py_dynamic_module
endfunction

if s:using_dynamic_module
" Rust dynamic module has the feature of truncating the long lines to make fuzzy matched items visible.
function! clap#filter#python#(query, candidates, winwidth, enable_icon) abort
function! clap#filter#sync#python#(query, candidates, winwidth, enable_icon) abort
" If the query is empty, neovim and vim's python client might crash.
if a:query ==# ''
return a:candidates
Expand All @@ -53,7 +53,7 @@ if s:using_dynamic_module
return filtered
endfunction
else
function! clap#filter#python#(query, candidates, _winwidth, enable_icon) abort
function! clap#filter#sync#python#(query, candidates, _winwidth, enable_icon) abort
let [g:__clap_fuzzy_matched_indices, filtered] = pyxeval(s:py_fn.'()')
return filtered
endfunction
Expand Down
Loading