Skip to content

Commit 98e8faa

Browse files
committed
update
1 parent 8fefe28 commit 98e8faa

File tree

4 files changed

+44
-56
lines changed

4 files changed

+44
-56
lines changed

lua/neo-tree/clipboard/init.lua

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
local events = require("neo-tree.events")
22
local manager = require("neo-tree.sources.manager")
33
local log = require("neo-tree.log")
4+
local renderer = require("neo-tree.ui.renderer")
45

56
local M = {}
67

@@ -38,17 +39,15 @@ M.setup = function(opts)
3839
log.error("invalid clipboard sync method, disabling sync")
3940
selected_backend = builtins.none
4041
end
41-
M.current_backend = assert(selected_backend:new())
42+
M.current_backend = log.assert(selected_backend:new())
4243

4344
events.subscribe({
4445
event = events.STATE_CREATED,
4546
---@param new_state neotree.State
4647
handler = function(new_state)
4748
local clipboard, err = M.current_backend:load(new_state)
4849
if not clipboard then
49-
if err then
50-
log.error(err)
51-
end
50+
log.assert(not err, err)
5251
return
5352
end
5453
new_state.clipboard = clipboard
@@ -73,7 +72,7 @@ function M.sync_to_clipboards(exclude_state)
7372
-- try loading the changed clipboard into all other states
7473
vim.schedule(function()
7574
manager._for_each_state(nil, function(state)
76-
if exclude_state == state then
75+
if state == exclude_state then
7776
return
7877
end
7978
local modified_clipboard, err = M.current_backend:load(state)
@@ -84,6 +83,7 @@ function M.sync_to_clipboards(exclude_state)
8483
return
8584
end
8685
state.clipboard = modified_clipboard
86+
renderer.redraw(state)
8787
end)
8888
end)
8989
end

lua/neo-tree/clipboard/sync/base.lua

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ local Backend = {}
77

88
---@alias neotree.clipboard.Contents table<string, neotree.clipboard.Node?>
99

10+
---A backend has the responsibility of storing a single instance of a clipboard for other clipboards to save.
1011
---@return neotree.clipboard.Backend?
1112
function Backend:new()
1213
local o = {}
@@ -15,15 +16,15 @@ function Backend:new()
1516
return o
1617
end
1718

18-
---Loads the clipboard from the backend
19-
---Return a nil clipboard to not make any changes.
19+
---Given a particular state, determines whether the backend should load its saved clipboard into the state.
20+
---Return nil if no clipboard change should be made
2021
---@param state neotree.State
21-
---@return neotree.clipboard.Contents|false? clipboard
22+
---@return neotree.clipboard.Contents? clipboard_or_nil
2223
---@return string? err
2324
function Backend:load(state) end
2425

25-
---Writes the clipboard to the backend
26-
---Returns nil when nothing was saved
26+
---Saves a state's clipboard to the backend.
27+
---Returns nil when the save is not applicable.
2728
---@param state neotree.State
2829
---@return boolean? success_or_noop
2930
function Backend:save(state)

lua/neo-tree/clipboard/sync/universal.lua

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
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.
33
local 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")
55
local 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
@@ -57,7 +56,7 @@ end
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)
8584
end
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")
126122
end
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
174173
end
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
209206
end
210207

211-
return FileBackend
208+
return UniversalBackend

lua/neo-tree/log.lua

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ log_maker.new = function(config)
9595
local log = {}
9696
---@diagnostic disable-next-line: cast-local-type
9797
config = vim.tbl_deep_extend("force", default_config, config)
98+
local prefix = table.concat(config.context, ".")
9899

99100
local title_opts = { title = config.plugin_short }
100101
---@param message string
@@ -116,17 +117,9 @@ log_maker.new = function(config)
116117

117118
local initial_filepath = string.format("%s/%s.log", vim.fn.stdpath("data"), config.plugin)
118119

119-
---@type file*?
120-
log.file = nil
121-
if config.use_file then
122-
log.use_file(initial_filepath)
123-
end
124-
125120
local last_logfile_check_time = 0
126-
local current_logfile_inode = -1
127121
local logfile_check_interval = 20 -- TODO: probably use filesystem events rather than this
128122
local inspect_opts = { depth = 2, newline = " " }
129-
local prefix = table.concat(config.context, ".")
130123
---@param log_type string
131124
---@param msg string
132125
local log_to_file = function(log_type, msg)
@@ -188,7 +181,6 @@ log_maker.new = function(config)
188181
local logfunc = function(log_level, message_maker)
189182
local can_log_to_file = log_level >= log.minimum_level.file
190183
local can_log_to_console = log_level >= log.minimum_level.console
191-
local log_verbose = vim.env.NEOTREE_TESTING == "true"
192184
if not can_log_to_file and not can_log_to_console then
193185
return function() end
194186
end
@@ -211,12 +203,7 @@ log_maker.new = function(config)
211203

212204
-- Output to console
213205
if config.use_console and can_log_to_console then
214-
local info = debug.getinfo(2, "Sl")
215206
vim.schedule(function()
216-
if log_verbose then
217-
local lineinfo = info.short_src .. ":" .. info.currentline
218-
msg = lineinfo .. msg
219-
end
220207
notify(msg, log_level)
221208
end)
222209
end
@@ -309,6 +296,7 @@ log_maker.new = function(config)
309296

310297
log.set_level(config.level)
311298

299+
local current_logfile_inode = -1
312300
---@param file string|boolean
313301
---@param quiet boolean?
314302
---@return boolean using_file
@@ -357,6 +345,12 @@ log_maker.new = function(config)
357345
return config.use_file
358346
end
359347

348+
---@type file*?
349+
log.file = nil
350+
if config.use_file then
351+
log.use_file(initial_filepath, true)
352+
end
353+
360354
---Quick wrapper around assert that also supports subsequent args being the same as string.format (to reduce work done on happy paths)
361355
---@see string.format
362356
---@generic T
@@ -385,13 +379,9 @@ log_maker.new = function(config)
385379
---@param context string
386380
log.new = function(context)
387381
local new_context = vim.deepcopy(config.context)
388-
return log_maker.new(
389-
vim.tbl_deep_extend(
390-
"force",
391-
config,
392-
{ context = vim.list_extend({ new_context }, { context }) }
393-
)
394-
)
382+
local new_config =
383+
vim.tbl_deep_extend("force", config, { context = vim.list_extend(new_context, { context }) })
384+
return log_maker.new(new_config)
395385
end
396386

397387
return log

0 commit comments

Comments
 (0)