Skip to content

How to get the syntax node, given a location #53746

Answered by jmarolf
martinrhan asked this question in Q&A
Discussion options

You must be logged in to vote

the location type has a reference to the syntax tree the location comes from. You need to get the "Root" node (the first node in the tree) and then call FindNode on the root to get the nearest child node at the given span. Should look something like this:

foreach (Location location in Locations) {
    SyntaxNode rootNode = location.SourceTree.GetRoot();
    SyntaxNode nodeAtLocation = rootNode.FindNode(location.SourceSpan);
}

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by martinrhan
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