Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: activate injection using a comment #3

Open
massimiliano-della-rovere opened this issue Jan 22, 2024 · 4 comments
Open

Comments

@massimiliano-della-rovere

When the buffer is parsed by treesitter, an ad-hoc written comment could activate one or more pre-saved injection

@Dronakurl
Copy link
Owner

Thank you for your request!

How could this work exactly?
Maybe, something like this example?
The injection given in the comment is activated?

# injectme: rst_for_docstring

def myfun():
    """ This should be formatted as rst"""
    pass

It is hard to get this to work, as injections are chosen by treesiter-nvim per filetype and not per buffer.
Unless you wouldn't mind that this comment just triggers the injection for all files of same type, in this example rst formatting for all python files that are open in the current nvim session

@massimiliano-della-rovere
Copy link
Author

My idea is "the comment activates injection for the next string".

This would be useful to reduce the amount of analysis that treesitter had to perform to identify other languages in every string: the comment would already be the analysis, like one key of an associative array where the values are treesitter functions that will interpret the string.

@Dronakurl
Copy link
Owner

This should be done by a treesitter query. When I monitor the strings with lua, only to activate treesitter on a certain comment, this would be much slower. Sorry, but I don't know what to make of this idea

@cseickel
Copy link

I used to have a query that conditionally marked the next text fragment to be parsed as an injected language. It just broke and I am trying to figure out why, I think what I had might be what @massimiliano-della-rovere is looking for.

For example, in typescript I would write:

export const CREATE = /* sql */ `
create or replace function safe_sqrt(double precision)
returns double precision
language sql IMMUTABLE PARALLEL SAFE
as $$
  SELECT CASE
    WHEN $1 is null THEN null
    WHEN $1 < 0 THEN null
    ELSE SQRT($1)
  END;
$$;
`;

and it would highlight it as sql, even if the given sql string does not fit the standard injection template that assumes a generic select query.

The injections.scm looked like this:

; extends

; /* html */ `<html>`
; /* sql */ `SELECT * FROM foo`
(variable_declarator
  (comment) @injection.language (#offset! @injection.language 0 3 0 -3)
  (template_string) @injection.content (#offset! @injection.content 0 1 0 -1)
  )

It broke on a recent update of treesitter. I have no idea why or how to make it work again. Do you think you can fix it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants