|
| 1 | +local fs_watch = require("neo-tree.sources.filesystem.lib.fs_watch") |
| 2 | +local manager = require("neo-tree.sources.manager") |
| 3 | +local events = require("neo-tree.events") |
| 4 | +local renderer = require("neo-tree.ui.renderer") |
| 5 | + |
| 6 | +local M = {} |
| 7 | + |
| 8 | +local clipboard_state_dir_path = vim.fn.stdpath("state") .. "/neo-tree/" |
| 9 | +local clipboard_file_path = clipboard_state_dir_path .. "filesystem-clipboard.json" |
| 10 | +local clipboard_file_last_mtime = nil |
| 11 | + |
| 12 | + |
| 13 | +M.save_clipboard = function(clipboard) |
| 14 | + local file = io.open(clipboard_file_path, "w+") |
| 15 | + -- We want to erase data in the file if clipboard is nil instead writing null |
| 16 | + if not clipboard or not file then |
| 17 | + return |
| 18 | + end |
| 19 | + |
| 20 | + file:write(vim.json.encode(clipboard)) |
| 21 | + file:flush() |
| 22 | + clipboard_file_last_mtime = vim.uv.fs_stat(clipboard_file_path).mtime |
| 23 | +end |
| 24 | + |
| 25 | +M._load_clipboard = function() |
| 26 | + local file = io.open(clipboard_file_path, "r") |
| 27 | + if not file then |
| 28 | + return nil |
| 29 | + end |
| 30 | + local content = file:read("*a") |
| 31 | + local is_success, clipboard = pcall(vim.json.decode, content) |
| 32 | + if not is_success then |
| 33 | + return nil |
| 34 | + end |
| 35 | + return clipboard |
| 36 | +end |
| 37 | + |
| 38 | +M._update_cilpboard = function() |
| 39 | + if not vim.fn.filereadable(clipboard_file_path) then |
| 40 | + return |
| 41 | + end |
| 42 | + local cur_mtime = vim.uv.fs_stat(clipboard_file_path).mtime |
| 43 | + if |
| 44 | + not clipboard_file_last_mtime |
| 45 | + -- We need exactly >= because it allows us to synchronize the clipboard |
| 46 | + -- even with trees openned in another window (in the current Neovim instance) |
| 47 | + or M._is_left_mtime_greater_or_equal(cur_mtime, clipboard_file_last_mtime) |
| 48 | + then |
| 49 | + local clipboard = M._load_clipboard() |
| 50 | + manager._for_each_state("filesystem", function(state) |
| 51 | + state.clipboard = clipboard |
| 52 | + vim.schedule(function() |
| 53 | + renderer.redraw(state) |
| 54 | + end) |
| 55 | + end) |
| 56 | + clipboard_file_last_mtime = cur_mtime |
| 57 | + end |
| 58 | +end |
| 59 | + |
| 60 | +M._is_left_mtime_greater_or_equal = function(a, b) |
| 61 | + if a.sec > b.sec then |
| 62 | + return true |
| 63 | + elseif a.sec == b.sec and a.nsec >= b.nsec then |
| 64 | + return true |
| 65 | + end |
| 66 | + return false |
| 67 | +end |
| 68 | + |
| 69 | +M.init = function() |
| 70 | + if vim.fn.isdirectory(clipboard_state_dir_path) == 0 then |
| 71 | + vim.fn.mkdir(clipboard_state_dir_path) |
| 72 | + end |
| 73 | + |
| 74 | + events.subscribe({ |
| 75 | + event = events.STATE_CREATED, |
| 76 | + handler = function(state) |
| 77 | + if state.name ~= "filesystem" then |
| 78 | + return |
| 79 | + end |
| 80 | + vim.schedule(function() |
| 81 | + M._update_cilpboard() |
| 82 | + end) |
| 83 | + end, |
| 84 | + }) |
| 85 | + |
| 86 | + -- Using watch_folder because we haven't "watch_file" :) |
| 87 | + fs_watch.watch_folder(clipboard_state_dir_path, function() |
| 88 | + M._update_cilpboard() |
| 89 | + end, true) |
| 90 | +end |
| 91 | + |
| 92 | +return M |
0 commit comments