Skip to content

Commit 040e9db

Browse files
committed
fix(godot): use neo-tree nested files and clean up event handlers
1 parent e1488bc commit 040e9db

File tree

1 file changed

+38
-51
lines changed

1 file changed

+38
-51
lines changed

lua/astrocommunity/pack/godot/init.lua

Lines changed: 38 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,14 @@
1-
local paths_to_check = { "/", "/../", "/../../" }
2-
local function is_godot_project()
3-
local cwd = vim.fn.getcwd()
1+
local function gd_move_handler(source, destination)
2+
if not vim.fs.root(source, "project.godot") then return end -- only run in godot projects
3+
local suffix = source:match "%.gd$" and "uid" or "import"
4+
local tmp_file = ("%s.%s"):format(source, suffix)
5+
local tmp_stat = vim.uv.fs_stat(tmp_file)
46

5-
for _, value in ipairs(paths_to_check) do
6-
if vim.uv.fs_stat(cwd .. value .. "project.godot") then return true end
7-
end
8-
end
9-
10-
local function move_handler(source, destination)
11-
-- Check if corresponding .uid file exists
12-
local uid_file = source .. ".uid"
13-
local uid_stat = vim.uv.fs_stat(uid_file)
14-
15-
if uid_stat then
16-
-- Rename the uid file
17-
local new_uid_file = destination .. ".uid"
18-
local success, err = vim.uv.fs_rename(uid_file, new_uid_file)
19-
if not success then vim.notify("Failed to rename Godot UID file: " .. (err or "unknown error")) end
20-
end
21-
22-
-- Check if corresponding .import file exists
23-
local import_file = source .. ".import"
24-
local import_stat = vim.uv.fs_stat(import_file)
25-
26-
if import_stat then
27-
-- Rename the import file
28-
local new_import_file = destination .. ".import"
29-
local success, err = vim.uv.fs_rename(import_file, new_import_file)
30-
if not success then vim.notify("Failed to rename Godot import file: " .. (err or "unknown error")) end
7+
if tmp_stat then
8+
-- Rename the temporary file
9+
local new_tmp_file = ("%s.%s"):format(destination, suffix)
10+
local success, err = vim.uv.fs_rename(tmp_file, new_tmp_file)
11+
if not success then vim.notify(("Failed to rename Godot %s file: %s"):format(suffix, err or "unknown error")) end
3112
end
3213
end
3314

@@ -56,28 +37,34 @@ return {
5637
{
5738
"nvim-neo-tree/neo-tree.nvim",
5839
optional = true,
59-
opts = function(_, opts)
60-
-- Only activate renaming and filtering within Godot projects
61-
if not is_godot_project() then return end
62-
63-
-- Add renaming Godot UID/import files without require LSP to run
64-
opts.event_handlers = opts.event_handlers or {}
65-
table.insert(opts.event_handlers, {
66-
event = "file_renamed",
67-
handler = function(args) move_handler(args.source, args.destination) end,
68-
})
69-
table.insert(opts.event_handlers, {
70-
event = "file_moved",
71-
handler = function(args) move_handler(args.source, args.destination) end,
72-
})
73-
74-
-- Add Godot UID/import files to the filter list
75-
opts.filesystem = opts.filesystem or {}
76-
opts.filesystem.filtered_items = opts.filesystem.filtered_items or {}
77-
opts.filesystem.filtered_items.hide_by_pattern = opts.filesystem.filtered_items.hide_by_pattern or {}
78-
table.insert(opts.filesystem.filtered_items.hide_by_pattern, "*.uid")
79-
table.insert(opts.filesystem.filtered_items.hide_by_pattern, "*.import")
80-
end,
40+
opts = {
41+
event_handlers = {
42+
{
43+
event = "file_renamed",
44+
handler = function(args) gd_move_handler(args.source, args.destination) end,
45+
id = "godot_renamed_handler",
46+
},
47+
{
48+
event = "file_moved",
49+
handler = function(args) gd_move_handler(args.source, args.destination) end,
50+
id = "godot_moved_handler",
51+
},
52+
},
53+
nesting_rules = {
54+
godot_import = {
55+
pattern = "^(.*)$",
56+
files = { -- pretty loose, but is probably a specific enough pattern to be alright
57+
"%1.import",
58+
},
59+
},
60+
godot_uid = {
61+
pattern = "^(.*%.gd)$",
62+
files = {
63+
"%1.uid",
64+
},
65+
},
66+
},
67+
},
8168
},
8269
{
8370
"Cretezy/godot-server.nvim",

0 commit comments

Comments
 (0)