feat(defaults): use ripgrep (rg) for 'grepprg' if available

This commit is contained in:
Luna Saphie Mittelbach
2024-04-14 12:54:10 +02:00
committed by Christian Clason
parent fe4583127f
commit 20b38677c2
7 changed files with 62 additions and 4 deletions

View File

@@ -383,6 +383,19 @@ local function check_terminal()
end
end
local function check_external_tools()
health.start('External Tools')
if vim.fn.executable('rg') == 1 then
local rg = vim.fn.exepath('rg')
local cmd = 'rg -V'
local out = vim.fn.system(vim.fn.split(cmd))
health.ok(('%s (%s)'):format(vim.trim(out), rg))
else
health.warn('ripgrep not available')
end
end
function M.check()
check_config()
check_runtime()
@@ -390,6 +403,7 @@ function M.check()
check_rplugin_manifest()
check_terminal()
check_tmux()
check_external_tools()
end
return M

View File

@@ -496,3 +496,11 @@ if tty then
end
end
end
--- Default 'grepprg' to ripgrep if available.
if vim.fn.executable('rg') == 1 then
-- Match :grep default, otherwise rg searches cwd by default
-- Use -uuu to make ripgrep not do its default filtering
vim.o.grepprg = 'rg --vimgrep -uuu $* ' .. (vim.fn.has('unix') == 1 and '/dev/null' or 'nul')
vim.o.grepformat = '%f:%l:%c:%m'
end

View File

@@ -2625,6 +2625,8 @@ vim.go.gd = vim.go.gdefault
--- This is a scanf-like string that uses the same format as the
--- 'errorformat' option: see `errorformat`.
---
--- If ripgrep ('grepprg') is available, this option defaults to `%f:%l:%c:%m`.
---
--- @type string
vim.o.grepformat = "%f:%l:%m,%f:%l%m,%f %l%m"
vim.o.gfm = vim.o.grepformat
@@ -2649,6 +2651,16 @@ vim.go.gfm = vim.go.grepformat
--- apply equally to 'grepprg'.
--- This option cannot be set from a `modeline` or in the `sandbox`, for
--- security reasons.
--- This option defaults to:
--- - `rg --vimgrep -uuu $* ...` if ripgrep is available (`:checkhealth`),
--- - `grep -n $* /dev/null` on Unix,
--- - `findstr /n $* nul` on Windows.
--- Ripgrep can perform additional filtering such as using .gitignore rules
--- and skipping hidden or binary files. This is disabled by default (see the -u option)
--- to more closely match the behaviour of standard grep.
--- You can make ripgrep match Vim's case handling using the
--- -i/--ignore-case and -S/--smart-case options.
--- An `OptionSet` autocmd can be used to set it up to match automatically.
---
--- @type string
vim.o.grepprg = "grep -n $* /dev/null"