fix: constrain cursor when entering insert mode
The main use case for this is hitting `I` from normal mode will now put the cursor in insert mode at the beginning of the first editable column.
This commit is contained in:
parent
250e0af7a5
commit
a60c6d10fd
1 changed files with 32 additions and 15 deletions
|
|
@ -233,6 +233,27 @@ local function get_first_mutable_column_col(adapter, ranges)
|
||||||
return min_col
|
return min_col
|
||||||
end
|
end
|
||||||
|
|
||||||
|
---Force cursor to be after hidden/immutable columns
|
||||||
|
local function constrain_cursor()
|
||||||
|
local parser = require("oil.mutator.parser")
|
||||||
|
|
||||||
|
local adapter = util.get_adapter(0)
|
||||||
|
if not adapter then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
local cur = vim.api.nvim_win_get_cursor(0)
|
||||||
|
local line = vim.api.nvim_buf_get_lines(0, cur[1] - 1, cur[1], true)[1]
|
||||||
|
local column_defs = columns.get_supported_columns(adapter)
|
||||||
|
local result = parser.parse_line(adapter, line, column_defs)
|
||||||
|
if result and result.ranges then
|
||||||
|
local min_col = get_first_mutable_column_col(adapter, result.ranges)
|
||||||
|
if cur[2] < min_col then
|
||||||
|
vim.api.nvim_win_set_cursor(0, { cur[1], min_col })
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
---Redraw original path virtual text for trash buffer
|
---Redraw original path virtual text for trash buffer
|
||||||
---@param bufnr integer
|
---@param bufnr integer
|
||||||
local function redraw_trash_virtual_text(bufnr)
|
local function redraw_trash_virtual_text(bufnr)
|
||||||
|
|
@ -338,31 +359,27 @@ M.initialize = function(bufnr)
|
||||||
end,
|
end,
|
||||||
})
|
})
|
||||||
local timer
|
local timer
|
||||||
|
vim.api.nvim_create_autocmd("InsertEnter", {
|
||||||
|
desc = "Constrain oil cursor position",
|
||||||
|
group = "Oil",
|
||||||
|
buffer = bufnr,
|
||||||
|
callback = function()
|
||||||
|
-- For some reason the cursor bounces back to its original position,
|
||||||
|
-- so we have to defer the call
|
||||||
|
vim.schedule(constrain_cursor)
|
||||||
|
end,
|
||||||
|
})
|
||||||
vim.api.nvim_create_autocmd("CursorMoved", {
|
vim.api.nvim_create_autocmd("CursorMoved", {
|
||||||
desc = "Update oil preview window",
|
desc = "Update oil preview window",
|
||||||
group = "Oil",
|
group = "Oil",
|
||||||
buffer = bufnr,
|
buffer = bufnr,
|
||||||
callback = function()
|
callback = function()
|
||||||
local oil = require("oil")
|
local oil = require("oil")
|
||||||
local parser = require("oil.mutator.parser")
|
|
||||||
if vim.wo.previewwindow then
|
if vim.wo.previewwindow then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Force the cursor to be after the (concealed) ID at the beginning of the line
|
constrain_cursor()
|
||||||
local adapter = util.get_adapter(bufnr)
|
|
||||||
if adapter then
|
|
||||||
local cur = vim.api.nvim_win_get_cursor(0)
|
|
||||||
local line = vim.api.nvim_buf_get_lines(bufnr, cur[1] - 1, cur[1], true)[1]
|
|
||||||
local column_defs = columns.get_supported_columns(adapter)
|
|
||||||
local result = parser.parse_line(adapter, line, column_defs)
|
|
||||||
if result and result.ranges then
|
|
||||||
local min_col = get_first_mutable_column_col(adapter, result.ranges)
|
|
||||||
if cur[2] < min_col then
|
|
||||||
vim.api.nvim_win_set_cursor(0, { cur[1], min_col })
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
if config.preview.update_on_cursor_moved then
|
if config.preview.update_on_cursor_moved then
|
||||||
-- Debounce and update the preview window
|
-- Debounce and update the preview window
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue