diff --git a/autoload/SpaceVim/layers/foldsearch.vim b/autoload/SpaceVim/layers/foldsearch.vim new file mode 100644 index 000000000..3637fb59e --- /dev/null +++ b/autoload/SpaceVim/layers/foldsearch.vim @@ -0,0 +1,57 @@ +"============================================================================= +" foldsearch.vim --- Fold search support in SpaceVim +" Copyright (c) 2016-2019 Wang Shidong & Contributors +" Author: Wang Shidong < wsdjeg@outlook.com > +" URL: https://spacevim.org +" License: GPLv3 +"============================================================================= + + +if exists('s:filename') + " @bug s:filename always return 'scheme' + " + " because this script will be loaded twice. This is the feature of vim, + " when call an autoload func, vim will try to load the script again + finish +endif + +let s:filename = expand(':~') +let s:lnum = expand('') + 2 +function! SpaceVim#layers#foldsearch#config() + + let g:_spacevim_mappings_space.F = {'name' : '+Foldsearch'} + let lnum = expand('') + s:lnum - 1 + call SpaceVim#mapping#space#def('nnoremap', ['F', 'w'], 'call call(' + \ . string(s:_function('s:foldsearch_word')) . ', [])', + \ ['foldsearch-word', + \ [ + \ 'SPC F w is to foldsearch input word', + \ '', + \ 'Definition: ' . s:filename . ':' . lnum, + \ ] + \ ], + \ 1) +endfunction + +function! s:foldsearch_word() abort + let word = input('foldsearch word >') + if !empty(word) + call SpaceVim#plugins#foldsearch#word(word) + endif +endfunction + +" function() wrapper +if v:version > 703 || v:version == 703 && has('patch1170') + function! s:_function(fstr) abort + return function(a:fstr) + endfunction +else + function! s:_SID() abort + return matchstr(expand(''), '\zs\d\+\ze__SID$') + endfunction + let s:_s = '' . s:_SID() . '_' + function! s:_function(fstr) abort + return function(substitute(a:fstr, 's:', s:_s, 'g')) + endfunction +endif +" vim:set et sw=2 cc=80: diff --git a/autoload/SpaceVim/plugins/foldsearch.vim b/autoload/SpaceVim/plugins/foldsearch.vim new file mode 100644 index 000000000..14cdeb75e --- /dev/null +++ b/autoload/SpaceVim/plugins/foldsearch.vim @@ -0,0 +1,51 @@ +"============================================================================= +" foldsearch.vim --- async foldsearch plugin +" Copyright (c) 2016-2019 Wang Shidong & Contributors +" Author: Wang Shidong < wsdjeg@outlook.com > +" URL: https://spacevim.org +" License: GPLv3 +"============================================================================= + +let s:JOB = SpaceVim#api#import('job') +let s:matched_lines = [] + +function! SpaceVim#plugins#foldsearch#word(word) + let argv = ['rg', '--line-number', a:word] + call s:foldsearch(argv) +endfunction + +function! SpaceVim#plugins#foldsearch#expr(expr) + let argv = ['rg', '--line-number', a:expr] + call s:foldsearch(argv) +endfunction + +function! s:foldsearch(argv) abort + let s:matched_lines = [] + let jobid = s:JOB.start(a:argv, { + \ 'on_stdout' : function('s:std_out'), + \ 'on_exit' : function('s:exit'), + \ }) + + call s:JOB.send(jobid, call('getline', [1, '$'])) + call s:JOB.chanclose(jobid, 'stdin') +endfunction + +function! s:std_out(id, data, event) abort + for line in filter(a:data, '!empty(v:val)') + call add(s:matched_lines, str2nr(matchstr(line, '^\d\+'))) + endfor +endfunction + +function! s:exit(id, data, event) abort + echom string(s:matched_lines) + let preview = 0 + for nr in s:matched_lines + if nr - preview >= 3 " first matched line is 3 + exe (preview + 1) . ',' . (nr - 1) . ':fold' + endif + let preview = nr + endfor + if line('$') - preview >=3 + exe (preview + 1) . ',' . line('$') . ':fold' + endif +endfunction diff --git a/docs/cn/layers/foldsearch.md b/docs/cn/layers/foldsearch.md new file mode 100644 index 000000000..ee5153160 --- /dev/null +++ b/docs/cn/layers/foldsearch.md @@ -0,0 +1,34 @@ +--- +title: "SpaceVim foldsearch 模块" +description: "这一模块为 SpaceVim 提供了 foldsearch 支持,实现的异步搜索折叠的功能。" +lang: zh +--- + +# [可用模块](../) >> foldsearch + + + +- [模块简介](#模块简介) +- [启用模块](#启用模块) +- [快捷键](#快捷键) + + + +## 模块简介 + +这一模块为 SpaceVim 提供了 foldsearch 支持,实现的异步搜索折叠的功能。 + +## 启用模块 + +可通过在配置文件内加入如下配置来启用该模块: + +```toml +[[layers]] + name = "foldsearch" +``` + +## 快捷键 + +| 快捷键 | 功能描述 | +| --------- | --------------- | +| `SPC F w` | Foldsearch word | diff --git a/docs/layers/foldsearch.md b/docs/layers/foldsearch.md new file mode 100644 index 000000000..96cb748e7 --- /dev/null +++ b/docs/layers/foldsearch.md @@ -0,0 +1,29 @@ +--- +title: "SpaceVim foldsearch layer" +description: "This layer provides functions that fold away lines that don't match a specific search pattern." +--- + +# [Available Layers](../) >> foldsearch + + + +- [Description](#description) +- [Key bindings](#key-bindings) + + + +## Description + +This layer provides functions that fold away lines that don't match a specific search pattern. +The search pattern can be a word or a regular expression. To enable this layer: + +```toml +[layers] + name = "foldsearch" +``` + +## Key bindings + +| Key bindings | Description | +| ------------ | --------------------- | +| `SPC F w` | foldsearch input word |