Use Manual Navigation
What you should know beforehand:
In some cases it might be useful to manually navigate the syntax tree (relatively to a particular tree node). For this purpose, any tree node has a number of properties and methods, like:
Parent: returns a parent tree node;
IEnumerablewith children nodes;
PrevSibling: next and previous sibling tree nodes;
lots of them.
For example, let's create an
ITreeNode extension method that walks up the syntax tree and finds (if there are any) a parent tree node of a particular type:
For example, we can use it to navigate to the method declaration while standing anywhere inside a method:
We use the
GetTreeNodeUnderCaret()method of the
NodeUnderCaretNavigatorclass shown in Get a Tree Node Under Caret.
Note that to prevent conflicts with user input during the navigation, we must obtain the read lock before calculating the node under the caret. This is done by means of the special method of the