diff --git a/nvim/lua/config/keys.lua b/nvim/lua/config/keys.lua index dd725ec..70d55cc 100644 --- a/nvim/lua/config/keys.lua +++ b/nvim/lua/config/keys.lua @@ -1,4 +1,5 @@ local config = require("config.features") +local gitbrowse_utils = require("core.utils.gitbrowse") local M = {} @@ -42,10 +43,44 @@ M.keymaps = { opts = { desc = "Fuzzy find help tags" }, }, { - mode = { "n", "v" }, - keys = "gc", - command = function() require("snacks").gitbrowse() end, - opts = { desc = "Open commit line(s) in browser" }, + mode = "n", + keys = "gbb", + command = function() + require("snacks").gitbrowse( + gitbrowse_utils.build_gitbrows_config(true, false) + ) + end, + opts = { desc = "Open file for branch in browser" }, + }, + { + mode = "v", + keys = "gbb", + command = function() + require("snacks").gitbrowse( + gitbrowse_utils.build_gitbrows_config(true, true) + ) + end, + opts = { desc = "Open lines for branch in browser" }, + }, + { + mode = "n", + keys = "gbm", + command = function() + require("snacks").gitbrowse( + gitbrowse_utils.build_gitbrows_config(false, false) + ) + end, + opts = { desc = "Open file for default branch in browser" }, + }, + { + mode = "v", + keys = "gbm", + command = function() + require("snacks").gitbrowse( + gitbrowse_utils.build_gitbrows_config(false, true) + ) + end, + opts = { desc = "Open lines for default branch in browser" }, }, { mode = "n", @@ -237,7 +272,9 @@ if config.use_copilot then table.insert(M.keymaps, { mode = "n", keys = "oe", - command = function() require("opencode").prompt("Explain @cursor and its context") end, + command = function() + require("opencode").prompt("Explain @cursor and its context") + end, opts = { desc = "Explain code near cursor" }, }) end @@ -248,9 +285,7 @@ if config.use_copilot then mode = "n", keys = "", command = function() - if not require("sidekick").nes_jump_or_apply() then - return "" - end + if not require("sidekick").nes_jump_or_apply() then return "" end end, opts = { desc = "Goto/Apply Next Edit Suggestion", expr = true }, }) @@ -305,7 +340,9 @@ if config.use_copilot then table.insert(M.keymaps, { mode = "n", keys = "ac", - command = function() require("sidekick.cli").toggle({ name = "claude", focus = true }) end, + command = function() + require("sidekick.cli").toggle({ name = "claude", focus = true }) + end, opts = { desc = "Sidekick Toggle Claude" }, }) end diff --git a/nvim/lua/core/utils/gitbrowse_utils.lua b/nvim/lua/core/utils/gitbrowse_utils.lua new file mode 100644 index 0000000..a406452 --- /dev/null +++ b/nvim/lua/core/utils/gitbrowse_utils.lua @@ -0,0 +1,74 @@ +local M = {} + +--- Get the current git branch name +---@returns string +local function get_current_git_branch() + local handle = io.popen("git rev-parse --abbrev-ref HEAD 2> /dev/null") + if handle then + local branch_name = handle:read("*l") + handle:close() + if branch_name then + return branch_name + else + return "" -- Return empty string if no branch name found + end + else + return "" -- Return empty string if command fails + end +end + +--- Get the current git branch name +---@returns string +local function get_default_git_branch() + local handle = + io.popen("git symbolic-ref refs/remotes/origin/HEAD | cut -d '/' -f4") + if handle then + local branch_name = handle:read("*l") + handle:close() + if branch_name then + return branch_name + else + return "" -- Return empty string if no branch name found + end + else + return "" -- Return empty string if command fails + end +end + +--- Build config to open lines in current branch +---@returns snacks.gitbrowse.Config +function M.build_gitbrows_config(in_current_branch, is_visual) + -- set defaults + if in_current_branch == nil then in_current_branch = true end + if is_visual == nil then is_visual = false end + + -- get the current visual selection lines if visual mode + local start_line = nil + local end_line = nil + if is_visual == true then + start_line = vim.fn.line("v") -- Get the starting line of the visual selection + end_line = vim.fn.line(".") -- Get the ending line of the visual selection + if start_line > end_line then + start_line, end_line = end_line, start_line -- Swap if necessary + end + end + + -- get the branch + local branch + if in_current_branch == true then + branch = get_current_git_branch() + else + branch = get_default_git_branch() + end + + -- build the config + return { + notify = true, + what = "file", + branch = branch, + line_start = start_line, + line_end = end_line, + } +end + +return M