From 3c238894c5b6f15c5f27cea8bd3785eb0f1ebf0f Mon Sep 17 00:00:00 2001 From: HampusM Date: Sun, 18 Sep 2022 11:39:25 +0200 Subject: add config --- lua/utility/autocmd.lua | 15 +++++++++++++++ lua/utility/git.lua | 29 +++++++++++++++++++++++++++++ lua/utility/misc.lua | 8 ++++++++ 3 files changed, 52 insertions(+) create mode 100644 lua/utility/autocmd.lua create mode 100644 lua/utility/git.lua create mode 100644 lua/utility/misc.lua (limited to 'lua/utility') diff --git a/lua/utility/autocmd.lua b/lua/utility/autocmd.lua new file mode 100644 index 0000000..948497d --- /dev/null +++ b/lua/utility/autocmd.lua @@ -0,0 +1,15 @@ +function autocmd(event, pattern, command) + vim.cmd("autocmd " .. event .. " " .. pattern .. " " .. command) +end + +function augroup(name, autocommands) + vim.cmd("augroup " .. name) + vim.cmd("autocmd!") + + for _, autocmd_args in ipairs(autocommands) do + autocmd(unpack(autocmd_args)) + end + + vim.cmd("augroup end") +end + diff --git a/lua/utility/git.lua b/lua/utility/git.lua new file mode 100644 index 0000000..b43ccc5 --- /dev/null +++ b/lua/utility/git.lua @@ -0,0 +1,29 @@ +require("utility.misc") + +function RepositoryRoot() + local current_file = GetCurrentFile() + + if current_file == "fzf" then + return "" + end + + local super_project_path = vim.fn.system("git -C " .. Dirname(current_file) + .. " rev-parse --show-superproject-working-tree") + + if vim.v.shell_error ~= 0 then + return "" + end + + if super_project_path then + return super_project_path + end + + local toplevel_path = vim.fn.system("git -C " .. Dirname(current_file) + .. " rev-parse --show-toplevel | tr -d '\n'") + + if vim.v.shell_error ~= 0 then + return "" + end + + return toplevel_path +end diff --git a/lua/utility/misc.lua b/lua/utility/misc.lua new file mode 100644 index 0000000..04ec1fc --- /dev/null +++ b/lua/utility/misc.lua @@ -0,0 +1,8 @@ +function GetCurrentFile() + return vim.fn.expand("%") +end + +function Dirname(file_path) + return vim.fn.fnamemodify(file_path, ":p:h") +end + -- cgit v1.2.3-18-g5258