|
1 | | -(local {: autoload} (require :nfnl.module)) |
| 1 | +(local {: autoload : define} (require :nfnl.module)) |
2 | 2 | (local core (autoload :nfnl.core)) |
3 | 3 | (local str (autoload :nfnl.string)) |
4 | 4 |
|
5 | | -(fn basename [path] |
| 5 | +(local M (define :nfnl.fs)) |
| 6 | + |
| 7 | +(fn M.basename [path] |
6 | 8 | "Remove the file part of the path." |
7 | 9 | (when path |
8 | 10 | (vim.fn.fnamemodify path ":h"))) |
9 | 11 |
|
10 | | -(fn filename [path] |
| 12 | +(fn M.filename [path] |
11 | 13 | "Just the filename / tail of a path." |
12 | 14 | (when path |
13 | 15 | (vim.fn.fnamemodify path ":t"))) |
14 | 16 |
|
15 | | -(fn file-name-root [path] |
| 17 | +(fn M.file-name-root [path] |
16 | 18 | "Remove the suffix / extension of the file in a path." |
17 | 19 | (when path |
18 | 20 | (vim.fn.fnamemodify path ":r"))) |
19 | 21 |
|
20 | | -(fn full-path [path] |
| 22 | +(fn M.full-path [path] |
21 | 23 | (when path |
22 | 24 | (vim.fn.fnamemodify path ":p"))) |
23 | 25 |
|
24 | | -(fn mkdirp [dir] |
| 26 | +(fn M.mkdirp [dir] |
25 | 27 | (when dir |
26 | 28 | (vim.fn.mkdir dir "p"))) |
27 | 29 |
|
28 | | -(fn replace-extension [path ext] |
| 30 | +(fn M.replace-extension [path ext] |
29 | 31 | (when path |
30 | | - (.. (file-name-root path) (.. "." ext)))) |
| 32 | + (.. (M.file-name-root path) (.. "." ext)))) |
31 | 33 |
|
32 | | -(fn read-first-line [path] |
| 34 | +(fn M.read-first-line [path] |
33 | 35 | (let [f (io.open path "r")] |
34 | 36 | (when (and f (not (core.string? f))) |
35 | 37 | (let [line (f:read "*line")] |
36 | 38 | (f:close) |
37 | 39 | line)))) |
38 | 40 |
|
39 | | -(fn absglob [dir expr] |
| 41 | +(fn M.absglob [dir expr] |
40 | 42 | "Glob all files under dir matching the expression and return the absolute paths." |
41 | 43 | (vim.fn.globpath dir expr true true)) |
42 | 44 |
|
43 | | -(fn relglob [dir expr] |
| 45 | +(fn M.relglob [dir expr] |
44 | 46 | "Glob all files under dir matching the expression and return the paths |
45 | 47 | relative to the dir argument." |
46 | 48 | (let [dir-len (+ 2 (string.len dir))] |
47 | | - (->> (absglob dir expr) |
| 49 | + (->> (M.absglob dir expr) |
48 | 50 | (core.map #(string.sub $1 dir-len))))) |
49 | 51 |
|
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." |
52 | 54 | (var newer? false) |
53 | 55 | (each [_ path (ipairs (relglob a-dir expr))] |
54 | 56 | (when (> (vim.fn.getftime (.. a-dir path)) |
55 | 57 | (vim.fn.getftime (.. b-dir (b-dir-path-fn path)))) |
56 | 58 | (set newer? true))) |
57 | 59 | newer?) |
58 | 60 |
|
59 | | -(fn path-sep [] |
| 61 | +(fn M.path-sep [] |
60 | 62 | ;; https://github.com/nvim-lua/plenary.nvim/blob/8bae2c1fadc9ed5bfcfb5ecbd0c0c4d7d40cb974/lua/plenary/path.lua#L20-L31 |
61 | 63 | (let [os (string.lower jit.os)] |
62 | 64 | (if (or (= :linux os) |
|
67 | 69 | "/" |
68 | 70 | "\\"))) |
69 | 71 |
|
70 | | -(fn findfile [name path] |
| 72 | +(fn M.findfile [name path] |
71 | 73 | "Wrapper around Neovim's findfile() that returns nil |
72 | 74 | instead of an empty string." |
73 | 75 | (let [res (vim.fn.findfile name path)] |
74 | 76 | (when (not (core.empty? res)) |
75 | | - (full-path res)))) |
| 77 | + (M.full-path res)))) |
76 | 78 |
|
77 | | -(fn split-path [path] |
78 | | - (str.split path (path-sep))) |
| 79 | +(fn M.split-path [path] |
| 80 | + (str.split path (M.path-sep))) |
79 | 81 |
|
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))) |
82 | 84 |
|
83 | | -(fn replace-dirs [path from to] |
| 85 | +(fn M.replace-dirs [path from to] |
84 | 86 | "Replaces directories in `path` that match `from` with `to`." |
85 | 87 | (->> (split-path path) |
86 | 88 | (core.map |
87 | 89 | (fn [segment] |
88 | 90 | (if (= from segment) |
89 | 91 | to |
90 | 92 | segment))) |
91 | | - (join-path))) |
| 93 | + (M.join-path))) |
92 | 94 |
|
93 | | -(fn fnl-path->lua-path [fnl-path] |
| 95 | +(fn M.fnl-path->lua-path [fnl-path] |
94 | 96 | (-> fnl-path |
95 | | - (replace-extension "lua") |
96 | | - (replace-dirs "fnl" "lua"))) |
| 97 | + (M.replace-extension "lua") |
| 98 | + (M.replace-dirs "fnl" "lua"))) |
97 | 99 |
|
98 | | -(fn glob-matches? [dir expr path] |
| 100 | +(fn M.glob-matches? [dir expr path] |
99 | 101 | "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])))] |
101 | 103 | (regex:match_str path))) |
102 | 104 |
|
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 |
0 commit comments