|
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 source:match "%.gd$" then return end |
| 3 | + for _, suffix in ipairs { "uid", "import" } do |
| 4 | + local tmp_file = ("%s.%s"):format(source, suffix) |
| 5 | + local tmp_stat = vim.uv.fs_stat(tmp_file) |
4 | 6 |
|
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 |
| 12 | + end |
31 | 13 | end |
32 | 14 | end |
33 | 15 |
|
@@ -56,28 +38,29 @@ return { |
56 | 38 | { |
57 | 39 | "nvim-neo-tree/neo-tree.nvim", |
58 | 40 | 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, |
| 41 | + opts = { |
| 42 | + event_handlers = { |
| 43 | + { |
| 44 | + event = "file_renamed", |
| 45 | + handler = function(args) gd_move_handler(args.source, args.destination) end, |
| 46 | + id = "gd_renamed_handler", |
| 47 | + }, |
| 48 | + { |
| 49 | + event = "file_moved", |
| 50 | + handler = function(args) gd_move_handler(args.source, args.destination) end, |
| 51 | + id = "gd_moved_handler", |
| 52 | + }, |
| 53 | + }, |
| 54 | + nesting_rules = { |
| 55 | + godot_uid = { |
| 56 | + pattern = "^(.*%.gd)$", |
| 57 | + files = { |
| 58 | + "%1.uid", |
| 59 | + "%1.import", |
| 60 | + }, |
| 61 | + }, |
| 62 | + }, |
| 63 | + }, |
81 | 64 | }, |
82 | 65 | { |
83 | 66 | "Cretezy/godot-server.nvim", |
|
0 commit comments