public static bool HasAttribute(this MemberDeclarationSyntax node, string value) { if (node == null) { return(false); } return(node.GetAttributes(value) != null && node.GetAttributes(value).Count > 0); }
public static ValueTuple <SyntaxToken, SyntaxToken> GetFirstAndLastMemberDeclarationTokensAfterAttributes(this MemberDeclarationSyntax node) { Contract.ThrowIfNull(node); // there are no attributes associated with the node. return back first and last token of the node. var attributes = node.GetAttributes(); if (attributes.Count == 0) { return(ValueTuple.Create(node.GetFirstToken(includeZeroWidth: true), node.GetLastToken(includeZeroWidth: true))); } var lastToken = node.GetLastToken(includeZeroWidth: true); var lastAttributeToken = attributes.Last().GetLastToken(includeZeroWidth: true); if (lastAttributeToken.Equals(lastToken)) { return(ValueTuple.Create(default(SyntaxToken), default(SyntaxToken))); } var firstTokenAfterAttribute = lastAttributeToken.GetNextToken(includeZeroWidth: true); // there are attributes, get first token after the tokens belong to attributes return(ValueTuple.Create(firstTokenAfterAttribute, lastToken)); }
private SyntaxNode VisitMemberDeclaration(MemberDeclarationSyntax node, SyntaxNode newNode) { var newMember = ((MemberDeclarationSyntax)newNode).WithoutAttributes(); var excludeSolutionAttr = node.GetAttributes <ExcludeFromSolutionAttribute>().SingleOrDefault(); var isSolutionPart = excludeSolutionAttr != null || node.HasAttribute <ExerciseAttribute>(); if (node is TypeDeclarationSyntax && node.HasAttribute <ExerciseAttribute>() || excludeSolutionAttr != null && (excludeSolutionAttr.ArgumentList == null || (bool)excludeSolutionAttr.GetObjArgument(0))) { Exercise.EthalonSolution += newMember.ToFullString(); } return(isSolutionPart ? null : newMember); }
private void AddCodeBlock(MemberDeclarationSyntax node) { var code = (string)CreateCodeBlock((dynamic)node); var lastBlock = Blocks.LastOrDefault() as CodeBlock; if (lastBlock != null) { lastBlock.Code = lastBlock.Code + "\r\n\r\n" + code; } else { Blocks.Add(new CodeBlock(code, LangId)); } var declarationWithIdentifier = node is MethodDeclarationSyntax || node is ClassDeclarationSyntax || node is PropertyDeclarationSyntax || node is EnumDeclarationSyntax; var identifier = declarationWithIdentifier ? node.Identifier().Text : ""; var showOnlyBody = node.GetAttributes <ShowBodyOnSlideAttribute>().Any(); (Blocks[Blocks.Count - 1] as CodeBlock)?.SourceCodeLabels.Add(new Label { Name = identifier, OnlyBody = showOnlyBody }); }
private SyntaxNode VisitMemberDeclaration(MemberDeclarationSyntax node, SyntaxNode newNode) { var newMember = ((MemberDeclarationSyntax)newNode).WithoutAttributes(); var excludeSolutionAttr = node.GetAttributes <ExcludeFromSolutionAttribute>().SingleOrDefault(); var excludedFromSolution = excludeSolutionAttr != null && (excludeSolutionAttr.ArgumentList == null || (bool)excludeSolutionAttr.GetObjArgument(0)); if (excludedFromSolution) { var nodeName = node is FieldDeclarationSyntax field?field.Declaration.Variables.First().Identifier.Text : node.Identifier().Text; Exercise.ExcludedFromSolution.Add(nodeName); } var isSolutionPart = excludeSolutionAttr != null || node.HasAttribute <ExerciseAttribute>(); if (node is TypeDeclarationSyntax && node.HasAttribute <ExerciseAttribute>() || excludedFromSolution) { Exercise.EthalonSolution += newMember.ToFullString(); } return(isSolutionPart ? null : newMember); }
public static bool HasAttribute <TAttr>(this MemberDeclarationSyntax node) where TAttr : Attribute { return(node.GetAttributes <TAttr>().Any()); }
public static MemberDeclarationSyntax AddAttributeLists( this MemberDeclarationSyntax member, AttributeListSyntax[] attributeLists) { return(member.WithAttributeLists(member.GetAttributes().AddRange(attributeLists))); }