public void AddUsingAlias(string alias, string aliasNamespace) { if (UsingAliases.ContainsKey(aliasNamespace)) { return; } UsingAliases.Add(aliasNamespace, alias); }
public void AddImport(Import imp) { _imports.Add(imp); var u = imp.NamespaceExpression.ToTypeReference(CompilerContext.InternProvider); if (imp.Alias == null) { Usings.Add(u as TypeNameExpression); } else { UsingAliases.Add(new KeyValuePair <string, TypeNameExpression>(imp.Alias.Value, u as TypeNameExpression)); } }
public void MergeWith(IOutputWriter cache) { // Remove all crawled items cache .Projects.ForEach(x => { Projects.RemoveAll(y => y.File == x.File); Files.RemoveAll(y => y.Project != null && y.Project.File == x.File); }); cache .Files.ForEach(x => { Files.RemoveAll(y => y.File == x.File); Usings.RemoveAll(y => y.File.File == x.File); UsingAliases.RemoveAll(y => y.File.File == x.File); Namespaces.RemoveAll(y => y.File.File == x.File); Classes.RemoveAll(y => y.File.File == x.File); Interfaces.RemoveAll(y => y.File.File == x.File); Structs.RemoveAll(y => y.File.File == x.File); Enums.RemoveAll(y => y.File.File == x.File); Fields.RemoveAll(y => y.File.File == x.File); Methods.RemoveAll(y => y.File.File == x.File); Parameters.RemoveAll(y => y.File.File == x.File); Variables.RemoveAll(y => y.File.File == x.File); }); // Write new items Projects.AddRange(cache.Projects); Files.AddRange(cache.Files); Usings.AddRange(cache.Usings); UsingAliases.AddRange(cache.UsingAliases); Namespaces.AddRange(cache.Namespaces); Classes.AddRange(cache.Classes); Interfaces.AddRange(cache.Interfaces); Structs.AddRange(cache.Structs); Enums.AddRange(cache.Enums); Fields.AddRange(cache.Fields); Methods.AddRange(cache.Methods); Parameters.AddRange(cache.Parameters); Variables.AddRange(cache.Variables); BuildTypeIndex(); }
protected virtual Microsoft.CodeAnalysis.SyntaxTree ToSyntaxTree(VSGraphModel graphModel, CompilationOptions options) { //TODO fix graph name, do not use the asset name var className = graphModel.TypeName; var baseClass = graphModel.Stencil.GetBaseClass().Name; var classDeclaration = ClassDeclaration(className) .WithModifiers( TokenList( Token(SyntaxKind.PublicKeyword))); if (!String.IsNullOrEmpty(baseClass)) { classDeclaration = classDeclaration.WithBaseList( BaseList( SingletonSeparatedList <BaseTypeSyntax>( SimpleBaseType( IdentifierName(baseClass))))); } if (graphModel.Stencil.addCreateAssetMenuAttribute) { classDeclaration = classDeclaration.WithAttributeLists( SingletonList( AttributeList( SingletonSeparatedList( Attribute( IdentifierName("CreateAssetMenu")) .WithArgumentList( AttributeArgumentList( SeparatedList <AttributeArgumentSyntax>( new SyntaxNodeOrToken[] { AttributeArgument( LiteralExpression( SyntaxKind.StringLiteralExpression, Literal(graphModel.Stencil.fileName))) .WithNameEquals( NameEquals( IdentifierName("fileName"))), Token(SyntaxKind.CommaToken), AttributeArgument( LiteralExpression( SyntaxKind.StringLiteralExpression, Literal(graphModel.Stencil.menuName))) .WithNameEquals( NameEquals( IdentifierName("menuName"))) }))))))); } var allMembers = new List <MemberDeclarationSyntax>(); m_AllFields = new List <MemberDeclarationSyntax>(); var allRemainingMembers = new List <MemberDeclarationSyntax>(); foreach (var fieldDecl in graphModel.GraphVariableModels) { var fieldSyntaxNode = fieldDecl.DeclareField(this); m_AllFields.Add(fieldSyntaxNode); } var entryPoints = graphModel.GetEntryPoints(); Dictionary <string, MethodDeclarationSyntax> declaredMethods = new Dictionary <string, MethodDeclarationSyntax>(); foreach (var stack in entryPoints) { var entrySyntaxNode = BuildNode(stack); foreach (var memberDeclaration in entrySyntaxNode.Cast <MemberDeclarationSyntax>()) { if (memberDeclaration is MethodDeclarationSyntax methodDeclarationSyntax) { string key = methodDeclarationSyntax.Identifier.ToString(); declaredMethods.Add(key, methodDeclarationSyntax); } else { allRemainingMembers.Add(memberDeclaration); } } } allMembers.AddRange(m_AllFields); m_AllFields = null; allMembers.AddRange(allRemainingMembers); if (m_EventRegistrations.Any()) { if (!declaredMethods.TryGetValue("Update", out var method)) { method = RoslynBuilder.DeclareMethod("Update", AccessibilityFlags.Public, typeof(void)) .WithParameterList( ParameterList( SeparatedList( Enumerable.Empty <ParameterSyntax>()))) .WithBody(Block()); } BlockSyntax blockSyntax = Block(m_EventRegistrations.Concat(method.Body.Statements)); method = method.WithBody(blockSyntax); declaredMethods["Update"] = method; } allMembers.AddRange(declaredMethods.Values); classDeclaration = classDeclaration.AddMembers(allMembers.ToArray()); var referencedNamespaces = UsingDirectives.OrderBy(n => n).Select(ns => UsingDirective(ParseName(ns))); var namespaceAliases = UsingAliases.OrderBy(n => n.Key).Select(pair => UsingDirective(ParseName(pair.Key)) .WithAlias(NameEquals( IdentifierName(pair.Value)))); var usings = referencedNamespaces.Concat(namespaceAliases).ToArray(); var compilationUnit = CompilationUnit() .WithUsings( List(usings)) .WithMembers( SingletonList <MemberDeclarationSyntax>(classDeclaration)).NormalizeWhitespace(); return(compilationUnit.SyntaxTree); }
public void WriteUsingAlias(UsingAlias alias) { alias.AllTypesAreResolved = !_visibility; UsingAliases.Add(alias); }