akinsho / akinsho/toggleterm.nvim

[BUG] Calling toggle in quick succession does not respect persist_mode/start_in_insert

Abierto
#522 2 comentarios 1 reacción 0 asignados Ver en GitHub
Lenguaje dominante
Lua
Estrellas
5.6k
Forks
206
Métricas de merge de PR
Sin PR fusionados en 30 d

Descripción

### Is there an existing issue for this?

- [X] I have searched the existing issues

### Current Behavior

Calling `toggle` twice in quick succession will not respect `persist_mode`/`start_in_insert`

### Expected Behavior

No amount of `toggle` calling changes the behavior.

### Steps To Reproduce

1. Open neovim with the following minimal config using `nvim -u repro.lua somerandomfile`:
```lua
-- DO NOT change the paths and don't remove the colorscheme
local root = vim.fn.fnamemodify("./.repro", ":p")

-- set stdpaths to use .repro
for _, name in ipairs({ "config", "data", "state", "cache" }) do
vim.env[("XDG_%s_HOME"):format(name:upper())] = root .. "/" .. name
end

-- bootstrap lazy
local lazypath = root .. "/plugins/lazy.nvim"
if not vim.loop.fs_stat(lazypath) then
vim.fn.system({ "git", "clone", "--filter=blob:none", "https://github.com/folke/lazy.nvim.git", lazypath })
end
vim.opt.runtimepath:prepend(lazypath)
vim.g.mapleader = " "

-- install plugins
local plugins = {
"folke/tokyonight.nvim",
-- add any other plugins here
}

local toggleterm_config = {
"akinsho/toggleterm.nvim",
config = function()
require("toggleterm").setup({
shading_factor = "0",
hide_numbers = false,
persist_mode = true,
start_in_insert = true,
direction = "float",
size = function(term)
if term.direction == "horizontal" then
return vim.o.lines * 0.5
elseif term.direction == "vertical" then
return vim.o.columns * 0.5
end
end,
})

local term = require("toggleterm.terminal").Terminal
vim.keymap.set("n", "tc", function()
local t = term:new({
dir = "%:p:h",
display_name = vim.fn.expand("%:p:h"),
})
t:toggle()
end, { desc = "Term current dir" })
vim.keymap.set("n", "t;", function()
local t = term:new({
display_name = "default",
})
t:toggle()
end, { desc = "Create term" })
vim.keymap.set({ "n", "t" }, "", "ToggleTerm", { desc = "Toggle term" })
vim.keymap.set({ "n", "t" }, "", function()
require("toggleterm").toggle(2)
require("toggleterm").toggle(1)
end, { desc = "Toggle term" })
end,
}

table.insert(plugins, toggleterm_config)
require("lazy").setup(plugins, {
root = root .. "/plugins",
})

vim.cmd.colorscheme("tokyonight")
-- add anything else here
```
2. Create 2 terminals using the provided keybinds (in my case, `tc` followed by `t;`
3. Once the 2nd terminal is opened, press ``
4. Notice that while switching back to the first terminal, this terminal will now be in `normal` mode

### Environment

```Markdown
- OS: arch Linux 6.6.3-arch1-1
- neovim version: v0.10.0-dev-38e9875
- Shell: zsh
```

### Anything else?

Here is a video of the `Steps to Reproduce` section:
https://github.com/akinsho/toggleterm.nvim/assets/7985687/a7c5e75d-609a-4c3b-a2ea-f490add9039c
Note that this behavior will happen also when calling toggle on the same terminal twice in succession as well, e.g.:
```lua
require("toggleterm").toggle(2)
require("toggleterm").toggle(2)
```
It also happens if we call the `:{count}ToggleTerm` twice in quick succession as well.

#### Why does that matter?

I think being able to call it in quick succession can be useful for some key bindings. For example. I was trying to setup the following keybinds to cycle through my current floating terminals:
```lua
local function get_term_index(current_id, terms)
local idx
for i, v in ipairs(terms) do
if v.id == current_id then
idx = i
end
end
return idx
end

local function go_prev_term()
local current_id = vim.b.toggle_number
if current_id == nil then
return
end

local terms = require("toggleterm.terminal").get_all(true)
local prev_index

local index = get_term_index(current_id, terms)
if index > 1 then
prev_index = index - 1
else
prev_index = #terms
end
require("toggleterm").toggle(index)
require("toggleterm").toggle(prev_index)
end

local function go_next_term()
local current_id = vim.b.toggle_number
if current_id == nil then
return
end

local terms = require("toggleterm.terminal").get_all(true)
local next_index

local index = get_term_index(current_id, terms)
if index == #terms then
next_index = 1
else
next_index = index + 1
end
require("toggleterm").toggle(index)
require("toggleterm").toggle(next_index)
end

vim.keymap.set({ "n", "t" }, "", function()
go_next_term()
end, { desc = "Toggle term" })

vim.keymap.set({ "n", "t" }, "", function()
go_prev_term()
end, { desc = "Toggle term" })
```

Guía de contribución

No hay ninguna guía de contribución indexada para este repositorio

Evaluación

Este issue todavía no se ha evaluado.

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.