1
0
mirror of https://github.com/SpaceVim/SpaceVim.git synced 2025-01-23 17:30:04 +08:00

Add foldsearch layer (#3486)

This commit is contained in:
Wang Shidong 2020-04-26 18:49:22 +08:00 committed by GitHub
parent 091d85f1a0
commit 289601c5e0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 171 additions and 0 deletions

View File

@ -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('<sfile>:~')
let s:lnum = expand('<slnum>') + 2
function! SpaceVim#layers#foldsearch#config()
let g:_spacevim_mappings_space.F = {'name' : '+Foldsearch'}
let lnum = expand('<slnum>') + 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('<sfile>'), '<SNR>\zs\d\+\ze__SID$')
endfunction
let s:_s = '<SNR>' . 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:

View File

@ -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

View File

@ -0,0 +1,34 @@
---
title: "SpaceVim foldsearch 模块"
description: "这一模块为 SpaceVim 提供了 foldsearch 支持,实现的异步搜索折叠的功能。"
lang: zh
---
# [可用模块](../) >> foldsearch
<!-- vim-markdown-toc GFM -->
- [模块简介](#模块简介)
- [启用模块](#启用模块)
- [快捷键](#快捷键)
<!-- vim-markdown-toc -->
## 模块简介
这一模块为 SpaceVim 提供了 foldsearch 支持,实现的异步搜索折叠的功能。
## 启用模块
可通过在配置文件内加入如下配置来启用该模块:
```toml
[[layers]]
name = "foldsearch"
```
## 快捷键
| 快捷键 | 功能描述 |
| --------- | --------------- |
| `SPC F w` | Foldsearch word |

29
docs/layers/foldsearch.md Normal file
View File

@ -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
<!-- vim-markdown-toc GFM -->
- [Description](#description)
- [Key bindings](#key-bindings)
<!-- vim-markdown-toc -->
## 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 |