diff --git a/lua/adopure/activate.lua b/lua/adopure/activate.lua index c7c65e7..c0d4b88 100644 --- a/lua/adopure/activate.lua +++ b/lua/adopure/activate.lua @@ -1,3 +1,4 @@ +local Path = require("plenary.path") local M = {} ---@param pull_request adopure.PullRequest @@ -15,9 +16,9 @@ local function buffer_marker_autocmd(state) local augroup = vim.api.nvim_create_augroup("adopure.nvim", { clear = true }) vim.api.nvim_create_autocmd({ "BufEnter" }, { group = augroup, - callback = function() - if state.pull_request_threads then - require("adopure.marker").create_buffer_extmarks(state.pull_request_threads) + callback = function(args) + if state.pull_request_threads and args.file ~= "" then + require("adopure.marker").create_buffer_extmarks(state.pull_request_threads, args.buf, args.file) end end, }) @@ -37,7 +38,8 @@ end ---@param state adopure.AdoState function M.activate_pull_request_context(state) - require("adopure.marker").create_buffer_extmarks(state.pull_request_threads) + local focused_file_path = Path:new(vim.fn.expand("%:.")).filename + require("adopure.marker").create_buffer_extmarks(state.pull_request_threads, 0, focused_file_path) require("adopure.git").confirm_checkout_and_open(state.active_pull_request, function() confirm_open_in_diffview(state.active_pull_request) end) diff --git a/lua/adopure/marker.lua b/lua/adopure/marker.lua index 8d18ac0..b2e1a1b 100644 --- a/lua/adopure/marker.lua +++ b/lua/adopure/marker.lua @@ -7,20 +7,6 @@ local M = { local Path = require("plenary.path") local namespace = vim.api.nvim_create_namespace("adopure-marker") ----Get open file paths with bufnrs ----@diagnostic disable-next-line: undefined-doc-name ----@return table -local function get_open_file_paths() - ---@diagnostic disable-next-line: undefined-doc-name - ---@type table - local open_file_paths = {} - local buffers = vim.api.nvim_list_bufs() - for _, buf in pairs(buffers) do - local buffer_path = vim.api.nvim_buf_get_name(buf) - open_file_paths[Path:new(buffer_path):absolute()] = buf - end - return open_file_paths -end local signs = { active = "󰅺 ", byDesign = "󱀡 ", @@ -63,6 +49,7 @@ local function create_extmark(bufnr, pull_request_thread, context) end if invalid_field == "col" and start_col ~= 0 then start_col = 0 + local _ = start_col -- fix incorrect unignorable warning break end break @@ -76,27 +63,25 @@ end ---Create extmarks for pull request threads ---@param pull_request_threads adopure.Thread[] -function M.create_buffer_extmarks(pull_request_threads) - for _, extmark in pairs(vim.api.nvim_buf_get_extmarks(0, namespace, 0, -1, {})) do - local extmark_id = extmark[1] - vim.api.nvim_buf_del_extmark(0, namespace, extmark_id) - end +---@param bufnr number +---@param file_path string +function M.create_buffer_extmarks(pull_request_threads, bufnr, file_path) + vim.api.nvim_buf_clear_namespace(bufnr, namespace, 0, -1) - local focused_file_path = Path:new(vim.fn.expand("%:.")) + local focused_file_path = tostring(Path:new(file_path):make_relative()) for _, pull_request_thread in ipairs(pull_request_threads) do - local open_file_paths = get_open_file_paths() - local file_path, context + local context if type(pull_request_thread.threadContext) == "table" then local path_reference = pull_request_thread.threadContext.filePath - file_path = Path:new(string.sub(path_reference, 2)) + file_path = tostring(Path:new(string.sub(path_reference, 2))) context = pull_request_thread.threadContext end + if file_path and context - and tostring(focused_file_path.filePath) == tostring(file_path.filePath) + and focused_file_path == file_path and not pull_request_thread.isDeleted - and open_file_paths[file_path:absolute()] and pull_request_thread.threadContext.rightFileStart then create_extmark(bufnr, pull_request_thread, context)