Skip to content

Commit c0118d2

Browse files
committed
Convert nfnl.fs to nfnl.module/define
1 parent 3e51a6b commit c0118d2

File tree

2 files changed

+59
-71
lines changed

2 files changed

+59
-71
lines changed

fnl/nfnl/fs.fnl

Lines changed: 31 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,64 @@
1-
(local {: autoload} (require :nfnl.module))
1+
(local {: autoload : define} (require :nfnl.module))
22
(local core (autoload :nfnl.core))
33
(local str (autoload :nfnl.string))
44

5-
(fn basename [path]
5+
(local M (define :nfnl.fs))
6+
7+
(fn M.basename [path]
68
"Remove the file part of the path."
79
(when path
810
(vim.fn.fnamemodify path ":h")))
911

10-
(fn filename [path]
12+
(fn M.filename [path]
1113
"Just the filename / tail of a path."
1214
(when path
1315
(vim.fn.fnamemodify path ":t")))
1416

15-
(fn file-name-root [path]
17+
(fn M.file-name-root [path]
1618
"Remove the suffix / extension of the file in a path."
1719
(when path
1820
(vim.fn.fnamemodify path ":r")))
1921

20-
(fn full-path [path]
22+
(fn M.full-path [path]
2123
(when path
2224
(vim.fn.fnamemodify path ":p")))
2325

24-
(fn mkdirp [dir]
26+
(fn M.mkdirp [dir]
2527
(when dir
2628
(vim.fn.mkdir dir "p")))
2729

28-
(fn replace-extension [path ext]
30+
(fn M.replace-extension [path ext]
2931
(when path
30-
(.. (file-name-root path) (.. "." ext))))
32+
(.. (M.file-name-root path) (.. "." ext))))
3133

32-
(fn read-first-line [path]
34+
(fn M.read-first-line [path]
3335
(let [f (io.open path "r")]
3436
(when (and f (not (core.string? f)))
3537
(let [line (f:read "*line")]
3638
(f:close)
3739
line))))
3840

39-
(fn absglob [dir expr]
41+
(fn M.absglob [dir expr]
4042
"Glob all files under dir matching the expression and return the absolute paths."
4143
(vim.fn.globpath dir expr true true))
4244

43-
(fn relglob [dir expr]
45+
(fn M.relglob [dir expr]
4446
"Glob all files under dir matching the expression and return the paths
4547
relative to the dir argument."
4648
(let [dir-len (+ 2 (string.len dir))]
47-
(->> (absglob dir expr)
49+
(->> (M.absglob dir expr)
4850
(core.map #(string.sub $1 dir-len)))))
4951

50-
(fn glob-dir-newer? [a-dir b-dir expr b-dir-path-fn]
51-
"Returns true if a-dir has newer changes than b-dir. All paths from a-dir are mapped through b-dir-path-fn before comparing to b-dir."
52+
(fn M.glob-dir-newer? [a-dir b-dir expr b-dir-path-fn]
53+
"Returns true if a-dir has newer changes than b-dir. All paths from a-dir are mapped through b-dir-path-fn M.before comparing to b-dir."
5254
(var newer? false)
5355
(each [_ path (ipairs (relglob a-dir expr))]
5456
(when (> (vim.fn.getftime (.. a-dir path))
5557
(vim.fn.getftime (.. b-dir (b-dir-path-fn path))))
5658
(set newer? true)))
5759
newer?)
5860

59-
(fn path-sep []
61+
(fn M.path-sep []
6062
;; https://github.com/nvim-lua/plenary.nvim/blob/8bae2c1fadc9ed5bfcfb5ecbd0c0c4d7d40cb974/lua/plenary/path.lua#L20-L31
6163
(let [os (string.lower jit.os)]
6264
(if (or (= :linux os)
@@ -67,53 +69,37 @@
6769
"/"
6870
"\\")))
6971

70-
(fn findfile [name path]
72+
(fn M.findfile [name path]
7173
"Wrapper around Neovim's findfile() that returns nil
7274
instead of an empty string."
7375
(let [res (vim.fn.findfile name path)]
7476
(when (not (core.empty? res))
75-
(full-path res))))
77+
(M.full-path res))))
7678

77-
(fn split-path [path]
78-
(str.split path (path-sep)))
79+
(fn M.split-path [path]
80+
(str.split path (M.path-sep)))
7981

80-
(fn join-path [parts]
81-
(str.join (path-sep) (core.concat parts)))
82+
(fn M.join-path [parts]
83+
(str.join (M.path-sep) (core.concat parts)))
8284

83-
(fn replace-dirs [path from to]
85+
(fn M.replace-dirs [path from to]
8486
"Replaces directories in `path` that match `from` with `to`."
8587
(->> (split-path path)
8688
(core.map
8789
(fn [segment]
8890
(if (= from segment)
8991
to
9092
segment)))
91-
(join-path)))
93+
(M.join-path)))
9294

93-
(fn fnl-path->lua-path [fnl-path]
95+
(fn M.fnl-path->lua-path [fnl-path]
9496
(-> fnl-path
95-
(replace-extension "lua")
96-
(replace-dirs "fnl" "lua")))
97+
(M.replace-extension "lua")
98+
(M.replace-dirs "fnl" "lua")))
9799

98-
(fn glob-matches? [dir expr path]
100+
(fn M.glob-matches? [dir expr path]
99101
"Return true if path matches the glob expression. The path should be absolute and the glob should be relative to dir."
100-
(let [regex (vim.regex (vim.fn.glob2regpat (join-path [dir expr])))]
102+
(let [regex (vim.regex (vim.fn.glob2regpat (M.join-path [dir expr])))]
101103
(regex:match_str path)))
102104

103-
{: basename
104-
: filename
105-
: file-name-root
106-
: full-path
107-
: mkdirp
108-
: replace-extension
109-
: absglob
110-
: relglob
111-
: glob-dir-newer?
112-
: path-sep
113-
: findfile
114-
: split-path
115-
: join-path
116-
: read-first-line
117-
: replace-dirs
118-
: fnl-path->lua-path
119-
: glob-matches?}
105+
M

lua/nfnl/fs.lua

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

0 commit comments

Comments
 (0)