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); }
/// ----------------------------------------------------------------------------- /// <summary> /// Returns contents of an XmlNode in a string. /// </summary> /// <param name="node">The XmlNode whose contents will be read into a string.</param> /// <returns>Xml formatted string with contents of "node" parameter.</returns> /// ----------------------------------------------------------------------------- public static string NodeToString( XmlNode node ) { System.Text.StringBuilder sb = new System.Text.StringBuilder( "" ); System.IO.StringWriter sw = new System.IO.StringWriter( sb ); XmlTextWriter writer = new XmlTextWriter( sw ); writer.Formatting = Formatting.Indented; if ( node == null ) { writer.WriteStartElement( "Node_Empty" ); } else { writer.WriteStartElement( node.Name ); // Write any attributes foreach ( XmlAttribute attr in node.Attributes ) { writer.WriteAttributeString( attr.Name, attr.Value ); } // Write child nodes XmlNodeList nodes = node.SelectNodes( "child::*" ); NodeNavigator nav = new NodeNavigator(); if ( nodes != null ) { foreach ( XmlNode n in nodes ) { nav.LoopThroughChildren( writer, n ); } } } writer.WriteEndElement(); writer.Close(); return sw.ToString(); }
public void NodeNavigator_Selection_FindInParents_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 node = nn.FindInAncestors<ClassDeclarationSyntax>(); // Assert Assert.AreEqual("C", node.Identifier.Text); }