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

feat: Add a content field and element to raw string literals. #201

Closed
wants to merge 4 commits into from

Conversation

Natr1x
Copy link

@Natr1x Natr1x commented Oct 29, 2023

This makes it possible to use language injection for this content.
Which is useful since this is something that raw string literals are often used for.

A query like the following could for example be used to provide sql highlighting when using sqlx:

; extends
((macro_invocation
   macro: (scoped_identifier
            path: (identifier) @macropath (#eq? @macropath "sqlx")
            name: (identifier) @macroname)
   (token_tree
     (raw_string_literal
       content: (_) @injection.content)))
 (#set! injection.language "sql")
)

Note: I have not ran tree-sitter-cli generate in order to keep the commit smaller for review purposes.

If you are interested in merging this but want me to change or add something (or run that code generation) then I am willing to do so.

This makes it possible to use language injection for this content.
src/scanner.c Outdated
Comment on lines 14 to 16
struct ScannerState {
unsigned opening_hash_count;
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A struct for one field is a bit overkill, let's just allocate a uint32_t instead (I'd rather not use unsigned)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure about this? Personally I feel keeping it in a struct makes it explicitly clear that this is the only state we keep track of.

Copy link
Member

@amaanq amaanq Nov 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alright yeah, if we do need to add more fields later on for whatever reason it'd be easier. Can we declare it as a typedef, use uint32_t, and call it just Scanner though?

typedef struct {
    uint32_t opening_hash_count;
} Scanner;

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not a big fan of anonymous structs, would f6caf4a work as a compromise?

If not I am okay with removing the name. It's mostly for the sake of things like lsp hover information.

src/scanner.c Outdated Show resolved Hide resolved
src/scanner.c Outdated Show resolved Hide resolved
@mkatychev
Copy link

mkatychev commented Feb 20, 2024

@Natr1x what else needs to get done for this PR to move out of draft. This would be extremely useful for sql injections and would remove #offset! guesswork.

@amaanq
Copy link
Member

amaanq commented Apr 7, 2024

reworked it in #220

@amaanq amaanq closed this Apr 7, 2024
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

Successfully merging this pull request may close these issues.

3 participants