Skip to content

Commit 983e3e3

Browse files
committed
Add a find and delete command for orphan files
1 parent ffe322f commit 983e3e3

File tree

5 files changed

+124
-15
lines changed

5 files changed

+124
-15
lines changed

fnl/nfnl/api.fnl

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,56 @@
11
(local {: autoload : define} (require :nfnl.module))
22
(local core (autoload :nfnl.core))
3+
(local str (autoload :nfnl.string))
34
(local compile (autoload :nfnl.compile))
45
(local config (autoload :nfnl.config))
56
(local notify (autoload :nfnl.notify))
67
(local fs (autoload :nfnl.fs))
8+
(local gc (autoload :nfnl.gc))
79

810
(local M (define :nfnl.api))
911

12+
(fn M.find-orphans []
13+
"Find orphan Lua files that were compiled from a Fennel file that no longer exists. Display them with notify."
14+
(let [dir (vim.fn.getcwd)
15+
{: config : root-dir : cfg} (config.find-and-load dir)]
16+
(if config
17+
(let [orphan-files (gc.find-orphan-lua-files {: root-dir : cfg})]
18+
(if (core.empty? orphan-files)
19+
(notify.info "No orphan files detected.")
20+
(notify.warn
21+
"Orphan files detected, delete them with :NfnlDeleteOrphans.\n"
22+
(->> orphan-files
23+
(core.map
24+
(fn [f]
25+
(.. " - " f)))
26+
(str.join "\n"))))
27+
orphan-files)
28+
(do
29+
(notify.warn "No .nfnl.fnl configuration found.")
30+
[]))))
31+
32+
(fn M.delete-orphans []
33+
"Delete orphan Lua files that were compiled from a Fennel file that no longer exists."
34+
(let [dir (vim.fn.getcwd)
35+
{: config : root-dir : cfg} (config.find-and-load dir)]
36+
(if config
37+
(let [orphan-files (gc.find-orphan-lua-files {: root-dir : cfg})]
38+
(if (core.empty? orphan-files)
39+
(notify.info "No orphan files detected.")
40+
(do
41+
(notify.info
42+
"Deleting orphan files:\n"
43+
(->> orphan-files
44+
(core.map
45+
(fn [f]
46+
(.. " - " f)))
47+
(str.join "\n")))
48+
(core.map os.remove orphan-files)))
49+
orphan-files)
50+
(do
51+
(notify.warn "No .nfnl.fnl configuration found.")
52+
[]))))
53+
1054
(fn M.compile-file [{: path : dir}]
1155
"Compiles a file into the matching Lua file. Returns the compilation result. Takes an optional `dir` key that changes the working directory.
1256

fnl/nfnl/callback.fnl

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,19 @@
7777
{:desc "Executes (nfnl.api/compile-all-files) which will, you guessed it, compile all of your files."
7878
:force true
7979
:complete "file"
80-
:nargs "?"}))))))
80+
:nargs "?"})
81+
82+
(vim.api.nvim_buf_create_user_command
83+
ev.buf :NfnlFindOrphans
84+
#(api.find-orphans)
85+
{:desc "Executes (nfnl.api/find-orphans) which will find and display all Lua files that no longer have a matching Fennel file."
86+
:force true})
87+
88+
(vim.api.nvim_buf_create_user_command
89+
ev.buf :NfnlDeleteOrphans
90+
#(api.delete-orphans)
91+
{:desc "Executes (nfnl.api/delete-orphans) deletes any orphan Lua files that no longer have their original Fennel file they were compiled from."
92+
:force true}))))))
8193

8294
{: fennel-filetype-callback
8395
: supported-path?}

lua/nfnl/api.lua

Lines changed: 58 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lua/nfnl/callback.lua

Lines changed: 9 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lua/nfnl/foo.lua

Lines changed: 0 additions & 2 deletions
This file was deleted.

0 commit comments

Comments
 (0)