From 0882b3d798edd64c7fc9611cfce5bb9e383944e4 Mon Sep 17 00:00:00 2001 From: Laurent Vauthrin Date: Tue, 21 Mar 2023 09:39:12 -0400 Subject: [PATCH 01/10] Added support for ranges when opening a file --- lua/openingh/init.lua | 13 ++++++++++--- plugin/openingh.lua | 12 +++++++++--- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/lua/openingh/init.lua b/lua/openingh/init.lua index 473a3e1..b614cff 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 = nil + + 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..8187157 100644 --- a/plugin/openingh.lua +++ b/plugin/openingh.lua @@ -5,9 +5,15 @@ 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() From 9a3fd56ea9863c40cfbd45beff24257c1dc31e53 Mon Sep 17 00:00:00 2001 From: Laurent Vauthrin Date: Tue, 21 Mar 2023 12:55:18 -0400 Subject: [PATCH 02/10] Fixed style issue in init.lua --- lua/openingh/init.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua/openingh/init.lua b/lua/openingh/init.lua index b614cff..0ac5ac9 100644 --- a/lua/openingh/init.lua +++ b/lua/openingh/init.lua @@ -38,7 +38,7 @@ function M.openFile(range_start, range_end) return end - local lines = nil + local lines if range_start and range_end then lines = "#L" .. range_start .. "-" .. "L" .. range_end From 9c50907ab1ca4b7fb219206b05c7409af11b8799 Mon Sep 17 00:00:00 2001 From: Laurent Vauthrin Date: Tue, 21 Mar 2023 12:55:43 -0400 Subject: [PATCH 03/10] Added tests for opening a range --- tests/openingh_spec.lua | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/tests/openingh_spec.lua b/tests/openingh_spec.lua index e6d09f8..5bee778 100644 --- a/tests/openingh_spec.lua +++ b/tests/openingh_spec.lua @@ -34,9 +34,20 @@ 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 = "/blob/main/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 = "/blob/main/README.md#L5-L10" + assert.equal(expected, status:sub(-#expected)) end) end) From 9936957f2c79b46a9e203b5f3cfebea0f4368b1d Mon Sep 17 00:00:00 2001 From: Laurent Vauthrin Date: Tue, 21 Mar 2023 12:56:14 -0400 Subject: [PATCH 04/10] Updated README.md for opening a range --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 3a68e32..399c72f 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 @@ -52,6 +52,7 @@ vim.api.nvim_set_keymap("n", "gr", ":OpenInGHRepo ", { expr = true, -- for current file page vim.api.nvim_set_keymap("n", "gf", ":OpenInGHFile ", { expr = true, noremap = true }) +vim.api.nvim_set_keymap("v", "gf", "'<,'>OpenInGHFile ", { expr = true, noremap = true }) ``` ## TODO From 3f8a4b6b5db657e5db1bd6442d27ae32d075b4e4 Mon Sep 17 00:00:00 2001 From: Laurent Vauthrin Date: Wed, 22 Mar 2023 13:40:55 -0400 Subject: [PATCH 05/10] Compare only filename in test since full url can vary based on how the repo was checked out --- tests/openingh_spec.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/openingh_spec.lua b/tests/openingh_spec.lua index 5bee778..3d91d8b 100644 --- a/tests/openingh_spec.lua +++ b/tests/openingh_spec.lua @@ -37,7 +37,7 @@ 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 = "/blob/main/README.md#L3" + local expected = "/README.md#L3" assert.equal(expected, status:sub(-#expected)) end) @@ -47,7 +47,7 @@ describe("openingh should open", function() vim.api.nvim_buf_set_mark(0, ">", 10, 0, {}) vim.cmd("'<,'>OpenInGHFile") local status = vim.g.OPENINGH_RESULT - local expected = "/blob/main/README.md#L5-L10" + local expected = "/README.md#L5-L10" assert.equal(expected, status:sub(-#expected)) end) end) From 93434c35ce937e4872f965748def88cf7282b7dd Mon Sep 17 00:00:00 2001 From: Laurent Vauthrin Date: Wed, 22 Mar 2023 13:41:15 -0400 Subject: [PATCH 06/10] Switched to dot notation to be consistent --- plugin/openingh.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin/openingh.lua b/plugin/openingh.lua index 8187157..43a09bf 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.openRepo() end, {}) From de1c1d9f8465a6ce6b1369589e31539a6c570ee7 Mon Sep 17 00:00:00 2001 From: Laurent Vauthrin Date: Wed, 22 Mar 2023 13:41:29 -0400 Subject: [PATCH 07/10] Checked second TODO item in README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 399c72f..4846c4c 100644 --- a/README.md +++ b/README.md @@ -58,7 +58,7 @@ vim.api.nvim_set_keymap("v", "gf", "'<,'>OpenInGHFile ", { ## 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 From 3ac1ce892f54c360cce0188e48408bcdcad10e83 Mon Sep 17 00:00:00 2001 From: Laurent Vauthrin Date: Thu, 23 Mar 2023 09:30:33 -0400 Subject: [PATCH 08/10] Updated reamde with cleaner keymappings --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 4846c4c..da25af8 100644 --- a/README.md +++ b/README.md @@ -48,11 +48,11 @@ 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("v", "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 From 2f61a3797505bb77a3b98fa61f9bbf7b6f81a438 Mon Sep 17 00:00:00 2001 From: Laurent Vauthrin Date: Thu, 23 Mar 2023 10:04:55 -0400 Subject: [PATCH 09/10] Added test for keymapping --- tests/openingh_spec.lua | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/openingh_spec.lua b/tests/openingh_spec.lua index 3d91d8b..c80ab33 100644 --- a/tests/openingh_spec.lua +++ b/tests/openingh_spec.lua @@ -50,4 +50,13 @@ describe("openingh should open", function() local expected = "/README.md#L5-L10" assert.equal(expected, status:sub(-#expected)) end) + + it("file range using a keymap", function() + vim.cmd("e ./README.md") + vim.api.nvim_set_keymap("n", "ghf", ":OpenInGHFile ", {}) + vim.cmd("normal ghf") + local status = vim.g.OPENINGH_RESULT + local expected = "/README.md#L1" + assert.equal(expected, status:sub(-#expected)) + end) end) From 65c504d314dc5209160ab51507123380712d8370 Mon Sep 17 00:00:00 2001 From: Laurent Vauthrin Date: Thu, 23 Mar 2023 10:09:23 -0400 Subject: [PATCH 10/10] Updated keymap test to be more deterministic --- tests/openingh_spec.lua | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/openingh_spec.lua b/tests/openingh_spec.lua index c80ab33..e40fd50 100644 --- a/tests/openingh_spec.lua +++ b/tests/openingh_spec.lua @@ -52,11 +52,12 @@ describe("openingh should open", function() end) it("file range using a keymap", function() - vim.cmd("e ./README.md") 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#L1" + local expected = "/README.md#L2" assert.equal(expected, status:sub(-#expected)) end) end)