public void NodeNavigator_FindInChildren_PropertyDeclarationSyntax_FindsPropertyDeclarationSyntax(string code, string expectedName) { // Arrange var tree = CSharpSyntaxTree.ParseText(code); var root = tree.GetCompilationUnitRoot(); var nn = new NodeNavigator(root); // Act var node = nn.FindInDescendents<PropertyDeclarationSyntax>().First(); // Assert Assert.AreEqual(expectedName, node.Identifier.Text); }
public void NodeNavigator_Selection_FindInChildren_ClassDeclarationSyntax_ReturnsNull(string code, int selectionStart, int selectionLength) { // Arrange var tree = CSharpSyntaxTree.ParseText(code); var root = tree.GetCompilationUnitRoot(); var selection = root.FindNode(new TextSpan(selectionStart, selectionLength)); var nn = new NodeNavigator(selection); // Act var nodes = nn.FindInDescendents<ClassDeclarationSyntax>(); // Assert Assert.IsEmpty(nodes); }
public sealed override async Task ComputeRefactoringsAsync(CodeRefactoringContext context) { // TODO: Replace the following code with your own analysis, generating a CodeAction for each refactoring to offer var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false); // Find the node at the selection. var node = root.FindNode(context.Span); var navigator = new NodeNavigator(node); // Only offer a refactoring if the selected node is a type declaration node. var expr = navigator.FindInAncestors<ObjectCreationExpressionSyntax>() ?? navigator.FindInDescendents<ObjectCreationExpressionSyntax>().FirstOrDefault(); if (expr == null) return; // For any type declaration node, create a code action to reverse the identifier text. // var action = CodeAction.Create("Reverse type name", c => ReverseTypeNameAsync(context.Document, typeDecl, c)); // Register this code action. // context.RegisterRefactoring(action); }