From aeff967d393b633287b3e512bddceaf52a1c2d76 Mon Sep 17 00:00:00 2001 From: msc94 Date: Sat, 26 Aug 2023 21:30:28 +0200 Subject: [PATCH 1/4] Add .gitignore file --- .gitignore | 1 + 1 file changed, 1 insertion(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9bbbeea --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.luarc.json From 618c0a873a1cde7150617eda4287aadecaf19582 Mon Sep 17 00:00:00 2001 From: msc94 Date: Sat, 26 Aug 2023 21:51:16 +0200 Subject: [PATCH 2/4] Make url creation and opening two steps --- lua/openingh/init.lua | 13 +++++++------ plugin/openingh.lua | 22 +++++++++++++++------- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/lua/openingh/init.lua b/lua/openingh/init.lua index 4da7982..88e34f6 100644 --- a/lua/openingh/init.lua +++ b/lua/openingh/init.lua @@ -34,7 +34,7 @@ local function get_current_branch_or_commit_with_priority(priority) end end -function M.open_file( +function M.get_file_url( priority, --[[optional]] range_start, @@ -67,12 +67,10 @@ function M.open_file( file_page_url = file_page_url .. "#L" .. range_start .. "-L" .. range_end end - if not utils.open_url(file_page_url) then - utils.notify("Could not open the built URL " .. file_page_url, vim.log.levels.ERROR) - end + return file_page_url end -function M.open_repo(priority) +function M.get_repo_url(priority) -- make sure to update the current directory M.setup() if M.is_no_git_origin then @@ -80,8 +78,11 @@ function M.open_repo(priority) return end - local url = M.repo_url .. "/tree/" .. get_current_branch_or_commit_with_priority(priority) + return url +end + +function M.open_url(url) if not utils.open_url(url) then utils.notify("Could not open the built URL " .. url, vim.log.levels.ERROR) end diff --git a/plugin/openingh.lua b/plugin/openingh.lua index 531db6d..a6dfa18 100644 --- a/plugin/openingh.lua +++ b/plugin/openingh.lua @@ -13,11 +13,15 @@ local function complete_func(arg_lead, _, _) end vim.api.nvim_create_user_command("OpenInGHFile", function(opts) + local url + if opts.range == 0 then -- Nothing was selected - openingh.open_file(opts.args) - else -- Current line or block was selected - openingh.open_file(opts.args, opts.line1, opts.line2) + url = openingh.get_file_url(opts.args) + else -- Current line or block was selected + url = openingh.get_file_url(opts.args, opts.line1, opts.line2) end + + openingh.open_url(url) end, { range = true, nargs = '?', @@ -25,11 +29,15 @@ end, { }) vim.api.nvim_create_user_command("OpenInGHFileLines", function(opts) + local url + if opts.range == 0 then -- Nothing was selected - openingh.open_file(opts.args, opts.line1) - else -- Current line or block was selected - openingh.open_file(opts.args, opts.line1, opts.line2) + url = openingh.get_file_url(opts.args, opts.line1) + else -- Current line or block was selected + url = openingh.get_file_url(opts.args, opts.line1, opts.line2) end + + openingh.open_url(url) end, { range = true, nargs = '?', @@ -37,7 +45,7 @@ end, { }) vim.api.nvim_create_user_command("OpenInGHRepo", function(opts) - openingh.open_repo(opts.args) + openingh.open_url(openingh.get_repo_url(opts.args)) end, { nargs = '?', complete = complete_func, From 8cd1c9d5aa9eb43a82655d9ea7967fa6a88f9d02 Mon Sep 17 00:00:00 2001 From: msc94 Date: Sat, 26 Aug 2023 23:00:22 +0200 Subject: [PATCH 3/4] Add ability to yank url into register --- plugin/openingh.lua | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/plugin/openingh.lua b/plugin/openingh.lua index a6dfa18..4178875 100644 --- a/plugin/openingh.lua +++ b/plugin/openingh.lua @@ -21,8 +21,14 @@ vim.api.nvim_create_user_command("OpenInGHFile", function(opts) url = openingh.get_file_url(opts.args, opts.line1, opts.line2) end - openingh.open_url(url) + if opts.reg == "" then + openingh.open_url(url) + else + vim.fn.setreg(opts.reg, url) + print("URL yanked") + end end, { + register = true, range = true, nargs = '?', complete = complete_func, @@ -37,16 +43,30 @@ vim.api.nvim_create_user_command("OpenInGHFileLines", function(opts) url = openingh.get_file_url(opts.args, opts.line1, opts.line2) end - openingh.open_url(url) + if opts.reg == "" then + openingh.open_url(url) + else + vim.fn.setreg(opts.reg, url) + print("URL yanked") + end end, { + register = true, range = true, nargs = '?', complete = complete_func, }) vim.api.nvim_create_user_command("OpenInGHRepo", function(opts) - openingh.open_url(openingh.get_repo_url(opts.args)) + local url = openingh.get_repo_url(opts.args) + + if opts.reg == "" then + openingh.open_url(url) + else + vim.fn.setreg(opts.reg, url) + print("URL yanked") + end end, { + register = true, nargs = '?', complete = complete_func, }) From 649195b374be052caf7e2da73ea90e0536f6433b Mon Sep 17 00:00:00 2001 From: msc94 Date: Sun, 27 Aug 2023 11:18:00 +0200 Subject: [PATCH 4/4] Add registers to README --- README.md | 7 +++++++ plugin/openingh.lua | 6 +++--- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 9484289..4b7b43a 100644 --- a/README.md +++ b/README.md @@ -45,6 +45,13 @@ end) - `:OpenInGHFileLines` - Opens the current file page in GitHub. This command supports ranges. + +## Registers + +All of the commands above optionally take a register, e.g. `:OpenInGHFileLines+`. +In this case, the URL will not be opened in the browser, but put into the register given. +This is especially useful if you're running neovim on a remote machine, but want to open the URL locally. + ## Usage You can call the commands directly or define mappings them: diff --git a/plugin/openingh.lua b/plugin/openingh.lua index 4178875..ffba143 100644 --- a/plugin/openingh.lua +++ b/plugin/openingh.lua @@ -25,7 +25,7 @@ vim.api.nvim_create_user_command("OpenInGHFile", function(opts) openingh.open_url(url) else vim.fn.setreg(opts.reg, url) - print("URL yanked") + print("URL put into register " .. opts.reg) end end, { register = true, @@ -47,7 +47,7 @@ vim.api.nvim_create_user_command("OpenInGHFileLines", function(opts) openingh.open_url(url) else vim.fn.setreg(opts.reg, url) - print("URL yanked") + print("URL put into register " .. opts.reg) end end, { register = true, @@ -63,7 +63,7 @@ vim.api.nvim_create_user_command("OpenInGHRepo", function(opts) openingh.open_url(url) else vim.fn.setreg(opts.reg, url) - print("URL yanked") + print("URL put into register " .. opts.reg) end end, { register = true,