Returns a token if position is in [start-of-leading-trivia, end), includes JSDoc only if requested
(file *SourceFile, position int, includeJSDoc bool)
| 2635 | |
| 2636 | // Returns a token if position is in [start-of-leading-trivia, end), includes JSDoc only if requested |
| 2637 | func GetNodeAtPosition(file *SourceFile, position int, includeJSDoc bool) *Node { |
| 2638 | current := file.AsNode() |
| 2639 | for { |
| 2640 | var child *Node |
| 2641 | if includeJSDoc { |
| 2642 | for _, jsdoc := range current.JSDoc(file) { |
| 2643 | if nodeContainsPosition(jsdoc, position) { |
| 2644 | child = jsdoc |
| 2645 | break |
| 2646 | } |
| 2647 | } |
| 2648 | } |
| 2649 | if child == nil { |
| 2650 | current.ForEachChild(func(node *Node) bool { |
| 2651 | if nodeContainsPosition(node, position) { |
| 2652 | child = node |
| 2653 | return true |
| 2654 | } |
| 2655 | return false |
| 2656 | }) |
| 2657 | } |
| 2658 | if child == nil || IsMetaProperty(child) { |
| 2659 | return current |
| 2660 | } |
| 2661 | current = child |
| 2662 | } |
| 2663 | } |
| 2664 | |
| 2665 | func nodeContainsPosition(node *Node, position int) bool { |
| 2666 | return node.Kind >= KindFirstNode && node.Pos() <= position && (position < node.End() || position == node.End() && node.Kind == KindEndOfFile) |
no test coverage detected