treesitter: add playground and first attempt at sql in rust
This commit is contained in:
parent
90e655005c
commit
5ed657277d
5 changed files with 121 additions and 0 deletions
3
.gitmodules
vendored
3
.gitmodules
vendored
|
|
@ -146,3 +146,6 @@
|
|||
[submodule "pack/general/opt/core.nvim"]
|
||||
path = pack/general/opt/core.nvim
|
||||
url = https://github.com/niuiic/core.nvim
|
||||
[submodule "pack/general/start/playground"]
|
||||
path = pack/general/start/playground
|
||||
url = https://github.com/nvim-treesitter/playground
|
||||
|
|
|
|||
83
after/plugin/rust_sql.lua
Normal file
83
after/plugin/rust_sql.lua
Normal file
|
|
@ -0,0 +1,83 @@
|
|||
local run_formatter = function(text)
|
||||
vim.notify(text)
|
||||
vim.notify("----")
|
||||
local result = {}
|
||||
require("plenary.job"):new({
|
||||
command = "sqlfluff",
|
||||
cwd = "/usr/bin",
|
||||
args = { "format", "-" },
|
||||
writer = text,
|
||||
on_stdout = function(_, line)
|
||||
table.insert(result, line)
|
||||
end,
|
||||
}):sync()
|
||||
|
||||
-- add the surrounding r#"..."# back
|
||||
for _, line in ipairs(result) do
|
||||
vim.notify(line)
|
||||
end
|
||||
-- table.insert(result, "\"#")
|
||||
return result
|
||||
end
|
||||
|
||||
local embedded_sql = vim.treesitter.query.parse(
|
||||
"rust",
|
||||
[[
|
||||
(macro_invocation
|
||||
(scoped_identifier
|
||||
path: (identifier) @path (#eq? @path "sqlx")
|
||||
name: (identifier) @name (#any-of? @name "query" "query_scalar" "query_as"))
|
||||
|
||||
(token_tree
|
||||
(raw_string_literal) @sql)
|
||||
(#offset! @sql 0 3 0 -2)
|
||||
)
|
||||
]]
|
||||
)
|
||||
|
||||
local get_root = function(bufnr)
|
||||
local parser = vim.treesitter.get_parser(bufnr, "rust", {})
|
||||
local tree = parser:parse()[1]
|
||||
return tree:root()
|
||||
end
|
||||
|
||||
local format = function(bufnr)
|
||||
bufnr = bufnr or vim.api.nvim_get_current_buf()
|
||||
|
||||
if vim.api.nvim_get_option_value("filetype", { buf = bufnr }) ~= "rust" then
|
||||
vim.notify("SQL format an only be used in Rust")
|
||||
return
|
||||
end
|
||||
|
||||
local root = get_root(bufnr)
|
||||
|
||||
local changes = {}
|
||||
for id, node in embedded_sql:iter_captures(root, bufnr, 0, -1) do
|
||||
local name = embedded_sql.captures[id]
|
||||
if name == "sql" then
|
||||
-- {start row, start col, end row, end col }
|
||||
local range = { node:range() }
|
||||
local indentation = string.rep(" ", range[2])
|
||||
-- run the formatter on the node text
|
||||
local formatted = run_formatter(vim.treesitter.get_node_text(node, bufnr))
|
||||
-- add indentation
|
||||
for idx, line in ipairs(formatted) do
|
||||
formatted[idx] = indentation .. line
|
||||
end
|
||||
-- add changes in reverse order
|
||||
table.insert(changes, 1, {
|
||||
start = range[1],
|
||||
final = range[3],
|
||||
formatted = formatted,
|
||||
})
|
||||
end
|
||||
end
|
||||
|
||||
for _, change in ipairs(changes) do
|
||||
vim.api.nvim_buf_set_lines(bufnr, change.start, change.final, false, change.formatted)
|
||||
end
|
||||
end
|
||||
|
||||
vim.api.nvim_create_user_command("SqlFormat", function()
|
||||
format()
|
||||
end, {})
|
||||
24
init.lua
24
init.lua
|
|
@ -159,6 +159,7 @@ require("nvim-treesitter.configs").setup({
|
|||
"php",
|
||||
"proto",
|
||||
"python",
|
||||
"query",
|
||||
"regex",
|
||||
"ron",
|
||||
"rst",
|
||||
|
|
@ -174,6 +175,29 @@ require("nvim-treesitter.configs").setup({
|
|||
"yaml",
|
||||
},
|
||||
highlight = { enable = true, indent = true },
|
||||
playground = {
|
||||
enable = true,
|
||||
disable = {},
|
||||
updatetime = 25, -- Debounced time for highlighting nodes in the playground from source code
|
||||
persist_queries = false, -- Whether the query persists across vim sessions
|
||||
keybindings = {
|
||||
toggle_query_editor = 'o',
|
||||
toggle_hl_groups = 'i',
|
||||
toggle_injected_languages = 't',
|
||||
toggle_anonymous_nodes = 'a',
|
||||
toggle_language_display = 'I',
|
||||
focus_language = 'f',
|
||||
unfocus_language = 'F',
|
||||
update = 'R',
|
||||
goto_node = '<cr>',
|
||||
show_help = '?',
|
||||
},
|
||||
},
|
||||
query_linter = {
|
||||
enable = true,
|
||||
use_virtual_text = true,
|
||||
lint_events = { "BufWrite", "CursorHold" },
|
||||
},
|
||||
})
|
||||
|
||||
---- Filetypes ---
|
||||
|
|
|
|||
1
pack/general/start/playground
Submodule
1
pack/general/start/playground
Submodule
|
|
@ -0,0 +1 @@
|
|||
Subproject commit ba48c6a62a280eefb7c85725b0915e021a1a0749
|
||||
10
queries/rust/injections.scm
Normal file
10
queries/rust/injections.scm
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
; Inject into sqlx::query!(r#"..."#, ...) as sql
|
||||
(macro_invocation
|
||||
(scoped_identifier
|
||||
path: (identifier) @path (#eq? @path "sqlx")
|
||||
name: (identifier) @name (#any-of? @name "query" "query_scalar" "query_as")
|
||||
)
|
||||
|
||||
(token_tree (raw_string_literal) @sql)
|
||||
(#offset! @sql 0 3 0 -2)
|
||||
)
|
||||
Loading…
Add table
Reference in a new issue