Skip to content

Commit

Permalink
feat(python): chase buffers following original buffers
Browse files Browse the repository at this point in the history
Chase buffers will turn hidden if the original buffer is turns
hidden, and will be displayed back when original buffer is
displayed again.

Created initial structure to track chase buffers. Now the original
buffer number will be set as a variable to the chase buffer so we
can unset the reference while closing the chase buffer while
focused.

Refs: #6 #7
  • Loading branch information
piraz committed Feb 7, 2024
1 parent 6a9a87c commit 776ad84
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 24 deletions.
116 changes: 101 additions & 15 deletions lua/chase/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ M.log = Log.new({
-- use_console = false,
})

M.buf_refs = {}
M.buf_win_refs = {}

-- tbl_deep_extend does not work the way you would think
-- yonk from harpoon, not public
local function merge_table_impl(t1, t2)
Expand Down Expand Up @@ -88,9 +91,9 @@ local chase_it_opts = { nargs = "*", complete=M.chase_it_complete }
vim.api.nvim_create_user_command("Chase", M.chase_it, chase_it_opts)
vim.api.nvim_create_user_command("C", M.chase_it, chase_it_opts)

function M.buf_chase(file)
local buf = M.buf_open(file .. "_run")
return buf
function M.buf_chase(file, buf)
local chase_buf = M.buf_open(file .. "_run", buf)
return chase_buf
end

function M.buf_is_visible(buf)
Expand Down Expand Up @@ -127,11 +130,11 @@ function M.buf_from_name(name)
return -1
end

function M.buf_open(name, type)
function M.buf_open(name, buf, type)
-- Get a boolean that tells us if the buffer number is visible anymore.
--
-- :help bufwinnr
local buf = -1
local chase_buf = -1
name = name or "MONSTER_OF_THE_LAKE"
type = type or "txt"

Expand All @@ -141,22 +144,105 @@ function M.buf_open(name, type)
end

local cur_win = vim.api.nvim_get_current_win()
if buf == -1 or not M.buf_is_visible(buf) then
if chase_buf == -1 or not M.buf_is_visible(chase_buf) then
vim.cmd("botright vsplit " .. name)
buf = vim.api.nvim_get_current_buf()
chase_buf = vim.api.nvim_get_current_buf()
-- vim.opt_local.readonly = true
vim.api.nvim_buf_set_option(buf, "readonly", true)
vim.api.nvim_buf_set_option(buf, "buftype", "nowrite")
vim.api.nvim_buf_set_option(buf, "filetype", type)
vim.api.nvim_buf_set_option(buf, "buflisted", false)
vim.api.nvim_buf_set_keymap(buf, "n", "q", ":q!<CR>", {})
vim.api.nvim_buf_set_option(chase_buf, "readonly", true)
vim.api.nvim_buf_set_option(chase_buf, "buftype", "nowrite")
vim.api.nvim_buf_set_option(chase_buf, "filetype", type)
vim.api.nvim_buf_set_option(chase_buf, "buflisted", false)
vim.api.nvim_buf_set_var(chase_buf, "original_buf", buf)
vim.api.nvim_buf_set_keymap(chase_buf, "n", "q", "",
{callback = function()
M.chase_buf_destroy(chase_buf)
end}
)
vim.api.nvim_set_current_win(cur_win)
return buf
M.buf_refs[buf] = chase_buf
return chase_buf
end
end

-- print(M.buf_from_name("MONSTER_OF_THE_LAKE"))
-- print(vim.inspect(M.all_listed_buffers()))
function M.on_buf_hidden()
local cur_buf = vim.api.nvim_get_current_buf()
local buf = M.buf_refs[cur_buf]
if buf then
M.buf_hide(buf)
end
end

function M.on_buf_enter()
local cur_buf = vim.api.nvim_get_current_buf()
for buf, buf_ref in pairs(M.buf_refs) do
if buf ~= cur_buf then
M.buf_hide(buf_ref)
end
if buf == cur_buf then
if M.buf_is_hidden(buf_ref) then
M.buf_show(buf_ref)
end
end
end
end

-- function M.on_buf_leave()
-- local cur_buf = vim.api.nvim_get_current_buf()
-- print("BufLeave" .. cur_buf)
-- end
--
-- function M.on_buf_unload()
-- local cur_buf = vim.api.nvim_get_current_buf()
-- print("BufUnload" .. cur_buf)
-- end

function M.chase_buf_close(buf)

end

function M.buf_is_hidden(buf)
local win = vim.fn.bufwinid(buf)
if win > 0 then
return false
end
return true
end

function M.buf_show(buf)
local win = vim.fn.bufwinid(buf)
local cur_win = vim.api.nvim_get_current_win()
if win == -1 then
vim.cmd("botright vsplit")
win = vim.api.nvim_get_current_win()
end
-- set buf to current window
vim.api.nvim_win_set_buf(win, buf)
vim.api.nvim_set_current_win(cur_win)
end

function M.buf_hide(buf)
local win = vim.fn.bufwinid(buf)
if win > 0 then
local buftype = vim.api.nvim_buf_get_option(buf, "buftype")
vim.api.nvim_buf_set_option(buf, "buftype", "")
vim.api.nvim_win_hide(win)
if buftype then
vim.api.nvim_buf_set_option(buf, "buftype", buftype)
end
end
end

function M.chase_buf_destroy(chase_buf)
local buf = vim.api.nvim_buf_get_var(chase_buf, "original_buf")
local buf_refs = {}
for buf_ref, buf_chase in pairs(M.buf_refs) do
if buf_ref ~= buf then
buf_refs[buf_ref] = buf_chase
end
end
M.buf_refs = buf_refs
vim.cmd("bd " .. chase_buf)
end

function M.buf_clear(buf)
vim.api.nvim_buf_set_option(buf, "buftype", "")
Expand Down
46 changes: 37 additions & 9 deletions lua/chase/python.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ M.vim_did_enter = false

M.python_version = nil

-- Output buffers table
M.bufs_out = {}
-- print(M.bufs_out)
M.win_ref = nil

M.buf_refs = {}

function M.buf_is_main(buf_number)
local lines = vim.api.nvim_buf_get_lines(buf_number, 0, -1, false)
Expand Down Expand Up @@ -90,23 +90,24 @@ function M.preferred_python()
end

function M.run_file(file)
local buf = vim.api.nvim_get_current_buf()
if chase.is_windows() then
file = file:gsub("/", chase.sep)
end
local relative_file = file:gsub(
chase.project_root.filename .. chase.sep, ""
)
local buf = chase.buf_chase(relative_file)
local chase_buf = chase.buf_chase(relative_file, buf)
local testing = file:match("_test.py$")
if not testing then
testing = file:match("test_.*.py$")
end
chase.buf_clear(buf)
chase.buf_clear(chase_buf)
local action = "Running "
if testing then
action = "Testing "
end
chase.buf_append(buf, {
chase.buf_append(chase_buf, {
"Candango Chase",
action .. relative_file,
"Python: " .. M.preferred_python(),
Expand All @@ -118,6 +119,9 @@ function M.run_file(file)
local py_cmd = M.preferred_python()
local py_args = ""
if testing then
local row, _ = unpack(vim.api.nvim_win_get_cursor(0))
local lines = vim.api.nvim_buf_get_lines(0, 0, row, false)
M.where_am_i(lines, row)
py_args = "-m unittest -v"
end
local cmd_list = { py_cmd, py_args, file }
Expand All @@ -132,10 +136,10 @@ function M.run_file(file)
data[i] = v:gsub("\r", "")
end
end
chase.buf_append(buf, data)
chase.buf_append(chase_buf, data)
end,
on_stderr = function(_, data)
chase.buf_append(buf, data)
chase.buf_append(chase_buf, data)
end,
})
end
Expand All @@ -161,6 +165,30 @@ function M.setup()
pattern = "*.py",
group = chase.group,
})

vim.api.nvim_create_autocmd("BufEnter", {
callback = chase.on_buf_enter,
pattern = "*.py",
group = chase.group,
})

-- vim.api.nvim_create_autocmd("BufLeave", {
-- callback = chase.on_buf_leave,
-- pattern = "*.py",
-- group = chase.group,
-- })
--
-- vim.api.nvim_create_autocmd("BufUnload", {
-- callback = chase.on_buf_unload,
-- pattern = "*.py",
-- group = chase.group,
-- })

vim.api.nvim_create_autocmd("BufHidden", {
callback = chase.on_buf_hidden,
pattern = "*.py",
group = chase.group,
})
end

function M.set_python(venv_path)
Expand All @@ -172,7 +200,7 @@ function M.set_python(venv_path)
chase.add_to_path(venv_bin)
-- let $VIRTUAL_ENV=<project_virtualenv>
vim.cmd("let $VIRTUAL_ENV='" .. venv_path.filename .. "'")
if chase.is_windows then
if chase.is_windows() then
vim.cmd("let $PYTHONPATH='.;" .. chase.project_root.filename .. "'")
return
end
Expand Down

0 comments on commit 776ad84

Please sign in to comment.