Skip to content

Commit

Permalink
feat: limit marking to open files
Browse files Browse the repository at this point in the history
  • Loading branch information
noortw01 committed Dec 16, 2024
1 parent 471da25 commit 9ec8595
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 29 deletions.
10 changes: 6 additions & 4 deletions lua/adopure/activate.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
local Path = require("plenary.path")
local M = {}

---@param pull_request adopure.PullRequest
Expand All @@ -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,
})
Expand All @@ -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)
Expand Down
35 changes: 10 additions & 25 deletions lua/adopure/marker.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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<Path, number>
local function get_open_file_paths()
---@diagnostic disable-next-line: undefined-doc-name
---@type table<Path, number>
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 = "󱀡 ",
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down

0 comments on commit 9ec8595

Please sign in to comment.