@@ -20,6 +20,10 @@ local clipboard = {
2020 copy = {},
2121}
2222
23+ --- @param source string
24+ --- @param destination string
25+ --- @return boolean
26+ --- @return string | nil
2327local function do_copy (source , destination )
2428 local source_stats , handle
2529 local success , errmsg
@@ -81,6 +85,12 @@ local function do_copy(source, destination)
8185 return true
8286end
8387
88+ --- @param source string
89+ --- @param dest string
90+ --- @param action_type string
91+ --- @param action_fn fun ( source : string , dest : string )
92+ --- @return boolean | nil -- success
93+ --- @return string | nil -- error message
8494local function do_single_paste (source , dest , action_type , action_fn )
8595 local dest_stats
8696 local success , errmsg , errcode
@@ -140,14 +150,17 @@ local function do_single_paste(source, dest, action_type, action_fn)
140150 end
141151end
142152
153+ --- @param node Node
154+ --- @param clip table
143155local function toggle (node , clip )
144156 if node .name == " .." then
145157 return
146158 end
147159 local notify_node = notify .render_path (node .absolute_path )
148160
149161 if utils .array_remove (clip , node ) then
150- return notify .info (notify_node .. " removed from clipboard." )
162+ notify .info (notify_node .. " removed from clipboard." )
163+ return
151164 end
152165
153166 table.insert (clip , node )
@@ -161,22 +174,28 @@ function M.clear_clipboard()
161174 renderer .draw ()
162175end
163176
177+ --- @param node Node
164178function M .copy (node )
165179 utils .array_remove (clipboard .cut , node )
166180 toggle (node , clipboard .copy )
167181 renderer .draw ()
168182end
169183
184+ --- @param node Node
170185function M .cut (node )
171186 utils .array_remove (clipboard .copy , node )
172187 toggle (node , clipboard .cut )
173188 renderer .draw ()
174189end
175190
191+ --- @param node Node
192+ --- @param action_type string
193+ --- @param action_fn fun ( source : string , dest : string )
176194local function do_paste (node , action_type , action_fn )
177195 node = lib .get_last_group_node (node )
178- if node .name == " .." then
179- node = core .get_explorer ()
196+ local explorer = core .get_explorer ()
197+ if node .name == " .." and explorer then
198+ node = explorer
180199 end
181200 local clip = clipboard [action_type ]
182201 if # clip == 0 then
@@ -202,10 +221,14 @@ local function do_paste(node, action_type, action_fn)
202221
203222 clipboard [action_type ] = {}
204223 if not M .config .filesystem_watchers .enable then
205- return reloaders .reload_explorer ()
224+ reloaders .reload_explorer ()
206225 end
207226end
208227
228+ --- @param source string
229+ --- @param destination string
230+ --- @return boolean
231+ --- @return string ?
209232local function do_cut (source , destination )
210233 log .line (" copy_paste" , " do_cut '%s' -> '%s'" , source , destination )
211234
@@ -225,12 +248,13 @@ local function do_cut(source, destination)
225248 return true
226249end
227250
251+ --- @param node Node
228252function M .paste (node )
229253 if clipboard .cut [1 ] ~= nil then
230- return do_paste (node , " cut" , do_cut )
254+ do_paste (node , " cut" , do_cut )
255+ else
256+ do_paste (node , " copy" , do_copy )
231257 end
232-
233- return do_paste (node , " copy" , do_copy )
234258end
235259
236260function M .print_clipboard ()
@@ -248,9 +272,10 @@ function M.print_clipboard()
248272 end
249273 end
250274
251- return notify .info (table.concat (content , " \n " ) .. " \n " )
275+ notify .info (table.concat (content , " \n " ) .. " \n " )
252276end
253277
278+ --- @param content string
254279local function copy_to_clipboard (content )
255280 local clipboard_name
256281 if M .config .actions .use_system_clipboard == true then
@@ -264,28 +289,36 @@ local function copy_to_clipboard(content)
264289 end
265290
266291 vim .api .nvim_exec_autocmds (" TextYankPost" , {})
267- return notify .info (string.format (" Copied %s to %s clipboard!" , content , clipboard_name ))
292+ notify .info (string.format (" Copied %s to %s clipboard!" , content , clipboard_name ))
268293end
269294
295+ --- @param node Node
270296function M .copy_filename (node )
271- return copy_to_clipboard (node .name )
297+ copy_to_clipboard (node .name )
272298end
273299
300+ --- @param node Node
274301function M .copy_path (node )
275302 local absolute_path = node .absolute_path
276- local relative_path = utils .path_relative (absolute_path , core .get_cwd ())
303+ local cwd = core .get_cwd ()
304+ if cwd == nil then
305+ return
306+ end
307+
308+ local relative_path = utils .path_relative (absolute_path , cwd )
277309 local content = node .nodes ~= nil and utils .path_add_trailing (relative_path ) or relative_path
278- return copy_to_clipboard (content )
310+ copy_to_clipboard (content )
279311end
280312
313+ --- @param node Node
281314function M .copy_absolute_path (node )
282315 local absolute_path = node .absolute_path
283316 local content = node .nodes ~= nil and utils .path_add_trailing (absolute_path ) or absolute_path
284- return copy_to_clipboard (content )
317+ copy_to_clipboard (content )
285318end
286319
287- --- Clipboard text highlight group and position when highlight_clipboard.
288- --- @param node table
320+ --- Clipboard text highlight group and position when highlight_clipboard.
321+ --- @param node Node
289322--- @return HL_POSITION position none when clipboard empty
290323--- @return string | nil group only when node present in clipboard
291324function M .get_highlight (node )
0 commit comments