Skip to content

Commit fa0fe82

Browse files
committed
docs and bugfixes
1 parent 137068e commit fa0fe82

File tree

2 files changed

+44
-20
lines changed

2 files changed

+44
-20
lines changed

doc/neo-tree.txt

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1913,6 +1913,17 @@ state to a string. The colors of the popup border are controlled by the
19131913
highlight group.
19141914

19151915

1916+
CLIPBOARD *neo-tree-clipboard*
1917+
1918+
Neo-tree's clipboard can be synced globally (within the same Neovim instance) or
1919+
universally (across multiple Neovim instances). The default is to not sync at
1920+
all. To change this option, change the `clipboard.sync` option (options are
1921+
`"none"|"global"|"universal"`). The universal sync option relies on a file
1922+
located under `stdpath("state") .. "/neo-tree.nvim/clipboards"` You can also
1923+
implement your own backend and pass it to that option as well - reading the
1924+
source code of `require('neo-tree.clipboard')` is a good way to do it.
1925+
1926+
19161927
================================================================================
19171928
OTHER SOURCES ~
19181929
================================================================================
@@ -2014,14 +2025,4 @@ Currently, this source supports the following commands:
20142025
["P"] = "preview", (and related commands)
20152026
["s"] = "split", (and related commands)
20162027
<
2017-
2018-
CLIPBOARD *neo-tree-clipboard*
2019-
2020-
Neo-tree's clipboard can be synced globally (within the same Neovim instance) or
2021-
universally (across multiple Neovim instances). The default is to not sync at
2022-
all. To change this option, change the `clipboard.sync` option (options are
2023-
`"none"|"global"|"universal"`). You can also implement your own backend and pass
2024-
it to that option as well - reading the source code at `lua/neo-tree/clipboard`
2025-
is a good way to do it.
2026-
20272028
vim:tw=80:ts=2:et:ft=help:

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

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
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.
43
local BaseBackend = require("neo-tree.clipboard.sync.base")
54
local log = require("neo-tree.log")
65
local uv = vim.uv or vim.loop
@@ -13,9 +12,10 @@ local uv = vim.uv or vim.loop
1312
local clipboard_states_dir = vim.fn.stdpath("state") .. "/neo-tree.nvim/clipboards"
1413
local 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
2929
local 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")
122126
end
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
186209
end
187210

0 commit comments

Comments
 (0)