|
747 | 747 | M.trash_node = function(path, callback, state) |
748 | 748 | local _, name = utils.split_path(path) |
749 | 749 |
|
750 | | - log.trace("Deleting node:", path) |
| 750 | + log.trace("Trashing node:", path) |
751 | 751 | local stat = uv.fs_stat(path) |
752 | 752 |
|
753 | 753 | local do_trash = function() |
@@ -793,22 +793,88 @@ end |
793 | 793 | ---@param callback fun(paths: string[])? |
794 | 794 | ---@param state neotree.State? |
795 | 795 | M.trash_nodes = function(paths, callback, state) |
796 | | - local msg = "Are you sure you want to delete " .. #paths .. " items?" |
| 796 | + local msg = "Are you sure you want to trash " .. #paths .. " items?" |
797 | 797 | inputs.confirm(msg, function(confirmed) |
798 | 798 | if not confirmed then |
799 | 799 | return |
800 | 800 | end |
801 | 801 |
|
802 | | - local success, err, restorer = trash.trash(paths) |
| 802 | + local success, err, restorefunc = trash.trash(paths) |
803 | 803 | if not success then |
804 | 804 | log.error(err) |
805 | 805 | end |
806 | 806 |
|
807 | | - if state then |
808 | | - table.insert(state.undostack, function() |
809 | | - trash.restore(paths, restorer) |
| 807 | + if state and restorefunc then |
| 808 | + table.insert(state.undostack, restorefunc) |
| 809 | + end |
| 810 | + if callback then |
| 811 | + vim.schedule(function() |
| 812 | + callback(paths) |
810 | 813 | end) |
811 | 814 | end |
| 815 | + end) |
| 816 | +end |
| 817 | + |
| 818 | +---@param path string |
| 819 | +---@param callback fun(path: string)? |
| 820 | +---@param state neotree.State? |
| 821 | +M.restore_node_from_trash = function(path, callback, state) |
| 822 | + local _, name = utils.split_path(path) |
| 823 | + |
| 824 | + log.trace("Restoring node:", path) |
| 825 | + local stat = uv.fs_stat(path) |
| 826 | + |
| 827 | + local do_trash = function() |
| 828 | + local complete = vim.schedule_wrap(function() |
| 829 | + events.fire_event(events.FILE_DELETED, path) |
| 830 | + if callback then |
| 831 | + callback(path) |
| 832 | + end |
| 833 | + end) |
| 834 | + |
| 835 | + local event_result = events.fire_event(events.BEFORE_FILE_DELETE, path) or {} |
| 836 | + if event_result.handled then |
| 837 | + complete() |
| 838 | + return |
| 839 | + end |
| 840 | + |
| 841 | + local paths = { path } |
| 842 | + local success, err = trash.restore(paths) |
| 843 | + if not success then |
| 844 | + log.error("Could not restore " .. path .. " from trash", err) |
| 845 | + return |
| 846 | + end |
| 847 | + |
| 848 | + complete() |
| 849 | + end |
| 850 | + |
| 851 | + local displayed_name = name |
| 852 | + if stat and stat.type == "directory" then |
| 853 | + displayed_name = name .. utils.path_separator |
| 854 | + end |
| 855 | + local msg = string.format("Are you sure you want to restore '%s'?", displayed_name) |
| 856 | + inputs.confirm(msg, function(confirmed) |
| 857 | + if confirmed then |
| 858 | + do_trash() |
| 859 | + end |
| 860 | + end) |
| 861 | +end |
| 862 | + |
| 863 | +---@param paths string[] |
| 864 | +---@param callback fun(paths: string[])? |
| 865 | +---@param state neotree.State? |
| 866 | +M.restore_nodes_from_trash = function(paths, callback, state) |
| 867 | + local msg = "Are you sure you want to restore " .. #paths .. " items?" |
| 868 | + inputs.confirm(msg, function(confirmed) |
| 869 | + if not confirmed then |
| 870 | + return |
| 871 | + end |
| 872 | + |
| 873 | + local success, err = trash.restore(paths) |
| 874 | + if not success then |
| 875 | + log.error(err) |
| 876 | + end |
| 877 | + |
812 | 878 | if callback then |
813 | 879 | vim.schedule(function() |
814 | 880 | callback(paths) |
|
0 commit comments