nvim terminal integration

This commit is contained in:
vincent 2023-10-09 20:32:15 +02:00
parent 77e2023fc5
commit 7d2d4c5b99
2 changed files with 93 additions and 0 deletions

View File

@ -3,6 +3,7 @@
------------ ------------
local keymap = vim.keymap.set local keymap = vim.keymap.set
local opts = { silent = true } local opts = { silent = true }
local Util = require("util")

View File

@ -0,0 +1,92 @@
local M = {}
M.root_patterns = { ".git", "lua" }
function M.get_clients(...)
local fn = vim.lsp.get_clients or vim.lsp.get_active_clients
return fn(...)
end
-- returns the root directory based on:
-- * lsp workspace folders
-- * lsp root_dir
-- * root pattern of filename of the current buffer
-- * root pattern of cwd
---@return string
function M.get_root()
---@type string?
local path = vim.api.nvim_buf_get_name(0)
path = path ~= "" and vim.loop.fs_realpath(path) or nil
---@type string[]
local roots = {}
if path then
for _, client in pairs(M.get_clients({ bufnr = 0 })) do
local workspace = client.config.workspace_folders
local paths = workspace and vim.tbl_map(function(ws)
return vim.uri_to_fname(ws.uri)
end, workspace) or client.config.root_dir and { client.config.root_dir } or {}
for _, p in ipairs(paths) do
local r = vim.loop.fs_realpath(p)
if path:find(r, 1, true) then
roots[#roots + 1] = r
end
end
end
end
table.sort(roots, function(a, b)
return #a > #b
end)
---@type string?
local root = roots[1]
if not root then
path = path and vim.fs.dirname(path) or vim.loop.cwd()
---@type string?
root = vim.fs.find(M.root_patterns, { path = path, upward = true })[1]
root = root and vim.fs.dirname(root) or vim.loop.cwd()
end
---@cast root string
return root
end
---@type table<string,LazyFloat>
local terminals = {}
-- Opens a floating terminal (interactive by default)
---@param cmd? string[]|string
---@param opts? LazyCmdOptions|{interactive?:boolean, esc_esc?:false, ctrl_hjkl?:false}
function M.float_term(cmd, opts)
opts = vim.tbl_deep_extend("force", {
ft = "lazyterm",
size = { width = 0.9, height = 0.9 },
}, opts or {}, { persistent = true })
---@cast opts LazyCmdOptions|{interactive?:boolean, esc_esc?:false}
local termkey = vim.inspect({ cmd = cmd or "shell", cwd = opts.cwd, env = opts.env, count = vim.v.count1 })
if terminals[termkey] and terminals[termkey]:buf_valid() then
terminals[termkey]:toggle()
else
terminals[termkey] = require("lazy.util").float_term(cmd, opts)
local buf = terminals[termkey].buf
vim.b[buf].lazyterm_cmd = cmd
if opts.esc_esc == false then
vim.keymap.set("t", "<esc>", "<esc>", { buffer = buf, nowait = true })
end
if opts.ctrl_hjkl == false then
vim.keymap.set("t", "<c-h>", "<c-h>", { buffer = buf, nowait = true })
vim.keymap.set("t", "<c-j>", "<c-j>", { buffer = buf, nowait = true })
vim.keymap.set("t", "<c-k>", "<c-k>", { buffer = buf, nowait = true })
vim.keymap.set("t", "<c-l>", "<c-l>", { buffer = buf, nowait = true })
end
vim.api.nvim_create_autocmd("BufEnter", {
buffer = buf,
callback = function()
vim.cmd.startinsert()
end,
})
end
return terminals[termkey]
end
return M