Skip to content

Accessing a capture's name #2793

Closed Answered by WillLillis
alexpovel asked this question in Q&A
Discussion options

You must be logged in to vote

Hi! I see two solutions here, the first is to match the string_fragment field:

To more directly address your question for associating captures with their names, I believe you can use capture_idx_for_name() along with nodes_for_capture_index(). You mentioned using the Rust bindings, so I threw together a quick program to make sure this works as expected.

fn main() {
    let text = "let x: string = `Hello ${world}`";
    let mut parser = tree_sitter::Parser::new();
    parser
        .set_language(tree_sitter_typescript::language_typescript())
        .unwrap();
    let tree = parser.parse(text, None).unwrap();
    let mut cursor = tree_sitter::QueryCursor::new();

    let query = tree_sitter

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by alexpovel
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants