11--- A backend for the clipboard that uses a file in stdpath('state')/neo-tree.nvim/clipboards/ .. self.filename
22--- to sync the clipboard between everything.
33local BaseBackend = require (" neo-tree.clipboard.sync.base" )
4- local log = require (" neo-tree.log" )
4+ local log = require (" neo-tree.log" ). new ( " clipboard.sync.universal " )
55local uv = vim .uv or vim .loop
66
77--- @class neotree.clipboard.FileBackend.Opts
@@ -14,7 +14,6 @@ local pid = uv.os_getpid()
1414
1515--- @class (exact ) neotree.clipboard.FileBackend.FileFormat
1616--- @field pid integer
17- --- @field time integer
1817--- @field state_name string
1918--- @field contents neotree.clipboard.Contents
2019
@@ -26,7 +25,7 @@ local pid = uv.os_getpid()
2625--- @field cached_contents neotree.clipboard.Contents
2726--- @field last_time_saved integer
2827--- @field saving boolean
29- local FileBackend = BaseBackend :new ()
28+ local UniversalBackend = BaseBackend :new ()
3029
3130--- @param filename string
3231--- @return boolean created
5756
5857--- @param opts neotree.clipboard.FileBackend.Opts ?
5958--- @return neotree.clipboard.FileBackend ?
60- function FileBackend :new (opts )
59+ function UniversalBackend :new (opts )
6160 local backend = {} -- create object if user does not provide one
6261 setmetatable (backend , self )
6362 self .__index = self
@@ -85,28 +84,26 @@ function FileBackend:new(opts)
8584end
8685
8786--- @return boolean started true if working
88- function FileBackend :_start ()
87+ function UniversalBackend :_start ()
8988 if self .handle then
9089 return true
9190 end
9291 local event_handle = uv .new_fs_event ()
9392 if event_handle then
9493 self .handle = event_handle
95- 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
94+ local start_success = event_handle :start (self .filename , {}, function (err )
9995 if err then
96+ log .error (" universal clipboard file handle error:" , err )
10097 event_handle :close ()
10198 return
10299 end
103100 require (" neo-tree.clipboard" ).sync_to_clipboards ()
104101 -- we should check whether we just wrote or not
105102 end )
106- log .info (" Watching " .. self .filename )
103+ log .info (" Watching" , self .filename )
107104 return start_success == 0
108105 else
109- log .warn (" could not watch shared clipboard on file events" )
106+ log .warn (" Could not watch shared clipboard on file events" )
110107 -- todo: implement polling?
111108 end
112109 return false
@@ -120,12 +117,11 @@ local validate_clipboard_from_file = function(wrapped_clipboard)
120117 return validate (" clipboard_from_file" , wrapped_clipboard , function (c )
121118 validate (" contents" , c .contents , " table" )
122119 validate (" pid" , c .pid , " number" )
123- validate (" time" , c .time , " number" )
124120 validate (" state_name" , c .state_name , " string" )
125121 end , false , " Clipboard from file could not be validated" )
126122end
127123
128- function FileBackend :load (state )
124+ function UniversalBackend :load (state )
129125 if state .name ~= " filesystem" then
130126 return nil , nil
131127 end
@@ -163,49 +159,50 @@ function FileBackend:load(state)
163159
164160 -- try creating a new file without content
165161 state .clipboard = {}
166- self :save (state )
162+ local ok , save_err = self :save (state )
163+ if ok == false then
164+ log .error (save_err )
165+ end
167166 -- clear the current clipboard
168167 return {}
169168 end
170- return nil , " could not parse a valid clipboard from clipboard file"
169+ return nil , " Could not parse a valid clipboard from clipboard file"
171170 end
172171
173172 return clipboard_file .contents
174173end
175174
176- function FileBackend :save (state )
175+ function UniversalBackend :save (state )
177176 if state .name ~= " filesystem" then
178177 return nil
179178 end
180179
181- local c = state .clipboard
182180 --- @type neotree.clipboard.FileBackend.FileFormat
183181 local wrapped = {
184182 pid = pid ,
185- time = os.time (),
186183 state_name = assert (state .name ),
187- contents = c ,
184+ contents = state . clipboard ,
188185 }
189186 if not file_touch (self .filename ) then
190- return false , " couldn 't write to " .. self .filename .. self .filename
187+ return false , " Couldn 't write to " .. self .filename .. self .filename
191188 end
192189 local encode_ok , str = pcall (vim .json .encode , wrapped )
193190 if not encode_ok then
194191 local encode_err = str
195- return false , " couldn 't encode clipboard into json: " .. encode_err
192+ return false , " Couldn 't encode clipboard into json: " .. encode_err
196193 end
197194 local file , err = io.open (self .filename , " w" )
198195 if not file or err then
199- return false , " couldn 't open " .. self .filename
196+ return false , " Couldn 't open " .. self .filename
200197 end
201198 local _ , write_err = file :write (str )
202199 if write_err then
203- return false , " couldn 't write to " .. self .filename
200+ return false , " Couldn 't write to " .. self .filename
204201 end
205202 file :flush ()
206- file :close ()
207203 self .last_time_saved = uv .fs_stat (self .filename ).mtime .nsec
204+ self .last_clipboard_saved = state .clipboard
208205 return true
209206end
210207
211- return FileBackend
208+ return UniversalBackend
0 commit comments