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

feat(searcher): rewrite searcher in lua

This commit is contained in:
wsdjeg 2022-10-10 22:27:53 +08:00
parent 85d3336201
commit d6b2a46129
2 changed files with 66 additions and 2 deletions

View File

@ -514,8 +514,13 @@ function! SpaceVim#mapping#space#init() abort
\ "call SpaceVim#plugins#flygrep#open({'input' : expand(\"<cword>\"), 'dir' : get(b:, \"rootDir\", getcwd())})",
\ 'grep-cword-in-project', 1)
" Searching background
call SpaceVim#mapping#space#def('nnoremap', ['s', 'j'],
\ 'call SpaceVim#plugins#searcher#find("", SpaceVim#mapping#search#default_tool()[0])', 'background-search-in-project', 1)
if has('nvim-0.7.0')
call SpaceVim#mapping#space#def('nnoremap', ['s', 'j'],
\ "lua require('spacevim.plugin.searcher').find('', require('spacevim.plugin.search').default_tool())", 'background-search-in-project', 1)
else
call SpaceVim#mapping#space#def('nnoremap', ['s', 'j'],
\ 'call SpaceVim#plugins#searcher#find("", SpaceVim#mapping#search#default_tool()[0])', 'background-search-in-project', 1)
endif
call SpaceVim#mapping#space#def('nnoremap', ['s', 'J'],
\ 'call SpaceVim#plugins#searcher#find(expand("<cword>"),SpaceVim#mapping#search#default_tool()[0])',
\ 'background-search-cwords-in-project', 1)

View File

@ -0,0 +1,59 @@
local M = {}
local rst = {}
local function get_search_cmd(exe, expr)
if exe == 'grep' then
return {'grep', '-inHR', '--exclude-dir', '.git', expr, '.'}
elseif exe == 'rg' then
return {'rg', '-g!.git', '--hidden', '--no-heading', '--color=never', '--with-filename', '--line-number', expr, '.'}
else
return {exe, expr}
end
end
local function search_stdout(id, data, event)
for _, d in ipairs(data) do
local info = vim.fn.split(d, [[\:\d\+\:]])
if #info == 2 then
local fname = info[1]
local text = info[2]
local lnum = string.sub(vim.fn.matchstr(d, [[\:\d\+\:]]), 2, -2)
table.insert(rst, {
filename = vim.fn.fnamemodify(fname, ':p'),
lnum = lnum,
text = text
})
end
end
end
local function search_stderr(id, data, event)
end
local function search_exit(id, data, event)
vim.fn.setqflist({}, 'r', {
title = ' ' .. #rst .. ' items',
items = rst
})
vim.cmd('botright copen')
end
function M.find(expr, exe)
if expr == '' then
expr = vim.fn.input('search expr:')
vim.cmd('noautocmd normal! :')
end
rst = {}
local id = vim.fn.jobstart(get_search_cmd(exe, expr), {
on_stdout = search_stdout,
on_stderr = search_stderr,
on_exit = search_exit,
})
if id > 0 then
vim.api.nvim_echo({{'searching ' .. expr, 'Comment'}}, false, {})
end
end
return M