grapp-dev / nui-components.nvim

A feature-rich and highly customizable library for creating user interfaces in Neovim.
https://nui-components.grapp.dev
MIT License
301 stars 5 forks source link

[Bug] Text input made readonly #46

Open Ajaymamtora opened 1 month ago

Ajaymamtora commented 1 month ago

Description

When setting the buffer to start in visual mode, the text input is readonly. Seems similar to https://github.com/grapp-dev/nui-components.nvim/issues/17

Repro

local M = {}

vim.api.nvim_create_user_command("NuiGoToLine", function()
  M.go_to_line_nui()
end, {})

local function go_to_line(input)
  -- Print the input
  print(input)

  -- Check if the input matches the format LINE or LINE:COLUMN using a Lua pattern
  local line, column = input:match("^(%d+):?(%d*)$")

  -- Return early if the input is not in the correct format
  if not line then
    print("Input must be in the format LINE or LINE:COLUMN")
    return
  end

  -- Convert line and column to numbers
  line = tonumber(line)
  column = tonumber(column)

  -- Default to column 1 if no column is provided
  if column == 0 then
    column = 1
  end

  -- Go to the specified line and column
  vim.api.nvim_win_set_cursor(0, { line, column })
end

function M.go_to_line_nui()
  local n = require("nui-components")
  local event = require("nui.utils.autocmd").event

  local r, c = unpack(vim.api.nvim_win_get_cursor(0))

  local signal = n.create_signal({
    value = r .. ":" .. c,
  })

  local subscription = signal:observe(function(previous_state, current_state)
    -- call side effects
  end)

  local renderer = n.create_renderer({
    width = 80,
    height = 1,
  })

  renderer:add_mappings({
    {
      mode = { "n", "i", "v" },
      key = "<CR>",
      handler = function()
        go_to_line(signal.value:get_value())
        renderer:close()
      end,
    },
    {
      mode = { "n", "i" },
      key = "<C-q>",
      handler = function()
        renderer:close()
      end,
    },
  })
  local content
  content = n.text_input({
    autofocus = true,
    autoresize = true,
    size = 1,
    value = signal.value,
    border_label = "Go to line",
    placeholder = "L:C",
    max_lines = 1,
    on_change = function(value, component)
      signal.value = value

      component:modify_buffer_content(function()
        -- component:set_border_text("bottom", "Length: " .. #value, "right")
      end)
    end,
    on_mount = function(component)
      local initial_value = signal.value:get_value()

      component:set_border_text("bottom", "Current: " .. r .. ":" .. c, "right")

      local _close = function()
        renderer:close()
      end
      vim.api.nvim_create_autocmd({ "BufLeave" }, { callback = _close, once = true })

      -- Start visual mode
      vim.cmd("normal! v")
    end,
  })

  local body = function()
    return content
  end
  renderer:on_unmount(function()
    subscription:unsubscribe()
  end)

  renderer:render(body)
end

return M

Current Behavior

Input set to readonly

Expected Behavior

Dont set to readonly

NuiComponents version

Main caecfe2

Neovim version

0.10 stable

Ajaymamtora commented 1 month ago

Another issue: If I remove vim.cmd("normal! v") I can edit the input, but then entering cc in normal mode empties the text and makes the buffer readonly.

It looks like just exiting insert mode makes the input readonly.

Ajaymamtora commented 1 month ago

Removing autoresize seems to be a workaround