Treesitter Injections for Julia

Hi,
I have asked this question on Reddit, unfortunately I did not got a response back. So I will try my luck here:

I have SQL queries in Julia code and I want to add Syntax highlighting to them.

My SQL statements are prefixed_string_literal nodes and the prefix: indentifier is “sql”

In Julia it looks like this:

x = sql"""
select * from table
"""

The AST looks like this:

assignment_expression [3, 0] - [6, 3]
  identifier [3, 0] - [3, 1]
  operator [3, 2] - [3, 3]
  prefixed_string_literal [3, 4] - [6, 3]
    prefix: identifier [3, 4] - [3, 7]

…and I can capture everything with the following scheme query:

( assignment_expression
    (
        (prefixed_string_literal
            prefix: (identifier) @prefix (#eq? @prefix "sql")
        ) @sql_query
    )
) @sql

My problem is now that the prefix is part of the prefixed_string_literal, but I think I only want the text in between the quotes. How do I achieve that?

I have tried adding this regex, but I get all sorts of errors (Vim:E866 and Vim:E871)

( assignment_expression
    (
        (prefixed_string_literal
            prefix: (identifier) @prefix (#eq? @prefix "sql")
        ) @sql_query
    )
    (#match? @sql_query "\"{3}([\s\S]*?){3}\"")
) @sql

How do I add a regex and escape the quotes properly?

Can someone help me?

Thanks & Cheers,
Matt

You need to use the (#offset! @capture_id start_row start_col end_row end_col) identifier. It will offset the results for @identifier

Look up treesitter-directive-offset. My guess is you need (#offset! @sql 1 0 -1 0)

Thanks for the suggestion. I have played around with your suggestion and I cannot get this running.

(assignment_expression
    (
        (prefixed_string_literal
            prefix: (identifier) @prefix (#eq? @prefix "sql")
        ) @sql_query
        (#offset! @sql_query 1 0 -1 0)
    )
) @sql

First off, the row_start/end and col_start/end parameters seem to not have an impact. I was changing the values, but nothing changed.

Secondly, for some reason this query above highlights the whole statement.

Here was the input:

x = sql"""
select * from table
"""

@sql_query matches sql"“”…“”"
@sql matches everything: `x = sql…“”"

This seems wrong and I don’t get why.

Is that related to: treesitter directive offset, row offset not updating column · Issue #16135 · neovim/neovim · GitHub ?

Currently the Julia grammar doesn’t distinguish between single and triple quoted strings, so you’d have to write the offsets for one or the other (afaik you can’t set the offsets dynamically).

Assuming you always use triple quoted strings, I think the following would work:

((prefixed_string_literal
  prefix: (identifier) @_prefix) @sql
  (#match? @_prefix "sql")
  (#offset! @sql 1 0 0 -3))

I don’t know why the @_prefix capture must have the leading underscore, but that’s how similar patterns are written.

Captures with underscores are treated as “private” by nvim-treesitter (not Neovim!) CI and ignored when matching against the list in nvim-treesitter/CONTRIBUTING.md at master · nvim-treesitter/nvim-treesitter · GitHub (which are the only user-facing ones allowed to be used in the bundled queries).

@savq Thanks. Your query gives me the same result. It matches everything, including the prefix and the triple quotes. I was on the notion, that (for injections) I need to match everything in between the triple quotes.

furthermore, I get the same result, when I comment out the line with #offset!

(
 (prefixed_string_literal
  prefix: (identifier) @_prefix) @sql
  (#match? @_prefix "sql")
  (#offset! @sql 1 0 0 -3)
) @sql_query

… when I hover over the @sql_query in the TS Playground.

Solved over at reddit using this query:

(
    (prefixed_string_literal
        prefix: (identifier) @_prefix (#eq? @_prefix "sql")
    ) @sql

    (#offset! @sql 0 6 0 -3)
)

… and as mentioned in the linked issue: #offset! does not work in the playground.