/// <summary> /// Interpreta una lista de atributos /// </summary> private void ParseAttributeList(SyntaxNode objNode, LanguageStructModel objParent) { AttributeListSyntax objListAttributes = objNode as AttributeListSyntax; // Interpreta los atributos foreach (AttributeSyntax objAttribute in objListAttributes.Attributes) { base.ParseAttributeList(objNode, objParent, objAttribute.ToFullString()); } }
public static Task <Document> RefactorAsync( Document document, AttributeListSyntax attributeList, CancellationToken cancellationToken) { return(document.ReplaceNodeAsync( attributeList, AttributeRefactoring.SplitAttributes(attributeList).Select(f => f.WithFormatterAnnotation()), cancellationToken)); }
public void AddEmptySyntaxList() { var attributes = new AttributeListSyntax[0]; var newMethodDeclaration = SyntaxFactory.MethodDeclaration( SyntaxFactory.ParseTypeName("void"), "M" ); newMethodDeclaration.AddAttributeLists(attributes); }
public static void Write(this AttributeListSyntax syntax, IIndentedTextWriterWrapper textWriter, IContext context) { textWriter.WriteLine("local attributes = {"); textWriter.Indent++; syntax.Attributes.Write(Write, textWriter, context); textWriter.Indent--; textWriter.WriteLine("};"); }
private static void Analyze(SyntaxNodeAnalysisContext context, AttributeListSyntax attributeList, SyntaxToken token) { if (attributeList.SyntaxTree.IsSingleLineSpan(TextSpan.FromBounds(attributeList.Span.End, token.SpanStart))) { DiagnosticHelpers.ReportDiagnostic( context, DiagnosticDescriptors.AddNewLineAfterAttributeList, Location.Create(token.SyntaxTree, token.Span.WithLength(0))); } }
private static void Analyze(SyntaxNodeAnalysisContext context, AttributeListSyntax attributeList, SyntaxNodeOrToken nodeOrToken) { if (attributeList.SyntaxTree.IsSingleLineSpan(TextSpan.FromBounds(attributeList.Span.End, nodeOrToken.SpanStart))) { DiagnosticHelpers.ReportDiagnostic( context, DiagnosticRules.PutAttributeListOnItsOwnLine, Location.Create(nodeOrToken.SyntaxTree, nodeOrToken.Span.WithLength(0))); } }
/// <summary> /// Interpreta una lista de atributos /// </summary> private void ParseAttributeList(SyntaxNode node, LanguageStructModel parent) { AttributeListSyntax objListAttributes = node as AttributeListSyntax; // Interpreta los atributos foreach (AttributeSyntax attribute in objListAttributes.Attributes) { ParseAttributeList(node, parent, attribute.ToFullString()); } }
public override SyntaxNode VisitAttributeList(AttributeListSyntax node) { node = (AttributeListSyntax)base.VisitAttributeList(node); if (node.Attributes.Count == 0) { return(null); } return(node); }
public static AttributeListSyntax TranslateMethodAttributeList(AttributeListSyntax attributeList) { for (int i = 0; i < attributeList.Attributes.Count; i++) { var attribute = attributeList.Attributes.ToList()[i]; AttributeSyntax translatedAttribute = TranslateAttribute(attribute); attributeList = attributeList.ReplaceNode(attribute, translatedAttribute); } return(attributeList); }
public override SyntaxNode VisitAttributeList(AttributeListSyntax node) { var result = base.VisitAttributeList(node) as AttributeListSyntax; if (result.Attributes.Count == 0) { return(null); } return(result); }
public void TestClassAttribute() { foreach (var root in roots) { NamespaceDeclarationSyntax namespaceDeclarationSyntax = (NamespaceDeclarationSyntax)root.Members[0]; ClassDeclarationSyntax classDeclarationSyntax = (ClassDeclarationSyntax)namespaceDeclarationSyntax.Members[0]; classDeclarationSyntax.AttributeLists.Count.Should().Be(1); AttributeListSyntax attributeArgumentSyntax = classDeclarationSyntax.AttributeLists[0]; attributeArgumentSyntax.ToFullString().Trim().Should().Be("[TestClass]"); } }
private MethodDeclarationSyntax CreateTestMethod(string methodName) { AttributeListSyntax attributes = SyntaxFactory.AttributeList(SyntaxFactory.SingletonSeparatedList( SyntaxFactory.Attribute(SyntaxFactory.IdentifierName("TestMethod"))) ).NormalizeWhitespace(); MethodDeclarationSyntax testMethod = SyntaxFactory.MethodDeclaration(SyntaxFactory.ParseTypeName("void"), "Test" + methodName); testMethod = testMethod.AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword)).AddAttributeLists(attributes).AddBodyStatements(SyntaxFactory.ParseStatement("Assert.Fail(\"autogenerated\");")); return(testMethod); }
private ClassDeclarationSyntax CreateTestClass(string className) { AttributeListSyntax attributes = SyntaxFactory.AttributeList(SyntaxFactory.SingletonSeparatedList( SyntaxFactory.Attribute(SyntaxFactory.IdentifierName("TestClass"))) ).NormalizeWhitespace(); ClassDeclarationSyntax testClass = SyntaxFactory.ClassDeclaration("Test" + className); testClass = testClass.AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword)).AddAttributeLists(attributes); return(testClass); }
public override void VisitAttributeList(AttributeListSyntax node) { foreach (AttributeSyntax attribute in node.Attributes) { attribute.Accept(this); } node.Target?.Accept(this); base.VisitAttributeList(node); }
public static IEnumerable <SyntaxToken> CommaTokens(this AttributeListSyntax attributeList) { if (attributeList == null) { throw new ArgumentNullException(nameof(attributeList)); } return(attributeList .ChildTokens() .Where(token => token.IsKind(SyntaxKind.CommaToken))); }
private static AttributeListSyntax GetNewAttributeList(AttributeListSyntax attributeList, ITypeSymbol optionalAttribute, SemanticModel semanticModel) { var attributeName = optionalAttribute.ToMinimalDisplayString(semanticModel, attributeList.SpanStart); attributeName = attributeName.Remove(attributeName.IndexOf("Attribute", System.StringComparison.Ordinal)); return attributeList.AddAttributes( SyntaxFactory.Attribute( SyntaxFactory.ParseName(attributeName))) .WithAdditionalAnnotations(Formatter.Annotation); }
private void HandleArchSpecific(SyntaxNode node, out bool removeNode, out AttributeListSyntax attributeList, ref string name) { var archGroupings = this.owner.map.GetSignatureArchGroupings(node).ToArray(); attributeList = null; removeNode = false; bool appliesToAll = archGroupings.Any(a => a == Architecture.All); if (appliesToAll) { // Get rid of items that are common to all architectures if we're not on x64 removeNode = this.currentArch != Architecture.X64; return; } for (int i = 0; i < archGroupings.Length; i++) { var archGroup = archGroupings[i]; if (archGroup.HasFlag(this.currentArch)) { var fullNameWithArch = GetFullNameWithArch(node, archGroup); lock (this.owner) { // If we've already visited it via another arch, get rid of this one if (this.owner.crossArchVisitedNames.Contains(fullNameWithArch)) { removeNode = true; return; } this.owner.crossArchVisitedNames.Add(fullNameWithArch); } if (i != 0) { name = $"{name}____{i}"; } string archValues = FormatArch(archGroup); string attributeArgs = $"({archValues})"; attributeList = SyntaxFactory.AttributeList( SyntaxFactory.SingletonSeparatedList <AttributeSyntax>( SyntaxFactory.Attribute( SyntaxFactory.ParseName("SupportedArchitecture"), SyntaxFactory.ParseAttributeArgumentList(attributeArgs)))); return; } } }
/// Visited for all AttributeListSyntax nodes /// The method replaces all PreviousAttribute attributes annotating a method by ReplacementAttribute attributes public override SyntaxNode VisitAttributeList(AttributeListSyntax node) { foreach (var pattern in attrReplacementList) { if (GotAttribute(node, pattern.Key)) { string attrVal = GetAttributeValue(node); return(CreateAttribute(pattern.Value, attrVal)); } } return(base.VisitAttributeList(node)); }
bool GotAttribute(AttributeListSyntax node, string containsName) { if (node.Attributes.Any( currentAttribute => currentAttribute.Name.GetText().ToString() == (containsName))) { return(true); } else { return(false); } }
private IEnumerable <IAttribute> CreateFromList(AttributeListSyntax syntaxAsList, IDom parent, SemanticModel model) { var list = new List <IAttribute>(); foreach (var attSyntax in syntaxAsList.Attributes) { var attr = CreateFromItem(attSyntax, parent, model); var rDomAttr = attr as IRoslynDom; list.Add(attr); } return(list); }
private static AttributeListSyntax GetNewAttributeList(AttributeListSyntax attributeList, ITypeSymbol optionalAttribute, SemanticModel semanticModel) { var attributeName = optionalAttribute.ToMinimalDisplayString(semanticModel, attributeList.SpanStart); attributeName = attributeName.Remove(attributeName.IndexOf("Attribute", System.StringComparison.Ordinal)); return(attributeList.AddAttributes( SyntaxFactory.Attribute( SyntaxFactory.ParseName(attributeName))) .WithAdditionalAnnotations(Formatter.Annotation)); }
public void SetUp() { var workspace = new AdhocWorkspace(); var language = LanguageNames.CSharp; _syntaxGenerator = SyntaxGenerator.GetGenerator(workspace, language); _attributeListActions = new AttributeListActions(); var seperatedList = SyntaxFactory.SeparatedList <AttributeSyntax>(); seperatedList.Add(SyntaxFactory.Attribute(SyntaxFactory.ParseName("Test"))); _node = SyntaxFactory.AttributeList(seperatedList); }
AttributeListSyntax FindInList(AttributeListSyntax originalAttributeList, bool add) { if (found) { return(originalAttributeList); } List <AttributeSyntax> attributes = new List <AttributeSyntax>(); foreach (var attribute in originalAttributeList.Attributes) { if (found) { attributes.Add(attribute); continue; } var p = semanticModel.GetSymbolInfo(attribute); if (p.Symbol.ContainingType.Equals(mocklisSymbols.GeneratedCodeAttribute)) { found = true; attributes.Add(typesForSymbols.GeneratedCodeAttribute()); } else if (add && p.Symbol.ContainingType.Equals(mocklisSymbols.MocklisClassAttribute)) { found = true; attributes.Add(attribute); attributes.Add(typesForSymbols.GeneratedCodeAttribute()); } else { attributes.Add(attribute); } } if (found) { var newAttributeList = F.AttributeList(F.SeparatedList(attributes)); if (originalAttributeList.HasLeadingTrivia) { newAttributeList = newAttributeList.WithLeadingTrivia(originalAttributeList.GetLeadingTrivia()); } if (originalAttributeList.HasTrailingTrivia) { newAttributeList = newAttributeList.WithTrailingTrivia(originalAttributeList.GetTrailingTrivia()); } return(newAttributeList); } return(found ? F.AttributeList(F.SeparatedList(attributes)) : originalAttributeList); }
public override Evaluation VisitAttributeList(AttributeListSyntax node) { EvaluationList list = new EvaluationList(); foreach (AttributeSyntax attribute in node.Attributes) { list.Add(attribute.Accept <Evaluation>(this)); } node.Target?.Accept <Evaluation>(this); return(list); }
private static void Analyze(SyntaxNodeAnalysisContext context, ImmutableHashSet <string> whitelist) { AttributeListSyntax attributesNode = (AttributeListSyntax)context.Node; if ( Helper.HasAttribute(attributesNode, context, attribute.Name, attribute.FullName, out var descriptionLocation) && !Helper.IsGeneratedCode(context) && !IsWhitelisted(whitelist, context.SemanticModel, attributesNode.Parent, out var id)) { Diagnostic diagnostic = Diagnostic.Create(attribute.Rule, descriptionLocation, id); context.ReportDiagnostic(diagnostic); } }
public static AttributeListSyntax JoinAttributes(IList <AttributeListSyntax> lists) { AttributeListSyntax list = lists[0]; for (int i = 1; i < lists.Count; i++) { list = list.AddAttributes(lists[i].Attributes.ToArray()); } return(list .WithLeadingTrivia(lists[0].GetLeadingTrivia()) .WithTrailingTrivia(lists.Last().GetTrailingTrivia())); }
/// <summary> /// Adds a compilation unit containing a part of the partial <paramref name="type" />, adding the /// <paramref name="attributes" /> to the type. /// </summary> /// <param name="type">The type the part should be declared for.</param> /// <param name="attributes">The attributes that should be added to the type.</param> protected void AddAttributes([NotNull] INamedTypeSymbol type, [NotNull] AttributeListSyntax attributes) { Requires.NotNull(type, nameof(type)); Requires.NotNull(attributes, nameof(attributes)); var generatedClass = (ClassDeclarationSyntax)Syntax.ClassDeclaration( name: type.Name, typeParameters: type.TypeParameters.Select(t => t.Name), modifiers: DeclarationModifiers.Partial); generatedClass = generatedClass.AddAttributeLists(attributes); AddNamespacedAndNested(type, generatedClass); }
public static T WithAttribute <T>([NotNull] this T node, AttributeListSyntax attributeListSyntax) where T : SyntaxNode { var result = ((SyntaxNode)node).TryCast().Returning <SyntaxNode>() .When <ParameterSyntax>(item => item.WithAttributeLists(item.AttributeLists.Add(attributeListSyntax))) .When <PropertyDeclarationSyntax>(item => item.WithAttributeLists(item.AttributeLists.Add(attributeListSyntax))) .When <MethodDeclarationSyntax>(item => item.WithAttributeLists(item.AttributeLists.Add(attributeListSyntax))) .When <FieldDeclarationSyntax>(item => item.WithAttributeLists(item.AttributeLists.Add(attributeListSyntax))) .When <ClassDeclarationSyntax>(item => item.WithAttributeLists(item.AttributeLists.Add(attributeListSyntax))) .When <InterfaceDeclarationSyntax>(item => item.WithAttributeLists(item.AttributeLists.Add(attributeListSyntax))) .ElseThrow("unsupported node: " + node); return((T)result); }
/// <summary> /// Create the syntax for an attribute. /// </summary> /// <param name="attributes">Attribute(s) to create.</param> /// <returns>The declared syntax list.</returns> public static SyntaxList <AttributeListSyntax> Create(params Attribute[] attributes) { var attributesSyntax = new AttributeListSyntax[attributes.Length]; for (int n = 0; n < attributes.Length; n++) { var attributeSyntax = Create(attributes[n]); attributesSyntax[n] = SyntaxFactory.AttributeList(SyntaxFactory.SingletonSeparatedList(attributeSyntax)); } return (SyntaxFactory.List <AttributeListSyntax>(attributesSyntax)); }
public static Task <Document> FixListAsync( Document document, AttributeListSyntax attributeList, ListFixMode fixMode = ListFixMode.Fix, CancellationToken cancellationToken = default) { return(FixListAsync( document, attributeList, attributeList.OpenBracketToken, attributeList.Attributes, fixMode, cancellationToken)); }
private bool CompareAttributeLists( AttributeListSyntax oldAttributeList, AttributeListSyntax newAttributeList, SyntaxNode newNodeParent, CodeModelEventQueue eventQueue) { return(CompareChildren( CompareAttributes, oldAttributeList.Attributes.AsReadOnlyList(), newAttributeList.Attributes.AsReadOnlyList(), newNodeParent, CodeModelEventType.Unknown, eventQueue)); }
private bool CompareAttributeLists( AttributeListSyntax oldAttributeList, AttributeListSyntax newAttributeList, SyntaxNode newNodeParent, CodeModelEventQueue eventQueue) { return CompareChildren( CompareAttributes, oldAttributeList.Attributes.AsReadOnlyList(), newAttributeList.Attributes.AsReadOnlyList(), newNodeParent, CodeModelEventType.Unknown, eventQueue); }
private static List<AttributeListSyntax> GetNewAttributeList(AttributeListSyntax attributeList, SyntaxTrivia indentationTrivia) { var newAttributeLists = new List<AttributeListSyntax>(); for (var i = 0; i < attributeList.Attributes.Count; i++) { var newAttributes = SyntaxFactory.SingletonSeparatedList( attributeList.Attributes[i].WithLeadingTrivia( attributeList.Attributes[i].GetLeadingTrivia().WithoutLeadingWhitespace())); var newAttributeList = SyntaxFactory.AttributeList(attributeList.Target, newAttributes); newAttributeList = (i == 0) ? newAttributeList.WithLeadingTrivia(attributeList.GetLeadingTrivia()) : newAttributeList.WithLeadingTrivia(indentationTrivia); newAttributeList = (i == (attributeList.Attributes.Count - 1)) ? newAttributeList.WithTrailingTrivia(attributeList.GetTrailingTrivia()) : newAttributeList.WithTrailingTrivia(SyntaxFactory.CarriageReturnLineFeed); newAttributeLists.Add(newAttributeList); } return newAttributeLists; }
/// <summary> /// /// </summary> /// <param name="node"></param> public override sealed void VisitAttributeList(AttributeListSyntax node) { this.OnNodeVisited(node); if (!this.traverseRootOnly) base.VisitAttributeList(node); }
private static void AnalyzeAttributeList(SyntaxNodeAnalysisContext context, AttributeListSyntax attributesList) { var openBracketToken = attributesList.OpenBracketToken; if (openBracketToken.IsMissing || attributesList.IsMissing || !attributesList.Attributes.Any()) { return; } var firstAttribute = attributesList.Attributes[0]; var firstAttributeLineSpan = firstAttribute.GetLineSpan(); if (!firstAttributeLineSpan.IsValid) { return; } var openBracketLineSpan = openBracketToken.GetLineSpan(); if (!openBracketLineSpan.IsValid) { return; } if (openBracketLineSpan.EndLinePosition.Line != firstAttributeLineSpan.StartLinePosition.Line && openBracketLineSpan.EndLinePosition.Line != (firstAttributeLineSpan.StartLinePosition.Line - 1)) { context.ReportDiagnostic(Diagnostic.Create(Descriptor, firstAttribute.GetLocation())); } }
public void VisitAttributeList(AttributeListSyntax node) { if (node == null) throw new ArgumentNullException("node"); node.Validate(); _writer.WriteSyntax(Syntax.OpenBracket); if (_writer.Configuration.Spaces.Other.WithinAttributeBrackets) _writer.WriteSpace(); if (node.Target != AttributeTarget.None) { switch (node.Target) { case AttributeTarget.Assembly: _writer.WriteKeyword(PrinterKeyword.Assembly); break; case AttributeTarget.Event: _writer.WriteKeyword(PrinterKeyword.Event); break; case AttributeTarget.Field: _writer.WriteKeyword(PrinterKeyword.Field); break; case AttributeTarget.Method: _writer.WriteKeyword(PrinterKeyword.Method); break; case AttributeTarget.Module: _writer.WriteKeyword(PrinterKeyword.Module); break; case AttributeTarget.Param: _writer.WriteKeyword(PrinterKeyword.Param); break; case AttributeTarget.Property: _writer.WriteKeyword(PrinterKeyword.Property); break; case AttributeTarget.Return: _writer.WriteKeyword(PrinterKeyword.Return); break; case AttributeTarget.Type: _writer.WriteKeyword(PrinterKeyword.Type); break; default: throw ThrowHelper.InvalidEnumValue(node.Target); } if (_writer.Configuration.Spaces.Other.BeforeColonInAttribute) _writer.WriteSpace(); _writer.WriteSyntax(Syntax.Colon); if (_writer.Configuration.Spaces.Other.AfterColonInAttribute) _writer.WriteSpace(); } bool hadOne = false; foreach (var attribute in node.Attributes) { if (hadOne) _writer.WriteListSeparator(); else hadOne = true; attribute.Accept(this); } if (_writer.Configuration.Spaces.Other.WithinAttributeBrackets) _writer.WriteSpace(); _writer.WriteSyntax(Syntax.CloseBracket); }
/// <summary> /// Normalizes the syntax trees of the <see cref="Compilation" />. /// </summary> protected override Compilation Normalize() { _compilerGeneratedAttribute = (AttributeListSyntax)Syntax.Attribute(typeof(CompilerGeneratedAttribute).FullName); return base.Normalize(); }
public void AddEmptySyntaxList() { var attributes = new AttributeListSyntax[0]; var newMethodDeclaration = SyntaxFactory.MethodDeclaration(SyntaxFactory.ParseTypeName("void"), "M"); Assert.DoesNotThrow(() => newMethodDeclaration.AddAttributeLists(attributes)); }
public static string AttributeList(AttributeListSyntax list) { return string.Join(" ", list.Attributes.Select(SyntaxNode)); }
private RewrittenDocument RewriteDocument(Project project, string documentPath, string documentContent, AttributeListSyntax attrs) { SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(documentContent, CSharpParseOptions.Default.WithPreprocessorSymbols("FRAMEWORK")); SyntaxNode syntaxNode = syntaxTree.GetRoot(); var rewrittenDocument = _auditVariablesRewriter.Rewrite(project.Name, documentPath, syntaxNode); if (attrs != null) rewrittenDocument.AddAttributeLists(attrs); _rewrittenDocumentsStorage.Store(project.Solution.FilePath, project.Name, documentPath, rewrittenDocument.SyntaxTree.GetRoot()); return rewrittenDocument; }
bool HasTestFixtureAttribute(AttributeListSyntax list, SemanticModel model) { return list.Attributes.Any(a => model.GetTypeInfo(a.Name).Type.GetFullName() == "NUnit.Framework.TestFixtureAttribute"); }
/// <summary> /// Normalizes the syntax trees of the <see cref="Compilation" />. /// </summary> protected override Compilation Normalize() { _debuggerHiddenAttribute = (AttributeListSyntax)Syntax.Attribute(typeof(DebuggerHiddenAttribute).FullName); _compilerGeneratedAttribute = (AttributeListSyntax)Syntax.Attribute(typeof(CompilerGeneratedAttribute).FullName); _requiredAttribute = (AttributeListSyntax)Syntax.Attribute(typeof(RequiredAttribute).FullName); _ignoreAttribute = (AttributeListSyntax)Syntax.Attribute(typeof(SuppressTransformationAttribute).FullName); _browsableAttribute = (AttributeListSyntax)Syntax.Attribute(typeof(DebuggerBrowsableAttribute).FullName, Syntax.MemberAccessExpression(Syntax.TypeExpression(Compilation.GetTypeSymbol<DebuggerBrowsableState>()), "Never")); return base.Normalize(); }
private void ClassifyUpdate(AttributeListSyntax oldNode, AttributeListSyntax newNode) { if (!SyntaxFactory.AreEquivalent(oldNode.Target, newNode.Target)) { ReportError(RudeEditKind.Update); return; } // changes in attribute separators are not interesting: }
public static MemberDeclarationSyntax AddAttributeLists( this MemberDeclarationSyntax member, AttributeListSyntax[] attributeLists) { return member.WithAttributeLists(member.GetAttributes().AddRange(attributeLists)); }
public AttributeListTranslation(AttributeListSyntax syntax, SyntaxTranslation parent) : base(syntax, parent) { }
/// <summary> /// /// </summary> /// <param name="node"></param> public override sealed void VisitAttributeList(AttributeListSyntax node) { this.OnNodeVisited(node, this.type.IsInstanceOfType(node)); base.VisitAttributeList(node); }
private static bool ShouldKeepSquareBrackets(AttributeListSyntax attributesParentNode) { return attributesParentNode.Attributes.Count > 1; }
private IEnumerable<ITypeSymbol> InferTypeInAttributeDeclaration(AttributeListSyntax attributeDeclaration, SyntaxToken? previousToken) { // If we have a position, then it has to be after the open bracket. if (previousToken.HasValue && previousToken.Value != attributeDeclaration.OpenBracketToken) { return SpecializedCollections.EmptyEnumerable<ITypeSymbol>(); } return SpecializedCollections.SingletonEnumerable(this.Compilation.AttributeType()); }
public void AddAttributeLists(AttributeListSyntax attrs) { var newNode=((CompilationUnitSyntax) SyntaxTree.GetRoot()).AddAttributeLists(attrs); SyntaxTree = newNode.SyntaxTree; }