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(autocmd): preview qflist files using <C-n> <C-p> #407

Merged
merged 1 commit into from
Sep 17, 2024
Merged
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
67 changes: 67 additions & 0 deletions lua/custom/autocommands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,71 @@ vim.api.nvim_create_autocmd({ 'LspAttach' }, {
end,
})

-- https://www.reddit.com/r/neovim/comments/1fhy2xi/comment/lnea46c/
vim.api.nvim_create_autocmd('FileType', {
pattern = 'qf',
desc = 'navigate and preview qf-list results using <C-p> and <C-n>',
callback = function(event)
local opts = { buffer = event.buf, silent = true }
local init_bufnr = vim.fn.bufnr('#')
vim.keymap.set('n', '<C-n>', function()
if vim.fn.line('.') == vim.fn.line('$') then
vim.notify('E553: No more items', vim.log.levels.ERROR)
return
end
vim.cmd('wincmd p') -- jump to current displayed file
vim.cmd(
(vim.fn.bufnr('%') ~= init_bufnr and vim.bo.filetype ~= 'qf')
and ('bd | wincmd p | cn | res %d'):format(
math.floor(
(
vim.o.lines
- vim.o.cmdheight
- (vim.o.laststatus == 0 and 0 or 1)
- (vim.o.tabline == '' and 0 or 1)
)
/ 3
* 2
+ 0.5
) - 1
)
or 'cn'
)
vim.cmd('execute "normal! zz"')
if vim.bo.filetype ~= 'qf' then
vim.cmd('wincmd p')
end
end, opts)

vim.keymap.set('n', '<C-p>', function()
if vim.fn.line('.') == 1 then
vim.notify('E553: No more items', vim.log.levels.ERROR)
return
end
vim.cmd('wincmd p') -- jump to current displayed file
vim.cmd(
(vim.fn.bufnr('%') ~= init_bufnr and vim.bo.filetype ~= 'qf')
and ('bd | wincmd p | cN | res %d'):format(
math.floor(
(
vim.o.lines
- vim.o.cmdheight
- (vim.o.laststatus == 0 and 0 or 1)
- (vim.o.tabline == '' and 0 or 1)
)
/ 3
* 2
+ 0.5
) - 1
)
or 'cN'
)
vim.cmd('execute "normal! zz"')
if vim.bo.filetype ~= 'qf' then
vim.cmd('wincmd p')
end
end, opts)
end,
})

vim.opt.updatetime = 100
Loading