1

I'm using treesitter inside neovim, for all of the awesome reasons we already know.

My (legacy) code has a lot of SQL statements in PHP strings. Using TS I've got it to recognise SQL inside PHP strings and then get various groups identifying and highlighting nicely, e.g. @keyword.sql, @variable.parameter.sql.

But the only thing that last one matches (that I've noticed so far) is ? - which is great when I'm using that sort of placeholder, but a lot of queries use :myParam etc instead...

Using :Inspect I can see on :myParam that the colon is still just @string.php, while the rest of it is @variable.member.sql.

I want to nicely highlight :myParam in its entirety. At the moment I can only highlight myParam, and only the same as all the other @variable.member.sql, which appears to be (at least) column names.

I spent ages with ChatGPT trying to work through this, and nothing it suggested worked. In the end we ("we"... FML) decided to use a regex rule outside of TS to find such placeholders and apply a new highlighting group to them... which works GREAT... except TreeSitter reapplies its syntax CONSTANTLY, so immediately overwrites the colours 🤦‍♂️

(I know it's working as my bg colour that I set this way doesn't get overwritten, as my TS highlight groups are only set to change the fg colour)

So............... back to the drawing board. How can I configure TS to recognise these placeholders?

AFAICS, the issue is that :myParam isn't actually valid SQL code. It's a PDO thing. So where do I go from here?

Thank you!

5
  • 1
    Is this answer of any help to you? Commented Oct 31 at 13:38
  • 1
    Maybe an example would help to clarify what is your problem and let other give concrete answers and not only guidance. Commented Oct 31 at 13:40
  • DB::getQuery('SELECT foo FROM bar WHERE id = :bar_id', ['bar_id' => $barId]); I'd like :bar_id to be targetable for syntax highlighting. Commented Oct 31 at 14:48
  • Now that I read more carefully your question, I'm afraid your have a problem that can only be solved if you have a variant of the SQL parser that make that :bar_id is a valid SQL token. Commented Oct 31 at 18:44
  • 1
    yep, I realised that. Like you hint at, :bar_id isn't part of SQL, it's part of PDO. But in nvim land anything is possible with lua etc, so I feel there must be a way to do this. Commented Nov 27 at 8:01

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.