neovim part 7 with better floaters

This commit is contained in:
RafayAhmad7548 2025-05-05 23:50:18 +05:00
parent 97482df1a5
commit 36c7c39213

View file

@ -20,22 +20,22 @@ local function open_floating_win(buf, w_percent, h_percent, opts)
vim.api.nvim_open_win(buf, true, opts) vim.api.nvim_open_win(buf, true, opts)
end end
---@class TermState
---@field bufs integer[]
---@field last_used integer[]
---@field win integer
local term_state = { local term_state = {
-1, bufs = { -1, -1, -1, -1 },
-1, last_used = { 1, 2, 3, 4 },
-1,
-1,
last_used = 1,
win = -1, win = -1,
} }
vim.api.nvim_create_user_command('Floaterminal', function(table) vim.api.nvim_create_user_command('Floaterminal', function(args_table)
-- TODO: update last_used buffer if it gets deleted
local is_new = false local is_new = false
if table.args ~= '' then if args_table.args ~= '' then
local num = tonumber(table.args) local num = tonumber(args_table.args)
if num < 1 or num > 4 then if num < 1 or num > 4 then
error('numbers from 1 to 4 allowed only') error('numbers from 1 to 4 allowed only')
end end
@ -43,22 +43,36 @@ vim.api.nvim_create_user_command('Floaterminal', function(table)
error('only specify arguments after opening the floaterminal') error('only specify arguments after opening the floaterminal')
end end
if not vim.api.nvim_buf_is_valid(term_state[num]) then if not vim.api.nvim_buf_is_valid(term_state.bufs[num]) then
term_state[num] = vim.api.nvim_create_buf(true, false) term_state.bufs[num] = vim.api.nvim_create_buf(true, false)
is_new = true is_new = true
end end
term_state.last_used = num for index, value in ipairs(term_state.last_used) do
vim.api.nvim_set_current_buf(term_state[num]) if value == num then
table.remove(term_state.last_used, index)
break
end
end
table.insert(term_state.last_used, 1, num)
vim.api.nvim_set_current_buf(term_state.bufs[num])
else else
if not vim.api.nvim_buf_is_valid(term_state[term_state.last_used]) then for _ = 1, 4 do
term_state[term_state.last_used] = vim.api.nvim_create_buf(true, false) if not vim.api.nvim_buf_is_valid(term_state.bufs[term_state.last_used[1]]) then
local invalid_buf_index = table.remove(term_state.last_used, 1)
table.insert(term_state.last_used, 4, invalid_buf_index)
end
end
if not vim.api.nvim_buf_is_valid(term_state.bufs[term_state.last_used[1]]) then
term_state.bufs[term_state.last_used[1]] = vim.api.nvim_create_buf(true, false)
is_new = true is_new = true
end end
open_floating_win(term_state[term_state.last_used]) open_floating_win(term_state.bufs[term_state.last_used[1]])
term_state.win = vim.api.nvim_get_current_win() term_state.win = vim.api.nvim_get_current_win()