private void DoTestAddInsertRemoveReplaceOnEmptyList(SyntaxTokenList list) { Assert.Equal(0, list.Count); var tokenD = SyntaxFactory.ParseToken("D "); var tokenE = SyntaxFactory.ParseToken("E "); var newList = list.Add(tokenD); Assert.Equal(1, newList.Count); Assert.Equal("D ", newList.ToFullString()); newList = list.AddRange(new[] { tokenD, tokenE }); Assert.Equal(2, newList.Count); Assert.Equal("D E ", newList.ToFullString()); newList = list.Insert(0, tokenD); Assert.Equal(1, newList.Count); Assert.Equal("D ", newList.ToFullString()); newList = list.InsertRange(0, new[] { tokenD, tokenE }); Assert.Equal(2, newList.Count); Assert.Equal("D E ", newList.ToFullString()); newList = list.Remove(tokenD); Assert.Equal(0, newList.Count); Assert.Equal(-1, list.IndexOf(tokenD)); Assert.Throws <ArgumentOutOfRangeException>(() => list.RemoveAt(0)); Assert.Throws <ArgumentOutOfRangeException>(() => list.Insert(1, tokenD)); Assert.Throws <ArgumentOutOfRangeException>(() => list.Insert(-1, tokenD)); Assert.Throws <ArgumentOutOfRangeException>( () => list.InsertRange(-1, new[] { tokenD }) ); Assert.Throws <ArgumentOutOfRangeException>( () => list.InsertRange(list.Count + 1, new[] { tokenD }) ); Assert.Throws <ArgumentOutOfRangeException>(() => list.Replace(tokenD, tokenE)); Assert.Throws <ArgumentOutOfRangeException>( () => list.ReplaceRange(tokenD, new[] { tokenE }) ); Assert.Throws <ArgumentOutOfRangeException>(() => list.Add(default(SyntaxToken))); Assert.Throws <ArgumentOutOfRangeException>(() => list.Insert(0, default(SyntaxToken))); Assert.Throws <ArgumentNullException>( () => list.AddRange((IEnumerable <SyntaxToken>)null) ); Assert.Throws <ArgumentNullException>( () => list.InsertRange(0, (IEnumerable <SyntaxToken>)null) ); }
public static SyntaxTokenList ToList(this IEnumerable <SyntaxToken> enumerable) { var stl = new SyntaxTokenList(); stl = stl.AddRange(enumerable); return(stl); }
public override VB.VisualBasicSyntaxNode VisitXmlCrefAttribute(CSS.XmlCrefAttributeSyntax node) { VBS.XmlNameSyntax Name = (VBS.XmlNameSyntax)node.Name.Accept(this); var cref = node.Cref.Accept(this); var SyntaxTokens = new SyntaxTokenList(); SyntaxTokens = SyntaxTokens.AddRange(cref.DescendantTokens()); VBS.XmlNodeSyntax Value = VBFactory.XmlString(global::VisualBasicSyntaxFactory.DoubleQuoteToken, SyntaxTokens, global::VisualBasicSyntaxFactory.DoubleQuoteToken); return VBFactory.XmlAttribute(Name, Value); }
public static SyntaxTokenList WithClassMemberInheritanceModifier( this SyntaxTokenList tokens, ClassMemberInheritanceModifier modifier, bool isAsync = false) { tokens = tokens.Remove(inheritanceModifierTokens); IList <SyntaxKind> kinds = new List <SyntaxKind>(); switch (modifier) { case ClassMemberInheritanceModifier.New: kinds.Add(SyntaxKind.NewKeyword); break; case ClassMemberInheritanceModifier.NewStatic: kinds.Add(SyntaxKind.NewKeyword); kinds.Add(SyntaxKind.StaticKeyword); break; case ClassMemberInheritanceModifier.NewVirtual: kinds.Add(SyntaxKind.NewKeyword); kinds.Add(SyntaxKind.VirtualKeyword); break; case ClassMemberInheritanceModifier.Override: kinds.Add(SyntaxKind.OverrideKeyword); break; case ClassMemberInheritanceModifier.SealedOverride: kinds.Add(SyntaxKind.SealedKeyword); kinds.Add(SyntaxKind.OverrideKeyword); break; case ClassMemberInheritanceModifier.Static: kinds.Add(SyntaxKind.StaticKeyword); break; case ClassMemberInheritanceModifier.Virtual: kinds.Add(SyntaxKind.VirtualKeyword); break; } if (kinds.Count != 0) { tokens = isAsync ? tokens.InsertRange(tokens.Count - 1, kinds) : tokens.AddRange(kinds); } return(tokens); }
private async Task<Document> AddPublicAccessModifierAsync(Document document, InterfaceDeclarationSyntax interfaceDeclaration, CancellationToken cancellationToken) { SyntaxTokenList newModifiers = new SyntaxTokenList(); newModifiers = newModifiers.Add(SyntaxFactory.Token(SyntaxKind.PublicKeyword)); newModifiers = newModifiers.AddRange(interfaceDeclaration.Modifiers.Where(x => !x.IsKind(SyntaxKind.ProtectedKeyword) && !x.IsKind(SyntaxKind.InternalKeyword) && !x.IsKind(SyntaxKind.PrivateKeyword)) ); InterfaceDeclarationSyntax newInterfaceDeclaration = interfaceDeclaration.WithModifiers(newModifiers); SyntaxNode root = await document.GetSyntaxRootAsync(cancellationToken); SyntaxNode newRoot = root.ReplaceNode(interfaceDeclaration, newInterfaceDeclaration); return document.WithSyntaxRoot(newRoot); }
/// <summary> /// Generates the class. /// </summary> public static ClassDeclarationSyntax GenerateClass(string className, SyntaxKind[] modifiers, MemberDeclarationSyntax[] methods, AttributeSyntax[] attributes = null) { var list = new SyntaxTokenList(); list = list.AddRange(modifiers.Select(SyntaxFactory.Token).ToArray()); var classDeclaration = SyntaxFactory.ClassDeclaration(className) .WithModifiers( list ) .WithMembers( SyntaxFactory.List(methods) ); if (attributes != null) { classDeclaration = classDeclaration.WithAttributeLists(SyntaxFactory.List(new[] { SyntaxFactory.AttributeList(SyntaxFactory.SeparatedList(attributes)) })); } return(classDeclaration); }
public static SyntaxTokenList WithClassFieldModifier(this SyntaxTokenList tokens, ClassFieldModifier modifier) { tokens = tokens.Remove(classFieldModifierTokens); switch (modifier) { case ClassFieldModifier.New: return(tokens.Add(SyntaxKind.NewKeyword)); case ClassFieldModifier.NewReadOnly: return(tokens.AddRange(SyntaxKind.NewKeyword, SyntaxKind.ReadOnlyKeyword)); case ClassFieldModifier.NewStatic: return(tokens.AddRange(SyntaxKind.NewKeyword, SyntaxKind.StaticKeyword)); case ClassFieldModifier.NewStaticReadOnly: return(tokens.AddRange(SyntaxKind.NewKeyword, SyntaxKind.StaticKeyword, SyntaxKind.ReadOnlyKeyword)); case ClassFieldModifier.NewStaticVolatile: return(tokens.AddRange(SyntaxKind.NewKeyword, SyntaxKind.StaticKeyword, SyntaxKind.VolatileKeyword)); case ClassFieldModifier.NewVolatile: return(tokens.AddRange(SyntaxKind.NewKeyword, SyntaxKind.VolatileKeyword)); case ClassFieldModifier.ReadOnly: return(tokens.Add(SyntaxKind.ReadOnlyKeyword)); case ClassFieldModifier.Static: return(tokens.Add(SyntaxKind.StaticKeyword)); case ClassFieldModifier.StaticReadOnly: return(tokens.AddRange(SyntaxKind.StaticKeyword, SyntaxKind.ReadOnlyKeyword)); case ClassFieldModifier.StaticVolatile: return(tokens.AddRange(SyntaxKind.StaticKeyword, SyntaxKind.VolatileKeyword)); case ClassFieldModifier.Volatile: return(tokens.Add(SyntaxKind.VolatileKeyword)); } return(tokens); }
private void DoTestAddInsertRemoveReplaceOnEmptyList(SyntaxTokenList list) { Assert.Equal(0, list.Count); var tokenD = SyntaxFactory.ParseToken("D "); var tokenE = SyntaxFactory.ParseToken("E "); var newList = list.Add(tokenD); Assert.Equal(1, newList.Count); Assert.Equal("D ", newList.ToFullString()); newList = list.AddRange(new[] { tokenD, tokenE }); Assert.Equal(2, newList.Count); Assert.Equal("D E ", newList.ToFullString()); newList = list.Insert(0, tokenD); Assert.Equal(1, newList.Count); Assert.Equal("D ", newList.ToFullString()); newList = list.InsertRange(0, new[] { tokenD, tokenE }); Assert.Equal(2, newList.Count); Assert.Equal("D E ", newList.ToFullString()); newList = list.Remove(tokenD); Assert.Equal(0, newList.Count); Assert.Equal(-1, list.IndexOf(tokenD)); Assert.Throws<ArgumentOutOfRangeException>(() => list.RemoveAt(0)); Assert.Throws<ArgumentOutOfRangeException>(() => list.Insert(1, tokenD)); Assert.Throws<ArgumentOutOfRangeException>(() => list.Insert(-1, tokenD)); Assert.Throws<ArgumentOutOfRangeException>(() => list.InsertRange(-1, new[] { tokenD })); Assert.Throws<ArgumentOutOfRangeException>(() => list.InsertRange(list.Count + 1, new[] { tokenD })); Assert.Throws<ArgumentOutOfRangeException>(() => list.Replace(tokenD, tokenE)); Assert.Throws<ArgumentOutOfRangeException>(() => list.ReplaceRange(tokenD, new[] { tokenE })); Assert.Throws<ArgumentOutOfRangeException>(() => list.Add(default(SyntaxToken))); Assert.Throws<ArgumentOutOfRangeException>(() => list.Insert(0, default(SyntaxToken))); Assert.Throws<ArgumentNullException>(() => list.AddRange((IEnumerable<SyntaxToken>)null)); Assert.Throws<ArgumentNullException>(() => list.InsertRange(0, (IEnumerable<SyntaxToken>)null)); }
public static SyntaxTokenList AddRange(this SyntaxTokenList tokens, params SyntaxKind[] kinds) { return(tokens.AddRange((IEnumerable <SyntaxKind>)kinds)); }
public static SyntaxTokenList AddRange(this SyntaxTokenList tokens, IEnumerable <SyntaxKind> kinds) { return(tokens.AddRange(kinds.Select(SyntaxFactory.Token))); }
static ClassDeclarationSyntax GenerateNewClassDeclarationForDescriptions(BaseTypeDeclarationSyntax classDeclaration, IReadOnlyCollection <LambdaJobDescription> descriptionsInClass) { var modifierList = new SyntaxTokenList(); if (classDeclaration.Modifiers.All(modifier => modifier.Kind() != SyntaxKind.UnsafeKeyword)) { modifierList = modifierList.Add(SyntaxFactory.Token(SyntaxKind.UnsafeKeyword)); } modifierList = modifierList.AddRange(classDeclaration.Modifiers); // Create new partial class and mark it as [CompilerGenerated] var newClassDeclaration = SyntaxFactory.ClassDeclaration(classDeclaration.Identifier) .WithBaseList(classDeclaration.BaseList) .WithModifiers(modifierList) .WithAttributeLists(SourceGenHelpers.AttributeListFromAttributeName("System.Runtime.CompilerServices.CompilerGenerated")); // Replace class methods containing lambda job descriptions foreach (var methodDeclaration in classDeclaration.DescendantNodes().OfType <MethodDeclarationSyntax>()) { var descriptionsInMethod = descriptionsInClass.Where(desc => desc.ContainingMethod == methodDeclaration); if (!descriptionsInMethod.Any()) { continue; } SourceGenHelpers.LogInfo($" Generating code for method: {methodDeclaration.Identifier}"); // Generate partial classes and methods for found lambda jobs var methodReplacements = new Dictionary <SyntaxNode, SyntaxNode>(); foreach (var description in descriptionsInMethod) { methodReplacements.Add(description.ContainingInvocationExpression, EntitiesSourceFactory.SchedulingInvocationFor(description)); } var methodReplacer = new SyntaxNodeReplacer(methodReplacements); var rewrittenMethod = (MethodDeclarationSyntax)methodReplacer.Visit(methodDeclaration); if (rewrittenMethod == methodDeclaration) { continue; } // Add rewritten method var newModifiers = new SyntaxTokenList(rewrittenMethod.Modifiers.Where(m => !m.IsKind(SyntaxKind.OverrideKeyword))); var dotsCompilerPatchedMethodArguments = SyntaxFactory.ParseAttributeArgumentList($"(\"{methodDeclaration.Identifier.Text}\")"); var dotsCompilerPatchedMethodAttribute = SyntaxFactory.Attribute(SyntaxFactory.IdentifierName("Unity.Entities.DOTSCompilerPatchedMethod"), dotsCompilerPatchedMethodArguments); var attributeList = new SyntaxList <AttributeListSyntax>(); attributeList = attributeList.Add(SyntaxFactory.AttributeList(SyntaxFactory.SeparatedList(new[] { dotsCompilerPatchedMethodAttribute }))); newClassDeclaration = newClassDeclaration.AddMembers( rewrittenMethod .WithoutPreprocessorTrivia() .WithIdentifier(SyntaxFactory.Identifier(methodDeclaration.Identifier.Text + $"_{Path.GetFileNameWithoutExtension(Path.GetRandomFileName())}")) .WithModifiers(newModifiers) .WithAttributeLists(attributeList) ); // Add all lambdajob members foreach (var description in descriptionsInClass.Where(desc => desc.ContainingMethod == methodDeclaration)) { newClassDeclaration = newClassDeclaration.AddMembers(EntitiesSourceFactory.JobStructFor(description)); if (description.LambdaJobKind == LambdaJobKind.Entities) { newClassDeclaration = newClassDeclaration.AddMembers(EntitiesSourceFactory.EntityQueryFieldFor(description)); } newClassDeclaration = newClassDeclaration.AddMembers(EntitiesSourceFactory.ExecuteMethodFor(description)); if (description.WithStructuralChangesAndLambdaBodyInSystem) { newClassDeclaration = newClassDeclaration.AddMembers(EntitiesSourceFactory.LambdaBodyMethodFor(description)); } } } // Add OnCreateForCompiler method newClassDeclaration = newClassDeclaration.AddMembers(EntitiesSourceFactory.OnCreateForCompilerMethodFor(descriptionsInClass)); return(newClassDeclaration); }