My nvim configuration
refactor: better sql awareness for rust treesitter
1 file changed, +57 -0
+57-0after/queries/rust/injections.scm
| @@ -21,3 +21,60 @@ | ||
| 21 | 21 | (#set! injection.language "sql") |
| 22 | 22 | (#set! injection.include-children) |
| 23 | 23 | ) |
| 24 | + | |
| 25 | +; Inject SQL when string literal is arg of a function/method whose name contains "sql" | |
| 26 | +; e.g. query.push_sql(r#"..."#), sqlx::query("..."), execute_sql("...") | |
| 27 | +( | |
| 28 | + (call_expression | |
| 29 | + function: [ | |
| 30 | + (identifier) @_fn | |
| 31 | + (scoped_identifier name: (identifier) @_fn) | |
| 32 | + (field_expression field: (field_identifier) @_fn) | |
| 33 | + (generic_function function: (identifier) @_fn) | |
| 34 | + (generic_function function: (scoped_identifier name: (identifier) @_fn)) | |
| 35 | + (generic_function function: (field_expression field: (field_identifier) @_fn)) | |
| 36 | + ] | |
| 37 | + arguments: (arguments | |
| 38 | + [ | |
| 39 | + (raw_string_literal (string_content) @injection.content) | |
| 40 | + (string_literal (string_content) @injection.content) | |
| 41 | + ])) | |
| 42 | + (#match? @_fn "[sS][qQ][lL]") | |
| 43 | + (#set! injection.language "sql") | |
| 44 | + (#set! injection.include-children) | |
| 45 | +) | |
| 46 | + | |
| 47 | +; Inject SQL when string literal assigned to a `let` binding whose name contains "sql" | |
| 48 | +; e.g. let sql = r#"..."#; let my_sql_str = "..."; | |
| 49 | +( | |
| 50 | + (let_declaration | |
| 51 | + pattern: (identifier) @_var | |
| 52 | + value: [ | |
| 53 | + (raw_string_literal (string_content) @injection.content) | |
| 54 | + (string_literal (string_content) @injection.content) | |
| 55 | + ]) | |
| 56 | + (#match? @_var "[sS][qQ][lL]") | |
| 57 | + (#set! injection.language "sql") | |
| 58 | + (#set! injection.include-children) | |
| 59 | +) | |
| 60 | + | |
| 61 | +; Inject SQL for const/static SQL = "..." | |
| 62 | +( | |
| 63 | + [ | |
| 64 | + (const_item | |
| 65 | + name: (identifier) @_var | |
| 66 | + value: [ | |
| 67 | + (raw_string_literal (string_content) @injection.content) | |
| 68 | + (string_literal (string_content) @injection.content) | |
| 69 | + ]) | |
| 70 | + (static_item | |
| 71 | + name: (identifier) @_var | |
| 72 | + value: [ | |
| 73 | + (raw_string_literal (string_content) @injection.content) | |
| 74 | + (string_literal (string_content) @injection.content) | |
| 75 | + ]) | |
| 76 | + ] | |
| 77 | + (#match? @_var "[sS][qQ][lL]") | |
| 78 | + (#set! injection.language "sql") | |
| 79 | + (#set! injection.include-children) | |
| 80 | +) |