Skip to content

Commit

Permalink
fix: supress swap file errors
Browse files Browse the repository at this point in the history
  • Loading branch information
benlubas authored and vhyrro committed Jun 11, 2024
1 parent 352570c commit 4420ddc
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 13 deletions.
14 changes: 5 additions & 9 deletions lua/neorg/modules/core/dirman/module.lua
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,14 @@ local Path = require("pathlib")

local neorg = require("neorg.core")
local log, modules, utils = neorg.log, neorg.modules, neorg.utils
local dirman_utils

local module = modules.create("core.dirman")

module.setup = function()
return {
success = true,
requires = { "core.autocommands", "core.ui", "core.storage" },
requires = { "core.autocommands", "core.ui", "core.storage", "core.dirman.utils" },
}
end

Expand All @@ -70,6 +71,8 @@ module.load = function()
module.config.public.workspaces[name] = Path(workspace_location):resolve():to_absolute()
end

dirman_utils = module.required["core.dirman.utils"]

modules.await("core.keybinds", function(keybinds)
keybinds.register_keybind(module.name, "new.note")
end)
Expand Down Expand Up @@ -497,14 +500,7 @@ module.on_event = function(event)
end
end

local ok, _ = pcall(vim.cmd.edit, index_path:cmd_string())
if not ok then
log.warn(
("Failed to edit file %s. This can result from vim finding a swapfile. If that was the case, ignore this warning."):format(
index_path
)
)
end
dirman_utils.edit_file(index_path:cmd_string())
return
end

Expand Down
16 changes: 16 additions & 0 deletions lua/neorg/modules/core/dirman/utils/module.lua
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,22 @@ module.public = {
end
return filepath
end,

---Call attempt to edit a file, catches and suppresses the error caused by a swap file being
---present. Re-raises other errors via log.error
---@param path string
edit_file = function(path)
local ok, err = pcall(vim.cmd.edit, path)
if not ok then
-- Vim:E325 is the swap file error, in which case, a lengthy message already shows to
-- the user, and we don't have to crash out of this function (which creates a long and
-- misleading error message).
if err and not err:match("Vim:E325") then
log.error("Failed to edit file %s. Error:\n%s"):format(path, err)
end
end
end,

---Resolve `$<workspace>/path/to/file` and return the real path
-- NOTE: Use `expand_pathlib` which returns a PathlibPath object instead.
---
Expand Down
9 changes: 5 additions & 4 deletions lua/neorg/modules/core/esupports/hop/module.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ prompted with a set of actions that you can perform on the broken link.

local neorg = require("neorg.core")
local config, lib, log, modules, utils = neorg.config, neorg.lib, neorg.log, neorg.modules, neorg.utils
local links
local links, dirman_utils

local module = modules.create("core.esupports.hop")

Expand All @@ -31,6 +31,7 @@ end

module.load = function()
links = module.required["core.links"]
dirman_utils = module.required["core.dirman.utils"]
modules.await("core.keybinds", function(keybinds)
keybinds.register_keybind(module.name, "hop-link")
end)
Expand Down Expand Up @@ -151,7 +152,7 @@ module.public = {
external_file = function()
open_split()

vim.api.nvim_cmd({ cmd = "edit", args = { located_link_information.path } }, {})
dirman_utils.edit_file(located_link_information.path)

if located_link_information.line then
jump_to_line(located_link_information.line)
Expand Down Expand Up @@ -499,7 +500,7 @@ module.public = {
-- Check whether our target is from a different file
if parsed_link_information.link_file_text then
local expanded_link_text =
module.required["core.dirman.utils"].expand_path(parsed_link_information.link_file_text)
dirman_utils.expand_path(parsed_link_information.link_file_text)

if expanded_link_text ~= vim.fn.expand("%:p") then
-- We are dealing with a foreign file
Expand Down Expand Up @@ -781,7 +782,7 @@ module.private = {

if parsed_link_information.link_file_text then
local expanded_link_text =
module.required["core.dirman.utils"].expand_path(parsed_link_information.link_file_text)
dirman_utils.expand_path(parsed_link_information.link_file_text)

if expanded_link_text ~= vim.fn.expand("%:p") then
-- We are dealing with a foreign file
Expand Down

0 comments on commit 4420ddc

Please sign in to comment.