Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TAB has some bug at Insert mode #79

Open
wqk151 opened this issue Oct 18, 2023 · 5 comments · May be fixed by #84
Open

TAB has some bug at Insert mode #79

wqk151 opened this issue Oct 18, 2023 · 5 comments · May be fixed by #84

Comments

@wqk151
Copy link

wqk151 commented Oct 18, 2023

before tab
1

after tab
2

my config

return {
  "gaoDean/autolist.nvim",
  priority = 1,
  ft = {
    "markdown",
    "text",
  },
  config = function()
    require("autolist").setup()
    vim.keymap.set("i", "<tab>", "<cmd>AutolistTab<cr>")
    vim.keymap.set("i", "<s-tab>", "<cmd>AutolistShiftTab<cr>")
    vim.keymap.set("i", "<CR>", "<CR><cmd>AutolistNewBullet<cr>")
    vim.keymap.set("n", "o", "o<cmd>AutolistNewBullet<cr>")
    vim.keymap.set("n", "O", "O<cmd>AutolistNewBulletBefore<cr>")
    vim.keymap.set("n", "<CR>", "<cmd>AutolistToggleCheckbox<cr><CR>")
    vim.keymap.set("n", "<C-r>", "<cmd>AutolistRecalculate<cr>")
  end,
  dependencies = {
    {
      "windwp/nvim-autopairs",
      event = "VeryLazy",
    },
  },
}
@gaoDean
Copy link
Owner

gaoDean commented Oct 18, 2023

Hi, could you try this branch? It has a potential fix: #80

@wqk151
Copy link
Author

wqk151 commented Oct 19, 2023

I tried , but it doesn't work properly.

@j-w-e
Copy link

j-w-e commented Oct 28, 2023

I confirm this behaviour with tab. This is the case even on a fresh config with only lazy.nvim and autolist.nvim installed (and , and on a pure markdown file with only text.

My the test init.lua I used is bootstrapping lazy.nvim, then

require("lazy").setup({
  {
    "gaoDean/autolist.nvim",
    ft = {
        "markdown",
        "text",
    },
    config = function()
        require("autolist").setup()
        vim.keymap.set("i", "<tab>", "<cmd>AutolistTab<cr>")
        vim.keymap.set("i", "<s-tab>", "<cmd>AutolistShiftTab<cr>")
    end,
  },
})

This produces the behaviour that @wqk151 observed.

@mcauley-penney
Copy link

mcauley-penney commented Dec 29, 2023

The problem seems to stem from line 25 in auto.lua:

    vim.cmd.normal({ "a" .. parsed_key, bang = true })

Because we append (the 'a' in the command above), the cursor will reenter insert mode one character forward. For example, if we use the text from the original image, we start like

|print(request.args.get("name"))

where '|' is the cursor in insert mode. When we press tab, the above lua code runs. This enters normal mode, then simulates pressing 'a' which enters insert after the current character (append), then issues the tab key press. This means we then have

p| rint(request.args.get("name"))

This goes away if we change 'a' to 'i', but I don't know yet what other functionality this breaks because I don't know this code base. I'm looking at fixing this right now because this bug is irksome.

@mcauley-penney
Copy link

mcauley-penney commented Dec 30, 2023

Reading the branch of that function intended for insert mode (the function is only called once right now, so it is only ever used in insert mode), it appears like the intention is to simply fall back to whatever key the user pressed. For example, if we are not currently in a list, the else in handle_indent runs and either a <tab> or <s-tab> keypress is simulated for the user, and we want to simply fall back to that keypress via the press function. To do this, we can skip the normal command from line 25 and simply use feedkeys:

local function press(key, mode)
  if not key or key == "" then return end
  local parsed_key = vim.api.nvim_replace_termcodes(key, true, false, true)
  if mode == "i" then
    vim.api.nvim_feedkeys(parsed_key, 'n', true)
  else
    vim.cmd.normal({ parsed_key, bang = true })
  end
end 

Using this version of the function, this is the functionality I have with tab and shift-tab:
autolist

  1. Tab and shift-tab indent and dedent when at the end of the line (the intended functionality of autolist)
  2. tab works correctly in other contexts, such as at the start of the line

Disclaimer: I don't have anything mapped to shift-tab normally, so I can't show it working outside of the list context.

mcauley-penney added a commit to mcauley-penney/autolist.nvim that referenced this issue Dec 30, 2023
Problem: the `press()` function is used to fall back to the original
functionality of `<tab>` and `<S-tab>` when not in a list context. When
it does this, it appends before executing the original keypress,
modifying the fall back functionality.

Solution: Do not append before using the fall back functionality of
`<tab>` and `<S-tab>`.

This commit also refactors `handle_indent()`,
     - removes unused variables
     - removes `run_recalculate_after_delay()`, which appears to have
       no effect
     - reuses variables when possible (`cur_line`)
     - renames variables for brevity (`in_list` and `at_line_end`)

Fixes gaoDean#79
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants