From d4eb4f3bbf7770d04070707c947655a5426d7f75 Mon Sep 17 00:00:00 2001 From: Steven Arcangeli Date: Sat, 9 Sep 2023 13:20:16 -0700 Subject: [PATCH] fix: lock cursor to first mutable column Previously we were forcing the cursor to be after the hidden ID at the start, but that still meant that it would end up on top of the icon. This made rename operations slightly more annoying than necessary, since you would need to first move the cursor forward to the file name. Now, the cursor will be locked to the beginning of the filename unless there is a mutable column earlier in the row. --- lua/oil/view.lua | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/lua/oil/view.lua b/lua/oil/view.lua index 047996b..1d530dd 100644 --- a/lua/oil/view.lua +++ b/lua/oil/view.lua @@ -230,6 +230,22 @@ M.delete_hidden_buffers = function() cache.clear_everything() end +---@param adapter oil.Adapter +---@param ranges table +---@return integer +local function get_first_mutable_column_col(adapter, ranges) + local min_col = ranges.name[1] + for col_name, start_len in pairs(ranges) do + local start = start_len[1] + local col_spec = columns.get_column(adapter, col_name) + local is_col_mutable = col_spec and col_spec.perform_action ~= nil + if is_col_mutable and start < min_col then + min_col = start + end + end + return min_col +end + ---@param bufnr integer M.initialize = function(bufnr) if bufnr == 0 then @@ -308,8 +324,8 @@ M.initialize = function(bufnr) 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.data then - local min_col = result.ranges.id[2] + 1 + 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