Merge pull request #22 from msc94/main

This commit is contained in:
Ali Almohaya 2023-08-29 02:51:33 +03:00 committed by GitHub
commit 27655e19d4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 50 additions and 13 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
.luarc.json

View file

@ -45,6 +45,13 @@ end)
- `:OpenInGHFileLines` - `:OpenInGHFileLines`
- Opens the current file page in GitHub. This command supports ranges. - 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 ## Usage
You can call the commands directly or define mappings them: You can call the commands directly or define mappings them:

View file

@ -34,7 +34,7 @@ local function get_current_branch_or_commit_with_priority(priority)
end end
end end
function M.open_file( function M.get_file_url(
priority, priority,
--[[optional]] --[[optional]]
range_start, range_start,
@ -67,12 +67,10 @@ function M.open_file(
file_page_url = file_page_url .. "#L" .. range_start .. "-L" .. range_end file_page_url = file_page_url .. "#L" .. range_start .. "-L" .. range_end
end end
if not utils.open_url(file_page_url) then return file_page_url
utils.notify("Could not open the built URL " .. file_page_url, vim.log.levels.ERROR)
end
end end
function M.open_repo(priority) function M.get_repo_url(priority)
-- make sure to update the current directory -- make sure to update the current directory
M.setup() M.setup()
if M.is_no_git_origin then if M.is_no_git_origin then
@ -80,8 +78,11 @@ function M.open_repo(priority)
return return
end end
local url = M.repo_url .. "/tree/" .. get_current_branch_or_commit_with_priority(priority) 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 if not utils.open_url(url) then
utils.notify("Could not open the built URL " .. url, vim.log.levels.ERROR) utils.notify("Could not open the built URL " .. url, vim.log.levels.ERROR)
end end

View file

@ -13,32 +13,60 @@ local function complete_func(arg_lead, _, _)
end end
vim.api.nvim_create_user_command("OpenInGHFile", function(opts) vim.api.nvim_create_user_command("OpenInGHFile", function(opts)
local url
if opts.range == 0 then -- Nothing was selected if opts.range == 0 then -- Nothing was selected
openingh.open_file(opts.args) url = openingh.get_file_url(opts.args)
else -- Current line or block was selected 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, opts.line2)
end
if opts.reg == "" then
openingh.open_url(url)
else
vim.fn.setreg(opts.reg, url)
print("URL put into register " .. opts.reg)
end end
end, { end, {
register = true,
range = true, range = true,
nargs = '?', nargs = '?',
complete = complete_func, complete = complete_func,
}) })
vim.api.nvim_create_user_command("OpenInGHFileLines", function(opts) vim.api.nvim_create_user_command("OpenInGHFileLines", function(opts)
local url
if opts.range == 0 then -- Nothing was selected if opts.range == 0 then -- Nothing was selected
openingh.open_file(opts.args, opts.line1) url = openingh.get_file_url(opts.args, opts.line1)
else -- Current line or block was selected 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, opts.line2)
end
if opts.reg == "" then
openingh.open_url(url)
else
vim.fn.setreg(opts.reg, url)
print("URL put into register " .. opts.reg)
end end
end, { end, {
register = true,
range = true, range = true,
nargs = '?', nargs = '?',
complete = complete_func, complete = complete_func,
}) })
vim.api.nvim_create_user_command("OpenInGHRepo", function(opts) vim.api.nvim_create_user_command("OpenInGHRepo", function(opts)
openingh.open_repo(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 put into register " .. opts.reg)
end
end, { end, {
register = true,
nargs = '?', nargs = '?',
complete = complete_func, complete = complete_func,
}) })