feat(defaults): jump between :terminal shell prompts with ]]/[[ #32736

This commit is contained in:
Gregory Anders
2025-03-07 07:45:34 -06:00
committed by GitHub
parent c8b64b7a43
commit b31132f1c1
10 changed files with 160 additions and 55 deletions

View File

@@ -534,14 +534,59 @@ do
end,
})
local nvim_terminal_prompt_ns = vim.api.nvim_create_namespace('nvim.terminal.prompt')
vim.api.nvim_create_autocmd('TermRequest', {
group = nvim_terminal_augroup,
desc = 'Mark shell prompts indicated by OSC 133 sequences for navigation',
callback = function(args)
if string.match(args.data.sequence, '^\027]133;A') then
local lnum = args.data.cursor[1] ---@type integer
vim.api.nvim_buf_set_extmark(args.buf, nvim_terminal_prompt_ns, lnum - 1, 0, {})
end
end,
})
---@param ns integer
---@param buf integer
---@param count integer
local function jump_to_prompt(ns, win, buf, count)
local row, col = unpack(vim.api.nvim_win_get_cursor(win))
local start = -1
local end_ ---@type 0|-1
if count > 0 then
start = row
end_ = -1
elseif count < 0 then
-- Subtract 2 because row is 1-based, but extmarks are 0-based
start = row - 2
end_ = 0
end
if start < 0 then
return
end
local extmarks = vim.api.nvim_buf_get_extmarks(
buf,
ns,
{ start, col },
end_,
{ limit = math.abs(count) }
)
if #extmarks > 0 then
local extmark = extmarks[math.min(#extmarks, math.abs(count))]
vim.api.nvim_win_set_cursor(win, { extmark[2] + 1, extmark[3] })
end
end
vim.api.nvim_create_autocmd('TermOpen', {
group = nvim_terminal_augroup,
desc = 'Default settings for :terminal buffers',
callback = function()
vim.bo.modifiable = false
vim.bo.undolevels = -1
vim.bo.scrollback = vim.o.scrollback < 0 and 10000 or math.max(1, vim.o.scrollback)
vim.bo.textwidth = 0
callback = function(args)
vim.bo[args.buf].modifiable = false
vim.bo[args.buf].undolevels = -1
vim.bo[args.buf].scrollback = vim.o.scrollback < 0 and 10000 or math.max(1, vim.o.scrollback)
vim.bo[args.buf].textwidth = 0
vim.wo[0][0].wrap = false
vim.wo[0][0].list = false
vim.wo[0][0].number = false
@@ -555,6 +600,13 @@ do
winhl = winhl .. ','
end
vim.wo[0][0].winhighlight = winhl .. 'StatusLine:StatusLineTerm,StatusLineNC:StatusLineTermNC'
vim.keymap.set('n', '[[', function()
jump_to_prompt(nvim_terminal_prompt_ns, 0, args.buf, -vim.v.count1)
end, { buffer = args.buf, desc = 'Jump [count] shell prompts backward' })
vim.keymap.set('n', ']]', function()
jump_to_prompt(nvim_terminal_prompt_ns, 0, args.buf, vim.v.count1)
end, { buffer = args.buf, desc = 'Jump [count] shell prompts forward' })
end,
})