Update: 2024-10-15

This commit is contained in:
2024-10-15 20:59:39 +03:00
parent 89989c222f
commit 814425e4b7
41 changed files with 1044 additions and 546 deletions

3
after/ftplugin/bru.lua Normal file
View File

@@ -0,0 +1,3 @@
vim.opt_local.tabstop = 2
vim.opt_local.shiftwidth = 2
vim.opt_local.softtabstop = 2

View File

@@ -1,3 +1,3 @@
vim.opt_local.tabstop = 2
vim.opt_local.shiftwidth = 2
vim.opt_local.softtabstop = 2
vim.opt_local.tabstop = 4
vim.opt_local.shiftwidth = 4
vim.opt_local.softtabstop = 4

View File

@@ -11,18 +11,89 @@ local c = ls.choice_node
ls.add_snippets("python", {
s(
"main",
"logger",
fmt(
[[
def main() -> None:
{}
if __name__ == "__main__":
main()
import logging
logger = logging.getLogger(__name__)
]],
{}
)
),
s(
"dbg",
fmt(
[[
logger.debug({})
]],
{
i(1, "pass"),
i(0),
}
)
),
s(
"info",
fmt(
[[
logger.info({})
]],
{
i(0),
}
)
),
s(
"warn",
fmt(
[[
logger.warning({})
]],
{
i(0),
}
)
),
s(
"err",
fmt(
[[
logger.error({})
]],
{
i(0),
}
)
),
s(
"exc",
fmt(
[[
logger.exception({})
]],
{
i(0),
}
)
),
s(
"crit",
fmt(
[[
logger.critical({})
]],
{
i(0),
}
)
),
s(
"fatal",
fmt(
[[
logger.fatal({})
]],
{
i(0),
}
)
),

View File

@@ -37,7 +37,6 @@ ls.add_snippets("rust", {
{}
)
),
s("pd", fmt([[println!("{}: {{:?}}", {});]], { same(1), i(1) })),
s(
"dead",
fmt(
@@ -48,17 +47,98 @@ ls.add_snippets("rust", {
)
),
s(
"some",
"component",
fmt(
[[
if let Some({}) = torrent.{}{{
torrent_fields.push({}.to_string());
}}
#[derive(Debug, Reflect, Component)]
#[reflect(Component)]
struct {}
]],
{
i(1),
}
)
),
s(
"event",
fmt(
[[
#[derive(Debug, Event)]
struct {}
]],
{
i(1),
}
)
),
s(
"resource",
fmt(
[[
#[derive(Debug, Default, Reflect, Resource)]
#[reflect(Resource)]
struct {}
]],
{
i(1),
}
)
),
s(
"schedule",
fmt(
[[
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, ScheduleLabel)]
struct {}
]],
{
i(1),
}
)
),
s(
"states",
fmt(
[[
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, States)]
enum {} {{
#[default]
{}
}}
]],
{
i(1),
i(2),
}
)
),
s(
"systemset",
fmt(
[[
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, SystemSet)]
enum {} {{
{}
}}
]],
{
i(1),
i(2),
}
)
),
s(
"plugin",
fmt(
[[
use bevy::prelude::*;
pub(super) fn plugin(app: &mut App) {{
{}
}}
]],
{
same(1),
i(1),
same(1),
}
)
),

View File

@@ -0,0 +1,101 @@
local run_formatter = function(text)
local split = vim.split(text, "\n")
local result = table.concat(vim.list_slice(split, 2, #split - 1), "\n")
local j = require("plenary.job"):new({
command = "pg_format",
writer = { result },
})
return j:sync()
end
local literals = {
rust = { "string_literal", "raw_string_literal" },
python = { "string_content" },
}
local function generate_query(lang, nodes)
local node_string = table.concat(nodes, ")\n\t\t(")
local query = string.format(
[[
([
(%s)
] @sql
(#match? @sql "(SELECT|select|INSERT|insert|UPDATE|update|DELETE|delete).+(FROM|from|INTO|into|VALUES|values|SET|set).*(WHERE|where|GROUP BY|group by)?")
(#offset! @sql 1 0 -1 0))
]],
node_string
)
return lang, query
end
local queries = {}
for lang, nodes in pairs(literals) do
local language, query_string = generate_query(lang, nodes)
queries[lang] = {
language = language,
query = query_string,
}
end
local get_root = function(bufnr, lang)
local parser = vim.treesitter.get_parser(bufnr, lang, {})
local tree = parser:parse()[1]
return tree:root()
end
local format_dat_sql = function(bufnr)
bufnr = bufnr or vim.api.nvim_get_current_buf()
local changes = {}
for _, query in pairs(queries) do
local root = get_root(bufnr, query.language)
-- if vim.bo[bufnr].filetype ~= query.language then
-- vim.notify("can only be used in {}", query.language)
-- return
-- end
local embedded_sql = vim.treesitter.query.parse(query.language, query.query)
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, based on the node text
local formatted = run_formatter(vim.treesitter.get_node_text(node, bufnr))
-- Add some indentation (can be anything you like!)
for idx, line in ipairs(formatted) do
formatted[idx] = indentation .. line
end
-- Keep track of changes
-- But insert them in reverse order of the file,
-- so that when we make modifications, we don't have
-- any out of date line numbers
table.insert(changes, 1, {
start = range[1] + 1,
final = range[3],
formatted = formatted,
})
end
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("SqlMagic", function()
format_dat_sql()
end, {})
local group = vim.api.nvim_create_augroup("rust-sql-magic", { clear = true })
vim.api.nvim_create_autocmd("BufWritePre", {
group = group,
pattern = "*.rs",
callback = function()
format_dat_sql()
end,
})

View File

@@ -1,88 +0,0 @@
local run_formatter = function(text)
local split = vim.split(text, "\n")
local result = table.concat(vim.list_slice(split, 2, #split - 1), "\n")
-- Finds sql-format-via-python somewhere in your nvim config path
local bin = vim.api.nvim_get_runtime_file("bin/sql-format-via-python.py", false)[1]
local j = require("plenary.job"):new({
command = "python",
args = { bin },
writer = { result },
})
return j:sync()
end
local embedded_sql = vim.treesitter.query.parse(
"rust",
[[
([
(string_literal)
(raw_string_literal)
] @sql
(#match? @sql "(SELECT|select|INSERT|insert|UPDATE|update|DELETE|delete).+(FROM|from|INTO|into|VALUES|values|SET|set).*(WHERE|where|GROUP BY|group by)?")
(#offset! @sql 0 1 0 -1))
]]
)
local get_root = function(bufnr)
local parser = vim.treesitter.get_parser(bufnr, "rust", {})
local tree = parser:parse()[1]
return tree:root()
end
local format_dat_sql = function(bufnr)
bufnr = bufnr or vim.api.nvim_get_current_buf()
if vim.bo[bufnr].filetype ~= "rust" then
vim.notify("can 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, based on the node text
local formatted = run_formatter(vim.treesitter.get_node_text(node, bufnr))
-- Add some indentation (can be anything you like!)
for idx, line in ipairs(formatted) do
formatted[idx] = indentation .. line
end
-- Keep track of changes
-- But insert them in reverse order of the file,
-- so that when we make modifications, we don't have
-- any out of date line numbers
table.insert(changes, 1, {
start = range[1] + 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("SqlMagic", function()
format_dat_sql()
end, {})
local group = vim.api.nvim_create_augroup("rust-sql-magic", { clear = true })
vim.api.nvim_create_autocmd("BufWritePre", {
group = group,
pattern = "*.rs",
callback = function()
format_dat_sql()
end,
})

View File

@@ -0,0 +1,7 @@
;extends
([
(string_content)
] @injection.content
(#match? @injection.content "(SELECT|select|INSERT|insert|UPDATE|update|DELETE|delete).+(FROM|from|INTO|into|VALUES|values|SET|set).*(WHERE|where|GROUP BY|group by)?")
(#offset! @injection.content 0 1 0 -1)
(#set! injection.language "sql"))

View File

@@ -0,0 +1,9 @@
;extends
;; Inject into sqlx::query!(r#"..."#, ...) as sql
([
(string_literal)
(raw_string_literal)
] @injection.content
(#match? @injection.content "(SELECT|select|INSERT|insert|UPDATE|update|DELETE|delete).+(FROM|from|INTO|into|VALUES|values|SET|set).*(WHERE|where|GROUP BY|group by)?")
(#offset! @injection.content 0 1 0 -1)
(#set! injection.language "sql"))