1- --- A backend for the clipboard that uses a file in stdpath('state')/neo-tree.nvim/clipboards/ to sync the clipboard
2- --- .. self.filename
3- --- between everything
1+ --- A backend for the clipboard that uses a file in stdpath('state')/neo-tree.nvim/clipboards/ .. self.filename
2+ --- to sync the clipboard between everything.
43local BaseBackend = require (" neo-tree.clipboard.sync.base" )
54local log = require (" neo-tree.log" )
65local uv = vim .uv or vim .loop
@@ -13,9 +12,10 @@ local uv = vim.uv or vim.loop
1312local clipboard_states_dir = vim .fn .stdpath (" state" ) .. " /neo-tree.nvim/clipboards"
1413local pid = vim .uv .os_getpid ()
1514
16- --- @class neotree.clipboard.FileBackend.FileFormat
15+ --- @class ( exact ) neotree.clipboard.FileBackend.FileFormat
1716--- @field pid integer
1817--- @field time integer
18+ --- @field state_name string
1919--- @field contents neotree.clipboard.Contents
2020
2121--- @class neotree.clipboard.FileBackend : neotree.clipboard.Backend
@@ -24,7 +24,7 @@ local pid = vim.uv.os_getpid()
2424--- @field source string
2525--- @field pid integer
2626--- @field cached_contents neotree.clipboard.Contents
27- --- @field last_time_saved neotree.clipboard.Contents
27+ --- @field last_time_saved integer
2828--- @field saving boolean
2929local FileBackend = BaseBackend :new ()
3030
@@ -36,9 +36,9 @@ local function file_touch(filename)
3636 return true
3737 end
3838 local dir = vim .fn .fnamemodify (filename , " :h" )
39- local code = vim .fn .mkdir (dir , " p" )
40- if code ~= 1 then
41- return false , " couldn't make dir" .. dir
39+ local mkdir_ok = vim .fn .mkdir (dir , " p" )
40+ if mkdir_ok == 0 then
41+ return false , " couldn't make dir " .. dir
4242 end
4343 local file , file_err = io.open (filename , " a+" )
4444 if not file then
@@ -93,6 +93,9 @@ function FileBackend:_start()
9393 if event_handle then
9494 self .handle = event_handle
9595 local start_success = event_handle :start (self .filename , {}, function (err , _ , fs_events )
96+ local write_time = uv .fs_stat (self .filename ).mtime .nsec
97+ if self .last_time_saved == write_time then
98+ end
9699 if err then
97100 event_handle :close ()
98101 return
@@ -118,6 +121,7 @@ local validate_clipboard_from_file = function(wrapped_clipboard)
118121 validate (" contents" , c .contents , " table" )
119122 validate (" pid" , c .pid , " number" )
120123 validate (" time" , c .time , " number" )
124+ validate (" state_name" , c .state_name , " string" )
121125 end , false , " Clipboard from file could not be validated" )
122126end
123127
@@ -147,7 +151,23 @@ function FileBackend:load(state)
147151 end
148152
149153 if not validate_clipboard_from_file (clipboard_file ) then
150- return nil , " could not validate clipboard from file"
154+ if
155+ require (" neo-tree.ui.inputs" ).confirm (
156+ " Neo-tree clipboard file seems invalid, clear out clipboard?"
157+ )
158+ then
159+ local success , delete_err = os.remove (self .filename )
160+ if not success then
161+ log .error (delete_err )
162+ end
163+
164+ -- try creating a new file without content
165+ state .clipboard = {}
166+ self :save (state )
167+ -- clear the current clipboard
168+ return {}
169+ end
170+ return nil , " could not parse a valid clipboard from clipboard file"
151171 end
152172
153173 return clipboard_file .contents
@@ -163,14 +183,16 @@ function FileBackend:save(state)
163183 local wrapped = {
164184 pid = pid ,
165185 time = os.time (),
186+ state_name = assert (state .name ),
166187 contents = c ,
167188 }
168189 if not file_touch (self .filename ) then
169190 return false , " couldn't write to " .. self .filename .. self .filename
170191 end
171192 local encode_ok , str = pcall (vim .json .encode , wrapped )
172193 if not encode_ok then
173- return false , " couldn't encode clipboard into json"
194+ local encode_err = str
195+ return false , " couldn't encode clipboard into json: " .. encode_err
174196 end
175197 local file , err = io.open (self .filename , " w" )
176198 if not file or err then
@@ -182,6 +204,7 @@ function FileBackend:save(state)
182204 end
183205 file :flush ()
184206 file :close ()
207+ self .last_time_saved = uv .fs_stat (self .filename ).mtime .nsec
185208 return true
186209end
187210
0 commit comments