Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
147 changes: 147 additions & 0 deletions lua/cfp/commands.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
local M = {}

-- Get the current buffer's file path relative to the current working directory
local function get_relative_path()
local buf_path = vim.api.nvim_buf_get_name(0)
if buf_path == "" then return nil end

local relative_path = vim.fn.fnamemodify(buf_path, ":.")

return relative_path
end

-- Copy text to clipboard
local function copy_to_clipboard(text)
vim.fn.setreg("+", text)
vim.notify("Copied to clipboard: " .. text, vim.log.levels.INFO)
end

-- 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
vim.notify("No file path available", vim.log.levels.WARN)
return
end

copy_to_clipboard(path)
end

-- Copy current file relative path with line number to clipboard
function M.copy_path_line()
local path = get_relative_path()
if not path then
vim.notify("No file path available", vim.log.levels.WARN)
return
end

local line_num = vim.api.nvim_win_get_cursor(0)[1]
local path_with_line = path .. ":" .. line_num

copy_to_clipboard(path_with_line)
end

-- 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

local url = create_github_url(path, "branch", false)
if url then
copy_to_clipboard(url)
end
end

-- 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

local url = create_github_url(path, "branch", true)
if url then
copy_to_clipboard(url)
end
end

-- 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

local url = create_github_url(path, "hash", false)
if url then
copy_to_clipboard(url)
end
end

-- 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

local url = create_github_url(path, "hash", true)
if url then
copy_to_clipboard(url)
end
end

return M
179 changes: 17 additions & 162 deletions lua/cfp/init.lua
Original file line number Diff line number Diff line change
@@ -1,168 +1,23 @@
local M = {}

local defaults = {
keymaps = {
copy_path = "<leader>cp",
copy_path_line = "<leader>cP",
copy_branch_url = "<leader>cb",
copy_branch_url_line = "<leader>cB",
copy_hash_url = "<leader>ch",
copy_hash_url_line = "<leader>cH",
},
}

local config = {}

function M.setup(opts)
config = vim.tbl_deep_extend("force", defaults, opts or {})
end

function M.get()
return config
end

-- Get the current buffer's file path relative to the current working directory
local function get_relative_path()
local buf_path = vim.api.nvim_buf_get_name(0)
if buf_path == "" then return nil end

local relative_path = vim.fn.fnamemodify(buf_path, ":.")

return relative_path
end

-- Copy text to clipboard
local function copy_to_clipboard(text)
vim.fn.setreg("+", text)
vim.notify("Copied to clipboard: " .. text, vim.log.levels.INFO)
end

-- 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
vim.notify("No file path available", vim.log.levels.WARN)
return
end

copy_to_clipboard(path)
end

-- Copy current file relative path with line number to clipboard
function M.copy_path_line()
local path = get_relative_path()
if not path then
vim.notify("No file path available", vim.log.levels.WARN)
return
end

local line_num = vim.api.nvim_win_get_cursor(0)[1]
local path_with_line = path .. ":" .. line_num

copy_to_clipboard(path_with_line)
end

-- 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

local url = create_github_url(path, "branch", false)
if url then
copy_to_clipboard(url)
end
end

-- 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

local url = create_github_url(path, "branch", true)
if url then
copy_to_clipboard(url)
end
end

-- 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

local url = create_github_url(path, "hash", false)
if url then
copy_to_clipboard(url)
end
end

-- 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

local url = create_github_url(path, "hash", true)
if url then
copy_to_clipboard(url)
end
local keymaps = require("cfp.keymaps")
local commands = require("cfp.commmands")

local defaults = {
keymaps = {
copy_path = "<leader>cp",
copy_path_line = "<leader>cP",
copy_branch_url = "<leader>cb",
copy_branch_url_line = "<leader>cB",
copy_hash_url = "<leader>ch",
copy_hash_url_line = "<leader>cH",
},
}

local config = vim.tbl_deep_extend("force", defaults, opts or {})

keymaps.set_keymaps(config.keymaps, commands)
end

return M
Expand Down
20 changes: 7 additions & 13 deletions lua/cfp/keymaps.lua
Original file line number Diff line number Diff line change
@@ -1,44 +1,38 @@
local M = {}

M.setup = function()
local config = require("cfp.config")
local cfp = require("cfp")

local opts = config.get()
local keymaps = opts.keymaps

function M.set_keymaps(keymaps, commands)
if keymaps.copy_path then
vim.keymap.set("n", keymaps.copy_path, cfp.copy_path, {
vim.keymap.set("n", keymaps.copy_path, commands.copy_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, {
vim.keymap.set("n", keymaps.copy_path_line, commands.copy_path_line, {
desc = "Copy current file relative path with line number to clipboard"
})
end

if keymaps.copy_branch_url then
vim.keymap.set("n", keymaps.copy_branch_url, cfp.copy_branch_url, {
vim.keymap.set("n", keymaps.copy_branch_url, commands.copy_branch_url, {
desc = "Copy current file relative path as GitHub URL with branch name to clipboard"
})
end

if keymaps.copy_branch_url_line then
vim.keymap.set("n", keymaps.copy_branch_url_line, cfp.copy_branch_url_line, {
vim.keymap.set("n", keymaps.copy_branch_url_line, commands.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_hash_url then
vim.keymap.set("n", keymaps.copy_hash_url, cfp.copy_hash_url, {
vim.keymap.set("n", keymaps.copy_hash_url, commands.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, {
vim.keymap.set("n", keymaps.copy_hash_url_line, commands.copy_hash_url_line, {
desc = "Copy current file relative path with line number as GitHub URL with commit hash to clipboard"
})
end
Expand Down