From acdcfa5aae0e9aa5702c092121f005a2931ae1b1 Mon Sep 17 00:00:00 2001 From: Wang Shidong Date: Wed, 7 Oct 2020 13:31:10 +0800 Subject: [PATCH] Add quickfix support for flygrep (#3872) --- autoload/SpaceVim/api/vim/buffer.vim | 24 ++++++++++++++ autoload/SpaceVim/plugins/flygrep.vim | 25 +++++++++++++++ .../2018-01-23-grep-on-the-fly-in-spacevim.md | 31 ++++++++++--------- .../2018-01-31-grep-on-the-fly-in-spacevim.md | 31 ++++++++++--------- docs/cn/documentation.md | 29 +++++++++-------- docs/documentation.md | 29 +++++++++-------- test/api/vim/buffer.vader | 8 +++++ 7 files changed, 121 insertions(+), 56 deletions(-) diff --git a/autoload/SpaceVim/api/vim/buffer.vim b/autoload/SpaceVim/api/vim/buffer.vim index 13a27074a..4faa28586 100644 --- a/autoload/SpaceVim/api/vim/buffer.vim +++ b/autoload/SpaceVim/api/vim/buffer.vim @@ -280,6 +280,30 @@ function! s:self.add_highlight(bufnr, hl, line, col, long) abort endfunction +function! s:self.buf_get_lines(bufnr, start, end, strict_indexing) abort + if exists('*nvim_buf_get_lines') + return nvim_buf_get_lines(a:bufnr, a:start, a:end, a:strict_indexing) + elseif exists('*getbufline') && exists('*bufload') && exists('*bufloaded') + let lct = self.line_count(a:bufnr) + if a:start > lct + return + elseif a:start >= 0 && a:end > a:start + " in vim, getbufline will not load buffer automatically + " but in neovim, nvim_buf_set_lines will do it. + " @fixme vim issue #5044 + " https://github.com/vim/vim/issues/5044 + if !bufloaded(a:bufnr) + call bufload(a:bufnr) + endif + return getbufline(a:bufnr, a:start + 1, a:end + 1) + elseif a:start >= 0 && a:end < 0 && lct + a:end >= a:start + return self.buf_get_lines(a:bufnr, a:start, lct + a:end + 1, a:strict_indexing) + elseif a:start <= 0 && a:end > a:start && a:end < 0 && lct + a:start >= 0 + return self.buf_get_lines(a:bufnr, lct + a:start + 1, lct + a:end + 2, a:strict_indexing) + endif + endif +endfunction + fu! SpaceVim#api#vim#buffer#get() abort return deepcopy(s:self) diff --git a/autoload/SpaceVim/plugins/flygrep.vim b/autoload/SpaceVim/plugins/flygrep.vim index d3c42b1c6..2194b2eb4 100644 --- a/autoload/SpaceVim/plugins/flygrep.vim +++ b/autoload/SpaceVim/plugins/flygrep.vim @@ -578,6 +578,30 @@ function! s:open_item_horizontally() abort endif endfunction +function! s:apply_to_quickfix() abort + let s:MPT._handle_fly = function('s:flygrep') + if getline('.') !=# '' + if s:grepid != 0 + call s:JOB.stop(s:grepid) + endif + let s:MPT._quit = 1 + if s:preview_able == 1 + call s:close_preview_win() + endif + let s:preview_able = 0 + let searching_result = s:BUFFER.buf_get_lines(s:buffer_id, 0, -1, 0) + noautocmd q + call s:update_history() + if !empty(searching_result) + cgetexpr join(searching_result, "\n") + call setqflist([], 'a', {'title' : 'FlyGrep partten:' . s:MPT._prompt.begin . s:MPT._prompt.cursor .s:MPT._prompt.end}) + call s:MPT._clear_prompt() + copen + endif + noautocmd normal! : + endif +endfunction + function! s:double_click() abort if line('.') !=# '' if s:grepid != 0 @@ -764,6 +788,7 @@ let s:MPT._function_key = { \ "\" : function('s:start_filter'), \ "\" : function('s:open_item_vertically'), \ "\" : function('s:open_item_horizontally'), + \ "\" : function('s:apply_to_quickfix'), \ "\" : function('s:start_replace'), \ "\" : function('s:toggle_preview'), \ "\" : function('s:toggle_expr_mode'), diff --git a/docs/_posts/2018-01-23-grep-on-the-fly-in-spacevim.md b/docs/_posts/2018-01-23-grep-on-the-fly-in-spacevim.md index de42d85e4..f32cfcbcc 100644 --- a/docs/_posts/2018-01-23-grep-on-the-fly-in-spacevim.md +++ b/docs/_posts/2018-01-23-grep-on-the-fly-in-spacevim.md @@ -82,18 +82,19 @@ The available scopes and corresponding keys are: **Within FlyGrep buffer:** -| Key Bindings | Descriptions | -| ----------------- | --------------------------------- | -| `` | close FlyGrep buffer | -| `` | open item in current window | -| `Ctrl-t` | open item in new tab | -| `` | move cursor line down | -| `Ctrl-j` | move cursor line down | -| `Shift-` | move cursor line up | -| `Ctrl-k` | move cursor line up | -| `` | remove last character | -| `Ctrl-w` | remove the word before the cursor | -| `Ctrl-u` | remove the line before the cursor | -| `Ctrl-k` | remove the line after the cursor | -| `Ctrl-a`/`` | Go to the beginning of the line | -| `Ctrl-e`/`` | Go to the end of the line | +| Key Bindings | Descriptions | +| ------------------- | ---------------------------------- | +| `` | close FlyGrep buffer | +| `` | open file at the cursor line | +| `Ctrl-t` | open item in new tab | +| `Ctrl-s` | open item in split window | +| `Ctrl-v` | open item in vertical split window | +| `Ctrl-q` | apply all items into quickfix | +| `` | move cursor line down | +| `Shift-` | move cursor line up | +| `` | remove last character | +| `Ctrl-w` | remove the Word before the cursor | +| `Ctrl-u` | remove the Line before the cursor | +| `Ctrl-k` | remove the Line after the cursor | +| `Ctrl-a` / `` | Go to the beginning of the line | +| `Ctrl-e` / `` | Go to the end of the line | diff --git a/docs/_posts/2018-01-31-grep-on-the-fly-in-spacevim.md b/docs/_posts/2018-01-31-grep-on-the-fly-in-spacevim.md index ec7c230b5..1e947f0a5 100644 --- a/docs/_posts/2018-01-31-grep-on-the-fly-in-spacevim.md +++ b/docs/_posts/2018-01-31-grep-on-the-fly-in-spacevim.md @@ -96,18 +96,19 @@ SpaceVim 中的搜索命令以 `SPC s` 为前缀,前一个键是使用的工 在 FlyGrep 内的快捷键如下: -| 快捷键 | 功能描述 | -| ------------------- | -------------------- | -| `` | 关闭 FlyGrep 窗口 | -| `` | 在当前窗口打开选中项 | -| `Ctrl-t` | 在新标签栏打开选中项 | -| `` | 移动至下一行 | -| `Ctrl-j` | 移动至下一行 | -| `` | 移动至上一行 | -| `Ctrl-k` | 移动至上一行 | -| `` | 删除光标前一个字符 | -| `Ctrl-w` | 删除光标后的单词 | -| `Ctrl-u` | 删除光标前所有字符 | -| `Ctrl-k` | 删除光标后所有字符 | -| `Ctrl-a` / `` | 将光标定位到行首 | -| `Ctrl-e` / `` | 将光标定位到行尾 | +| 快捷键 | 功能描述 | +| ------------------- | ------------------------- | +| `` | 关闭搜索窗口 | +| `` | 打开当前选中的文件位置 | +| `Ctrl-t` | 在新标签栏打开选中项 | +| `Ctrl-s` | 在分屏打开选中项 | +| `Ctrl-v` | 在垂直分屏打开选中项 | +| `Ctrl-q` | 将搜索结果转移至 quickfix | +| `` | 选中下一行文件位置 | +| `Shift-` | 选中上一行文件位置 | +| `` | 删除上一个输入字符 | +| `Ctrl-w` | 删除光标前的单词 | +| `Ctrl-u` | 删除光标前所有内容 | +| `Ctrl-k` | 删除光标后所有内容 | +| `Ctrl-a` / `` | 将光标移至行首 | +| `Ctrl-e` / `` | 将光标移至行尾 | diff --git a/docs/cn/documentation.md b/docs/cn/documentation.md index 025273cbd..0d6979459 100644 --- a/docs/cn/documentation.md +++ b/docs/cn/documentation.md @@ -1566,19 +1566,22 @@ endfunction Flygrep 搜索窗口结果窗口内的常用快捷键: -| 快捷键 | 功能描述 | -| ------------------- | ---------------------- | -| `` | 关闭搜索窗口 | -| `` | 打开当前选中的文件位置 | -| `Ctrl-t` | 在新标签栏打开选中项 | -| `` | 选中下一行文件位置 | -| `Shift-` | 选中上一行文件位置 | -| `` | 删除上一个输入字符 | -| `Ctrl-w` | 删除光标前的单词 | -| `Ctrl-u` | 删除光标前所有内容 | -| `Ctrl-k` | 删除光标后所有内容 | -| `Ctrl-a` / `` | 将光标移至行首 | -| `Ctrl-e` / `` | 将光标移至行尾 | +| 快捷键 | 功能描述 | +| ------------------- | ------------------------- | +| `` | 关闭搜索窗口 | +| `` | 打开当前选中的文件位置 | +| `Ctrl-t` | 在新标签栏打开选中项 | +| `Ctrl-s` | 在分屏打开选中项 | +| `Ctrl-v` | 在垂直分屏打开选中项 | +| `Ctrl-q` | 将搜索结果转移至 quickfix | +| `` | 选中下一行文件位置 | +| `Shift-` | 选中上一行文件位置 | +| `` | 删除上一个输入字符 | +| `Ctrl-w` | 删除光标前的单词 | +| `Ctrl-u` | 删除光标前所有内容 | +| `Ctrl-k` | 删除光标后所有内容 | +| `Ctrl-a` / `` | 将光标移至行首 | +| `Ctrl-e` / `` | 将光标移至行尾 | #### 保持高亮 diff --git a/docs/documentation.md b/docs/documentation.md index 5fa9c8ac6..c0216bc3a 100644 --- a/docs/documentation.md +++ b/docs/documentation.md @@ -1600,19 +1600,22 @@ Background search keyword in a project, when searching done, the count will be s Key bindings in FlyGrep buffer: -| Key Bindings | Descriptions | -| ------------------- | --------------------------------- | -| `` | close FlyGrep buffer | -| `` | open file at the cursor line | -| `Ctrl-t` | open item in new tab | -| `` | move cursor line down | -| `Shift-` | move cursor line up | -| `` | remove last character | -| `Ctrl-w` | remove the Word before the cursor | -| `Ctrl-u` | remove the Line before the cursor | -| `Ctrl-k` | remove the Line after the cursor | -| `Ctrl-a` / `` | Go to the beginning of the line | -| `Ctrl-e` / `` | Go to the end of the line | +| Key Bindings | Descriptions | +| ------------------- | ---------------------------------- | +| `` | close FlyGrep buffer | +| `` | open file at the cursor line | +| `Ctrl-t` | open item in new tab | +| `Ctrl-s` | open item in split window | +| `Ctrl-v` | open item in vertical split window | +| `Ctrl-q` | apply all items into quickfix | +| `` | move cursor line down | +| `Shift-` | move cursor line up | +| `` | remove last character | +| `Ctrl-w` | remove the Word before the cursor | +| `Ctrl-u` | remove the Line before the cursor | +| `Ctrl-k` | remove the Line after the cursor | +| `Ctrl-a` / `` | Go to the beginning of the line | +| `Ctrl-e` / `` | Go to the end of the line | #### Persistent highlighting diff --git a/test/api/vim/buffer.vader b/test/api/vim/buffer.vader index 21ecb67fa..32b6d7914 100644 --- a/test/api/vim/buffer.vader +++ b/test/api/vim/buffer.vader @@ -30,3 +30,11 @@ Execute ( SpaceVim api: vim#buffer add_highlight): let nr = buffer.bufnr() call buffer.add_highlight(nr,'String', 1, 1, 1) AssertEqual highlit.syntax_at(1, 1), 'String' + +Execute ( SpaceVim api: vim#buffer buf_get_lines): + let buffer = SpaceVim#api#import('vim#buffer') + let nr = buffer.bufadd('') + call setbufvar(nr, '&buftype', 'nofile') + call setbufvar(nr, '&buflisted', 0) + call buffer.buf_set_lines(nr, 0, 1, 0, ['line 1', 'line 2', 'line 3', 'line 4']) + AssertEqual buffer.buf_get_lines(nr, 1, 2, 0), ['line 2']