|
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( |
2 | 17 | "rust", |
3 | 18 | [[ |
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")) |
11 | 23 |
|
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)) |
16 | 27 | ]] |
17 | 28 | ) |
18 | 29 |
|
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 |
20 | 35 |
|
21 | 36 | 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() |
25 | 38 |
|
26 | 39 | if vim.bo[bufnr].filetype ~= "rust" then |
27 | 40 | vim.notify "can only be used in rust" |
28 | 41 | return |
29 | 42 | end |
30 | 43 |
|
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) |
36 | 45 |
|
37 | 46 | 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 } |
50 | 51 | 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)) |
51 | 56 |
|
52 | | - local formatted = j:sync() |
53 | | - local rep = string.rep(" ", range[2]) |
| 57 | + -- Add some indentation (can be anything you like!) |
54 | 58 | for idx, line in ipairs(formatted) do |
55 | | - formatted[idx] = rep .. line |
| 59 | + formatted[idx] = indentation .. line |
56 | 60 | end |
57 | 61 |
|
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 | + }) |
59 | 71 | end |
60 | 72 | end |
61 | 73 |
|
|
0 commit comments