From 5d297cec4d0c1a27b6ee98dc12d8d5a195fd77f0 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Sat, 15 Feb 2025 23:44:16 +0800 Subject: [PATCH] feat(flygrep): make flygrep support input and cwd --- autoload/SpaceVim/mapping/space.vim | 506 ++++++++++++++-------------- bundle/flygrep.nvim/README.md | 7 +- bundle/flygrep.nvim/lua/flygrep.lua | 45 ++- docs/documentation.md | 2 +- 4 files changed, 296 insertions(+), 264 deletions(-) diff --git a/autoload/SpaceVim/mapping/space.vim b/autoload/SpaceVim/mapping/space.vim index 14283887c..f9c4e3b52 100644 --- a/autoload/SpaceVim/mapping/space.vim +++ b/autoload/SpaceVim/mapping/space.vim @@ -43,52 +43,52 @@ function! SpaceVim#mapping#space#init() abort \ . string(s:_function('s:windows_transient_state')) . ', [])', \ ['buffer-transient-state', \ [ - \ '[SPC w .] is to open the buffer transient state', - \ '', - \ 'Definition: ' . s:file . ':' . s:lnum, - \ ] - \ ] - \ , 1) + \ '[SPC w .] is to open the buffer transient state', + \ '', + \ 'Definition: ' . s:file . ':' . s:lnum, + \ ] + \ ] + \ , 1) let s:lnum = expand('') + s:funcbeginline call SpaceVim#mapping#space#def('nnoremap', ['w', 'd'], 'close', \ ['close-current-windows', \ [ - \ '[SPC w d] is to close current windows', - \ '', - \ 'Definition: ' . s:file . ':' . s:lnum, - \ ] - \ ] - \ , 1) + \ '[SPC w d] is to close current windows', + \ '', + \ 'Definition: ' . s:file . ':' . s:lnum, + \ ] + \ ] + \ , 1) let s:lnum = expand('') + s:funcbeginline call SpaceVim#mapping#space#def('nnoremap', ['w', 'f'], 'setlocal scrollbind!', \ ['toggle-follow-mode', \ [ - \ '[SPC w f] is to toggle follow mode', - \ '', - \ 'Definition: ' . s:file . ':' . s:lnum, - \ ] - \ ] - \ , 1) + \ '[SPC w f] is to toggle follow mode', + \ '', + \ 'Definition: ' . s:file . ':' . s:lnum, + \ ] + \ ] + \ , 1) let s:lnum = expand('') + s:funcbeginline call SpaceVim#mapping#space#def('nnoremap', ['w', 'D'], 'ChooseWin | close | wincmd w', \ ['delete-window-(other-windows)', \ [ - \ '[SPC w D] is to select a windows to close', - \ '', - \ 'Definition: ' . s:file . ':' . s:lnum, - \ ] - \ ] - \ , 1) + \ '[SPC w D] is to select a windows to close', + \ '', + \ 'Definition: ' . s:file . ':' . s:lnum, + \ ] + \ ] + \ , 1) let s:lnum = expand('') + s:funcbeginline call SpaceVim#mapping#space#def('nnoremap', ['w', 'F'], 'tabnew', \ ['create-new-tab', \ [ - \ '[SPC w F] is to create new tab', - \ '', - \ 'Definition: ' . s:file . ':' . s:lnum, - \ ] - \ ] - \ , 1) + \ '[SPC w F] is to create new tab', + \ '', + \ 'Definition: ' . s:file . ':' . s:lnum, + \ ] + \ ] + \ , 1) " let s:lnum = expand('') + s:funcbeginline " call SpaceVim#mapping#space#def('nnoremap', ['w', 'F'], 'call call(' " \ . string(function('s:create_new_named_tab')) @@ -105,216 +105,216 @@ function! SpaceVim#mapping#space#init() abort call SpaceVim#mapping#space#def('nnoremap', ['w', 'h'], 'wincmd h', \ ['window-left', \ [ - \ '[SPC w h] is to jump to the left window', - \ '', - \ 'Definition: ' . s:file . ':' . s:lnum, - \ ] - \ ] - \ , 1) + \ '[SPC w h] is to jump to the left window', + \ '', + \ 'Definition: ' . s:file . ':' . s:lnum, + \ ] + \ ] + \ , 1) let s:lnum = expand('') + s:funcbeginline call SpaceVim#mapping#space#def('nnoremap', ['w', 'j'], 'wincmd j', \ ['window-down', \ [ - \ '[SPC w j] is to jump to the window below current windows', - \ '', - \ 'Definition: ' . s:file . ':' . s:lnum, - \ ] - \ ] - \ , 1) + \ '[SPC w j] is to jump to the window below current windows', + \ '', + \ 'Definition: ' . s:file . ':' . s:lnum, + \ ] + \ ] + \ , 1) let s:lnum = expand('') + s:funcbeginline call SpaceVim#mapping#space#def('nnoremap', ['w', 'x'], 'wincmd x', \ ['window-switch-placement', \ [ - \ '[SPC w x] is to jump to exchange current window with next one.', - \ '', - \ 'Definition: ' . s:file . ':' . s:lnum, - \ ] - \ ] - \ , 1) + \ '[SPC w x] is to jump to exchange current window with next one.', + \ '', + \ 'Definition: ' . s:file . ':' . s:lnum, + \ ] + \ ] + \ , 1) let s:lnum = expand('') + s:funcbeginline call SpaceVim#mapping#space#def('nnoremap', ['w', 'k'], 'wincmd k', \ ['window-up', \ [ - \ '[SPC w k] is to jump to the window above current windows', - \ '', - \ 'Definition: ' . s:file . ':' . s:lnum, - \ ] - \ ] - \ , 1) + \ '[SPC w k] is to jump to the window above current windows', + \ '', + \ 'Definition: ' . s:file . ':' . s:lnum, + \ ] + \ ] + \ , 1) let s:lnum = expand('') + s:funcbeginline call SpaceVim#mapping#space#def('nnoremap', ['w', 'l'], 'wincmd l', \ ['window-right', \ [ - \ '[SPC w l] is to jump to the right window', - \ '', - \ 'Definition: ' . s:file . ':' . s:lnum, - \ ] - \ ] - \ , 1) + \ '[SPC w l] is to jump to the right window', + \ '', + \ 'Definition: ' . s:file . ':' . s:lnum, + \ ] + \ ] + \ , 1) let s:lnum = expand('') + s:funcbeginline call SpaceVim#mapping#space#def('nnoremap', ['w', 'H'], 'wincmd H', \ ['window-far-left', \ [ - \ '[SPC w H] is to jump to the far left window', - \ '', - \ 'Definition: ' . s:file . ':' . s:lnum, - \ ] - \ ] - \ , 1) + \ '[SPC w H] is to jump to the far left window', + \ '', + \ 'Definition: ' . s:file . ':' . s:lnum, + \ ] + \ ] + \ , 1) let s:lnum = expand('') + s:funcbeginline call SpaceVim#mapping#space#def('nnoremap', ['w', 'J'], 'wincmd J', \ ['window-far-down', \ [ - \ '[SPC w J] is to jump to the far down window', - \ '', - \ 'Definition: ' . s:file . ':' . s:lnum, - \ ] - \ ] - \ , 1) + \ '[SPC w J] is to jump to the far down window', + \ '', + \ 'Definition: ' . s:file . ':' . s:lnum, + \ ] + \ ] + \ , 1) let s:lnum = expand('') + s:funcbeginline call SpaceVim#mapping#space#def('nnoremap', ['w', 'K'], 'wincmd K', \ ['window-far-up', \ [ - \ '[SPC w K] is to jump to the far up window', - \ '', - \ 'Definition: ' . s:file . ':' . s:lnum, - \ ] - \ ] - \ , 1) + \ '[SPC w K] is to jump to the far up window', + \ '', + \ 'Definition: ' . s:file . ':' . s:lnum, + \ ] + \ ] + \ , 1) let s:lnum = expand('') + s:funcbeginline call SpaceVim#mapping#space#def('nnoremap', ['w', 'L'], 'wincmd L', \ ['window-far-right', \ [ - \ '[SPC w L] is to jump to the far right window', - \ '', - \ 'Definition: ' . s:file . ':' . s:lnum, - \ ] - \ ] - \ , 1) + \ '[SPC w L] is to jump to the far right window', + \ '', + \ 'Definition: ' . s:file . ':' . s:lnum, + \ ] + \ ] + \ , 1) let s:lnum = expand('') + s:funcbeginline call SpaceVim#mapping#space#def('nnoremap', ['w', 'm'], 'call call(' \ . string(function('s:maximize_minimize_win')) \ . ', [])', \ ['maximize/minimize window', \ [ - \ '[SPC w m] is to maximize/minimize window', - \ '', - \ 'Definition: ' . s:file . ':' . s:lnum, - \ ] - \ ] - \ , 1) + \ '[SPC w m] is to maximize/minimize window', + \ '', + \ 'Definition: ' . s:file . ':' . s:lnum, + \ ] + \ ] + \ , 1) let s:lnum = expand('') + s:funcbeginline call SpaceVim#mapping#space#def('nnoremap', ['w', 'M'], \ "execute eval(\"winnr('$')<=2 ? 'wincmd x' : 'ChooseWinSwap'\")", \ ['swap window', \ [ - \ '[SPC w M] is to swap window', - \ '', - \ 'Definition: ' . s:file . ':' . s:lnum, - \ ] - \ ] - \ , 1) + \ '[SPC w M] is to swap window', + \ '', + \ 'Definition: ' . s:file . ':' . s:lnum, + \ ] + \ ] + \ , 1) let s:lnum = expand('') + s:funcbeginline call SpaceVim#mapping#space#def('nnoremap', ['w', 'o'], 'tabnext', \ ['other-tabs', \ [ - \ '[SPC w o] is to switch to next tabs', - \ '', - \ 'Definition: ' . s:file . ':' . s:lnum, - \ ] - \ ] - \ , 1) + \ '[SPC w o] is to switch to next tabs', + \ '', + \ 'Definition: ' . s:file . ':' . s:lnum, + \ ] + \ ] + \ , 1) let s:lnum = expand('') + s:funcbeginline call SpaceVim#mapping#space#def('nnoremap', ['w', '/'], 'belowright vsplit | wincmd w', \ ['split-windows-right', \ [ - \ '[SPC w /] is to split windows on the right', - \ '', - \ 'Definition: ' . s:file . ':' . s:lnum, - \ ] - \ ] - \ , 1) + \ '[SPC w /] is to split windows on the right', + \ '', + \ 'Definition: ' . s:file . ':' . s:lnum, + \ ] + \ ] + \ , 1) let s:lnum = expand('') + s:funcbeginline call SpaceVim#mapping#space#def('nnoremap', ['w', 'v'], 'belowright vsplit | wincmd w', \ ['split-windows-right', \ [ - \ '[SPC w v] is to split windows on the right', - \ '', - \ 'Definition: ' . s:file . ':' . s:lnum, - \ ] - \ ] - \ , 1) + \ '[SPC w v] is to split windows on the right', + \ '', + \ 'Definition: ' . s:file . ':' . s:lnum, + \ ] + \ ] + \ , 1) let s:lnum = expand('') + s:funcbeginline call SpaceVim#mapping#space#def('nnoremap', ['w', '-'], 'bel split | wincmd w', \ ['split-windows-below', \ [ - \ '[SPC w -] is to split windows below', - \ '', - \ 'Definition: ' . s:file . ':' . s:lnum, - \ ] - \ ] - \ , 1) + \ '[SPC w -] is to split windows below', + \ '', + \ 'Definition: ' . s:file . ':' . s:lnum, + \ ] + \ ] + \ , 1) let s:lnum = expand('') + s:funcbeginline call SpaceVim#mapping#space#def('nnoremap', ['w', 's'], 'bel split | wincmd w', \ ['split-windows-below', \ [ - \ '[SPC w s] is to split windows below', - \ '', - \ 'Definition: ' . s:file . ':' . s:lnum, - \ ] - \ ] - \ , 1) + \ '[SPC w s] is to split windows below', + \ '', + \ 'Definition: ' . s:file . ':' . s:lnum, + \ ] + \ ] + \ , 1) let s:lnum = expand('') + s:funcbeginline call SpaceVim#mapping#space#def('nnoremap', ['w', 'S'], 'bel split', \ ['split-focus-windows-below', \ [ - \ '[SPC w S] is to split windows below and focus on new windows', - \ '', - \ 'Definition: ' . s:file . ':' . s:lnum, - \ ] - \ ] - \ , 1) + \ '[SPC w S] is to split windows below and focus on new windows', + \ '', + \ 'Definition: ' . s:file . ':' . s:lnum, + \ ] + \ ] + \ , 1) let s:lnum = expand('') + s:funcbeginline call SpaceVim#mapping#space#def('nnoremap', ['w', '2'], 'silent only | vs | wincmd w', \ ['layout-double-columns', \ [ - \ '[SPC w 2] is to change current windows layout to double columns', - \ '', - \ 'Definition: ' . s:file . ':' . s:lnum, - \ ] - \ ] - \ , 1) + \ '[SPC w 2] is to change current windows layout to double columns', + \ '', + \ 'Definition: ' . s:file . ':' . s:lnum, + \ ] + \ ] + \ , 1) let s:lnum = expand('') + s:funcbeginline call SpaceVim#mapping#space#def('nnoremap', ['w', '3'], 'silent only | vs | vs | wincmd H', \ ['layout-three-columns', \ [ - \ '[SPC w 3] is to change current windows layout to three columns', - \ '', - \ 'Definition: ' . s:file . ':' . s:lnum, - \ ] - \ ] - \ , 1) + \ '[SPC w 3] is to change current windows layout to three columns', + \ '', + \ 'Definition: ' . s:file . ':' . s:lnum, + \ ] + \ ] + \ , 1) let s:lnum = expand('') + s:funcbeginline call SpaceVim#mapping#space#def('nnoremap', ['w', 'V'], \ 'bel vs', \ ['split-window-right-focus', \ [ - \ '[SPC w V] is to split window on the right', - \ '', - \ 'Definition: ' . s:file . ':' . s:lnum, - \ ] - \ ] - \ , 1) + \ '[SPC w V] is to split window on the right', + \ '', + \ 'Definition: ' . s:file . ':' . s:lnum, + \ ] + \ ] + \ , 1) let s:lnum = expand('') + s:funcbeginline call SpaceVim#mapping#space#def('nnoremap', ['w', '='], \ 'wincmd =', \ ['balance-windows', \ [ - \ '[SPC w =] is to balance all the windows in current tab', - \ '', - \ 'Definition: ' . s:file . ':' . s:lnum, - \ ] - \ ], 1) + \ '[SPC w =] is to balance all the windows in current tab', + \ '', + \ 'Definition: ' . s:file . ':' . s:lnum, + \ ] + \ ], 1) call SpaceVim#mapping#space#def('nnoremap', ['w', 'w'], \ 'wincmd w', 'cycle and focus between windows', 1) call SpaceVim#mapping#space#def('nnoremap', ['w', 'W'], @@ -326,14 +326,14 @@ function! SpaceVim#mapping#space#init() abort \ . string(function('s:next_buffer')) \ . ', [])', ['next-buffer', \ [ - \ '[SPC b n] is running :bnext, jump to next buffer', - \ 'which is a vim build in command', - \ 'It is bound to SPC b n, ] b,', - \ '', - \ 'Definition: ' . s:file . ':' . s:lnum, - \ ] - \ ] - \ , 1) + \ '[SPC b n] is running :bnext, jump to next buffer', + \ 'which is a vim build in command', + \ 'It is bound to SPC b n, ] b,', + \ '', + \ 'Definition: ' . s:file . ':' . s:lnum, + \ ] + \ ] + \ , 1) call SpaceVim#mapping#space#def('nnoremap', ['b', 's'], \ 'call call(' \ . string(function('s:switch_scratch_buffer')) @@ -343,14 +343,14 @@ function! SpaceVim#mapping#space#init() abort \ . string(function('s:previous_buffer')) \ . ', [])', ['previous-buffer', \ [ - \ 'SPC b p is running :bp, jump to previous buffer', - \ 'which is a vim build in command', - \ 'It is bound to SPC b p, [ b,', - \ '', - \ 'Definition: ' . s:file . ':' . s:lnum, - \ ] - \ ] - \ , 1) + \ 'SPC b p is running :bp, jump to previous buffer', + \ 'which is a vim build in command', + \ 'It is bound to SPC b p, [ b,', + \ '', + \ 'Definition: ' . s:file . ':' . s:lnum, + \ ] + \ ] + \ , 1) if g:spacevim_relativenumber nnoremap [SPC]tn :setlocal nonumber! norelativenumber! @@ -371,13 +371,13 @@ function! SpaceVim#mapping#space#init() abort \ . ')', \ ['grep-in-current-buffer', \ [ - \ 'SPC s s will search text in current buffer, an input promot will be opened.', - \ 'The default searching tool is based on search_tools option', - \ '', - \ 'Definition: ' . s:file . ':' . s:lnum, - \ ] - \ ] - \ , 1) + \ 'SPC s s will search text in current buffer, an input promot will be opened.', + \ 'The default searching tool is based on search_tools option', + \ '', + \ 'Definition: ' . s:file . ':' . s:lnum, + \ ] + \ ] + \ , 1) let s:lnum = expand('') + s:funcbeginline call SpaceVim#mapping#space#def('nnoremap', ['s', 'S'], @@ -386,13 +386,13 @@ function! SpaceVim#mapping#space#init() abort \ . ')', \ ['grep-cword-in-current-buffer', \ [ - \ 'SPC s S will search the word under cursor within current buffer, an input promot will be opened.', - \ 'The default searching tool is based on search_tools option', - \ '', - \ 'Definition: ' . s:file . ':' . s:lnum, - \ ] - \ ] - \ , 1) + \ 'SPC s S will search the word under cursor within current buffer, an input promot will be opened.', + \ 'The default searching tool is based on search_tools option', + \ '', + \ 'Definition: ' . s:file . ':' . s:lnum, + \ ] + \ ] + \ , 1) " Searching in all loaded buffers let s:lnum = expand('') + s:funcbeginline call SpaceVim#mapping#space#def('nnoremap', ['s', 'b'], @@ -401,13 +401,13 @@ function! SpaceVim#mapping#space#init() abort \ . ')', \ ['grep-in-all-buffers', \ [ - \ 'SPC s b will search text in all loaded buffers, an input promot will be opened.', - \ 'The default searching tool is based on search_tools option', - \ '', - \ 'Definition: ' . s:file . ':' . s:lnum, - \ ] - \ ] - \ , 1) + \ 'SPC s b will search text in all loaded buffers, an input promot will be opened.', + \ 'The default searching tool is based on search_tools option', + \ '', + \ 'Definition: ' . s:file . ':' . s:lnum, + \ ] + \ ] + \ , 1) let s:lnum = expand('') + s:funcbeginline call SpaceVim#mapping#space#def('nnoremap', ['s', 'B'], @@ -416,13 +416,13 @@ function! SpaceVim#mapping#space#init() abort \ . ')', \ ['grep-cword-in-all-buffers', \ [ - \ 'SPC s B will search cursor word in all loaded buffers, an input promot will be opened.', - \ 'The default searching tool is based on search_tools option', - \ '', - \ 'Definition: ' . s:file . ':' . s:lnum, - \ ] - \ ] - \ , 1) + \ 'SPC s B will search cursor word in all loaded buffers, an input promot will be opened.', + \ 'The default searching tool is based on search_tools option', + \ '', + \ 'Definition: ' . s:file . ':' . s:lnum, + \ ] + \ ] + \ , 1) " Searching in buffer directory let s:lnum = expand('') + s:funcbeginline call SpaceVim#mapping#space#def('nnoremap', ['s', 'd'], @@ -431,13 +431,13 @@ function! SpaceVim#mapping#space#init() abort \ . ')', \ ['grep-in-buffer-directory', \ [ - \ 'SPC s d will search text in buffer directory, an input promot will be opened.', - \ 'The default searching tool is based on search_tools option', - \ '', - \ 'Definition: ' . s:file . ':' . s:lnum, - \ ] - \ ] - \ , 1) + \ 'SPC s d will search text in buffer directory, an input promot will be opened.', + \ 'The default searching tool is based on search_tools option', + \ '', + \ 'Definition: ' . s:file . ':' . s:lnum, + \ ] + \ ] + \ , 1) let s:lnum = expand('') + s:funcbeginline call SpaceVim#mapping#space#def('nnoremap', ['s', 'D'], \ 'call SpaceVim#plugins#flygrep#open(' @@ -445,13 +445,13 @@ function! SpaceVim#mapping#space#init() abort \ . ')', \ ['grep-cword-in-buffer-directory', \ [ - \ 'SPC s d will search cursor word in buffer directory, an input promot will be opened.', - \ 'The default searching tool is based on search_tools option', - \ '', - \ 'Definition: ' . s:file . ':' . s:lnum, - \ ] - \ ] - \ , 1) + \ 'SPC s d will search cursor word in buffer directory, an input promot will be opened.', + \ 'The default searching tool is based on search_tools option', + \ '', + \ 'Definition: ' . s:file . ':' . s:lnum, + \ ] + \ ] + \ , 1) " Searching in files in an arbitrary directory let s:lnum = expand('') + s:funcbeginline call SpaceVim#mapping#space#def('nnoremap', ['s', 'f'], @@ -460,13 +460,13 @@ function! SpaceVim#mapping#space#init() abort \ . ')', \ ['grep-in-arbitrary-directory', \ [ - \ 'SPC s f will search text in arbitrary directory, an input promot will be opened.', - \ 'The default searching tool is based on search_tools option', - \ '', - \ 'Definition: ' . s:file . ':' . s:lnum, - \ ] - \ ] - \ , 1) + \ 'SPC s f will search text in arbitrary directory, an input promot will be opened.', + \ 'The default searching tool is based on search_tools option', + \ '', + \ 'Definition: ' . s:file . ':' . s:lnum, + \ ] + \ ] + \ , 1) let s:lnum = expand('') + s:funcbeginline call SpaceVim#mapping#space#def('nnoremap', ['s', 'F'], \ 'call SpaceVim#plugins#flygrep#open(' @@ -474,21 +474,27 @@ function! SpaceVim#mapping#space#init() abort \ . ')', \ ['grep-cword-in-arbitrary-directory', \ [ - \ 'SPC s F will search cursor world in arbitrary directory, an input promot will be opened.', - \ 'The default searching tool is based on search_tools option', - \ '', - \ 'Definition: ' . s:file . ':' . s:lnum, - \ ] - \ ] - \ , 1) + \ 'SPC s F will search cursor world in arbitrary directory, an input promot will be opened.', + \ 'The default searching tool is based on search_tools option', + \ '', + \ 'Definition: ' . s:file . ':' . s:lnum, + \ ] + \ ] + \ , 1) " Searching in project call SpaceVim#mapping#space#def('nnoremap', ['s', 'p'], \ 'call SpaceVim#plugins#flygrep#open(' . \ "{'input' : input(\"grep pattern:\"), 'dir' : get(b:, \"rootDir\", getcwd())})", \ 'grep-in-project', 1) - call SpaceVim#mapping#space#def('nnoremap', ['s', 'P'], - \ "call SpaceVim#plugins#flygrep#open({'input' : expand(\"\"), 'dir' : get(b:, \"rootDir\", getcwd())})", - \ 'grep-cword-in-project', 1) + if g:spacevim_flygrep_next_version && has('nvim-0.10.0') + call SpaceVim#mapping#space#def('nnoremap', ['s', 'P'], + \ "lua require('flygrep').open({input = vim.fn.expand('')})", + \ 'grep-cword-in-project', 1) + else + call SpaceVim#mapping#space#def('nnoremap', ['s', 'P'], + \ "call SpaceVim#plugins#flygrep#open({'input' : expand(\"\"), 'dir' : get(b:, \"rootDir\", getcwd())})", + \ 'grep-cword-in-project', 1) + endif " Searching background if has('nvim-0.7.0') call SpaceVim#mapping#space#def('nnoremap', ['s', 'j'], @@ -706,12 +712,12 @@ function! SpaceVim#mapping#space#init() abort \ . string(function('s:describe_current_time')) \ . ', [])', ['describe-current-time', \ [ - \ 'SPC h d t is to display current time.', - \ '', - \ 'Definition: ' . s:file . ':' . s:lnum, - \ ] - \ ] - \ , 1) + \ 'SPC h d t is to display current time.', + \ '', + \ 'Definition: ' . s:file . ':' . s:lnum, + \ ] + \ ] + \ , 1) if has('nvim-0.7.0') call SpaceVim#mapping#space#def('nnoremap', ['a', 'o'], 'lua require("spacevim.plugin.todo").list()', 'open-todo-manager', 1) else @@ -904,20 +910,20 @@ function! s:windows_transient_state() abort call state.set_title('Buffer Selection Transient State') call state.defind_keys( \ { - \ 'layout' : 'vertical split', - \ 'left' : [ - \ ], - \ 'right' : [ - \ { - \ 'key' : 'n', - \ 'desc' : 'next buffer', - \ 'func' : '', - \ 'cmd' : 'bnext', - \ 'exit' : 0, - \ }, - \ ], - \ } - \ ) + \ 'layout' : 'vertical split', + \ 'left' : [ + \ ], + \ 'right' : [ + \ { + \ 'key' : 'n', + \ 'desc' : 'next buffer', + \ 'func' : '', + \ 'cmd' : 'bnext', + \ 'exit' : 0, + \ }, + \ ], + \ } + \ ) call state.open() endfunction diff --git a/bundle/flygrep.nvim/README.md b/bundle/flygrep.nvim/README.md index 69ac01d01..63bfea4b2 100644 --- a/bundle/flygrep.nvim/README.md +++ b/bundle/flygrep.nvim/README.md @@ -12,7 +12,7 @@ - [Intro](#intro) - [Requirements](#requirements) - [Install](#install) -- [Command](#command) +- [Usage](#usage) - [Configuration](#configuration) - [Key Bindings](#key-bindings) - [Feedback](#feedback) @@ -36,9 +36,12 @@ Plug 'wsdjeg/flygrep.nvim' ``` -## Command +## Usage - `:FlyGrep`: open flygrep in current directory +- `:lua require('flygrep').open(opt)`: opt supports following keys, + - cwd: root directory of searching job + - input: default input text in prompt window ## Configuration diff --git a/bundle/flygrep.nvim/lua/flygrep.lua b/bundle/flygrep.nvim/lua/flygrep.lua index 38d636432..9ce3e25f4 100644 --- a/bundle/flygrep.nvim/lua/flygrep.lua +++ b/bundle/flygrep.nvim/lua/flygrep.lua @@ -15,9 +15,11 @@ if not ok then ok, cmp = pcall(require, 'cmp') end +local grep_root_dir = '.' + local grep_timer_id = -1 local grep_input = '' -local search_jobid = -1 +local search_jobid = 0 local search_hi_id = -1 local fix_string = false local include_hidden_file = false @@ -67,7 +69,7 @@ end -- 搜索结果行转换成文件名、光标位置 local function get_file_pos(line) - local filename = vim.fn.fnameescape(vim.fn.split(line, [[:\d\+:]])[1]) + local filename = vim.fn.fnamemodify(vim.fn.fnameescape(vim.fn.split(line, [[:\d\+:]])[1]), ':p') local linenr = vim.fn.str2nr(string.sub(vim.fn.matchstr(line, [[:\d\+:]]), 2, -2)) local colum = vim.fn.str2nr(string.sub(vim.fn.matchstr(line, [[\(:\d\+\)\@<=:\d\+:]]), 2, -2)) return filename, linenr, colum @@ -131,6 +133,7 @@ local function grep_timer(t) update_result_count() end end, + cwd = grep_root_dir, }) end @@ -144,7 +147,7 @@ local function build_prompt_title() table.insert(t, { ' string ', 'FlyGrep_b' }) end table.insert(t, { '', 'FlyGrep_b' }) - table.insert(t, { ' ' .. vim.fn.getcwd() .. ' ', 'FlyGrep_b' }) + table.insert(t, { ' ' .. grep_root_dir .. ' ', 'FlyGrep_b' }) table.insert(t, { '', 'FlyGrep_b_Normal' }) -- return {{}, {}, {}} return t @@ -332,8 +335,9 @@ local function open_win() }, }) end - vim.cmd('noautocmd startinsert') - + if grep_root_dir ~= vim.fn.getcwd() then + vim.cmd('cd ' .. grep_root_dir) + end local augroup = vim.api.nvim_create_augroup('floatgrep', { clear = true, }) @@ -346,9 +350,14 @@ local function open_win() if grep_input ~= '' then pcall(vim.fn.matchdelete, search_hi_id, result_winid) pcall(vim.fn.timer_stop, grep_timer_id) - job.stop(search_jobid) - search_hi_id = - vim.fn.matchadd(conf.matched_higroup, grep_input:gsub('~', '\\~'), 10, -1, { window = result_winid }) + pcall(job.stop, search_jobid) + search_hi_id = vim.fn.matchadd( + conf.matched_higroup, + grep_input:gsub('~', '\\~'), + 10, + -1, + { window = result_winid } + ) grep_timer_id = vim.fn.timer_start(conf.timeout, grep_timer, { ['repeat'] = 1 }) else pcall(vim.fn.matchdelete, search_hi_id, result_winid) @@ -447,15 +456,29 @@ local function open_win() -1, { window = result_winid } ) + if grep_input ~= '' then + vim.api.nvim_buf_set_lines(prompt_bufid, 0, -1, false, {grep_input}) + end + vim.cmd('noautocmd startinsert!') + end -function M.open() +function M.open(opt) + if not opt then + opt = {} + end saved_mouse_opt = vim.o.mouse + grep_input = opt.input or '' + if opt.cwd and vim.fn.isdirectory(opt.cwd) == 1 then + grep_root_dir = vim.fn.fnamemodify(opt.cwd, ':p') + else + grep_root_dir = vim.fn.getcwd() + end open_win() end -function M.setup(conf) - require('flygrep.config').setup(conf) +function M.setup(c) + require('flygrep.config').setup(c) end return M diff --git a/docs/documentation.md b/docs/documentation.md index 80a2ae2fe..30c8bdde5 100644 --- a/docs/documentation.md +++ b/docs/documentation.md @@ -1795,7 +1795,7 @@ This option maybe removed when `flygrep.nvim` development is done. flygrep_next_version = true ``` -When this option is `true`, only `SPC s /` will be mapped to `:FlyGrep` command from `flygrep.nvim`. And the key binding in `flygrep.nvim` window is: +When this option is `true`, `SPC s /` and `SPC s P` will use `flygrep.nvim`. And the key binding in `flygrep.nvim` window is: | Key bindings | descretion | | ------------ | ---------------------------------- |