Skip to content

Commit

Permalink
feat: interactive ripgrep and fzf interface
Browse files Browse the repository at this point in the history
  • Loading branch information
arsham committed Dec 26, 2021
1 parent a59a09f commit b110737
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 21 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -338,8 +338,10 @@ Most actions can apply to multiple selected items if possible.
| `<leader>:` | Commands |
| `<leader>ff` | **F**ind in contents of all files in current folder. |
| `<leader>fa` | **F**ind **A**ll disabling `.gitignore` handling. |
| `<leader>fi` | **I**ncrementally **F**ind. |
| `<leader>rg` | Search (**rg**) with current word. |
| `<leader>ra` | Search (**rg**) disabling `.gitignore` handling. |
| `<leader>ri` | **I**ncrementally search (**rg**) with current word. |
| `<leader>fh` | **F**ile **H**istory |
| `<leader>fl` | **F**ile **l**ocate (requires mlocate) |
| `<leader>gf` | **GFiles** |
Expand Down
14 changes: 10 additions & 4 deletions lua/settings/fzf/mappings.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,23 @@ keymap.imap{'<c-x><c-f>', '<plug>(fzf-complete-path)'}
keymap.imap{'<c-x><c-l>', '<plug>(fzf-complete-line)'}

-- Open the search tool.
keymap.nnoremap{"<leader>ff", function() util.do_rg("") end}
keymap.nnoremap{"<leader>ff", function() util.ripgrep_search("") end}
-- Open the search tool, ignoring .gitignore.
keymap.nnoremap{"<leader>fa", function() util.do_rg("", '-u') end}
keymap.nnoremap{"<leader>fa", function() util.ripgrep_search("", true) end}
-- Incremental search.
keymap.nnoremap{"<leader>fi", function() util.ripgrep_search_incremental("", true) end}

-- Search over current word.
keymap.nnoremap{"<leader>rg", function()
util.do_rg(vim.fn.expand("<cword>"))
util.ripgrep_search(vim.fn.expand("<cword>"))
end}
-- Search over current word, ignoring .gitignore.
keymap.nnoremap{"<leader>ra", function()
util.do_rg(vim.fn.expand("<cword>"), '-u')
util.ripgrep_search(vim.fn.expand("<cword>"), true)
end}
-- Incremental search over current word, ignoring .gitignore.
keymap.nnoremap{"<leader>ri", function()
util.ripgrep_search(vim.fn.expand("<cword>"), true)
end}

keymap.nnoremap{'<leader>mm', ":Marks<CR>"}
Expand Down
103 changes: 86 additions & 17 deletions lua/settings/fzf/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,96 @@ local util = require('util')

local M = {}

---Do a ripgrep search with a fzf search interface.
---Launches a ripgrep search with a fzf search interface.
---@param term? string if empty, the search will only happen on the content.
---@param opts string options to pass to ripgrep call.
function M.do_rg(term, opts)
local delimiter = term and ' --delimiter : --nth 4..' or ''
---@param no_ignore? string disables the ignore rules.
function M.ripgrep_search(term, no_ignore)
term = vim.fn.shellescape(term)
local nth = ''
local with_nth = ''
local delimiter = ''
if term then
with_nth = '--nth 2..'
nth = '--nth 1,4..'
delimiter = '--delimiter :'
end
no_ignore = no_ignore and '' or '--no-ignore'

local rg_cmd = table.concat({
'rg',
'--line-number', '--column',
'--no-heading',
'--color=always',
'--smart-case',
'--hidden',
'-g "!.git/" ', no_ignore,
'--', term,
}, " ")

local args = {
options = '--prompt="Search Files> " --preview-window nohidden' .. delimiter,
options = table.concat({
'--prompt="Search in files> "',
'--preview-window nohidden',
delimiter,
with_nth,
nth,
}, ' ')
}
local preview = vim.fn["fzf#vim#with_preview"](args, 'right:60%:+{2}-/2', 'ctrl-/')

local preview = vim.fn["fzf#vim#with_preview"](args)
vim.fn["fzf#vim#grep"](rg_cmd, 1, preview)
end

---Launches a ripgrep search with a fzf search interface.
---@param term? string if empty, the search will only happen on the content.
---@param no_ignore? string disables the ignore rules.
function M.ripgrep_search_incremental(term, no_ignore)
term = vim.fn.shellescape(term)
local query = ''
local nth = ''
local with_nth = ''
local delimiter = ''
if term then
query = '--query ' .. term
with_nth = '--nth 2..'
nth = '--nth 1,4..'
delimiter = '--delimiter :'
end
no_ignore = no_ignore and '' or '--no-ignore'

local rg_cmd = table.concat({
'rg',
'--column',
'--line-number',
'--line-number', '--column',
'--no-heading',
'--color=always',
'--smart-case',
'--hidden',
'-g "!.git/" ',
opts or '',
'--',
vim.fn.shellescape(term),
'-g "!.git/" ', no_ignore,
'-- %s || true',
}, " ")
vim.fn["fzf#vim#grep"](rg_cmd, 1, preview)

local initial = string.format(rg_cmd, term)
local reload_cmd = string.format(rg_cmd, '{q}')

local args = {
options = table.concat({
'--prompt="1. Ripgrep> "',
'--header="<Alt-Enter>:Reload on current query"',
'--header-lines=1',
'--preview-window nohidden',
'--phony', '--disabled',
--- with_nth,
query,
'--bind',
string.format("'change:reload:%s'", reload_cmd),
'--bind "alt-enter:unbind(change,alt-enter)+change-prompt(2. FZF> )+enable-search+clear-query"',
delimiter,
nth,
}, ' ')
}

local preview = vim.fn["fzf#vim#with_preview"](args)
vim.fn["fzf#vim#grep"](initial, 1, preview)
end

function M.delete_buffer()
Expand Down Expand Up @@ -89,6 +156,8 @@ function M.delete_buffer()
vim.fn["fzf#run"](preview)
end

---Searches in the lines of current buffer. It provides an incremental search
---that would switch to fzf filtering on <Alt-Enter>.
function M.lines_grep()
local options = table.concat({
'--prompt="Current Buffer> "',
Expand Down Expand Up @@ -149,7 +218,7 @@ function M.reload_config()
source = source,
options = table.concat({
'--prompt="Open Config> "',
'--header="<C-a>:reloads all"',
'--header="<C-a>:Reload all"',
'--delimiter="\t"',
'--with-nth=2..', '--nth=1',
'--multi',
Expand Down Expand Up @@ -238,7 +307,7 @@ function M.delete_marks()
source = mark_list,
options = table.concat({
'--prompt="Delete Mark> "',
'--header="<C-a>:deletes all"',
'--header="<C-a>:Delete all"',
'--header-lines=1',
'--delimiter="\t"',
'--with-nth=2..', '--nth=3',
Expand All @@ -265,7 +334,7 @@ function M.git_grep(term)
local wrapped = vim.fn["fzf#wrap"]({
source = source,
options = table.concat({
'--prompt="Search In Tree> "',
'--prompt="Search in tree> "',
'+m',
'--delimiter="\t"',
'--with-nth=2..', '--nth=1',
Expand All @@ -287,7 +356,7 @@ function M.git_grep(term)
local toplevel = vim.fn.system("git rev-parse --show-toplevel")
toplevel = string.gsub(toplevel, "\n", '')
local str = string.format([[fugitive://%s/.git//%s]], toplevel, sha)
vim.ex.edit(str)
nvim.ex.edit(str)
end
end
end
Expand Down

0 comments on commit b110737

Please sign in to comment.