Skip to content

Commit

Permalink
feat: constrain_cursor option (closes #257)
Browse files Browse the repository at this point in the history
  • Loading branch information
stevearc committed Dec 24, 2023
1 parent a60c6d1 commit 71b1ef5
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 1 deletion.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.<name>",
Expand Down
3 changes: 3 additions & 0 deletions doc/oil.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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.<name>",
Expand Down
3 changes: 3 additions & 0 deletions lua/oil/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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.<name>",
Expand Down
14 changes: 13 additions & 1 deletion lua/oil/view.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit 71b1ef5

Please sign in to comment.