Merge pull request #13 from PlaiyTiziano/tp/do-not-set-lines-when-nothing-is-selected
This commit is contained in:
commit
8c28db76c9
4 changed files with 29 additions and 20 deletions
|
|
@ -22,7 +22,12 @@ function M.setup()
|
||||||
M.repo_url = string.format("http://%s/%s/%s", gh.host, gh.user_or_org, gh.reponame)
|
M.repo_url = string.format("http://%s/%s/%s", gh.host, gh.user_or_org, gh.reponame)
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.openFile(range_start, range_end)
|
function M.open_file(
|
||||||
|
--[[optional]]
|
||||||
|
range_start,
|
||||||
|
--[[optional]]
|
||||||
|
range_end
|
||||||
|
)
|
||||||
-- 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
|
||||||
|
|
@ -38,18 +43,12 @@ function M.openFile(range_start, range_end)
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
local lines
|
local file_page_url = M.repo_url .. "/blob/" .. utils.get_current_branch_or_commit() .. file_path
|
||||||
|
|
||||||
if range_start and range_end then
|
if range_start and range_end then
|
||||||
lines = "#L" .. range_start .. "-" .. "L" .. range_end
|
file_page_url = file_page_url .. "#L" .. range_start .. "-L" .. range_end
|
||||||
else
|
|
||||||
lines = "#L" .. utils.get_line_number_from_buf()
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local current_branch_name_or_commit_hash = utils.get_current_branch_or_commit()
|
|
||||||
|
|
||||||
local file_page_url = M.repo_url .. "/blob/" .. current_branch_name_or_commit_hash .. file_path .. lines
|
|
||||||
|
|
||||||
local result = utils.open_url(file_page_url)
|
local result = utils.open_url(file_page_url)
|
||||||
|
|
||||||
if result == false then
|
if result == false then
|
||||||
|
|
@ -57,7 +56,7 @@ function M.openFile(range_start, range_end)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.openRepo()
|
function M.open_repo()
|
||||||
-- 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
|
||||||
|
|
|
||||||
|
|
@ -38,7 +38,7 @@ function M.parse_gh_remote(url)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- get the remote default branch
|
-- get the remote default branch
|
||||||
function M.get_defualt_branch()
|
function M.get_default_branch()
|
||||||
-- will return origin/[branch_name]
|
-- will return origin/[branch_name]
|
||||||
local branch_with_origin = vim.fn.system("git rev-parse --abbrev-ref origin/HEAD")
|
local branch_with_origin = vim.fn.system("git rev-parse --abbrev-ref origin/HEAD")
|
||||||
local branch_name = M.split(branch_with_origin, "/")[2]
|
local branch_name = M.split(branch_with_origin, "/")[2]
|
||||||
|
|
@ -72,8 +72,8 @@ end
|
||||||
|
|
||||||
-- get the line number in the buffer
|
-- get the line number in the buffer
|
||||||
function M.get_line_number_from_buf()
|
function M.get_line_number_from_buf()
|
||||||
local lineNum = vim.api.nvim_win_get_cursor(0)[1]
|
local line_num = vim.api.nvim_win_get_cursor(0)[1]
|
||||||
return lineNum
|
return line_num
|
||||||
end
|
end
|
||||||
|
|
||||||
-- opens a url in the correct OS
|
-- opens a url in the correct OS
|
||||||
|
|
|
||||||
|
|
@ -6,15 +6,15 @@ vim.g.openingh = true
|
||||||
local openingh = require("openingh")
|
local openingh = require("openingh")
|
||||||
|
|
||||||
vim.api.nvim_create_user_command("OpenInGHFile", function(opts)
|
vim.api.nvim_create_user_command("OpenInGHFile", function(opts)
|
||||||
if opts.range == 0 or opts.range == 1 then
|
if opts.range == 0 then -- Nothing was selected
|
||||||
openingh.openFile()
|
openingh.open_file()
|
||||||
else
|
else -- Current line or block was selected
|
||||||
openingh.openFile(opts.line1, opts.line2)
|
openingh.open_file(opts.line1, opts.line2)
|
||||||
end
|
end
|
||||||
end, {
|
end, {
|
||||||
range = true,
|
range = true,
|
||||||
})
|
})
|
||||||
|
|
||||||
vim.api.nvim_create_user_command("OpenInGHRepo", function()
|
vim.api.nvim_create_user_command("OpenInGHRepo", function()
|
||||||
openingh.openRepo()
|
openingh.open_repo()
|
||||||
end, {})
|
end, {})
|
||||||
|
|
|
||||||
|
|
@ -37,7 +37,17 @@ describe("openingh should open", function()
|
||||||
vim.api.nvim_win_set_cursor(0, { 3, 0 })
|
vim.api.nvim_win_set_cursor(0, { 3, 0 })
|
||||||
vim.cmd("OpenInGHFile")
|
vim.cmd("OpenInGHFile")
|
||||||
local status = vim.g.OPENINGH_RESULT
|
local status = vim.g.OPENINGH_RESULT
|
||||||
local expected = "/README.md#L3"
|
local expected = "/README.md"
|
||||||
|
assert.equal(expected, status:sub(-#expected))
|
||||||
|
end)
|
||||||
|
|
||||||
|
it("file range on :'<,'>OpenInGHFile", function()
|
||||||
|
vim.cmd("e ./README.md")
|
||||||
|
vim.api.nvim_buf_set_mark(0, "<", 5, 0, {})
|
||||||
|
vim.api.nvim_buf_set_mark(0, ">", 5, 0, {})
|
||||||
|
vim.cmd("'<,'>OpenInGHFile")
|
||||||
|
local status = vim.g.OPENINGH_RESULT
|
||||||
|
local expected = "/README.md#L5-L5"
|
||||||
assert.equal(expected, status:sub(-#expected))
|
assert.equal(expected, status:sub(-#expected))
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
|
@ -57,7 +67,7 @@ describe("openingh should open", function()
|
||||||
vim.api.nvim_win_set_cursor(0, { 2, 0 })
|
vim.api.nvim_win_set_cursor(0, { 2, 0 })
|
||||||
vim.cmd("normal ghf")
|
vim.cmd("normal ghf")
|
||||||
local status = vim.g.OPENINGH_RESULT
|
local status = vim.g.OPENINGH_RESULT
|
||||||
local expected = "/README.md#L2"
|
local expected = "/README.md"
|
||||||
assert.equal(expected, status:sub(-#expected))
|
assert.equal(expected, status:sub(-#expected))
|
||||||
end)
|
end)
|
||||||
end)
|
end)
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue