Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use user command lines ranges #825

Merged
merged 2 commits into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions lua/octo/commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ local config = require "octo.config"
local colors = require "octo.ui.colors"
local vim = vim

-- a global variable where command handlers can access the details of the last
-- command ran.
--
-- this came into existence since some commands like "comment add" need to
-- understand the line range the comment should be created on.
-- this is problematic without the command options as you exit visual mode when
-- enterting the command line.
OctoLastCmdOpts = nil

local M = {}

local get_current_buffer = function()
Expand All @@ -32,7 +41,9 @@ end

function M.setup()
vim.api.nvim_create_user_command("Octo", function(opts)
OctoLastCmdOpts = opts
require("octo.commands").octo(unpack(opts.fargs))
OctoLastCmdOpts = nil
end, { complete = require("octo.completion").octo_command_complete, nargs = "*", range = true })
local conf = config.values

Expand Down Expand Up @@ -536,6 +547,7 @@ function M.octo(object, action, ...)
utils.error(action and "Incorrect action: " .. action or "No action specified")
return
end

res = pcall(a, ...)
if not res then
utils.error(action and "Failed action: " .. action)
Expand Down
9 changes: 8 additions & 1 deletion lua/octo/reviews/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -329,8 +329,15 @@ function Review:add_comment(isSuggestion)
return
end

-- get visual selected line range
-- get visual selected line range, used if coming from a keymap where current
-- mode can be evaluated.
local line1, line2 = utils.get_lines_from_context "visual"
-- if we came from the command line the command options will provide line
-- range
if OctoLastCmdOpts ~= nil then
line1 = OctoLastCmdOpts.line1
line2 = OctoLastCmdOpts.line2
end

local comment_ranges, current_bufnr
if split == "RIGHT" then
Expand Down
4 changes: 2 additions & 2 deletions lua/octo/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1642,8 +1642,8 @@ function M.get_lines_from_context(calling_context)
line_number_start = vim.fn.line "."
line_number_end = line_number_start
elseif calling_context == "visual" then
line_number_start = vim.fn.line "'<"
line_number_end = vim.fn.line "'>"
line_number_start = vim.fn.line "v"
line_number_end = vim.fn.line "."
elseif calling_context == "motion" then
line_number_start = vim.fn.getpos("'[")[2]
line_number_end = vim.fn.getpos("']")[2]
Expand Down