Skip to content

Commit

Permalink
Refactor fs.fnl to use nvim 0.8 builtins
Browse files Browse the repository at this point in the history
  • Loading branch information
saccarosium committed Dec 15, 2024
1 parent b4b9b90 commit d6dcdbc
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 49 deletions.
2 changes: 1 addition & 1 deletion README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ This will temporarily download the plugin, launch Neovim with `:ConjureSchool` r

== Installation

Requires Neovim 0.7 or newer.
Requires Neovim 0.8 or newer.

Alternatively you can use https://github.com/Olical/magic-kit[Magic Kit], an opinionated starter kit that includes all sorts of essential tools.

Expand Down
38 changes: 18 additions & 20 deletions fnl/conjure/fs.fnl
Original file line number Diff line number Diff line change
@@ -1,39 +1,37 @@
(local {: autoload} (require :nfnl.module))
(local nvim (autoload :conjure.aniseed.nvim))
(local a (autoload :conjure.aniseed.core))
(local text (autoload :conjure.text))
(local str (autoload :conjure.aniseed.string))
(local afs (autoload :conjure.aniseed.fs))
(local config (autoload :conjure.config))

(local path-sep (if (= jit.os :Windows) "\\" "/"))

(fn env [k]
(let [v (nvim.fn.getenv k)]
(let [v (vim.fn.getenv k)]
(when (and (a.string? v) (not (a.empty? v)))
v)))

(fn config-dir []
"Return $XDG_CONFIG_HOME/conjure.
Defaulting the config directory to $HOME/.config."
(.. (or (env "XDG_CONFIG_HOME")
(.. (env "HOME") afs.path-sep ".config"))
afs.path-sep "conjure"))
(vim.fs.normalize (if (env "XDG_CONFIG_HOME")
"$XDG_CONFIG_HOME/conjure"
"~/.config/conjure")))

(fn absolute-path [path]
(vim.fn.fnamemodify path ":p"))
(vim.fs.normalize (vim.fn.fnamemodify path ":p")))

(fn findfile [name path]
"Wrapper around Neovim's findfile() that returns nil
instead of an empty string."
(let [res (nvim.fn.findfile name path)]
(let [res (vim.fn.findfile name path)]
(when (not (a.empty? res))
(absolute-path res))))

(fn split-path [path]
(->> (str.split path afs.path-sep)
(a.filter #(not (a.empty? $)))))
(vim.split path path-sep {:trimempty true}))

(fn join-path [parts]
(str.join afs.path-sep (a.concat parts)))
(str.join path-sep (a.concat parts)))

(fn parent-dir [path]
(let [res (-> path
Expand All @@ -42,7 +40,7 @@
(join-path))]
(if (= "" res)
nil
(.. afs.path-sep res))))
(.. path-sep res))))

(fn upwards-file-search [file-names from-dir]
"Given a list of relative filenames and an absolute path to a directory,
Expand All @@ -68,12 +66,12 @@
first file name in the first directory, everything will short circuit and
return that full path."
(or
(upwards-file-search names (nvim.fn.expand "%:p:h"))
(upwards-file-search names (nvim.fn.getcwd))
(upwards-file-search names (vim.fn.expand "%:p:h"))
(upwards-file-search names (vim.fn.getcwd))
(upwards-file-search names (config-dir))))

(fn file-readable? [path]
(= 1 (nvim.fn.filereadable path)))
(= 1 (vim.fn.filereadable path)))

(fn resolve-relative-to [path root]
"Successively remove parts of the path until we get to a relative path that
Expand Down Expand Up @@ -112,7 +110,7 @@

(fn current-source []
(let [info (debug.getinfo 2 "S")]
(when (text.starts-with (a.get info :source) "@")
(when (vim.startswith (a.get info :source) "@")
(string.sub info.source 2))))

(local conjure-source-directory
Expand All @@ -125,10 +123,10 @@
(when file-path
(a.some
(fn [mod-name]
(let [mod-path (string.gsub mod-name "%." afs.path-sep)]
(let [mod-path (string.gsub mod-name "%." path-sep)]
(when (or
(text.ends-with file-path (.. mod-path ".fnl"))
(text.ends-with file-path (.. mod-path "/init.fnl")))
(vim.endswith file-path (.. mod-path ".fnl"))
(vim.endswith file-path (.. mod-path "/init.fnl")))
mod-name)))
(a.keys package.loaded))))

Expand Down
59 changes: 33 additions & 26 deletions lua/conjure/fs.lua

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions plugin/conjure.vim
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
if has("nvim-0.7")
if has("nvim-0.8")
lua require("conjure.main").main()
else
echoerr "Conjure requires Neovim > v0.7"
echoerr "Conjure requires Neovim > v0.8"
endif

0 comments on commit d6dcdbc

Please sign in to comment.