private void InsertUsingEntry(UsingEntry entry) { if (m_Usings == null) { m_Usings = new List <UsingEntry>(); } // find insertion index so usings are sorted by alias int i = 0; while (i < m_Usings.Count && String.CompareOrdinal(entry.alias, m_Usings[i].alias) > 0) { i++; } m_Usings.Insert(i, entry); }
public ClassInformation Create(string filePath) { var fileContent = _fileSystem.File.ReadAllText(filePath); var tree = CSharpSyntaxTree.ParseText(fileContent); var root = tree.GetRoot(); var classDeclaration = root .DescendantNodes() .OfType <ClassDeclarationSyntax>() .FirstOrDefault(); var fullNamespace = root .DescendantNodes() .OfType <NamespaceDeclarationSyntax>().First() .Name .ToString(); var className = classDeclaration?.Identifier.Text; var ctorDeclarations = root.DescendantNodes().OfType <ConstructorDeclarationSyntax>(); var ctors = ctorDeclarations.Select( ctorDecl => { var ctorParams = ctorDecl.DescendantNodes() .OfType <ParameterSyntax>() .Select( f => { var typeName = ((IdentifierNameSyntax)f.Type).Identifier.Text; var parameterName = f.Identifier.Text; return(new Parameter(typeName, parameterName)); }) .ToList(); return(new Constructor(ctorParams)); }).ToList(); var usingEntries = root .DescendantNodes() .OfType <UsingDirectiveSyntax>() .Select(f => UsingEntry.CreateFrom(f.Name.ToString())) .ToList(); var classInfo = new ClassInformation(className, fullNamespace, ctors.First(), usingEntries); return(classInfo); }
private static CompilationUnitSyntax AppendUsings( CompilationUnitSyntax syntaxFactory, ClassInformation classInfo, ITestFramework testFramework) { classInfo.AppendUsing(UsingEntry.CreateFrom("Moq")); classInfo.AppendUsing(testFramework.UsingEntry); classInfo.AppendUsing(UsingEntry.CreateFrom(classInfo.NamespaceDecl)); foreach (var usingName in classInfo.SortedUsingEntries) { syntaxFactory = syntaxFactory .AddUsings( SyntaxFactory.UsingDirective( SyntaxFactory.ParseName(usingName.Value))); } return(syntaxFactory); }
public void AddUsing([Annotations.NotNull] UsingEntry entry) { if (entry == null) { throw new ArgumentNullException("entry"); } var aliasEntry = entry as UsingAliasEntry; if (aliasEntry != null) { AddUsingAlias(aliasEntry); return; } var @namespace = entry.Resolve(this) as NamespaceExpression; if ((@namespace != null) && !_importedNamespaces.Contains(@namespace.Tracker)) { _importedNamespaces.Add(@namespace.Tracker); } }