From 71b1ef5edfcee7c58fe611fdd79bfafcb9fb0531 Mon Sep 17 00:00:00 2001 From: Steven Arcangeli Date: Sat, 23 Dec 2023 19:16:53 -0500 Subject: [PATCH] feat: constrain_cursor option (closes #257) --- README.md | 3 +++ doc/oil.txt | 3 +++ lua/oil/config.lua | 3 +++ lua/oil/view.lua | 14 +++++++++++++- 4 files changed, 22 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index a2512f1..80325cf 100644 --- a/README.md +++ b/README.md @@ -165,6 +165,9 @@ require("oil").setup({ -- Set to true to autosave buffers that are updated with LSP willRenameFiles -- Set to "unmodified" to only save unmodified buffers lsp_rename_autosave = false, + -- Constrain the cursor to the editable parts of the oil buffer + -- Set to `false` to disable, or "name" to keep it on the file names + constrain_cursor = "editable", -- Keymaps in oil buffer. Can be any value that `vim.keymap.set` accepts OR a table of keymap -- options with a `callback` (e.g. { callback = function() ... end, desc = "", mode = "n" }) -- Additionally, if it is a string that matches "actions.", diff --git a/doc/oil.txt b/doc/oil.txt index ce54284..b2f01dc 100644 --- a/doc/oil.txt +++ b/doc/oil.txt @@ -55,6 +55,9 @@ OPTIONS *oil-option -- Set to true to autosave buffers that are updated with LSP willRenameFiles -- Set to "unmodified" to only save unmodified buffers lsp_rename_autosave = false, + -- Constrain the cursor to the editable parts of the oil buffer + -- Set to `false` to disable, or "name" to keep it on the file names + constrain_cursor = "editable", -- Keymaps in oil buffer. Can be any value that `vim.keymap.set` accepts OR a table of keymap -- options with a `callback` (e.g. { callback = function() ... end, desc = "", mode = "n" }) -- Additionally, if it is a string that matches "actions.", diff --git a/lua/oil/config.lua b/lua/oil/config.lua index 7b04d93..e7000c9 100644 --- a/lua/oil/config.lua +++ b/lua/oil/config.lua @@ -41,6 +41,9 @@ local default_config = { -- Set to true to autosave buffers that are updated with LSP willRenameFiles -- Set to "unmodified" to only save unmodified buffers lsp_rename_autosave = false, + -- Constrain the cursor to the editable parts of the oil buffer + -- Set to `false` to disable, or "name" to keep it on the file names + constrain_cursor = "editable", -- Keymaps in oil buffer. Can be any value that `vim.keymap.set` accepts OR a table of keymap -- options with a `callback` (e.g. { callback = function() ... end, desc = "", mode = "n" }) -- Additionally, if it is a string that matches "actions.", diff --git a/lua/oil/view.lua b/lua/oil/view.lua index ecb7e75..339c03a 100644 --- a/lua/oil/view.lua +++ b/lua/oil/view.lua @@ -235,6 +235,9 @@ end ---Force cursor to be after hidden/immutable columns local function constrain_cursor() + if not config.constrain_cursor then + return + end local parser = require("oil.mutator.parser") local adapter = util.get_adapter(0) @@ -247,7 +250,16 @@ local function constrain_cursor() 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) + local min_col + if config.constrain_cursor == "editable" then + min_col = get_first_mutable_column_col(adapter, result.ranges) + elseif config.constrain_cursor == "name" then + min_col = result.ranges.name[1] + else + error( + string.format('Unexpected value "%s" for option constrain_cursor', config.constrain_cursor) + ) + end if cur[2] < min_col then vim.api.nvim_win_set_cursor(0, { cur[1], min_col }) end