Skip to content

Commit

Permalink
Update remove and rename actions to unwatch old paths
Browse files Browse the repository at this point in the history
  • Loading branch information
chipsenkbeil committed Jun 26, 2023
1 parent 37ebba8 commit 2024af2
Showing 1 changed file with 47 additions and 1 deletion.
48 changes: 47 additions & 1 deletion lua/distant/nav/actions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -212,16 +212,39 @@ M.rename = function(opts)
return
end

-- NOTE: We lock watching the buffer tied to this path to prevent notifications
local buffer = plugin.buf.find({ path = old_path })
local watched
if buffer then
watched = buffer.watched()
buffer.set_watched('locked')
end

local err = plugin.api(client_id).rename({ src = old_path, dst = new_path })

if not err then
-- If we succeeded, attempt to unwatch the old path and clear the watch state
if buffer then
-- Perform unwatch asynchronously, but don't worry about success
plugin.api.unwatch({ path = old_path }, function()
end)

buffer.set_watched(nil)
end

-- Reload the directory buffer
plugin.editor.open({
client_id = client_id,
path = base_path,
reload = true,
})
else
log.error(string.format('Failed to rename %s to %s: %s', old_path, new_path, err))

-- If we failed, restore the previous watch state
if buffer then
buffer.set_watched(watched)
end
end
end
end
Expand Down Expand Up @@ -259,6 +282,14 @@ M.remove = function(opts)
force = choice == 2
end

-- NOTE: We lock watching the buffer tied to this path to prevent notifications
local buffer = plugin.buf.find({ path = path })
local watched
if buffer then
watched = buffer.watched()
buffer.set_watched('locked')
end

local err = plugin.api(client_id).remove({
path = path,
force = force,
Expand All @@ -267,13 +298,28 @@ M.remove = function(opts)
})

if not err then
-- If we succeeded, attempt to unwatch the path and clear the watch state
if buffer then
-- Perform unwatch asynchronously, but don't worry about success
plugin.api.unwatch({ path = path }, function()
end)

buffer.set_watched(nil)
end

-- Reload the directory buffer
plugin.editor.open({
client_id = client_id,
path = base_path,
reload = true
reload = true,
})
else
log.fmt_error('Failed to remove %s: %s', path, tostring(err))

-- If we failed, restore the previous watch state
if buffer then
buffer.set_watched(watched)
end
end
end
end
Expand Down

0 comments on commit 2024af2

Please sign in to comment.