本文整理汇总了C#中SyntaxTree.GetNodeContaining方法的典型用法代码示例。如果您正苦于以下问题:C# SyntaxTree.GetNodeContaining方法的具体用法?C# SyntaxTree.GetNodeContaining怎么用?C# SyntaxTree.GetNodeContaining使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SyntaxTree
的用法示例。
在下文中一共展示了SyntaxTree.GetNodeContaining方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ExtendSelection
// could work to extend selection to set of adjacent statements separated by blank lines
Selection ExtendSelection(ITextEditor editor, SyntaxTree parsedCU, IList<AstNode> commentsBlankLines, out AstNode selectedResultNode, Type[] interestingNodeTypes)
{
selectedResultNode = null;
var selectionStart = editor.Document.GetLocation(editor.SelectionStart);
var selectionEnd = editor.Document.GetLocation(editor.SelectionStart + editor.SelectionLength);
AstNode currentNode = parsedCU.GetNodeContaining(selectionStart, selectionEnd);
if (currentNode == null) return null;
if (!IsNodeTypeInteresting(currentNode, interestingNodeTypes)) {
// ignore uninteresting nodes in the AST
currentNode = GetInterestingParent(currentNode, interestingNodeTypes);
}
if (currentNode == null) return null;
selectedResultNode = currentNode;
// whole node already selected -> expand selection
if (currentNode.StartLocation == selectionStart && currentNode.EndLocation == selectionEnd) {
bool extendToComments = false;
if (IsNodeTypeInteresting(currentNode, interestingNodeTypes)) {
// if interesting node already selected, we can try to also add comments
var selectionExtendedToComments = ExtendSelectionToComments(editor.Document, selectionStart, selectionEnd, commentsBlankLines);
if (selectionExtendedToComments != null) {
// Can be extended to comments -> extend
selectionStart = selectionExtendedToComments.Start;
selectionEnd = selectionExtendedToComments.End;
extendToComments = true;
}
}
if (!extendToComments) {
var parent = GetInterestingParent(currentNode, interestingNodeTypes);
// it can happen that parent region exactly matches child region - in this case we need to advance even to the next parent
// bc otherwise the selection would never move
while (parent != null && parent.StartLocation == selectionStart && parent.EndLocation == selectionEnd) {
parent = GetInterestingParent(parent, interestingNodeTypes);
}
if (parent == null) {
return new Selection { Start = selectionStart, End = selectionEnd };
}
// Select the parent
var extendedSelectionStart = parent.StartLocation;
var extendedLocationEnd = parent.EndLocation;
selectedResultNode = parent;
// if the extended selection would contain blank lines, extend the selection only to the blank lines/comments on both sides (use siblings)
// if the selection contains blank lines or comments on both sides, dont do this
// var blankLines = commentsBlankLines.Where(s => s is BlankLine).Cast<BlankLine>().ToList();
//if (SelectionContainsBlankLines(extendedSelectionStart, extendedLocationEnd, blankLines)) {
if (false) { // blank line separators - implement later
} else {
selectionStart = extendedSelectionStart;
selectionEnd = extendedLocationEnd;
}
}
} else {
// select current node
selectionStart = currentNode.StartLocation;
selectionEnd = currentNode.EndLocation;
selectedResultNode = currentNode;
}
return new Selection { Start = selectionStart, End = selectionEnd };
}