feat(nvim): add flatten.nvim

This commit is contained in:
Price Hiller 2023-09-04 19:01:36 -05:00
parent d6af3786d6
commit d1154daa1c
Signed by: Price
SSH Key Fingerprint: SHA256:Y4S9ZzYphRn1W1kbJerJFO6GGsfu9O70VaBSxJO7dF8

View File

@ -0,0 +1,65 @@
return {
{
"willothy/flatten.nvim",
priority = 1000,
opts = function()
---@type Terminal?
local saved_terminal
return {
window = {
open = "alternate"
},
callbacks = {
should_block = function(argv)
-- Note that argv contains all the parts of the CLI command, including
-- Neovim's path, commands, options and files.
-- See: :help v:argv
-- In this case, we would block if we find the `-b` flag
-- This allows you to use `nvim -b file1` instead of `nvim --cmd 'let g:flatten_wait=1' file1`
return vim.tbl_contains(argv, "-b")
-- Alternatively, we can block if we find the diff-mode option
-- return vim.tbl_contains(argv, "-d")
end,
pre_open = function()
local term = require("toggleterm.terminal")
local termid = term.get_focused_id()
saved_terminal = term.get(termid)
end,
post_open = function(bufnr, winnr, ft, is_blocking)
if is_blocking and saved_terminal then
-- Hide the terminal while it's blocking
saved_terminal:close()
else
-- If it's a normal file, just switch to its window
vim.api.nvim_set_current_win(winnr)
end
-- If the file is a git commit, create one-shot autocmd to delete its buffer on write
-- If you just want the toggleable terminal integration, ignore this bit
if ft == "gitcommit" or ft == "gitrebase" then
vim.api.nvim_create_autocmd("BufWritePost", {
buffer = bufnr,
once = true,
callback = vim.schedule_wrap(function()
vim.api.nvim_buf_delete(bufnr, {})
end)
})
end
end,
block_end = function()
-- After blocking ends (for a git commit, etc), reopen the terminal
vim.schedule(function()
if saved_terminal then
saved_terminal:open()
saved_terminal = nil
end
end)
end
}
}
end
}
}