Skip to content
New issue

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

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

Already on GitHub? # to your account

Split out filter native viml impl #267

Merged
merged 2 commits into from
Jan 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ CHANGELOG
- Add `maple version` to get the detailed maple info and include it in `:Clap debug`.([#262](https://github.com/liuchengxu/vim-clap/pull/262))
- Add `g:clap_forerunner_status_sign` and deprecate `g:clap_forerunner_status_sign_done` and `g:clap_forerunner_status_sign_running`.

### Internal

- Split out the native VimScript filter implementation in favor of the potential vim9 improvement.([#267](https://github.com/liuchengxu/vim-clap/pull/267))

### Changed

- Use  as the icon of markdown.
Expand Down
2 changes: 1 addition & 1 deletion autoload/clap/api.vim
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,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#matchadd_pattern()
let pattern = a:0 > 0 ? a:1 : clap#filter#viml#matchadd_pattern()
if empty(pattern)
return
endif
Expand Down
57 changes: 2 additions & 55 deletions autoload/clap/filter.vim
Original file line number Diff line number Diff line change
Expand Up @@ -4,66 +4,13 @@
let s:save_cpo = &cpoptions
set cpoptions&vim

let s:pattern_builder = {}

if has('python3') || has('python')
let s:py_exe = has('python3') ? 'python3' : 'python'
let s:pyfile = has('python3') ? 'py3file' : 'pyfile'
else
let s:py_exe = v:null
endif

function! s:pattern_builder._force_case() abort
" Smart case
if self.input =~? '\u'
return '\C'
else
return '\c'
endif
endfunction

function! s:pattern_builder.smartcase() abort
let l:_force_case = self._force_case()
let s:matchadd_pattern = l:_force_case.self.input
return l:_force_case.self.input
endfunction

function! s:pattern_builder.substring() abort
let l:_force_case = self._force_case()
let l:filter_pattern = ['\V\^', l:_force_case]
let s:matchadd_pattern = []
for l:s in split(self.input)
call add(filter_pattern, printf('\.\*\zs%s\ze', l:s))
" FIXME can not distinguish `f f` highlight
" these two f should be highlighed with different colors
call add(s:matchadd_pattern, l:_force_case.l:s)
endfor
return join(l:filter_pattern, '')
endfunction

function! s:pattern_builder.build() abort
if stridx(self.input, ' ') != -1
return self.substring()
else
return self.smartcase()
endif
endfunction

" Return substring pattern or the smartcase input pattern.
function! clap#filter#matchadd_pattern() abort
return get(s:, 'matchadd_pattern', '')
endfunction

function! s:filter(line, pattern) abort
return a:line =~ a:pattern
endfunction

function! s:fallback_filter(query, candidates) abort
let s:pattern_builder.input = a:query
let l:filter_pattern = s:pattern_builder.build()
return filter(copy(a:candidates), 's:filter(v:val, l:filter_pattern)')
endfunction

let s:can_use_python = v:false
let s:has_py_dynamic_module = v:false

Expand Down Expand Up @@ -137,12 +84,12 @@ if s:can_use_python
return s:ext_filter(a:query, a:candidates)
catch
call clap#helper#echo_error(v:exception)
return s:fallback_filter(a:query, a:candidates)
return clap#filter#viml#(a:query, a:candidates)
endtry
endfunction
else
function! clap#filter#(query, candidates) abort
return s:fallback_filter(a:query, a:candidates)
return clap#filter#viml#(a:query, a:candidates)
endfunction
endif

Expand Down
62 changes: 62 additions & 0 deletions autoload/clap/filter/viml.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
" Author: liuchengxu <xuliuchengxlc@gmail.com>
" Description: Native VimL implementation of filter.
" Used when there is no +python3 and external binary.

let s:save_cpo = &cpoptions
set cpoptions&vim

let s:pattern_builder = {}

function! s:pattern_builder._force_case() abort
" Smart case
if self.input =~? '\u'
return '\C'
else
return '\c'
endif
endfunction

function! s:pattern_builder.smartcase() abort
let l:_force_case = self._force_case()
let s:matchadd_pattern = l:_force_case.self.input
return l:_force_case.self.input
endfunction

function! s:pattern_builder.substring() abort
let l:_force_case = self._force_case()
let l:filter_pattern = ['\V\^', l:_force_case]
let s:matchadd_pattern = []
for l:s in split(self.input)
call add(filter_pattern, printf('\.\*\zs%s\ze', l:s))
" FIXME can not distinguish `f f` highlight
" these two f should be highlighed with different colors
call add(s:matchadd_pattern, l:_force_case.l:s)
endfor
return join(l:filter_pattern, '')
endfunction

function! s:pattern_builder.build() abort
if stridx(self.input, ' ') != -1
return self.substring()
else
return self.smartcase()
endif
endfunction

function! s:filter(line, pattern) abort
return a:line =~ a:pattern
endfunction

" Return substring pattern or the smartcase input pattern.
function! clap#filter#viml#matchadd_pattern() abort
return get(s:, 'matchadd_pattern', '')
endfunction

function! clap#filter#viml#(query, candidates) abort
let s:pattern_builder.input = a:query
let l:filter_pattern = s:pattern_builder.build()
return filter(copy(a:candidates), 's:filter(v:val, l:filter_pattern)')
endfunction

let &cpoptions = s:save_cpo
unlet s:save_cpo