Skip to content

Commit

Permalink
Fix deprecation. Reuse buffer by name. Close #24
Browse files Browse the repository at this point in the history
  • Loading branch information
quolpr committed Jan 15, 2025
1 parent 6841048 commit 72f9f55
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 51 deletions.
4 changes: 2 additions & 2 deletions lua/quicktest/fs_utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ M.path = (function()
end

local function path_join(...)
return table.concat(vim.tbl_flatten({ ... }), path_separator)
return table.concat(vim.fn.flatten({ ... }), path_separator)
end

-- Traverse the path calling cb along the way.
Expand Down Expand Up @@ -193,7 +193,7 @@ function M.find_ancestor_of_file(start_path, file)
end

function M.root_pattern(...)
local patterns = vim.tbl_flatten({ ... })
local patterns = vim.fn.flatten({ ... })
local function matcher(path)
for _, pattern in ipairs(patterns) do
for _, p in ipairs(vim.fn.glob(M.path.join(path, pattern), true, true)) do
Expand Down
8 changes: 4 additions & 4 deletions lua/quicktest/module.lua
Original file line number Diff line number Diff line change
Expand Up @@ -326,9 +326,9 @@ end
--- @param default_mode WinModeWithoutAuto
--- @return WinModeWithoutAuto
function M.current_win_mode(default_mode)
if ui.is_split_opened then
if ui.is_split_opened() then
return "split"
elseif ui.is_popup_opened then
elseif ui.is_popup_opened() then
return "popup"
else
return default_mode
Expand All @@ -351,7 +351,7 @@ end
---@param mode WinModeWithoutAuto
function M.toggle_win(mode)
if mode == "split" then
if ui.is_split_opened then
if ui.is_split_opened() then
ui.try_close_win("split")
else
ui.try_open_win("split")
Expand All @@ -361,7 +361,7 @@ function M.toggle_win(mode)
end
end
else
if ui.is_popup_opened then
if ui.is_popup_opened() then
ui.try_close_win("popup")
else
ui.try_open_win("popup")
Expand Down
120 changes: 75 additions & 45 deletions lua/quicktest/ui.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,76 @@ local M = {}

local api = vim.api

local split_buf = api.nvim_create_buf(false, true)
local popup_buf = api.nvim_create_buf(false, true)
local function find_win_by_bufnr(bufnr)
for _, win in ipairs(vim.api.nvim_list_wins()) do
if vim.api.nvim_win_get_buf(win) == bufnr then
return win
end
end

return -1
end

vim.api.nvim_buf_set_option(split_buf, "undolevels", -1)
vim.api.nvim_buf_set_option(popup_buf, "undolevels", -1)
local function is_buf_visible(bufnr)
return find_win_by_bufnr(bufnr) ~= -1
end

vim.api.nvim_buf_set_option(split_buf, "filetype", "quicktest-output")
vim.api.nvim_buf_set_option(popup_buf, "filetype", "quicktest-output")
-- Function to get or create a buffer with a specific name
local function get_or_create_buf(name)
local full_name = "quicktest://" .. name

--- @type NuiSplit | nil
local split
--- @type NuiPopup | nil
local popup
-- Find buffer with name if it exists
for _, buf in ipairs(api.nvim_list_bufs()) do
local buf_name = api.nvim_buf_get_name(buf)
if buf_name == full_name then
return buf
end
end

M.buffers = { split_buf, popup_buf }
M.is_split_opened = false
M.is_popup_opened = false
-- Create new buffer if not found
local buf = api.nvim_create_buf(false, false)
api.nvim_buf_set_name(buf, full_name)
vim.bo[buf].undolevels = -1
vim.bo[buf].filetype = "quicktest-output"
vim.bo[buf].swapfile = false
vim.bo[buf].modifiable = true
vim.bo[buf].buftype = "nowrite" -- Buffer that cannot be written
vim.bo[buf].modified = false -- Start as unmodified

-- Add autocmd to prevent modification flag
vim.api.nvim_create_autocmd({ "TextChanged", "TextChangedI", "BufModifiedSet" }, {
buffer = buf,
callback = function()
if vim.bo[buf].modified then
vim.bo[buf].modified = false
end
end,
})

return buf
end

-- Get or create buffers with specific names
local function get_split_buf()
return get_or_create_buf("quicktest-split")
end

local function get_popup_buf()
return get_or_create_buf("quicktest-popup")
end

M.buffers = { get_split_buf(), get_popup_buf() }
M.is_split_opened = function()
return is_buf_visible(get_split_buf())
end
M.is_popup_opened = function()
return is_buf_visible(get_popup_buf())
end

local function open_popup()
local popup_options = vim.tbl_deep_extend("force", {
enter = true,
bufnr = popup_buf,
bufnr = get_popup_buf(),
focusable = true,
border = {
style = "rounded",
Expand All @@ -39,50 +87,31 @@ local function open_popup()
},
}, require("quicktest").config.popup_options)

popup = Popup(popup_options)

popup:on(event.WinClosed, function()
M.is_popup_opened = false

popup = nil
end, { once = true })

popup:mount()

M.is_popup_opened = true

return popup
Popup(popup_options):mount()
end

local function open_split()
split = Split({
local split = Split({
relative = "editor",
position = "bottom",
size = "30%",
enter = false,
bufnr = get_split_buf(),
})
split.bufnr = split_buf

split:on(event.WinClosed, function()
M.is_split_opened = false
end, { once = true })
split.bufnr = get_split_buf()

-- mount/open the component
split:mount()

M.is_split_opened = true

return split
end

local function try_open_split()
if not M.is_split_opened then
if not M.is_split_opened() then
open_split()
end
end

local function try_open_popup()
if not M.is_popup_opened then
if not M.is_popup_opened() then
open_popup()
end
end
Expand All @@ -98,14 +127,15 @@ end

---@param mode WinModeWithoutAuto
function M.try_close_win(mode)
local win_id
if mode == "popup" then
if popup then
popup:hide()
end
win_id = find_win_by_bufnr(get_popup_buf())
else
if split then
split:hide()
end
win_id = find_win_by_bufnr(get_split_buf())
end

if win_id ~= -1 then
vim.api.nvim_win_close(win_id, true)
end
end

Expand Down

0 comments on commit 72f9f55

Please sign in to comment.