Skip to content

Commit

Permalink
#611 - strip comments from meta commands
Browse files Browse the repository at this point in the history
  • Loading branch information
Olical committed Oct 18, 2024
1 parent 4c21ff7 commit 41a4bb5
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 23 deletions.
40 changes: 40 additions & 0 deletions fnl/conjure-spec/client/sql/stdio_spec.fnl
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
(local {: describe : it} (require :plenary.busted))
(local assert (require :luassert.assert))
(local sql (require :conjure.client.sql.stdio))

(describe "conjure.client.sql.stdio"
(fn []
(describe "prep-code"
(fn []
(it "prepares sql code appropriately"
(fn []
;; Remove trailing comments from meta commands
;; I may regret this when there's a meta command that takes double dash args...
(assert.same
"foo\n"
(sql.prep-code
{:code "foo -- bar"
:node {:type (fn [] :meta)}}))

;; Only works on the last line so we don't mangle code, hopefully
(assert.same
"foo -- bar\nsomething\n"
(sql.prep-code
{:code "foo -- bar\nsomething -- quuz"
:node {:type (fn [] :meta)}}))

;; If the last line has an extra blank line we do nothing, shouldn't really ever happen, but here's the behaviour anyway
(assert.same
"foo -- bar\nsomething -- quux\n\n"
(sql.prep-code
{:code "foo -- bar\nsomething -- quux\n"
:node {:type (fn [] :meta)}}))

;; If something is a statement it gets a semi colon on the end too
(assert.same
"foo;\n"
(sql.prep-code
{:code "foo -- bar"
:node {:type (fn [] :statement)}}))

nil))))))
36 changes: 24 additions & 12 deletions fnl/conjure/client/sql/stdio.fnl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
(local str (autoload :conjure.aniseed.string))
(local client (autoload :conjure.client))
(local log (autoload :conjure.log))
(local text (autoload :conjure.text))
(local stdio (autoload :conjure.remote.stdio-rt))
(local config (autoload :conjure.config))
(local mapping (autoload :conjure.mapping))
Expand Down Expand Up @@ -98,21 +99,31 @@
s
[s]))

(fn prep-code [opts]
(let [node (a.get opts :node)
suffix (if (and node (= "statement" (node:type)))
";\n"
"\n")

;; Removes trailing "-- ..." SQL comments from the code string
;; These interfere with meta commands like .tables
;; Only works on the last comment, intended for single lines
;; This is because running it across multiple lines may mangle your source code
code (string.gsub opts.code "%s*%-%-[^\n]*$" "")]

(.. code suffix)))

(fn eval-str [opts]
(with-repl-or-warn
(fn [repl]
(let [node (a.get opts :node)
suffix (if (and node (= "statement" (node:type)))
";\n"
"\n")]
(repl.send
(.. opts.code suffix)
(fn [msgs]
(let [msgs (->list msgs)]
(when opts.on-result
(opts.on-result (str.join "\n" (remove-blank-lines (a.last msgs)))))
(a.run! display-result msgs)))
{:batch? false})))))
(repl.send
(prep-code opts)
(fn [msgs]
(let [msgs (->list msgs)]
(when opts.on-result
(opts.on-result (str.join "\n" (remove-blank-lines (a.last msgs)))))
(a.run! display-result msgs)))
{:batch? false}))))
;;;;-------- End from client/fennel/stdio.fnl ------------------

(fn eval-file [opts]
Expand Down Expand Up @@ -200,6 +211,7 @@
: get-form-modifier
: comment-node?
: ->list
: prep-code
: eval-str
: eval-file
: interrupt
Expand Down
32 changes: 32 additions & 0 deletions lua/conjure-spec/client/sql/stdio_spec.lua

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

27 changes: 16 additions & 11 deletions lua/conjure/client/sql/stdio.lua

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

0 comments on commit 41a4bb5

Please sign in to comment.