diff --git a/README.md b/README.md index 3a68e32..da25af8 100644 --- a/README.md +++ b/README.md @@ -40,7 +40,7 @@ end) - Opens the project's git repository page in GitHub. - `:OpenInGHFile` - - Opens the current file page in GitHub. + - Opens the current file page in GitHub. This command supports ranges. ## Usage @@ -48,16 +48,17 @@ You can call the commands directly or define mappings them: ```lua -- for repository page -vim.api.nvim_set_keymap("n", "gr", ":OpenInGHRepo ", { expr = true, noremap = true }) +vim.api.nvim_set_keymap("n", "gr", ":OpenInGHRepo ", { silent = true, noremap = true }) -- for current file page -vim.api.nvim_set_keymap("n", "gf", ":OpenInGHFile ", { expr = true, noremap = true }) +vim.api.nvim_set_keymap("n", "gf", ":OpenInGHFile ", { silent = true, noremap = true }) +vim.api.nvim_set_keymap("v", "gf", ":OpenInGHFile ", { silent = true, noremap = true }) ``` ## TODO - [x] Support the current file cursor position - - [ ] Support visual mode to open a file in range selection + - [x] Support visual mode to open a file in range selection - [x] Support other version control websites ## Contribution diff --git a/lua/openingh/init.lua b/lua/openingh/init.lua index 473a3e1..0ac5ac9 100644 --- a/lua/openingh/init.lua +++ b/lua/openingh/init.lua @@ -22,7 +22,7 @@ function M.setup() M.repo_url = string.format("http://%s/%s/%s", gh.host, gh.user_or_org, gh.reponame) end -function M.openFile() +function M.openFile(range_start, range_end) -- make sure to update the current directory M.setup() if M.is_no_git_origin then @@ -38,10 +38,17 @@ function M.openFile() return end - local line_number = utils.get_line_number_from_buf() + local lines + + if range_start and range_end then + lines = "#L" .. range_start .. "-" .. "L" .. range_end + else + lines = "#L" .. utils.get_line_number_from_buf() + 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 .. "#L" .. line_number + 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) diff --git a/plugin/openingh.lua b/plugin/openingh.lua index 71ae98a..43a09bf 100644 --- a/plugin/openingh.lua +++ b/plugin/openingh.lua @@ -5,10 +5,16 @@ vim.g.openingh = true local openingh = require("openingh") -vim.api.nvim_create_user_command("OpenInGHFile", function() - openingh:openFile() -end, {}) +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) + end +end, { + range = true, +}) vim.api.nvim_create_user_command("OpenInGHRepo", function() - openingh:openRepo() + openingh.openRepo() end, {}) diff --git a/tests/openingh_spec.lua b/tests/openingh_spec.lua index e6d09f8..e40fd50 100644 --- a/tests/openingh_spec.lua +++ b/tests/openingh_spec.lua @@ -34,9 +34,30 @@ describe("openingh should open", function() it("file on :OpenInGHFile", function() vim.cmd("e ./README.md") + vim.api.nvim_win_set_cursor(0, { 3, 0 }) vim.cmd("OpenInGHFile") - local status = vim.g.OPENINGH_RESULT - assert.truthy(status) + local expected = "/README.md#L3" + 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, ">", 10, 0, {}) + vim.cmd("'<,'>OpenInGHFile") + local status = vim.g.OPENINGH_RESULT + local expected = "/README.md#L5-L10" + assert.equal(expected, status:sub(-#expected)) + end) + + it("file range using a keymap", function() + vim.api.nvim_set_keymap("n", "ghf", ":OpenInGHFile ", {}) + vim.cmd("e ./README.md") + vim.api.nvim_win_set_cursor(0, { 2, 0 }) + vim.cmd("normal ghf") + local status = vim.g.OPENINGH_RESULT + local expected = "/README.md#L2" + assert.equal(expected, status:sub(-#expected)) end) end)