-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathSyntaxHelper.cs
53 lines (41 loc) · 1.77 KB
/
SyntaxHelper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Microsoft.CodeAnalysis.CSharp.Syntax;
namespace Gir.Integration.CSharp;
internal static class SyntaxHelper
{
#region Methods
public static AttributeSyntax GetAttributeSyntax(MemberDeclarationSyntax member, string attributeName)
{
if (!TryGetAttributeSyntax(member, attributeName, out var attributeSyntax))
throw new Exception($"Attribute {attributeName} not found.");
return attributeSyntax;
}
public static bool TryGetAttributeSyntax(MemberDeclarationSyntax member, string attributeName, [NotNullWhen(true)] out AttributeSyntax? attributeSyntax)
{
var attributes = member.AttributeLists.SelectMany(x => x.Attributes);
attributeSyntax = attributes.FirstOrDefault(x => x.Name.ToString() == attributeName);
return attributeSyntax is not null;
}
public static string GetFirstArgument(AttributeSyntax attribute)
{
if (attribute.ArgumentList is null)
throw new Exception($"Attribute does not contain an argument");
return attribute.ArgumentList.Arguments.First().ToString();
}
public static IEnumerable<FieldDeclarationSyntax> GetFieldSyntaxes(ClassDeclarationSyntax classDeclarationSyntax)
{
return classDeclarationSyntax.Members.Where(x => x is FieldDeclarationSyntax).Cast<FieldDeclarationSyntax>();
}
public static bool HasAttribute(MemberDeclarationSyntax memberSyntax, string attribute)
{
return TryGetAttributeSyntax(memberSyntax, attribute, out _);
}
public static string GetFirstFieldName(FieldDeclarationSyntax field)
{
return field.Declaration.Variables.First().Identifier.ValueText;
}
#endregion
}