From faf75f3e0ac70e1b7a325fd684f69bb48ed06e7e Mon Sep 17 00:00:00 2001 From: PlaiyTiziano Date: Sat, 8 Apr 2023 20:25:43 +0200 Subject: [PATCH 1/2] Stop the appending `#L` when nothing is selected --- lua/openingh/init.lua | 17 ++++++++--------- plugin/openingh.lua | 8 ++++---- tests/openingh_spec.lua | 14 ++++++++++++-- 3 files changed, 24 insertions(+), 15 deletions(-) diff --git a/lua/openingh/init.lua b/lua/openingh/init.lua index 0ac5ac9..b266710 100644 --- a/lua/openingh/init.lua +++ b/lua/openingh/init.lua @@ -22,7 +22,12 @@ function M.setup() M.repo_url = string.format("http://%s/%s/%s", gh.host, gh.user_or_org, gh.reponame) 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 M.setup() if M.is_no_git_origin then @@ -38,18 +43,12 @@ function M.openFile(range_start, range_end) return 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 - lines = "#L" .. range_start .. "-" .. "L" .. range_end - else - lines = "#L" .. utils.get_line_number_from_buf() + file_page_url = file_page_url .. "#L" .. range_start .. "-L" .. range_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) if result == false then diff --git a/plugin/openingh.lua b/plugin/openingh.lua index 43a09bf..16223ab 100644 --- a/plugin/openingh.lua +++ b/plugin/openingh.lua @@ -6,10 +6,10 @@ vim.g.openingh = true local openingh = require("openingh") vim.api.nvim_create_user_command("OpenInGHFile", function(opts) - if opts.range == 0 or opts.range == 1 then - openingh.openFile() - else - openingh.openFile(opts.line1, opts.line2) + if opts.range == 0 then -- Nothing was selected + openingh.open_file() + else -- Current line or block was selected + openingh.open_file(opts.line1, opts.line2) end end, { range = true, diff --git a/tests/openingh_spec.lua b/tests/openingh_spec.lua index e40fd50..834bdf1 100644 --- a/tests/openingh_spec.lua +++ b/tests/openingh_spec.lua @@ -37,7 +37,17 @@ describe("openingh should open", function() vim.api.nvim_win_set_cursor(0, { 3, 0 }) vim.cmd("OpenInGHFile") 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)) end) @@ -57,7 +67,7 @@ describe("openingh should open", function() vim.api.nvim_win_set_cursor(0, { 2, 0 }) vim.cmd("normal ghf") local status = vim.g.OPENINGH_RESULT - local expected = "/README.md#L2" + local expected = "/README.md" assert.equal(expected, status:sub(-#expected)) end) end) From 01974bb315417b28e057c6b90d04e82495fda84b Mon Sep 17 00:00:00 2001 From: PlaiyTiziano Date: Sat, 8 Apr 2023 20:30:49 +0200 Subject: [PATCH 2/2] Get rid of camelCasing --- lua/openingh/init.lua | 2 +- lua/openingh/utils.lua | 6 +++--- plugin/openingh.lua | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/lua/openingh/init.lua b/lua/openingh/init.lua index b266710..d2f9650 100644 --- a/lua/openingh/init.lua +++ b/lua/openingh/init.lua @@ -56,7 +56,7 @@ function M.open_file( end end -function M.openRepo() +function M.open_repo() -- make sure to update the current directory M.setup() if M.is_no_git_origin then diff --git a/lua/openingh/utils.lua b/lua/openingh/utils.lua index bc06b66..daa8d00 100644 --- a/lua/openingh/utils.lua +++ b/lua/openingh/utils.lua @@ -38,7 +38,7 @@ function M.parse_gh_remote(url) end -- get the remote default branch -function M.get_defualt_branch() +function M.get_default_branch() -- will return origin/[branch_name] local branch_with_origin = vim.fn.system("git rev-parse --abbrev-ref origin/HEAD") local branch_name = M.split(branch_with_origin, "/")[2] @@ -72,8 +72,8 @@ end -- get the line number in the buffer function M.get_line_number_from_buf() - local lineNum = vim.api.nvim_win_get_cursor(0)[1] - return lineNum + local line_num = vim.api.nvim_win_get_cursor(0)[1] + return line_num end -- opens a url in the correct OS diff --git a/plugin/openingh.lua b/plugin/openingh.lua index 16223ab..51ea601 100644 --- a/plugin/openingh.lua +++ b/plugin/openingh.lua @@ -16,5 +16,5 @@ end, { }) vim.api.nvim_create_user_command("OpenInGHRepo", function() - openingh.openRepo() + openingh.open_repo() end, {})