QueryMulti executes a tree-sitter query on a specific Node. Nodes captured by the query are grouped into a QueryResult and passed to the provided callback function. See https://tree-sitter.github.io/tree-sitter/using-parsers#pattern-matching-with-queries for query syntax documentation.
(query string, fn func(QueryResult))
| 426 | // See https://tree-sitter.github.io/tree-sitter/using-parsers#pattern-matching-with-queries |
| 427 | // for query syntax documentation. |
| 428 | func (n *Node) QueryMulti(query string, fn func(QueryResult)) { |
| 429 | if !n.IsValid() { |
| 430 | return |
| 431 | } |
| 432 | q, err := sitter.NewQuery( |
| 433 | []byte(query), |
| 434 | javascript.GetLanguage(), |
| 435 | ) |
| 436 | if err != nil { |
| 437 | return |
| 438 | } |
| 439 | |
| 440 | qc := sitter.NewQueryCursor() |
| 441 | defer qc.Close() |
| 442 | |
| 443 | qc.Exec(q, n.node) |
| 444 | |
| 445 | for { |
| 446 | match, exists := qc.NextMatch() |
| 447 | if !exists || match == nil { |
| 448 | break |
| 449 | } |
| 450 | |
| 451 | match = qc.FilterPredicates(match, n.source) |
| 452 | |
| 453 | qr := NewQueryResult() |
| 454 | |
| 455 | for _, capture := range match.Captures { |
| 456 | node := NewNode(capture.Node, n.source) |
| 457 | node.captureName = q.CaptureNameForId(capture.Index) |
| 458 | qr.Add(node) |
| 459 | } |
| 460 | if len(qr) == 0 { |
| 461 | continue |
| 462 | } |
| 463 | fn(qr) |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | // IsStringy returns true if a Node is a string |
| 468 | // or is an expression starting with a string |
no test coverage detected