diff --git a/README.md b/README.md index ce7ac79..0a5dbcf 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,8 @@ A Neovim plugin for easily copying file paths and GitHub URLs to clipboard. - 📋 Copy current file relative path to clipboard - 📍 Copy current file path with line number -- 🔗 Copy current file path as GitHub URL with line number +- 🔗 Copy current file path as GitHub URL (with branch or commit hash) +- 📍 Copy current file path as GitHub URL with line number (with branch or commit hash) - ⚡ Simple and lightweight - 🎨 Customizable keymaps @@ -47,8 +48,11 @@ Default configuration: require("cfp").setup({ keymaps = { copy_path = "cp", - copy_path_line = "cl", - copy_path_url = "cu", + copy_path_line = "cP", + copy_branch_url = "cb", + copy_branch_url_line = "cB", + copy_hash_url = "ch", + copy_hash_url_line = "cH", }, }) ``` @@ -60,7 +64,10 @@ require("cfp").setup({ keymaps = { copy_path = "cp", copy_path_line = nil, -- disable this keymap - copy_path_url = "cu", + copy_branch_url = "cb", + copy_branch_url_line = nil, -- disable this keymap + copy_hash_url = "ch", + copy_hash_url_line = "cH", }, }) ``` @@ -73,21 +80,30 @@ require("cfp").setup({ |---------|-------------| | `:CopyPath` | Copy current file relative path to clipboard | | `:CopyPathLine` | Copy current file relative path with line number to clipboard | -| `:CopyPathURL` | Copy current file relative path with line number as GitHub URL to clipboard | +| `:CopyBranchURL` | Copy current file relative path as GitHub URL with branch to clipboard | +| `:CopyBranchURLLine` | Copy current file relative path with line number as GitHub URL with branch to clipboard | +| `:CopyHashURL` | Copy current file relative path as GitHub URL with commit hash to clipboard | +| `:CopyHashURLLine` | Copy current file relative path with line number as GitHub URL with commit hash to clipboard | ### Default Keymaps | Keymap | Command | Description | |--------|---------|-------------| | `cp` | `:CopyPath` | Copy file path | -| `cl` | `:CopyPathLine` | Copy file path with line number | -| `cu` | `:CopyPathURL` | Copy file path as GitHub URL | +| `cP` | `:CopyPathLine` | Copy file path with line number | +| `cb` | `:CopyBranchURL` | Copy file path as GitHub URL with branch | +| `cB` | `:CopyBranchURLLine` | Copy file path as GitHub URL with branch and line number | +| `ch` | `:CopyHashURL` | Copy file path as GitHub URL with commit hash | +| `cH` | `:CopyHashURLLine` | Copy file path as GitHub URL with commit hash and line number | ## Example Output - **CopyPath**: `lua/cfp/init.lua` - **CopyPathLine**: `lua/cfp/init.lua:42` -- **CopyPathURL**: `https://github.com/tapihdev/cfp.nvim/blob/main/lua/cfp/init.lua#L42` +- **CopyBranchURL**: `https://github.com/tapihdev/cfp.nvim/blob/main/lua/cfp/init.lua` +- **CopyBranchURLLine**: `https://github.com/tapihdev/cfp.nvim/blob/main/lua/cfp/init.lua#L42` +- **CopyHashURL**: `https://github.com/tapihdev/cfp.nvim/blob/5e62bbb/lua/cfp/init.lua` +- **CopyHashURLLine**: `https://github.com/tapihdev/cfp.nvim/blob/5e62bbb/lua/cfp/init.lua#L42` ## License diff --git a/lua/cfp/config.lua b/lua/cfp/config.lua index 477d8d1..77d1033 100644 --- a/lua/cfp/config.lua +++ b/lua/cfp/config.lua @@ -3,10 +3,11 @@ local M = {} local defaults = { keymaps = { copy_path = "cp", - copy_path_line = "cl", - copy_path_url = "cu", - copy_path_hash = "cw", - copy_path_with_hash = "cW", + copy_path_line = "cP", + copy_branch_url = "cb", + copy_branch_url_line = "cB", + copy_hash_url = "ch", + copy_hash_url_line = "cH", }, } @@ -20,4 +21,4 @@ function M.get() return config end -return M \ No newline at end of file +return M diff --git a/lua/cfp/init.lua b/lua/cfp/init.lua index b627528..a7ba8e1 100644 --- a/lua/cfp/init.lua +++ b/lua/cfp/init.lua @@ -16,7 +16,54 @@ local function copy_to_clipboard(text) vim.notify("Copied to clipboard: " .. text, vim.log.levels.INFO) end --- Copy relative path +-- Create GitHub URL for file +local function create_github_url(path, ref_type, include_line) + -- Get Git remote URL + local git_remote = vim.fn.system("git config --get remote.origin.url"):gsub("\n", "") + if vim.v.shell_error ~= 0 then + vim.notify("Not in a Git repository", vim.log.levels.WARN) + return nil + end + + -- Convert SSH/HTTPS Git URL to GitHub URL + local github_url = git_remote + if github_url:match("^git@github.com:") then + github_url = github_url:gsub("^git@github.com:", "https://github.com/") + github_url = github_url:gsub("%.git$", "") + elseif github_url:match("^https://github.com/") then + github_url = github_url:gsub("%.git$", "") + else + vim.notify("Remote origin is not a GitHub repository", vim.log.levels.WARN) + return nil + end + + -- Get reference (branch or commit hash) + local ref + if ref_type == "branch" then + ref = vim.fn.system("git rev-parse --abbrev-ref HEAD"):gsub("\n", "") + if vim.v.shell_error ~= 0 then + vim.notify("Failed to get current branch", vim.log.levels.WARN) + return nil + end + elseif ref_type == "hash" then + ref = vim.fn.system("git rev-parse HEAD"):gsub("\n", "") + if vim.v.shell_error ~= 0 then + vim.notify("Failed to get current commit hash", vim.log.levels.WARN) + return nil + end + end + + -- Build URL + local url = github_url .. "/blob/" .. ref .. "/" .. path + if include_line then + local line_num = vim.api.nvim_win_get_cursor(0)[1] + url = url .. "#L" .. line_num + end + + return url +end + +-- Copy current file relative path to clipboard function M.copy_path() local path = get_relative_path() if not path then @@ -27,7 +74,7 @@ function M.copy_path() copy_to_clipboard(path) end --- Copy relative path with line number +-- Copy current file relative path with line number to clipboard function M.copy_path_line() local path = get_relative_path() if not path then @@ -41,95 +88,60 @@ function M.copy_path_line() copy_to_clipboard(path_with_line) end --- Copy relative path with line number as GitHub URL -function M.copy_path_url() +-- Copy current file relative path as GitHub URL with branch name to clipboard +function M.copy_branch_url() local path = get_relative_path() if not path then vim.notify("No file path available", vim.log.levels.WARN) return end - -- Get Git remote URL - local git_remote = vim.fn.system("git config --get remote.origin.url"):gsub("\n", "") - if vim.v.shell_error ~= 0 then - vim.notify("Not in a Git repository", vim.log.levels.WARN) - return - end - - -- Convert SSH/HTTPS Git URL to GitHub URL - local github_url = git_remote - if github_url:match("^git@github.com:") then - github_url = github_url:gsub("^git@github.com:", "https://github.com/") - github_url = github_url:gsub("%.git$", "") - elseif github_url:match("^https://github.com/") then - github_url = github_url:gsub("%.git$", "") - else - vim.notify("Remote origin is not a GitHub repository", vim.log.levels.WARN) - return - end - - -- Get current branch - local branch = vim.fn.system("git rev-parse --abbrev-ref HEAD"):gsub("\n", "") - if vim.v.shell_error ~= 0 then - vim.notify("Failed to get current branch", vim.log.levels.WARN) - return + local url = create_github_url(path, "branch", false) + if url then + copy_to_clipboard(url) end - - local line_num = vim.api.nvim_win_get_cursor(0)[1] - local github_file_url = github_url .. "/blob/" .. branch .. "/" .. path .. "#L" .. line_num - - copy_to_clipboard(github_file_url) end --- Copy relative path with line number as GitHub URL with commit hash -function M.copy_path_hash() +-- Copy current file relative path with line number as GitHub URL with branch name to clipboard +function M.copy_branch_url_line() local path = get_relative_path() if not path then vim.notify("No file path available", vim.log.levels.WARN) return end - -- Get Git remote URL - local git_remote = vim.fn.system("git config --get remote.origin.url"):gsub("\n", "") - if vim.v.shell_error ~= 0 then - vim.notify("Not in a Git repository", vim.log.levels.WARN) - return + local url = create_github_url(path, "branch", true) + if url then + copy_to_clipboard(url) end +end - -- Convert SSH/HTTPS Git URL to GitHub URL - local github_url = git_remote - if github_url:match("^git@github.com:") then - github_url = github_url:gsub("^git@github.com:", "https://github.com/") - github_url = github_url:gsub("%.git$", "") - elseif github_url:match("^https://github.com/") then - github_url = github_url:gsub("%.git$", "") - else - vim.notify("Remote origin is not a GitHub repository", vim.log.levels.WARN) +-- Copy current file relative path as GitHub URL with commit hash to clipboard +function M.copy_hash_url() + local path = get_relative_path() + if not path then + vim.notify("No file path available", vim.log.levels.WARN) return end - -- Get current commit hash - local commit_hash = vim.fn.system("git rev-parse HEAD"):gsub("\n", "") - if vim.v.shell_error ~= 0 then - vim.notify("Failed to get current commit hash", vim.log.levels.WARN) - return + local url = create_github_url(path, "hash", false) + if url then + copy_to_clipboard(url) end - - local line_num = vim.api.nvim_win_get_cursor(0)[1] - local github_file_url = github_url .. "/blob/" .. commit_hash .. "/" .. path .. "#L" .. line_num - - copy_to_clipboard(github_file_url) end --- Copy relative path (for cW) -function M.copy_path_with_hash() +-- Copy current file relative path with line number as GitHub URL with commit hash to clipboard +function M.copy_hash_url_line() local path = get_relative_path() if not path then vim.notify("No file path available", vim.log.levels.WARN) return end - copy_to_clipboard(path) + local url = create_github_url(path, "hash", true) + if url then + copy_to_clipboard(url) + end end return M diff --git a/lua/cfp/keymaps.lua b/lua/cfp/keymaps.lua index 17f5fee..0272d9e 100644 --- a/lua/cfp/keymaps.lua +++ b/lua/cfp/keymaps.lua @@ -9,33 +9,39 @@ M.setup = function() if keymaps.copy_path then vim.keymap.set("n", keymaps.copy_path, cfp.copy_path, { - desc = "Copy file path" + desc = "Copy current file relative path to clipboard" }) end if keymaps.copy_path_line then vim.keymap.set("n", keymaps.copy_path_line, cfp.copy_path_line, { - desc = "Copy file path with line number" + desc = "Copy current file relative path with line number to clipboard" }) end - if keymaps.copy_path_url then - vim.keymap.set("n", keymaps.copy_path_url, cfp.copy_path_url, { - desc = "Copy file path as GitHub URL" + if keymaps.copy_branch_url then + vim.keymap.set("n", keymaps.copy_branch_url, cfp.copy_branch_url, { + desc = "Copy current file relative path as GitHub URL with branch name to clipboard" }) end - if keymaps.copy_path_hash then - vim.keymap.set("n", keymaps.copy_path_hash, cfp.copy_path_hash, { - desc = "Copy file path as GitHub URL with commit hash" + if keymaps.copy_branch_url_line then + vim.keymap.set("n", keymaps.copy_branch_url_line, cfp.copy_branch_url_line, { + desc = "Copy current file relative path with line number as GitHub URL with branch name to clipboard", }) end - if keymaps.copy_path_with_hash then - vim.keymap.set("n", keymaps.copy_path_with_hash, cfp.copy_path_with_hash, { - desc = "Copy file path" + if keymaps.copy_hash_url then + vim.keymap.set("n", keymaps.copy_hash_url, cfp.copy_hash_url, { + desc = "Copy current file relative path as GitHub URL with commit hash to clipboard" + }) + end + + if keymaps.copy_hash_url_line then + vim.keymap.set("n", keymaps.copy_hash_url_line, cfp.copy_hash_url_line, { + desc = "Copy current file relative path with line number as GitHub URL with commit hash to clipboard" }) end end -return M \ No newline at end of file +return M diff --git a/plugin/cfp.lua b/plugin/cfp.lua index 76608b4..aa75a24 100644 --- a/plugin/cfp.lua +++ b/plugin/cfp.lua @@ -7,7 +7,19 @@ vim.api.nvim_create_user_command("CopyPathLine", function() require("cfp").copy_ desc = "Copy current file relative path with line number to clipboard", }) -vim.api.nvim_create_user_command("CopyPathURL", function() require("cfp").copy_path_url() end, { - desc = "Copy current file relative path with line number as GitHub URL to clipboard", +vim.api.nvim_create_user_command("CopyBranchURL", function() require("cfp").copy_path_url() end, { + desc = "Copy current file relative path as GitHub URL with branch name to clipboard", +}) + +vim.api.nvim_create_user_command("CopyBranchURLLine", function() require("cfp").copy_path_url() end, { + desc = "Copy current file relative path with line number as GitHub URL with branch name to clipboard", +}) + +vim.api.nvim_create_user_command("CopyHashURL", function() require("cfp").copy_path_hash() end, { + desc = "Copy current file relative path as GitHub URL with commit hash to clipboard", +}) + +vim.api.nvim_create_user_command("CopyHashURLLine", function() require("cfp").copy_path_with_hash() end, { + desc = "Copy current file relative path with line number as GitHub URL with commit hash to clipboard", })