Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert tree-sitter.fnl, timer.fnl, text.fnl, stack.fnl, and tests #597

Merged
merged 3 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions fnl/conjure-spec/stack_spec.fnl
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
(local {: describe : it} (require :plenary.busted))
(local assert (require :luassert.assert))
(local stack (require :conjure.stack))

(describe "conjure.stack"
(fn []
(describe "push"
(fn []
(it "item on a stack"
(fn []
(assert.same [1 2 3] (-> (stack.push [] 1)
(stack.push 2)
(stack.push 3)))))))

(describe "pop"
(fn []
(it "top of stack"
(fn []
(assert.same [1 2] (stack.pop [1 2 3]))))

(it "empty stack"
(fn []
(assert.same [] (stack.pop []))))))

(describe "peek"
(fn []
(it "top of stack"
(fn []
(assert.are.equals 3 (stack.peek [1 2 3]))))

(it "empty stack"
(fn []
(assert.are.equals nil (stack.peek []))))
))))
155 changes: 155 additions & 0 deletions fnl/conjure-spec/text_spec.fnl
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
(local {: describe : it} (require :plenary.busted))
(local assert (require :luassert.assert))
(local text (require :conjure.text))

(describe "text"
(fn []
(describe "left-sample"
(fn []
(it "handles empty strings"
(fn []
(assert.are.equals "" (text.left-sample "" 0))))

(it "handles single characters"
(fn []
(assert.are.equals "f" (text.left-sample "f" 1))))

(it "does nothing if correct"
(fn []
(assert.are.equals "foo bar" (text.left-sample "foo bar" 10))))

(it "replaces lots of whitespace with a space"
(fn []
(assert.are.equals "foo bar" (text.left-sample "foo \n\n bar" 10))))

(it "cuts the string if too long"
(fn []
(assert.are.equals "foo bar b..." (text.left-sample "foo \n\n bar \n\n baz" 10))))

(it "trims leading and trailing whitespace"
(fn []
(assert.are.equals "foo bar" (text.left-sample " foo \n \n bar \n" 10))))))

(describe "right-sample"
(fn []
(it "same as left-sample, but we want the right"
(fn []
(assert.are.equals "...o bar baz" (text.right-sample "foo \n\n bar \n\n baz" 10))))))

(describe "split-lines"
(fn []
(it "nothing to nothing"
(fn []
(assert.same [""] (text.split-lines "") "")))

(it "basic split"
(fn []
(assert.same ["foo" "bar"] (text.split-lines "foo\nbar") "")))

(it "blank lines"
(fn []
(assert.same ["foo" "" "bar"] (text.split-lines "foo\n\nbar") "")))

(it "Windows CRLF"
(fn []
(assert.same ["foo" "bar"] (text.split-lines "foo\r\nbar") "")
))))

(describe "prefixed-lines"
(fn []
(it "nothing to nothing"
(fn []
(assert.same ["; "] (text.prefixed-lines "" "; "))))

(it "single line"
(fn []
(assert.same ["; foo"] (text.prefixed-lines "foo" "; "))))

(it "multiple lines"
(fn []
(assert.same ["; foo" "; bar"] (text.prefixed-lines "foo\nbar" "; "))))))

(describe "starts-with"
(fn []
(it "foo"
(fn []
(assert.are.equals true (text.starts-with "foobar" "foo"))))

(it "foob"
(fn []
(assert.are.equals true (text.starts-with "foobar" "foob"))))

(it "foox"
(fn []
(assert.are.equals false (text.starts-with "foobar" "foox"))))

(it "ohno"
(fn []
(assert.are.equals nil (text.starts-with nil "ohno"))))))

(describe "ends-with"
(fn []
(it "bar"
(fn []
(assert.are.equals true (text.ends-with "foobar" "bar"))))

(it "obar"
(fn []
(assert.are.equals true (text.ends-with "foobar" "obar"))))

(it "xbar"
(fn []
(assert.are.equals false (text.ends-with "foobar" "xbar"))))

(it "ohno"
(fn []
(assert.are.equals nil (text.ends-with nil "ohno"))))))

(describe "first-and-last-chars"
(fn []
(it "of parentheses around words"
(fn []
(assert.are.equals "()" (text.first-and-last-chars "(hello-world)"))))
(it "of empty string"
(fn []
(assert.are.equals "" (text.first-and-last-chars ""))))
(it "of single opening parenthesis"
(fn []
(assert.are.equals "(" (text.first-and-last-chars "("))))
(it "of nil"
(fn []
(assert.are.equals nil (text.first-and-last-chars nil))))))

(describe "chars"
(fn []
(it "of nothing"
(fn []
(assert.same [] (text.chars))))
(it "of empty string"
(fn []
(assert.same [] (text.chars ""))))
(it "of \"abc\""
(fn []
(assert.same [:a :b :c] (text.chars "abc"))))))

(describe "upper-first"
(fn []
(it "of empty string"
(fn []
(assert.are.equals "" (text.upper-first ""))))
(it "of \"A\""
(fn []
(assert.are.equals "A" (text.upper-first "A"))))
(it "of \"a\""
(fn []
(assert.are.equals "A" (text.upper-first "a"))))
(it "of first word of many words"
(fn []
(assert.are.equals "Foo bar bAZ 5" (text.upper-first "foo bar bAZ 5"))))
(it "of nil"
(fn []
(assert.are.equals nil (text.upper-first nil))))
(it "of string of numbers"
(fn []
(assert.are.equals "123" (text.upper-first "123"))
))))))
19 changes: 11 additions & 8 deletions fnl/conjure/stack.fnl
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
(import-macros {: module : def : defn : defonce : def- : defn- : defonce- : wrap-last-expr : wrap-module-body : deftest} :nfnl.macros.aniseed)
(local {: autoload} (require :nfnl.module))
(local a (autoload :conjure.aniseed.core))

(module conjure.stack
{autoload {a conjure.aniseed.core}})

(defn push [s v]
(fn push [s v]
(table.insert s v)
s)

(defn pop [s]
(fn pop [s]
(table.remove s)
s)

(defn peek [s]
(fn peek [s]
(a.last s))

*module*
{
: push
: pop
: peek
}

47 changes: 29 additions & 18 deletions fnl/conjure/text.fnl
Original file line number Diff line number Diff line change
@@ -1,30 +1,28 @@
(import-macros {: module : def : defn : defonce : def- : defn- : defonce- : wrap-last-expr : wrap-module-body : deftest} :nfnl.macros.aniseed)
(local {: autoload} (require :nfnl.module))
(local a (autoload :conjure.aniseed.core))
(local str (autoload :conjure.aniseed.string))

(module conjure.text
{require {a conjure.aniseed.core
str conjure.aniseed.string}})

(defn trailing-newline? [s]
(fn trailing-newline? [s]
(string.match s "\r?\n$"))

(defn trim-last-newline [s]
(fn trim-last-newline [s]
(string.gsub s "\r?\n$" ""))

(defn left-sample [s limit]
(fn left-sample [s limit]
(let [flat (-> (string.gsub s "\n" " ")
(string.gsub "%s+" " ")
(str.trim))]
(if (>= limit (a.count flat))
flat
(.. (string.sub flat 0 (a.dec limit)) "..."))))

(defn right-sample [s limit]
(fn right-sample [s limit]
(string.reverse (left-sample (string.reverse s) limit)))

(defn split-lines [s]
(fn split-lines [s]
(str.split s "\r?\n"))

(defn prefixed-lines [s prefix opts]
(fn prefixed-lines [s prefix opts]
(->> (split-lines s)
(a.map-indexed
(fn [[n line]]
Expand All @@ -33,38 +31,51 @@
line
(.. prefix line))))))

(defn starts-with [str start]
(fn starts-with [str start]
(when str
(= (string.sub str 1 (a.count start)) start)))

(defn ends-with [str end]
(fn ends-with [str end]
(when str
(or (= end "") (= end (string.sub str (- (a.count end)))))))

(defn first-and-last-chars [str]
(fn first-and-last-chars [str]
(when str
(if (> (a.count str) 1)
(.. (string.sub str 1 1)
(string.sub str -1 -1))
str)))

(defn strip-ansi-escape-sequences [s]
(fn strip-ansi-escape-sequences [s]
(-> s
(string.gsub "\x1b%[%d+;%d+;%d+;%d+;%d+m" "")
(string.gsub "\x1b%[%d+;%d+;%d+;%d+m" "")
(string.gsub "\x1b%[%d+;%d+;%d+m" "")
(string.gsub "\x1b%[%d+;%d+m" "")
(string.gsub "\x1b%[%d+m" "")))

(defn chars [s]
(fn chars [s]
(local res [])
(when s
(each [c (string.gmatch s ".")]
(table.insert res c)))
res)

(defn upper-first [s]
(fn upper-first [s]
(when s
(s:gsub "^%l" string.upper)))

*module*
{
: trailing-newline?
: trim-last-newline
: left-sample
: right-sample
: split-lines
: prefixed-lines
: starts-with
: ends-with
: first-and-last-chars
: strip-ansi-escape-sequences
: chars
: upper-first
}
18 changes: 10 additions & 8 deletions fnl/conjure/timer.fnl
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
(import-macros {: module : def : defn : defonce : def- : defn- : defonce- : wrap-last-expr : wrap-module-body : deftest} :nfnl.macros.aniseed)
(local {: autoload} (require :nfnl.module))
(local a (autoload :conjure.aniseed.core))
(local nvim (autoload :conjure.aniseed.nvim))

(module conjure.timer
{autoload {a conjure.aniseed.core
nvim conjure.aniseed.nvim}})

(defn defer [f ms]
(fn defer [f ms]
;; vim.loop is deprecated in Neovim 0.10. Use vim.uv instead.
(let [t (vim.loop.new_timer)]
(t:start ms 0 (vim.schedule_wrap f))
t))

(defn destroy [t]
(fn destroy [t]
(when t
(t:stop)
(t:close))
nil)

*module*
{
: defer
: destroy
}
Loading