Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add previewLocation actions #239

Merged
merged 2 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ _Note:_ sync is disabled when doing multiline replacement (`--multiline` flag)
_Note:_ if you would like sync to work when doing a replacement with empty string, please add `--replace=`
to the flags.

### Going to / Opening Result Location
### Going to / Opening / Previewing Result Location
When the cursor is placed on a result file path, you can go to that file by pressing `<enter>` in normal mode (`Goto` action default keybind).
When it's placed over a result match line, you will be taken to the file/line/column of the match. By default, the file buffer
is opened in the last window you were in before opening grug-far, which is typically the other vertical split.
Expand All @@ -156,6 +156,8 @@ _Note:_ for both `Goto` and `Open` actions, if a `<count>` is entered beforehand

In order to smoothly `Open` each result location in sequence, you can use the `Open Next` and `Open Prev` actions.

If you would like to keep the buffers layout, you can use the `Preview` action instead, which will open location in a floating window.

### Opening result lines in quickfix list

Result lines can be opened in the quickfix list. Deleting result lines will cause them not to be included.
Expand Down
38 changes: 38 additions & 0 deletions lua/grug-far/actions/previewLocation.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
local resultsList = require('grug-far/render/resultsList')

local function previewLocation(params)
local buf = params.buf
local context = params.context
local grugfar_win = vim.fn.bufwinid(buf)
local cursor_row = unpack(vim.api.nvim_win_get_cursor(grugfar_win))
local location = resultsList.getResultLocation(cursor_row - 1, buf, context)
if location == nil then
return
end

local width = vim.api.nvim_win_get_width(0)
local height = vim.api.nvim_win_get_height(0)
local opts = {
relative = 'win',
border = 'rounded',
width = width,
height = math.floor(height / 3),
bufpos = { vim.fn.line('.') - 1, vim.fn.col('.') },
focusable = true,
win = grugfar_win,
style = 'minimal',
}

local w = vim.api.nvim_open_win(0, true, opts)
local bufnr = vim.fn.bufnr(location.filename)
if bufnr == -1 then
vim.fn.win_execute(w, 'e ' .. vim.fn.fnameescape(location.filename), true)
else
vim.api.nvim_win_set_buf(w, bufnr)
end
vim.api.nvim_win_set_cursor(w, { location.lnum, location.col - 1 })
local b = vim.fn.winbufnr(w)
vim.api.nvim_set_option_value('bufhidden', 'wipe', { buf = b })
end

return previewLocation
9 changes: 9 additions & 0 deletions lua/grug-far/farBuffer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ local historyOpen = require('grug-far/actions/historyOpen')
local historyAdd = require('grug-far/actions/historyAdd')
local toggleShowCommand = require('grug-far/actions/toggleShowCommand')
local swapEngine = require('grug-far/actions/swapEngine')
local previewLocation = require('grug-far/actions/previewLocation')
local utils = require('grug-far/utils')
local resultsList = require('grug-far/render/resultsList')
local inputs = require('grug-far/inputs')
Expand Down Expand Up @@ -153,6 +154,14 @@ local function getActions(buf, context)
toggleShowCommand({ buf = buf, context = context })
end,
},
{
text = 'Preview',
keymap = keymaps.previewLocation,
description = 'Preview location in floating window.',
action = function()
previewLocation({ buf = buf, context = context })
end,
},
}
end

Expand Down
3 changes: 3 additions & 0 deletions lua/grug-far/opts.lua
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ M.defaultOptions = {
help = { n = 'g?' },
toggleShowCommand = { n = '<localleader>p' },
swapEngine = { n = '<localleader>e' },
previewLocation = { n = '<localleader>i' },
},

-- separator between inputs and results, default depends on nerdfont
Expand Down Expand Up @@ -306,6 +307,7 @@ M.defaultOptions = {
---@field abort KeymapDef
---@field help KeymapDef
---@field swapEngine KeymapDef
---@field previewLocation KeymapDef
leisiji marked this conversation as resolved.
Show resolved Hide resolved

---@class KeymapsOverride
---@field replace? KeymapDef
Expand All @@ -326,6 +328,7 @@ M.defaultOptions = {
---@field abort? KeymapDef
---@field help? KeymapDef
---@field swapEngine? KeymapDef
---@field previewLocation? KeymapDef

---@class AutoSaveTable
---@field enabled boolean
Expand Down
1 change: 1 addition & 0 deletions lua/grug-far/test/helpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ function M.getSetupOptions()
pickHistoryEntry = { n = '<enter>' },
toggleShowCommand = { n = ',p' },
swapEngine = { n = ',e' },
previewLocation = { n = ',i' },
},
history = {
historyDir = vim.uv.cwd() .. '/temp_history_dir',
Expand Down
44 changes: 44 additions & 0 deletions tests/base/test_preview.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
local MiniTest = require('mini.test')
local helpers = require('grug-far/test/helpers')
local keymaps = helpers.getKeymaps()

---@type NeovimChild
local child = MiniTest.new_child_neovim()

local T = MiniTest.new_set({
hooks = {
pre_case = function()
helpers.initChildNeovim(child)
end,
-- Stop once all test cases are finished
post_once = child.stop,
},
})

T['can preview a given location'] = function()
helpers.writeTestFiles({
{ filename = 'file1.txt', content = [[
grug walks
then grug swims
]] },
{
filename = 'file2.doc',
content = [[
grug talks and grug drinks
then grug thinks
]],
},
})

helpers.childRunGrugFar(child, {
prefills = { search = 'grug' },
})
helpers.childWaitForFinishedStatus(child)

child.type_keys('<esc>10G')
child.type_keys('<esc>' .. keymaps.previewLocation.n)
helpers.childWaitForScreenshotText(child, '│ grug walks')
helpers.childExpectScreenshot(child)
end

return T
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
--|---------|---------|---------|---------|---------|---------|---------|---------|
01|  Actions / Help <g?>  Replace <,r>  Sync All <,s> ...
02|
03|  Search:
04| grug
05|  Replace:
06|
07|  Files Filter:
08|
09| 󰮚 Flags:
10|
11|  Paths:
12|
13|
14| STATUS_SUCCESS ⟪ ripgrep ⟫
15|╭──────────────────────────────────────────────────────────────────────────────╮
16|│ │
17|│ grug walks │
18|│ then grug swims │
19|│ │
20|│ │
21|│ │
22|│ │
23|╰──────────────────────────────────────────────────────────────────────────────╯
24| 2,8 All

--|---------|---------|---------|---------|---------|---------|---------|---------|
01|01111111111111111111112221111111111111122211111111111111122222222222222222222222
02|02222222222222222222222222222222222222222222222222222222222222222222222222222222
03|33333333332222222222222222222222222222222222222222222222222222222222222222222222
04|02222222222222222222222222222222222222222222222222222222222222222222222222222222
05|33333333333222222222222222222222222222222222222222222222222222222222222222222222
06|02222222222222222222222222222222222222222222222222222222222222222222222222222222
07|33333333333333332222222222222222222222222222222222222222222222222222222222222222
08|02222222222222222222222222222222222222222222222222222222222222222222222222222222
09|33333333322222222222222222222222222222222222222222222222222222222222222222222222
10|02222222222222222222222222222222222222222222222222222222222222222222222222222222
11|33333333322222222222222222222222222222222222222222222222222222222222222222222222
12|02222222222222222222222222222222222222222222222222222222222222222222222222222222
13|22222222222222222222222222222222222222222222222222222222222222222222222222222222
14|44444444444444444444444444444444444444444444444444444444444444444444444444444444
15|55555555555555555555555555555555555555555555555555555555555555555555555555555555
16|55555555555555555555555555555555555555555555555555555555555555555555555555555555
17|55555555555555555555555555555555555555555555555555555555555555555555555555555555
18|55555555555555555555555555555555555555555555555555555555555555555555555555555555
19|55555555555555555555555555555555555555555555555555555555555555555555555555555555
20|56666666666666666666666666666666666666666666666666666666666666666666666666666665
21|56666666666666666666666666666666666666666666666666666666666666666666666666666665
22|56666666666666666666666666666666666666666666666666666666666666666666666666666665
23|55555555555555555555555555555555555555555555555555555555555555555555555555555555
24|77777777777777777777777777777777777777777777777777777777777777777777777777777777
Loading