Skip to content

Commit

Permalink
fix: constrain cursor when entering insert mode
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
stevearc committed Dec 24, 2023
1 parent 250e0af commit a60c6d1
Showing 1 changed file with 32 additions and 15 deletions.
47 changes: 32 additions & 15 deletions lua/oil/view.lua
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,27 @@ local function get_first_mutable_column_col(adapter, ranges)
return min_col
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
---@param bufnr integer
local function redraw_trash_virtual_text(bufnr)
Expand Down Expand Up @@ -338,31 +359,27 @@ M.initialize = function(bufnr)
end,
})
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", {
desc = "Update oil preview window",
group = "Oil",
buffer = bufnr,
callback = function()
local oil = require("oil")
local parser = require("oil.mutator.parser")
if vim.wo.previewwindow then
return
end

-- Force the cursor to be after the (concealed) ID at the beginning of the line
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
constrain_cursor()

if config.preview.update_on_cursor_moved then
-- Debounce and update the preview window
Expand Down

0 comments on commit a60c6d1

Please sign in to comment.