diff --git a/autoload/SpaceVim/mapping/space.vim b/autoload/SpaceVim/mapping/space.vim index 44b9f5cae..0800a362b 100644 --- a/autoload/SpaceVim/mapping/space.vim +++ b/autoload/SpaceVim/mapping/space.vim @@ -514,8 +514,13 @@ function! SpaceVim#mapping#space#init() abort \ "call SpaceVim#plugins#flygrep#open({'input' : expand(\"\"), '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(""),SpaceVim#mapping#search#default_tool()[0])', \ 'background-search-cwords-in-project', 1) diff --git a/lua/spacevim/plugin/searcher.lua b/lua/spacevim/plugin/searcher.lua new file mode 100644 index 000000000..2240b2471 --- /dev/null +++ b/lua/spacevim/plugin/searcher.lua @@ -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