Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(toggleterm)!: simplified keybinds with removed abstraction #3812

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions lua/lvim/config/_deprecated.lua
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,23 @@ function M.post_load()
convert_spec_to_lazy(plugin)
end
end

if lvim.builtin.terminal.execs then
deprecate(
"lvim.builtin.terminal.execs",
"Use `lvim.builtin.terminal.terminals` instead. See https://www.lunarvim.org/docs/configuration/keybindings#toggleterm-terminal-mappings"
)
for _, v in ipairs(lvim.builtin.terminal.execs) do
local keybind = {
cmd = v[1],
keymap = v[2],
desc = v[3],
direction = v[4],
size = v[5],
}
lvim.builtin.terminal.terminals[#lvim.builtin.terminal.terminals + 1] = keybind
end
end
end

return M
117 changes: 55 additions & 62 deletions lua/lvim/core/terminal.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ local M = {}
local Log = require "lvim.core.log"

M.config = function()
lvim.builtin["terminal"] = {
lvim.builtin.terminal = {
active = true,
on_config_done = nil,
-- size can be a number or function which is passed the current terminal
Expand Down Expand Up @@ -35,15 +35,16 @@ M.config = function()
background = "Normal",
},
},
-- Add executables on the config.lua
-- { cmd, keymap, description, direction, size }
-- lvim.builtin.terminal.execs = {...} to overwrite
-- lvim.builtin.terminal.execs[#lvim.builtin.terminal.execs+1] = {"gdb", "tg", "GNU Debugger"}
-- TODO: pls add mappings in which key and refactor this
execs = {
{ nil, "<M-1>", "Horizontal Terminal", "horizontal", 0.3 },
{ nil, "<M-2>", "Vertical Terminal", "vertical", 0.4 },
{ nil, "<M-3>", "Float Terminal", "float", nil },
terminals_defaults = {
direction = "horizontal",
horizontal_size = 0.3,
vertical_size = 0.4,
},
terminals = {
{ keymap = "<M-1>", desc = "Horizontal Terminal", direction = "horizontal" },
{ keymap = "<M-2>", desc = "Vertical Terminal", direction = "vertical" },
{ keymap = "<M-3>", desc = "Float Terminal", direction = "float" },
{ keymap = "<leader>gg", desc = "LazyGit", cmd = "lazygit", size = 1 },
},
}
end
Expand Down Expand Up @@ -77,23 +78,53 @@ local function get_dynamic_terminal_size(direction, size)
end
end

local function term_toggle(term_opts)
local Terminal = require("toggleterm.terminal").Terminal
local term = Terminal:new(term_opts)
term:toggle(term_opts.size, term_opts.direction)
end

local function add_term_keymap(term_opts)
local binary = term_opts.cmd:match "(%S+)"
if vim.fn.executable(binary) ~= 1 then
Log:debug("Skipping configuring executable " .. binary .. ". Please make sure it is installed properly.")
return
end

local modes = { "n" }
if not term_opts.keymap:find "<leader>" and not term_opts.keymap:find "<space>" then
table.insert(modes, "t")
end
vim.keymap.set(modes, term_opts.keymap, function()
term_toggle(term_opts)
end, { desc = term_opts.desc, silent = true })
end

--- Setup the terminal cmds
M.init = function()
for i, exec in pairs(lvim.builtin.terminal.execs) do
local direction = exec[4] or lvim.builtin.terminal.direction
for i, term_opts in ipairs(lvim.builtin.terminal.terminals) do
-- size == 1 is a special case for full screen
if term_opts.size == 1 then
term_opts.direction = "float"
term_opts.float_opts = {
opalmay marked this conversation as resolved.
Show resolved Hide resolved
border = "none",
width = 100000,
height = 100000,
}
end

local opts = {
cmd = exec[1] or lvim.builtin.terminal.shell or vim.o.shell,
keymap = exec[2],
label = exec[3],
-- NOTE: unable to consistently bind id/count <= 9, see #2146
count = i + 100,
direction = direction,
size = function()
return get_dynamic_terminal_size(direction, exec[5])
end,
}
term_opts.direction = term_opts.direction or lvim.builtin.terminal.terminals_defaults.direction
term_opts.size = term_opts.size or lvim.builtin.terminal.terminals_defaults[term_opts.direction .. "_size"]
-- size is calculated dynamically as a percentage of the current buffer
term_opts.size = get_dynamic_terminal_size(term_opts.direction, term_opts.size)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this needs to be a callback

Suggested change
term_opts.size = get_dynamic_terminal_size(term_opts.direction, term_opts.size)
term_opts.size = function()
get_dynamic_terminal_size(term_opts.direction, term_opts.size)
end

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure? It doesn't work and it works without a callback

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

basically, it won't be dynamically calculated if it's not a function.

term_opts.cmd = term_opts.cmd or lvim.builtin.terminal.shell or vim.o.shell
-- desc is used for the keymap description
term_opts.desc = term_opts.desc or term_opts.cmd

M.add_exec(opts)
term_opts.count = i + 100

-- the table is passed to toggleterm:new directly
add_term_keymap(term_opts)
end
end

Expand All @@ -105,24 +136,6 @@ M.setup = function()
end
end

M.add_exec = function(opts)
local binary = opts.cmd:match "(%S+)"
if vim.fn.executable(binary) ~= 1 then
Log:debug("Skipping configuring executable " .. binary .. ". Please make sure it is installed properly.")
return
end

vim.keymap.set({ "n", "t" }, opts.keymap, function()
M._exec_toggle { cmd = opts.cmd, count = opts.count, direction = opts.direction, size = opts.size() }
end, { desc = opts.label, noremap = true, silent = true })
end

M._exec_toggle = function(opts)
local Terminal = require("toggleterm.terminal").Terminal
local term = Terminal:new { cmd = opts.cmd, count = opts.count, direction = opts.direction }
term:toggle(opts.size, opts.direction)
end

---Toggles a log viewer according to log.viewer.layout_config
---@param logfile string the fullpath to the logfile
M.toggle_log_view = function(logfile)
Expand All @@ -146,24 +159,4 @@ M.toggle_log_view = function(logfile)
log_view:toggle()
end

M.lazygit_toggle = function()
local Terminal = require("toggleterm.terminal").Terminal
local lazygit = Terminal:new {
cmd = "lazygit",
hidden = true,
direction = "float",
float_opts = {
border = "none",
width = 100000,
height = 100000,
},
on_open = function(_)
vim.cmd "startinsert!"
opalmay marked this conversation as resolved.
Show resolved Hide resolved
end,
on_close = function(_) end,
count = 99,
}
lazygit:toggle()
end

return M
1 change: 0 additions & 1 deletion lua/lvim/core/which-key.lua
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,6 @@ M.config = function()
-- " Debugging
g = {
name = "Git",
g = { "<cmd>lua require 'lvim.core.terminal'.lazygit_toggle()<cr>", "Lazygit" },
j = { "<cmd>lua require 'gitsigns'.next_hunk({navigation_message = false})<cr>", "Next Hunk" },
k = { "<cmd>lua require 'gitsigns'.prev_hunk({navigation_message = false})<cr>", "Prev Hunk" },
opalmay marked this conversation as resolved.
Show resolved Hide resolved
l = { "<cmd>lua require 'gitsigns'.blame_line()<cr>", "Blame" },
Expand Down