1
0
mirror of https://github.com/SpaceVim/SpaceVim.git synced 2025-02-23 04:41:33 +08:00

feat(flygrep): make flygrep support input and cwd

This commit is contained in:
Eric Wong 2025-02-15 23:44:16 +08:00
parent 15ef9c83cf
commit 5d297cec4d
No known key found for this signature in database
GPG Key ID: 41BB7053E835C848
4 changed files with 296 additions and 264 deletions

View File

@ -486,9 +486,15 @@ function! SpaceVim#mapping#space#init() abort
\ 'call SpaceVim#plugins#flygrep#open(' . \ 'call SpaceVim#plugins#flygrep#open(' .
\ "{'input' : input(\"grep pattern:\"), 'dir' : get(b:, \"rootDir\", getcwd())})", \ "{'input' : input(\"grep pattern:\"), 'dir' : get(b:, \"rootDir\", getcwd())})",
\ 'grep-in-project', 1) \ 'grep-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('<cword>')})",
\ 'grep-cword-in-project', 1)
else
call SpaceVim#mapping#space#def('nnoremap', ['s', 'P'], call SpaceVim#mapping#space#def('nnoremap', ['s', 'P'],
\ "call SpaceVim#plugins#flygrep#open({'input' : expand(\"<cword>\"), 'dir' : get(b:, \"rootDir\", getcwd())})", \ "call SpaceVim#plugins#flygrep#open({'input' : expand(\"<cword>\"), 'dir' : get(b:, \"rootDir\", getcwd())})",
\ 'grep-cword-in-project', 1) \ 'grep-cword-in-project', 1)
endif
" Searching background " Searching background
if has('nvim-0.7.0') if has('nvim-0.7.0')
call SpaceVim#mapping#space#def('nnoremap', ['s', 'j'], call SpaceVim#mapping#space#def('nnoremap', ['s', 'j'],

View File

@ -12,7 +12,7 @@
- [Intro](#intro) - [Intro](#intro)
- [Requirements](#requirements) - [Requirements](#requirements)
- [Install](#install) - [Install](#install)
- [Command](#command) - [Usage](#usage)
- [Configuration](#configuration) - [Configuration](#configuration)
- [Key Bindings](#key-bindings) - [Key Bindings](#key-bindings)
- [Feedback](#feedback) - [Feedback](#feedback)
@ -36,9 +36,12 @@
Plug 'wsdjeg/flygrep.nvim' Plug 'wsdjeg/flygrep.nvim'
``` ```
## Command ## Usage
- `:FlyGrep`: open flygrep in current directory - `: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 ## Configuration

View File

@ -15,9 +15,11 @@ if not ok then
ok, cmp = pcall(require, 'cmp') ok, cmp = pcall(require, 'cmp')
end end
local grep_root_dir = '.'
local grep_timer_id = -1 local grep_timer_id = -1
local grep_input = '' local grep_input = ''
local search_jobid = -1 local search_jobid = 0
local search_hi_id = -1 local search_hi_id = -1
local fix_string = false local fix_string = false
local include_hidden_file = false local include_hidden_file = false
@ -67,7 +69,7 @@ end
-- 搜索结果行转换成文件名、光标位置 -- 搜索结果行转换成文件名、光标位置
local function get_file_pos(line) 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 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)) local colum = vim.fn.str2nr(string.sub(vim.fn.matchstr(line, [[\(:\d\+\)\@<=:\d\+:]]), 2, -2))
return filename, linenr, colum return filename, linenr, colum
@ -131,6 +133,7 @@ local function grep_timer(t)
update_result_count() update_result_count()
end end
end, end,
cwd = grep_root_dir,
}) })
end end
@ -144,7 +147,7 @@ local function build_prompt_title()
table.insert(t, { ' string ', 'FlyGrep_b' }) table.insert(t, { ' string ', 'FlyGrep_b' })
end end
table.insert(t, { '', 'FlyGrep_b' }) 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' }) table.insert(t, { '', 'FlyGrep_b_Normal' })
-- return {{}, {}, {}} -- return {{}, {}, {}}
return t return t
@ -332,8 +335,9 @@ local function open_win()
}, },
}) })
end 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', { local augroup = vim.api.nvim_create_augroup('floatgrep', {
clear = true, clear = true,
}) })
@ -346,9 +350,14 @@ local function open_win()
if grep_input ~= '' then if grep_input ~= '' then
pcall(vim.fn.matchdelete, search_hi_id, result_winid) pcall(vim.fn.matchdelete, search_hi_id, result_winid)
pcall(vim.fn.timer_stop, grep_timer_id) pcall(vim.fn.timer_stop, grep_timer_id)
job.stop(search_jobid) pcall(job.stop, search_jobid)
search_hi_id = search_hi_id = vim.fn.matchadd(
vim.fn.matchadd(conf.matched_higroup, grep_input:gsub('~', '\\~'), 10, -1, { window = result_winid }) conf.matched_higroup,
grep_input:gsub('~', '\\~'),
10,
-1,
{ window = result_winid }
)
grep_timer_id = vim.fn.timer_start(conf.timeout, grep_timer, { ['repeat'] = 1 }) grep_timer_id = vim.fn.timer_start(conf.timeout, grep_timer, { ['repeat'] = 1 })
else else
pcall(vim.fn.matchdelete, search_hi_id, result_winid) pcall(vim.fn.matchdelete, search_hi_id, result_winid)
@ -447,15 +456,29 @@ local function open_win()
-1, -1,
{ window = result_winid } { 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 end
function M.open() function M.open(opt)
if not opt then
opt = {}
end
saved_mouse_opt = vim.o.mouse 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() open_win()
end end
function M.setup(conf) function M.setup(c)
require('flygrep.config').setup(conf) require('flygrep.config').setup(c)
end end
return M return M

View File

@ -1795,7 +1795,7 @@ This option maybe removed when `flygrep.nvim` development is done.
flygrep_next_version = true 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 | | Key bindings | descretion |
| ------------ | ---------------------------------- | | ------------ | ---------------------------------- |