public static string NameFrom(this SyntaxNode node) { var qualifiedNameNode = node.ChildNodes() .OfType<QualifiedNameSyntax>() .SingleOrDefault(); var identifierNameNodes = node.ChildNodes() .OfType<IdentifierNameSyntax>() .ToList(); var name = ""; if (qualifiedNameNode != null) { name = name + qualifiedNameNode.ToString(); } foreach (var identifierNameNode in identifierNameNodes) { var identifierName = identifierNameNode.ToString(); if (!(string.IsNullOrWhiteSpace(name) || string.IsNullOrWhiteSpace(identifierName))) { name += "."; } name = name += identifierName; } if (!string.IsNullOrWhiteSpace(name)) return name; var nameToken = node.ChildTokens() .Where(x => x.Kind() == SyntaxKind.IdentifierToken) .SingleOrDefault(); return nameToken.ValueText; }
private static TypeDeclarationSyntax WithBackingField(this TypeDeclarationSyntax node, ITypeSymbol typeSymbol, string backingFieldName, SemanticModel model, Workspace workspace) { PropertyDeclarationSyntax property = node.ChildNodes().Where(n => n.HasAnnotation(UpdatedPropertyAnnotation)).FirstOrDefault() as PropertyDeclarationSyntax; if (property == null) { return null; } MemberDeclarationSyntax fieldDecl = GenerateBackingField(typeSymbol, backingFieldName, workspace); node = node.InsertNodesBefore(property, new[] { fieldDecl }); return node; }
/// <summary> /// Return all Class definitions from the Syntax node. /// </summary> /// <param name="inNode"></param> /// <returns></returns> public static List<CFunction> GetFunctions(this SyntaxNode inNode) { var tmpList = new List<CFunction>(); foreach (var tmpNode in inNode.ChildNodes()) { if (tmpNode.GetType() == typeof(MethodDeclarationSyntax)) { tmpList.Add(new CFunction { Node = tmpNode as MethodDeclarationSyntax }); } tmpList.AddRange(tmpNode.GetFunctions()); } return tmpList; }
/// <summary> /// Return all Class definitions from the Syntax node. /// </summary> /// <param name="inNode"></param> /// <returns></returns> public static List<ClassContainer> GetClasses(this SyntaxNode inNode) { var tmpList = new List<ClassContainer>(); foreach (var tmpNode in inNode.ChildNodes()) { if (tmpNode.GetType() == typeof(ClassDeclarationSyntax)) { tmpList.Add(new ClassContainer { Node = tmpNode as ClassDeclarationSyntax }); } tmpList.AddRange(tmpNode.GetClasses()); } return tmpList; }
internal static CompilationUnitSyntax AddUsings(this CompilationUnitSyntax compilationUnit, params string[] usings) { foreach (var @using in usings) { var needsUsing = !compilationUnit.ChildNodes().OfType<UsingDirectiveSyntax>().Any(u => u.Name.ToString().Equals(@using)); if (needsUsing) { var usingDirective = SyntaxFactory.UsingDirective(SyntaxFactory.ParseName(@using)); compilationUnit = compilationUnit.AddUsings(usingDirective).WithAdditionalAnnotations(Formatter.Annotation); } } return compilationUnit; }
public static void PrintTree(this SyntaxNode node, int depth = 0) { var prefix = new string(' ', depth * 2); var annotation = node.GetAnnotations("ltype").FirstOrDefault(); var sannot = null == annotation ? string.Empty : annotation.Data; var head = prefix + node.GetType().Name; head = head.PadRight(50); head += sannot.PadRight(30); head += string.Format("{0:X8} ", node.GetHashCode()); head += node.ToString(); Log.Trace("{0}", head); foreach (var childnode in node.ChildNodes()) { PrintTree(childnode, depth + 1); } }
internal static bool HasUsing(this SyntaxNode @this, string qualifiedName) { if (@this == null) { return false; } if (@this.Kind() == SyntaxKind.UsingDirective) { var usingNode = @this as UsingDirectiveSyntax; if (usingNode.Name.ToFullString() == qualifiedName) { return true; } } return @this.ChildNodes().Where(_ => _.HasUsing(qualifiedName)).Any(); }
/// <summary> /// Adds the string specified using statement to the CompilationUnitSyntax if that using is not already present. /// </summary> /// <remarks> /// The using statement, if inserted, will be followed by a CR/LF. /// </remarks> /// <param name="unit"> /// The type being extended. /// </param> /// <param name="usingString"> /// The string statement such as "System.Diagnostics" or "Microsoft.CodeAnalysis.CSharp.Syntax". /// </param> /// <returns> /// The CompilationUnitSyntax in <paramref name="unit"/>. /// </returns> public static CompilationUnitSyntax AddUsingIfNotPresent(this CompilationUnitSyntax unit, String usingString) { var t = unit.ChildNodes().OfType<UsingDirectiveSyntax>().Where(u => u.Name.ToString().Equals(usingString)); if (!t.Any()) { UsingDirectiveSyntax usingDirective = SyntaxFactoryHelper.QualifiedUsing(usingString); // I'll never understand why WithAdditionalAnnotations(Formatter.Annotation) isn't the default. Picking // up the default formatting should be the default and would make developing rules much easier. unit = unit.AddUsings(usingDirective).WithAdditionalAnnotations(Formatter.Annotation); } return unit; }
public static string GetArgumentNameByColon(this ArgumentSyntax syn) { return syn.ChildNodes().Take(1).OfType<NameColonSyntax>() .SelectMany(r => r.ChildNodes().OfType<IdentifierNameSyntax>()) .Select(r => r.GetText().ToString().Trim()) .FirstOrDefault(); }