Skip to content

Commit

Permalink
Add some magic to extact the current line when running meta commands
Browse files Browse the repository at this point in the history
  • Loading branch information
Olical committed Oct 4, 2024
1 parent 0ae4f0a commit 75f475e
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 22 deletions.
8 changes: 6 additions & 2 deletions dev/sql/sandbox.sql
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ CREATE TABLE lines (
);


-- duckdb meta commands
.tables foo
.tables bar


-- John has no orders.

-- Zack has an order with 1 item.
Expand All @@ -92,9 +97,8 @@ INSERT INTO orders VALUES (5, 3, 5, 'AA-00003');
INSERT INTO orders VALUES (6, 3, 6, 'AA-00003');

-- Evaluate the following before sending an interrupt.
\watch
SELECT 1;
.tables foo
\watch


-- From: https://www.postgresqltutorial.com/postgresql-indexes/postgresql-create-index/
Expand Down
22 changes: 18 additions & 4 deletions fnl/conjure/client/sql/stdio.fnl
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,30 @@
(local comment-prefix "-- ")

;; Rough equivalent of a Lisp form.
(fn form-node? [node]
(or
(fn get-form-modifier [node]
(if
;; Must either be a statement which we have to add ; to because the
;; tree-sitter node excludes it for some reason.
(= "statement" (node:type))
{:modifier :none}

;; Or an unknown node that starts with a command escape character.
;; It has the type of ERROR at the time of writing, but this might change
;; if the tree sitter grammar is updated.
(a.string? (string.match (ts.node->str node) (cfg [:meta_prefix_pattern])))))
;; We have to use the :raw override because the grammar returns all adjacent meta commands.
;; We just take the current line in this case.
(a.string? (string.match (ts.node->str node) (cfg [:meta_prefix_pattern])))
(let [line (vim.api.nvim_get_current_line)
[row _col] (vim.api.nvim_win_get_cursor 0)]
{:modifier :raw
:node-table {:node node
:content line
:range {:start [row 0]
:end [row (a.count line)]}}})

;; Default to looking at the parent node.
{:modifier :parent}))


;; Comment nodes are comment (--) and marginalia (/*...*/)
(fn comment-node? [node]
Expand Down Expand Up @@ -184,7 +198,7 @@

{: buf-suffix
: comment-prefix
: form-node?
: get-form-modifier
: comment-node?
: ->list
: eval-str
Expand Down
42 changes: 26 additions & 16 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 75f475e

Please sign in to comment.