diff --git a/lua/octo/commands.lua b/lua/octo/commands.lua index 6fa92808e..8cdd7aad2 100644 --- a/lua/octo/commands.lua +++ b/lua/octo/commands.lua @@ -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() @@ -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 @@ -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) diff --git a/lua/octo/reviews/file-entry.lua b/lua/octo/reviews/file-entry.lua index 105860283..b7b31b210 100644 --- a/lua/octo/reviews/file-entry.lua +++ b/lua/octo/reviews/file-entry.lua @@ -32,12 +32,12 @@ M._null_buffer = {} ---@field right_binary boolean|nil ---@field left_bufid integer ---@field right_bufid integer ---- If this table is empty, the buffer is not ready to be displayed ---- If the file is actually empty for the revision, table will be filled with a single empty line ---@field left_lines string[] ---- If this table is empty, the buffer is not ready to be displayed ---- If the file is actually empty for the revision, table will be filled with a single empty line ---@field right_lines string[] +--- If either left_fetched or right_fetched is false, +--- the buffer is not ready to be displayed. +---@field left_fetched boolean +---@field right_fetched boolean ---@field left_winid number ---@field right_winid number ---@field left_comment_ranges table @@ -78,6 +78,8 @@ function FileEntry:new(opt) right_binary = opt.right_binary, left_lines = {}, right_lines = {}, + left_fetched = false, + right_fetched = false, diffhunks = diffhunks, associated_bufs = {}, viewed_state = pr.files[opt.path], @@ -205,10 +207,12 @@ function FileEntry:fetch() if self.pull_request.local_right then utils.get_file_at_commit(right_path, right_sha, function(lines) self.right_lines = lines + self.right_fetched = true end) else utils.get_file_contents(self.pull_request.repo, right_abbrev, right_path, function(lines) self.right_lines = lines + self.right_fetched = true end) end @@ -216,10 +220,12 @@ function FileEntry:fetch() if self.pull_request.local_left then utils.get_file_at_commit(left_path, left_sha, function(lines) self.left_lines = lines + self.left_fetched = true end) else utils.get_file_contents(self.pull_request.repo, left_abbrev, left_path, function(lines) self.left_lines = lines + self.left_fetched = true end) end @@ -232,7 +238,7 @@ end ---Determines whether the file content has been loaded and the file is ready to render ---@return boolean function FileEntry:is_ready_to_render() - return #self.left_lines > 0 and #self.right_lines > 0 + return self.left_fetched and self.right_fetched end ---Load the buffers. diff --git a/lua/octo/reviews/init.lua b/lua/octo/reviews/init.lua index 0583b6bf2..5a203e535 100644 --- a/lua/octo/reviews/init.lua +++ b/lua/octo/reviews/init.lua @@ -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 diff --git a/lua/octo/utils.lua b/lua/octo/utils.lua index 494cc3fbc..a578dfd2e 100644 --- a/lua/octo/utils.lua +++ b/lua/octo/utils.lua @@ -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]