public TranslationUnitPorter(CXCursor translationUnit, string ns, IBindingLocator bindingLocator) { if (translationUnit.kind != CXCursorKind.CXCursor_TranslationUnit) throw new ArgumentException (); if (string.IsNullOrWhiteSpace (ns)) throw new ArgumentException (); if (bindingLocator == null) throw new ArgumentNullException (); this.translationUnit = translationUnit; this.bindingLocator = bindingLocator; cu = SyntaxFactory.CompilationUnit (); IEnumerable<UsingDirectiveSyntax> usings = CreateUsings (); foreach (var u in usings) cu = cu.AddUsings (u); var nsDecl = CreateNamespace (ns); foreach (var c in PortClasses ()) nsDecl = nsDecl.AddMembers (c); cu = cu.AddMembers (nsDecl); }
static CompilationUnitSyntax AddRequiredUsings(CompilationUnitSyntax root) { NameSyntax name = SyntaxFactory.IdentifierName(" System"); if (root.Usings.Contains(SyntaxFactory.UsingDirective(name)) == false) return root.AddUsings(SyntaxFactory.UsingDirective(name)); return root; }
private static CompilationUnitSyntax ImportNeededNamespace(CompilationUnitSyntax root, SemanticModel semanticModel, MemberAccessExpressionSyntax callerMethod) { var symbolInfo = semanticModel.GetSymbolInfo(callerMethod.Name); var methodSymbol = symbolInfo.Symbol as IMethodSymbol; if (methodSymbol == null) return root; var namespaceDisplayString = methodSymbol.ContainingNamespace.ToDisplayString(); var hasNamespaceImported = root .DescendantNodes() .OfType<UsingDirectiveSyntax>() .Select(s => s.Name.ToString()) .Any(n => n == namespaceDisplayString); if (!hasNamespaceImported) { var namespaceQualifiedName = methodSymbol.ContainingNamespace.ToNameSyntax(); root = root.AddUsings(SyntaxFactory.UsingDirective(namespaceQualifiedName)); } return root; }
/// <summary> /// Adds a using directive if one doesn't already exist at the top of file /// after existing using directives. /// /// Does not handle the scenarios where usings are defined within an inner node of /// given root node, ex, if the root node is CompilationUnit and usings are defined /// within a Namespace Declaration instead of top of the file, the new using is /// just added at the top of the file. /// </summary> /// <param name="namespaceName">The namespace to be added.</param> /// <param name="rootNode">Parent syntax node for which the childs are examined /// to see if a using with the given namespace already exists</param> /// <returns>A new syntax node containing the new using statement as an immediate /// child of given rootNode. If the using statement is already present, the rootNode /// is returned. Otherwise, a new statement is added at the end of existing /// usings and the new node is returned.</returns> public static CompilationUnitSyntax AddUsingDirectiveIfNeeded(string namespaceName, CompilationUnitSyntax rootNode) { Contract.Assert(rootNode != null); if (String.IsNullOrEmpty(namespaceName)) { return rootNode; } if (rootNode.Usings.Any(usingNode => usingNode.Name.ToString() == namespaceName)) { // Using already present, return this node return rootNode; } var insertTree = CSharpSyntaxTree.ParseText("using " + namespaceName + ";" + Environment.NewLine); var usingStatement = insertTree.GetRoot().ChildNodes().First() as UsingDirectiveSyntax; Debug.Assert(usingStatement != null); return rootNode.AddUsings(usingStatement); }
public static CompilationUnitSyntax AddUsingStatements(this CompilationUnitSyntax compilationUnit, string[] usingStatements) { if (compilationUnit == null) { throw new ArgumentNullException(nameof(compilationUnit)); } if (usingStatements == null) { throw new ArgumentNullException(nameof(usingStatements)); } if (usingStatements.Length == 0) { return(compilationUnit); } compilationUnit = compilationUnit.AddUsings( usingStatements .Select(s => SyntaxFactory.UsingDirective(SyntaxFactory.ParseName(s))) .ToArray()); return(compilationUnit.WithUsings(compilationUnit.Usings.Sort())); }
public static CompilationUnitSyntax AddUsings(this CompilationUnitSyntax unit, IEnumerable <string> usings, string currentNamespace) { usings = usings.Where(x => !currentNamespace.StartsWith(x)).OrderBy(x => x).Distinct(); return(unit.AddUsings(usings.Select(x => SyntaxFactory.UsingDirective(SyntaxFactory.ParseName(x))).ToArray())); }
/// <summary> /// A rearrangement of the AST and a replacement of classes into new namespaces need to happen, this will mess up the references, /// thus we need to transplant the using directives in the new tree as well. /// /// TODO: This approach generates a lot of duplicated using directives. It is not harmful. Make it better. /// </summary> private void RetrieveAllUsingDirectivesAndCopyAtRootLevel() { List<UsingDirectiveSyntax> usingDirectives = new List<UsingDirectiveSyntax>(); // Collecting new MultiPurposeASTWalker(this.newNode, astNode => astNode as UsingDirectiveSyntax != null, delegate (SyntaxNode astNode) { var usingNode = astNode as UsingDirectiveSyntax; usingDirectives.Add(usingNode); }) .Start(); // Copying at root level in the new node newNode = newNode.AddUsings(usingDirectives.ToArray()); // Add using directives for namespaces of types which has been overriden var additionalNamespaces = this.GetOriginalNamespaces().Select(ns => SyntaxFactory.UsingDirective(SyntaxFactory.ParseName(ns)).NormalizeWhitespace()); newNode = newNode.AddUsings(additionalNamespaces.ToArray()); }