From 0c3b9215886b6ef2d1ba7e21cd06061699ae8c4a Mon Sep 17 00:00:00 2001 From: Mist Date: Fri, 9 Feb 2024 20:06:08 +0800 Subject: [PATCH] feat: add features for support copy command --- lua/code-link/clipboard.lua | 14 +++++++++++++ lua/code-link/init.lua | 2 +- lua/code-link/link.lua | 40 +++++++++++++++++++++++-------------- plugin/code-link.lua | 11 +++++----- 4 files changed, 46 insertions(+), 21 deletions(-) create mode 100644 lua/code-link/clipboard.lua diff --git a/lua/code-link/clipboard.lua b/lua/code-link/clipboard.lua new file mode 100644 index 0000000..ea5dce3 --- /dev/null +++ b/lua/code-link/clipboard.lua @@ -0,0 +1,14 @@ +local static = require("code-link.static") +local ClipBoard = {} + +function ClipBoard.copy_or(content) + if static.config.copy_command == nil then + return + end + + local parsed_copy_command = static.config.copy_command(content) + + os.execute(parsed_copy_command) +end + +return ClipBoard diff --git a/lua/code-link/init.lua b/lua/code-link/init.lua index 6498eab..72c7fd9 100644 --- a/lua/code-link/init.lua +++ b/lua/code-link/init.lua @@ -7,7 +7,7 @@ function main.setup(config) end function main.link() - return CodeLink.new() + return CodeLink.new(static.config) end return main diff --git a/lua/code-link/link.lua b/lua/code-link/link.lua index ac248d2..7c65917 100644 --- a/lua/code-link/link.lua +++ b/lua/code-link/link.lua @@ -3,12 +3,14 @@ local command_util = require("code-link.utils.command") local string_util = require("code-link.utils.string") local CodeLink = {} -local function line(num) +local function github_line(num) return "L" .. num end -function CodeLink.new() - local instance = {} +function CodeLink.new(config) + local origin = CodeLink.get_git_origin(config) + local branch_name = string_util.trim(command_util.exec_command("git branch --show-current", "r")) + local instance = { branch_name = branch_name, origin = origin } setmetatable(instance, { __index = CodeLink, @@ -17,15 +19,14 @@ function CodeLink.new() return instance end -function CodeLink:get_git_origin(config) - if not config.origin == nil then - self.origin = config.origin - return +function CodeLink.get_git_origin(config) + if config.origin ~= nil then + return config.origin end local origin = string_util.trim(command_util.exec_command("git config --get remote.origin.url", "r")) - self.origin = string_util.ends_with(origin, "/") and origin:sub(1, -2) or origin + return (string_util.ends_with(origin, "/") and origin:sub(1, -2) or origin) .. "/blob/" end function CodeLink.get_current_file_path() @@ -35,19 +36,28 @@ function CodeLink.get_current_file_path() return full_file_path:gsub(string_util.escape(cwd), "") end -function CodeLink:create_line_param() - local selection = Selection.new() +function CodeLink.get_file_path_with_cwd() + local full_file_path = vim.fn.expand("%:p") + local cwd = vim.fn.getcwd() - return selection.line_start == selection.line_end and line(selection.line_start) - or line(selection.line_start) .. "-" .. line(selection.line_end) + return full_file_path:gsub(string_util.escape(cwd), "") end -function CodeLink:create_link() - self.branch_name = string_util.trim(command_util.exec_command("git branch --show-current", "r")) +function CodeLink.create_line_param(line_formatter, separator) + local selection = Selection.new() + + return selection.line_start == selection.line_end and line_formatter(selection.line_start) + or line_formatter(selection.line_start) .. separator .. line_formatter(selection.line_end) +end +function CodeLink:create_link(branch_name) local path = CodeLink.get_current_file_path() - return self.origin .. "/blob/" .. self.branch_name .. path .. "?plain=1#" .. CodeLink:create_line_param() + return self.origin + .. (branch_name ~= "" and branch_name or self.branch_name) + .. path + .. "?plain=1#" + .. CodeLink.create_line_param(github_line, "-") end return CodeLink diff --git a/plugin/code-link.lua b/plugin/code-link.lua index 5d7b1c2..6c1ad9d 100644 --- a/plugin/code-link.lua +++ b/plugin/code-link.lua @@ -1,11 +1,12 @@ local code_link = require("code-link") +local clipboard = require("code-link.clipboard") -vim.api.nvim_create_user_command("CodeLink", function() +vim.api.nvim_create_user_command("CodeLink", function(args) + local branch_name = args.args local link = code_link.link() + local generated_link = link:create_link(branch_name) - link:get_git_origin({}) - - local generated_link = link:create_link() + clipboard.copy_or(generated_link) vim.notify(generated_link) -end, {}) +end, { nargs = "*" })