From 7d2d4c5b99072e6f021e6663991f3a7d7cc57ef0 Mon Sep 17 00:00:00 2001 From: vincent Date: Mon, 9 Oct 2023 20:32:15 +0200 Subject: [PATCH] nvim terminal integration --- nvim/.config/nvim/lua/keymapping.lua | 1 + nvim/.config/nvim/lua/util/init.lua | 92 ++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+) create mode 100644 nvim/.config/nvim/lua/util/init.lua diff --git a/nvim/.config/nvim/lua/keymapping.lua b/nvim/.config/nvim/lua/keymapping.lua index 1a23c9c..4aeb059 100644 --- a/nvim/.config/nvim/lua/keymapping.lua +++ b/nvim/.config/nvim/lua/keymapping.lua @@ -3,6 +3,7 @@ ------------ local keymap = vim.keymap.set local opts = { silent = true } +local Util = require("util") diff --git a/nvim/.config/nvim/lua/util/init.lua b/nvim/.config/nvim/lua/util/init.lua new file mode 100644 index 0000000..ceac787 --- /dev/null +++ b/nvim/.config/nvim/lua/util/init.lua @@ -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 +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", "", "", { buffer = buf, nowait = true }) + end + if opts.ctrl_hjkl == false then + vim.keymap.set("t", "", "", { buffer = buf, nowait = true }) + vim.keymap.set("t", "", "", { buffer = buf, nowait = true }) + vim.keymap.set("t", "", "", { buffer = buf, nowait = true }) + vim.keymap.set("t", "", "", { 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