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

More conversions #600

Merged
merged 14 commits into from
Aug 30, 2024
2 changes: 2 additions & 0 deletions dev/python/sandbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ def fn_with_multiline_str():
fn_with_multiline_str()


"""
async def slow_fn():
return "slow_fn result, this is async!"

Expand All @@ -66,6 +67,7 @@ async def capture():

await capture()
result
"""


import csv
Expand Down
21 changes: 21 additions & 0 deletions fnl/conjure-spec/remote/stdio_spec.fnl
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
(local {: describe : it} (require :plenary.busted))
(local assert (require :luassert.assert))
(local a (require :nfnl.core))
(local stdio (require :conjure.remote.stdio))

(describe "conjure.remote.stdio"
(fn []
(describe "parse-cmd"
(fn []
(it "parses a string"
(fn []
(assert.same {:cmd "foo" :args []} (stdio.parse-cmd "foo"))))
(it "parses a list of one string"
(fn []
(assert.same {:cmd "foo" :args []} (stdio.parse-cmd ["foo"]))))
(it "parses a string with words separated by spaces"
(fn []
(assert.same {:cmd "foo" :args ["bar" "baz"]} (stdio.parse-cmd "foo bar baz"))))
(it "parses a list of more than one string"
(fn []
(assert.same {:cmd "foo" :args ["bar" "baz"]} (stdio.parse-cmd ["foo" "bar" "baz"]))))))))
17 changes: 17 additions & 0 deletions fnl/conjure-spec/remote/transport/base64_spec.fnl
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
(local {: describe : it} (require :plenary.busted))
(local assert (require :luassert.assert))
(local b64 (require :conjure.remote.transport.base64))

(describe "conjure.remote.transport.base64"
(fn []
(describe "basic"
(fn []
(it "empty to empty"
(fn []
(assert.are.equals "" (b64.encode ""))))
(it "simple text to base64"
(fn []
(assert.are.equals "SGVsbG8sIFdvcmxkIQ==" (b64.encode "Hello, World!"))))
(it "base64 back to text"
(fn []
(assert.are.equals "Hello, World!" (b64.decode "SGVsbG8sIFdvcmxkIQ=="))))))))
68 changes: 68 additions & 0 deletions fnl/conjure-spec/remote/transport/bencode_spec.fnl
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
(local {: describe : it} (require :plenary.busted))
(local assert (require :luassert.assert))
(local a (require :nfnl.core))
(local bencode (require :conjure.remote.transport.bencode))

(describe "conjure.remote.transport.bencode"
(fn []
(describe "basic"
(fn []
(let [bs (bencode.new)
data {:foo [:bar]}]
(it "data starts empty"
(fn []
(assert.are.equals bs.data "")))
(it "a single bencoded value"
(fn []
(assert.same [data] (bencode.decode-all bs (bencode.encode data)))))
(it "data is empty after a decode"
(fn []
(assert.are.equals bs.data ""))))))

(describe "multiple-values"
(fn []
(let [bs (bencode.new)
data-a {:foo [:bar]}
data-b [1 2 3]]
(it "data starts empty"
(fn []
(assert.are.equals bs.data "")))
(it "two bencoded values"
(fn []
(assert.same [data-a data-b]
(bencode.decode-all
bs
(.. (bencode.encode data-a)
(bencode.encode data-b))))))
(it "data is empty after a decode"
(fn []
(assert.are.equals bs.data ""))))))

(describe "partial-values"
(fn []
(let [bs (bencode.new)
data-a {:foo [:bar]}
data-b [1 2 3]
encoded-b (bencode.encode data-b)]
(it "data starts empty"
(fn []
(assert.are.equals bs.data "")))
(it "first value"
(fn []
(assert.same [data-a]
(bencode.decode-all
bs
(.. (bencode.encode data-a)
(string.sub encoded-b 1 3))))))
(it "after first, data contains partial data-b"
(fn []
(assert.are.equals "li1" bs.data)))
(it "second value after rest of data"
(fn []
(assert.same [data-b]
(bencode.decode-all
bs
(string.sub encoded-b 4)))))
(it "data is empty after a decode"
(fn []
(assert.are.equals bs.data ""))))))))
80 changes: 43 additions & 37 deletions fnl/conjure/client/fennel/stdio.fnl
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
(import-macros {: module : def : defn : defonce : def- : defn- : defonce- : wrap-last-expr : wrap-module-body : deftest} :nfnl.macros.aniseed)

(module conjure.client.fennel.stdio
{autoload {a conjure.aniseed.core
str conjure.aniseed.string
nvim conjure.aniseed.nvim
stdio conjure.remote.stdio
afs conjure.aniseed.fs
config conjure.config
text conjure.text
mapping conjure.mapping
client conjure.client
log conjure.log
ts conjure.tree-sitter}
require-macros [conjure.macros]})
(local {: autoload} (require :nfnl.module))
(local a (autoload :conjure.aniseed.core))
(local afs (autoload :conjure.aniseed.fs))
(local str (autoload :conjure.aniseed.string))
(local stdio (autoload :conjure.remote.stdio))
(local config (autoload :conjure.config))
(local text (autoload :conjure.text))
(local mapping (autoload :conjure.mapping))
(local client (autoload :conjure.client))
(local log (autoload :conjure.log))
(local ts (autoload :conjure.tree-sitter))

(config.merge
{:client
Expand All @@ -30,30 +26,28 @@
:stop "cS"
:eval_reload "eF"}}}}}))

(def- cfg (config.get-in-fn [:client :fennel :stdio]))
(local cfg (config.get-in-fn [:client :fennel :stdio]))
(local state (client.new-state #(do {:repl nil})))
(local buf-suffix ".fnl")
(local comment-prefix "; ")
(local form-node? ts.node-surrounded-by-form-pair-chars?)
(local comment-node? ts.lisp-comment-node?)

(defonce- state (client.new-state #(do {:repl nil})))

(def buf-suffix ".fnl")
(def comment-prefix "; ")
(def form-node? ts.node-surrounded-by-form-pair-chars?)
(def comment-node? ts.lisp-comment-node?)

(defn- with-repl-or-warn [f opts]
(fn with-repl-or-warn [f opts]
(let [repl (state :repl)]
(if repl
(f repl)
(log.append [(.. comment-prefix "No REPL running")]))))

(defn- format-message [msg]
(fn format-message [msg]
(str.split (or msg.out msg.err) "\n"))

(defn- display-result [msg]
(fn display-result [msg]
(log.append
(->> (format-message msg)
(a.filter #(not (= "" $1))))))

(defn eval-str [opts]
(fn eval-str [opts]
(with-repl-or-warn
(fn [repl]
(repl.send
Expand All @@ -69,10 +63,10 @@
(a.run! display-result msgs)))
{:batch? true}))))

(defn eval-file [opts]
(fn eval-file [opts]
(eval-str (a.assoc opts :code (a.slurp opts.file-path))))

(defn eval-reload []
(fn eval-reload []
(let [file-path (nvim.fn.expand "%")
relative-no-suf (nvim.fn.fnamemodify file-path ":.:r")
module-path (string.gsub relative-no-suf afs.path-sep ".")]
Expand All @@ -83,24 +77,24 @@
:file-path file-path
:code (.. ",reload " module-path)})))

(defn doc-str [opts]
(fn doc-str [opts]
(eval-str (a.update opts :code #(.. ",doc " $1 "\n"))))

(defn- display-repl-status [status]
(fn display-repl-status [status]
(let [repl (state :repl)]
(when repl
(log.append
[(.. comment-prefix (a.pr-str (a.get-in repl [:opts :cmd])) " (" status ")")]
{:break? true}))))

(defn stop []
(fn stop []
(let [repl (state :repl)]
(when repl
(repl.destroy)
(display-repl-status :stopped)
(a.assoc (state) :repl nil))))

(defn start []
(fn start []
(if (state :repl)
(log.append [(.. comment-prefix "Can't start, REPL is already running.")
(.. comment-prefix "Stop the REPL with "
Expand Down Expand Up @@ -133,13 +127,13 @@
(fn [msg]
(display-result msg))}))))

(defn on-load []
(fn on-load []
(start))

(defn on-exit []
(fn on-exit []
(stop))

(defn on-filetype []
(fn on-filetype []
(mapping.buf
:FnlStart
(cfg [:mapping :start])
Expand All @@ -158,4 +152,16 @@
eval-reload
{:desc "Use ,reload on the file"}))

*module*
{: buf-suffix
: comment-prefix
: form-node?
: comment-node?
: eval-str
: eval-file
: eval-reload
: doc-str
: stop
: start
: on-load
: on-exit
: on-filetype}
Loading