From ed6d81ef53c95d7cd27909c29ac3ca15047e2c96 Mon Sep 17 00:00:00 2001 From: Daniil Tsivinsky Date: Thu, 31 Mar 2022 23:36:27 +0300 Subject: [PATCH] organize git functions in git.lua --- neovim/.config/nvim/init.lua | 2 +- neovim/.config/nvim/lua/user/git.lua | 104 +++++++++++----------- neovim/.config/nvim/lua/user/gitsigns.lua | 53 +++++++++++ neovim/.config/nvim/lua/user/remaps.lua | 8 +- neovim/.config/nvim/lua/user/utils.lua | 51 +---------- 5 files changed, 113 insertions(+), 105 deletions(-) create mode 100644 neovim/.config/nvim/lua/user/gitsigns.lua diff --git a/neovim/.config/nvim/init.lua b/neovim/.config/nvim/init.lua index ed9490f..d04d19d 100644 --- a/neovim/.config/nvim/init.lua +++ b/neovim/.config/nvim/init.lua @@ -151,7 +151,7 @@ require("nvim-autopairs").setup({}) require("user.file-tree") require("user.treesitter") require("user.telescope") -require("user.git") +require("user.gitsigns") require("user.comp") require("user.lsp") require("user.comment") diff --git a/neovim/.config/nvim/lua/user/git.lua b/neovim/.config/nvim/lua/user/git.lua index 0a3cc3f..8d1ecbb 100644 --- a/neovim/.config/nvim/lua/user/git.lua +++ b/neovim/.config/nvim/lua/user/git.lua @@ -1,53 +1,55 @@ -local a = require("gitsigns.actions") -local reload_nvimtree = require("nvim-tree.actions.reloaders").reload_explorer +local u = require("user.utils") -require("gitsigns").setup({ - signcolumn = true, - attach_to_untracked = false, - current_line_blame = true, - current_line_blame_opts = { - delay = 1000, - }, - current_line_blame_formatter_opts = { - relative_time = true, - }, - on_attach = function(bufnr) - vim.keymap.set("n", "gs", function() - a.stage_hunk() - reload_nvimtree() - end) - vim.keymap.set("n", "gu", function() - a.undo_stage_hunk() - reload_nvimtree() - end) - vim.keymap.set("n", "gr", function() - a.reset_hunk() - reload_nvimtree() - end) - vim.keymap.set("n", "gp", function() - a.preview_hunk() - reload_nvimtree() - end) - vim.keymap.set("n", "gn", function() - a.next_hunk() - reload_nvimtree() - end) - vim.keymap.set("n", "gN", function() - a.prev_hunk() - reload_nvimtree() - end) +local git = {} - vim.keymap.set("v", "gs", function() - a.stage_hunk() - reload_nvimtree() - end) - vim.keymap.set("v", "gu", function() - a.undo_stage_hunk() - reload_nvimtree() - end) - vim.keymap.set("v", "gr", function() - a.reset_hunk() - reload_nvimtree() - end) - end, -}) +--- @return string commit_hash +function git.get_commit_hash_for_current_line() + local fullpath = vim.api.nvim_buf_get_name(0) + local cwd = vim.loop.cwd() + local relative_path = string.gsub(fullpath, cwd .. "/", "") + + local line = unpack(vim.api.nvim_win_get_cursor(0)) + + local f = io.popen("git blame -L " .. line .. ",+1 " .. relative_path, "r") + local data = f:read("*a") + f:close() + + local commit_hash = vim.split(data, " ")[1] + + return commit_hash +end + +--- @return string git_remote_url +function git.get_remote_url() + local f = io.popen("git remote get-url origin", "r") + local remote_url = f:read("*l") + f:close() + + if string.sub(remote_url, 0, 4) == "git@" then + remote_url = string.gsub(remote_url, "git@", "") + remote_url = string.gsub(remote_url, ".git", "") + remote_url = string.gsub(remote_url, ":", "/") + end + + if string.sub(remote_url, 0, 5) ~= "https" then + remote_url = "https://" .. remote_url + end + + return remote_url +end + +function git.open_commit_on_github() + local commit_hash = git.get_commit_hash_for_current_line() + local remote_url = git.get_remote_url() + + if commit_hash == "00000000" then + print("Not committed yet") + return + end + + local commit_url = remote_url .. "/commit/" .. commit_hash + + u.open_url_in_browser(commit_url) +end + +return git diff --git a/neovim/.config/nvim/lua/user/gitsigns.lua b/neovim/.config/nvim/lua/user/gitsigns.lua new file mode 100644 index 0000000..0a3cc3f --- /dev/null +++ b/neovim/.config/nvim/lua/user/gitsigns.lua @@ -0,0 +1,53 @@ +local a = require("gitsigns.actions") +local reload_nvimtree = require("nvim-tree.actions.reloaders").reload_explorer + +require("gitsigns").setup({ + signcolumn = true, + attach_to_untracked = false, + current_line_blame = true, + current_line_blame_opts = { + delay = 1000, + }, + current_line_blame_formatter_opts = { + relative_time = true, + }, + on_attach = function(bufnr) + vim.keymap.set("n", "gs", function() + a.stage_hunk() + reload_nvimtree() + end) + vim.keymap.set("n", "gu", function() + a.undo_stage_hunk() + reload_nvimtree() + end) + vim.keymap.set("n", "gr", function() + a.reset_hunk() + reload_nvimtree() + end) + vim.keymap.set("n", "gp", function() + a.preview_hunk() + reload_nvimtree() + end) + vim.keymap.set("n", "gn", function() + a.next_hunk() + reload_nvimtree() + end) + vim.keymap.set("n", "gN", function() + a.prev_hunk() + reload_nvimtree() + end) + + vim.keymap.set("v", "gs", function() + a.stage_hunk() + reload_nvimtree() + end) + vim.keymap.set("v", "gu", function() + a.undo_stage_hunk() + reload_nvimtree() + end) + vim.keymap.set("v", "gr", function() + a.reset_hunk() + reload_nvimtree() + end) + end, +}) diff --git a/neovim/.config/nvim/lua/user/remaps.lua b/neovim/.config/nvim/lua/user/remaps.lua index fe8e6f3..0028fdb 100644 --- a/neovim/.config/nvim/lua/user/remaps.lua +++ b/neovim/.config/nvim/lua/user/remaps.lua @@ -1,4 +1,6 @@ local ts = require("telescope.builtin") +local u = require("user.utils") +local git = require("user.git") -- General keymaps vim.keymap.set("n", "", ":nohl") @@ -92,7 +94,7 @@ vim.keymap.set("n", "dd", function() vim.diagnostic.open_float(nil, { focus = false, scope = "line" }) end) vim.keymap.set("n", "dy", function() - require("user.utils").copy_diagnostic_message() + u.copy_diagnostic_message() end) vim.keymap.set("n", "dn", function() vim.diagnostic.goto_next({ float = false }) @@ -105,10 +107,10 @@ vim.keymap.set("n", "do", ":lopen") -- Git vim.keymap.set("n", "gg", ":G") vim.keymap.set("n", "go", function() - require("user.utils").open_commit_on_github() + git.open_commit_on_github() end) -- Refactoring vim.keymap.set("n", "ri", function() - require("user.utils").lsp_organize_imports() + u.lsp_organize_imports() end) diff --git a/neovim/.config/nvim/lua/user/utils.lua b/neovim/.config/nvim/lua/user/utils.lua index 987e83a..b9de4bb 100644 --- a/neovim/.config/nvim/lua/user/utils.lua +++ b/neovim/.config/nvim/lua/user/utils.lua @@ -1,5 +1,3 @@ -local Job = require("plenary.job") - local M = {} M.yank = function(message) @@ -53,41 +51,8 @@ M.lsp_organize_imports = function(bufnr, timeout) vim.lsp.buf_request_sync(bufnr, "workspace/executeCommand", params, timeout or 500) end -M.get_commit_hash_for_current_line = function() - local fullpath = vim.api.nvim_buf_get_name(0) - local cwd = vim.loop.cwd() - local relative_path = string.gsub(fullpath, cwd .. "/", "") - - local line = unpack(vim.api.nvim_win_get_cursor(0)) - - local f = io.popen("git blame -L " .. line .. ",+1 " .. relative_path, "r") - local data = f:read("*a") - f:close() - - local commit_hash = vim.split(data, " ")[1] - - return commit_hash -end - -M.get_git_remote_url = function() - local f = io.popen("git remote get-url origin", "r") - local remote_url = f:read("*l") - f:close() - - if string.sub(remote_url, 0, 4) == "git@" then - remote_url = string.gsub(remote_url, "git@", "") - remote_url = string.gsub(remote_url, ".git", "") - remote_url = string.gsub(remote_url, ":", "/") - end - - if string.sub(remote_url, 0, 5) ~= "https" then - remote_url = "https://" .. remote_url - end - - return remote_url -end - M.open_url_in_browser = function(url) + -- TODO: implement windows support if vim.fn.has("win32") == 1 then print("windows not supported, sorry") return @@ -97,18 +62,4 @@ M.open_url_in_browser = function(url) f:close() end -M.open_commit_on_github = function() - local commit_hash = M.get_commit_hash_for_current_line() - local remote_url = M.get_git_remote_url() - - if commit_hash == "00000000" then - print("Not committed yet") - return - end - - local commit_url = remote_url .. "/commit/" .. commit_hash - - M.open_url_in_browser(commit_url) -end - return M