Skip to content

Commit b43facf

Browse files
committed
comps
1 parent 62b1de0 commit b43facf

File tree

6 files changed

+85
-56
lines changed

6 files changed

+85
-56
lines changed

xdg_config/nvim/after/plugin/colorscheme.lua

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,9 @@ Group.new("WinSeparator", nil, nil)
4545
-- Group.new("TSKeyword", c.purple, nil, s.underline, c.blue)
4646
-- Group.new("LuaFunctionCall", c.green, nil, s.underline + s.nocombine, g.TSKeyword.guisp)
4747

48-
-- Hello
4948
Group.new("TSTitle", c.blue)
49+
50+
-- TODO: It would be nice if we could only highlight
51+
-- the text with characters or something like that...
52+
-- but we'll have to stick to that for later.
53+
Group.new("InjectedLanguage", nil, g.Normal.bg:dark())

xdg_config/nvim/after/plugin/sql_rust_automagic.lua

Lines changed: 49 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,73 @@
1-
local query = vim.treesitter.parse_query(
1+
local run_formatter = function(text)
2+
local split = vim.split(text, "\n")
3+
local result = table.concat(vim.list_slice(split, 2, #split - 1), "\n")
4+
5+
-- Finds sql-format-via-python somewhere in your nvim config path
6+
local bin = vim.api.nvim_get_runtime_file("bin/sql-format-via-python.py", false)[1]
7+
8+
local j = require("plenary.job"):new {
9+
command = "python",
10+
args = { bin },
11+
writer = { result },
12+
}
13+
return j:sync()
14+
end
15+
16+
local embedded_sql = vim.treesitter.parse_query(
217
"rust",
318
[[
4-
(
5-
(macro_invocation
6-
(scoped_identifier
7-
path: (identifier) @_path
8-
name: (identifier) @_identifier)
9-
10-
(token_tree (raw_string_literal) @raw))
19+
(macro_invocation
20+
(scoped_identifier
21+
path: (identifier) @path (#eq? @path "sqlx")
22+
name: (identifier) @name (#eq? @name "query"))
1123
12-
(#eq? @_path "sqlx")
13-
(#eq? @_identifier "query")
14-
(#offset! @raw 1 0 -1 0)
15-
)
24+
(token_tree
25+
(raw_string_literal) @sql)
26+
(#offset! @sql 1 0 -1 0))
1627
]]
1728
)
1829

19-
local Job = require "plenary.job"
30+
local get_root = function(bufnr)
31+
local parser = vim.treesitter.get_parser(bufnr, "rust", {})
32+
local tree = parser:parse()[1]
33+
return tree:root()
34+
end
2035

2136
local format_dat_sql = function(bufnr)
22-
if not bufnr then
23-
bufnr = vim.api.nvim_get_current_buf()
24-
end
37+
bufnr = bufnr or vim.api.nvim_get_current_buf()
2538

2639
if vim.bo[bufnr].filetype ~= "rust" then
2740
vim.notify "can only be used in rust"
2841
return
2942
end
3043

31-
local parser = vim.treesitter.get_parser(bufnr, "rust", {})
32-
local tree = parser:parse()[1]
33-
34-
-- Finds sql-format-via-python somewhere in your nvim config path
35-
local bin = vim.api.nvim_get_runtime_file("bin/sql-format-via-python.py", false)[1]
44+
local root = get_root(bufnr)
3645

3746
local changes = {}
38-
for id, node, metadata in query:iter_captures(tree:root(), bufnr, 0, -1) do
39-
if id == 3 then
40-
local text = vim.treesitter.get_node_text(node, bufnr)
41-
local split = vim.split(text, "\n")
42-
local result = table.concat(vim.list_slice(split, 2, #split - 1), "\n")
43-
44-
local j = Job:new {
45-
command = "python",
46-
args = { bin },
47-
writer = { result },
48-
}
49-
47+
for id, node in embedded_sql:iter_captures(root, bufnr, 0, -1) do
48+
local name = embedded_sql.captures[id]
49+
if name == "sql" then
50+
-- { start row, start col, end row, end col }
5051
local range = { node:range() }
52+
local indentation = string.rep(" ", range[2])
53+
54+
-- Run the formatter, based on the node text
55+
local formatted = run_formatter(vim.treesitter.get_node_text(node, bufnr))
5156

52-
local formatted = j:sync()
53-
local rep = string.rep(" ", range[2])
57+
-- Add some indentation (can be anything you like!)
5458
for idx, line in ipairs(formatted) do
55-
formatted[idx] = rep .. line
59+
formatted[idx] = indentation .. line
5660
end
5761

58-
table.insert(changes, 1, { start = range[1] + 1, final = range[3], formatted = formatted })
62+
-- Keep track of changes
63+
-- But insert them in reverse order of the file,
64+
-- so that when we make modifications, we don't have
65+
-- any out of date line numbers
66+
table.insert(changes, 1, {
67+
start = range[1] + 1,
68+
final = range[3],
69+
formatted = formatted,
70+
})
5971
end
6072
end
6173

xdg_config/nvim/bin/sql-format-via-python.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import sys
22
import sqlparse
33

4-
54
# TODO: Decide what to do about $/?
65
# contents = contents.replace(f"${identifier}", f"__id_{identifier}")
76

xdg_config/nvim/queries/rust/highlights.scm

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,3 +244,15 @@
244244
; (let_declaration (type_identifier) @LetType)
245245
; (struct_expression
246246
; name: (type_identifier) @StructType)
247+
248+
; (
249+
; (macro_invocation
250+
; (scoped_identifier
251+
; path: (identifier) @_path
252+
; name: (identifier) @_identifier)
253+
;
254+
; (token_tree (raw_string_literal) @InjectedLanguage))
255+
;
256+
; (#eq? @_path "sqlx")
257+
; (#eq? @_identifier "query")
258+
; )

xdg_config/nvim/queries/rust/injections.scm

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,12 @@
1818
(#eq? @_html_def "html")
1919
)
2020

21-
(
22-
(macro_invocation
23-
(scoped_identifier
24-
path: (identifier) @_path
25-
name: (identifier) @_identifier)
26-
27-
(token_tree (raw_string_literal) @sql))
28-
29-
(#eq? @_path "sqlx")
30-
(#eq? @_identifier "query")
31-
(#offset! @sql 1 0 0 0)
32-
)
21+
; Inject into sqlx::query!(r#"..."#, ...) as sql
22+
(macro_invocation
23+
(scoped_identifier
24+
path: (identifier) @_path (#eq? @_path "sqlx")
25+
name: (identifier) @_name (#eq? @_name "query"))
26+
27+
(token_tree
28+
(raw_string_literal) @sql)
29+
(#offset! @sql 1 0 0 0))

xdg_config/nvim/scratch/automagic/part3.lua

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ local test_function_query_string = [[
1818
]]
1919

2020
local find_test_line = function(go_bufnr, name)
21-
local query = vim.treesitter.parse_query("go", string.format(test_function_query_string, name))
21+
local formatted = string.format(test_function_query_string, name)
22+
local query = vim.treesitter.parse_query("go", formatted)
2223
local parser = vim.treesitter.get_parser(go_bufnr, "go", {})
2324
local tree = parser:parse()[1]
2425
local root = tree:root()
@@ -57,6 +58,7 @@ end
5758
-- local display_golang_output = function(state, bufnr) end
5859

5960
local ns = vim.api.nvim_create_namespace "live-tests"
61+
local group = vim.api.nvim_create_augroup("teej-automagic", { clear = true })
6062

6163
local attach_to_buffer = function(bufnr, command)
6264
local state = {
@@ -65,18 +67,17 @@ local attach_to_buffer = function(bufnr, command)
6567
}
6668

6769
vim.api.nvim_buf_create_user_command(bufnr, "GoTestLineDiag", function()
68-
-- print(vim.inspect(state))
6970
local line = vim.fn.line "." - 1
7071
for _, test in pairs(state.tests) do
7172
if test.line == line then
72-
vim.cmd.vnew()
73+
vim.cmd.new()
7374
vim.api.nvim_buf_set_lines(vim.api.nvim_get_current_buf(), 0, -1, false, test.output)
7475
end
7576
end
7677
end, {})
7778

7879
vim.api.nvim_create_autocmd("BufWritePost", {
79-
group = vim.api.nvim_create_augroup(string.format("teej-automagic-%s", bufnr), { clear = true }),
80+
group = group,
8081
pattern = "*.go",
8182
callback = function()
8283
vim.api.nvim_buf_clear_namespace(bufnr, ns, 0, -1)
@@ -146,6 +147,10 @@ local attach_to_buffer = function(bufnr, command)
146147
})
147148
end
148149

150+
vim.api.nvim_create_user_command("GoTestOnSave", function()
151+
attach_to_buffer(vim.api.nvim_get_current_buf(), { "go", "test", "./...", "-v", "-json" })
152+
end, {})
153+
149154
-- attach_to_buffer(80, { "go", "run", "main.go" })
150-
attach_to_buffer(16, { "go", "test", "./...", "-v", "-json" })
155+
-- attach_to_buffer(16, { "go", "test", "./...", "-v", "-json" })
151156
-- attach_to_buffer(1, { "go", "test", "./...", "-v", "-json", "-run", "TestDoesFailStill" })

0 commit comments

Comments
 (0)