feat: prevent backspace key to delete into the prefix area when using constrain cursor in insert mode #133

Closed
opened 2026-03-15 16:58:09 +00:00 by kuglee · 2 comments
kuglee commented 2026-03-15 16:58:09 +00:00

Prerequisites

Problem

The fix for #93 fixes arrow key navigation, but it doesn't prevent deleting into the prefix are using the backspace key.

Steps to reproduce

  1. Enter insert mode
  2. Start deleting the filename with the backspace key until it deletes text in the previous columns

Proposed solution

I don't know oil's codebase so I don't suggest that this exact solution should be used, but this is the fix that Claude came up with:

view.lua:

---Setup insert mode key mappings to respect cursor constraints
---@param bufnr integer
local function setup_insert_mode_constraints(bufnr)
  local mode_config = config.constrain_cursor
  if not mode_config then
    return
  end

  -- Prevent backspace from deleting into previous columns
  vim.keymap.set('i', '<BS>', function()
    local adapter = util.get_adapter(bufnr, true)
    if not adapter then
      return '<BS>'
    end

    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]

    if not line or line == '' then
      return '<BS>'
    end

    local parser = require('oil.mutator.parser')
    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
      if mode_config == 'editable' then
        min_col = get_first_mutable_column_col(adapter, result.ranges)
      elseif mode_config == 'name' then
        min_col = result.ranges.name[1]
      else
        return '<BS>'
      end

      -- Block backspace if cursor is at or before the start of the editable region
      if cur[2] <= min_col then
        return ''
      end
    end

    return '<BS>'
  end, {
    buffer = bufnr,
    expr = true,
    desc = 'Oil: Constrained backspace',
  })
end

An this should be called at end of M.initialize

Alternatives considered

No response

### Prerequisites - [x] I have searched [existing issues](https://github.com/barrettruth/canola.nvim/issues) ### Problem The fix for #93 fixes arrow key navigation, but it doesn't prevent deleting into the prefix are using the backspace key. **Steps to reproduce** 1. Enter insert mode 2. Start deleting the filename with the backspace key until it deletes text in the previous columns ### Proposed solution I don't know oil's codebase so I don't suggest that this exact solution should be used, but this is the fix that Claude came up with: view.lua: ```lua ---Setup insert mode key mappings to respect cursor constraints ---@param bufnr integer local function setup_insert_mode_constraints(bufnr) local mode_config = config.constrain_cursor if not mode_config then return end -- Prevent backspace from deleting into previous columns vim.keymap.set('i', '<BS>', function() local adapter = util.get_adapter(bufnr, true) if not adapter then return '<BS>' end 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] if not line or line == '' then return '<BS>' end local parser = require('oil.mutator.parser') 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 if mode_config == 'editable' then min_col = get_first_mutable_column_col(adapter, result.ranges) elseif mode_config == 'name' then min_col = result.ranges.name[1] else return '<BS>' end -- Block backspace if cursor is at or before the start of the editable region if cur[2] <= min_col then return '' end end return '<BS>' end, { buffer = bufnr, expr = true, desc = 'Oil: Constrained backspace', }) end ``` An this should be called at end of `M.initialize` ### Alternatives considered _No response_
barrettruth commented 2026-03-15 17:20:13 +00:00

@kuglee glad you're using this.

Looking into the fix now.

@kuglee glad you're using this. Looking into the fix now.
kuglee commented 2026-03-15 17:50:05 +00:00

Awesome. Thanks 🙏

Awesome. Thanks 🙏
Sign in to join this conversation.
No description provided.