forked from UbiquityDotNET/Ubiquity.NET.Utils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemberDeclarationSyntaxExtensions.cs
More file actions
82 lines (75 loc) · 3.95 KB
/
Copy pathMemberDeclarationSyntaxExtensions.cs
File metadata and controls
82 lines (75 loc) · 3.95 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Copyright (c) Ubiquity.NET Contributors. All rights reserved.
// Licensed under the Apache-2.0 WITH LLVM-exception license. See the LICENSE.md file in the project root for full license information.
namespace Ubiquity.NET.CodeAnalysis.Utils
{
/// <summary>Utility class to provide extensions for <see cref="MemberDeclarationSyntax"/></summary>
public static class MemberDeclarationSyntaxExtensions
{
/// <summary>Determines if a <see cref="MemberDeclarationSyntax"/> contains a specified attribute or not</summary>
/// <param name="self"><see cref="MemberDeclarationSyntax"/> to test</param>
/// <param name="attributeName">name of the attribute</param>
/// <returns><see langword="true"/> if the attribute is found or <see langword="false"/> if not</returns>
/// <exception cref="ArgumentNullException"><paramref name="self"/> is <see langword="null"/></exception>
public static bool HasAttribute(this MemberDeclarationSyntax self, string attributeName)
{
return self is not null
? self.TryGetAttribute(attributeName, out _)
: throw new ArgumentNullException(nameof(self));
}
/// <summary>Tries to get an <see cref="AttributeSyntax"/> from a <see cref="MemberDeclarationSyntax"/></summary>
/// <param name="self">The <see cref="MemberDeclarationSyntax"/> to get the attribute from</param>
/// <param name="attributeName">name of the attribute</param>
/// <param name="value">resulting <see cref="AttributeSyntax"/> or <see langword="null"/> if not found</param>
/// <returns><see langword="true"/> if the attribute is found or <see langword="false"/> if not</returns>
public static bool TryGetAttribute(
this MemberDeclarationSyntax self,
string attributeName,
[NotNullWhen(true)] out AttributeSyntax? value
)
{
value = null;
string shortName = attributeName;
if (attributeName.EndsWith("Attribute"))
{
shortName = shortName[..^9];
}
else
{
attributeName += "Attribute";
}
var q = from attributeList in self.AttributeLists
from attribute in attributeList.Attributes
let name = attribute.GetIdentifierName()
where name == attributeName || name == shortName
select attribute;
value = q.FirstOrDefault();
return value != null;
}
/// <summary>Determines if a declaration has an extern modifier</summary>
/// <param name="self"><see cref="MemberDeclarationSyntax"/> to test</param>
/// <returns><see langword="true"/> if <paramref name="self"/> has the modifier or <see langword="false"/> not</returns>
/// <exception cref="ArgumentNullException"><paramref name="self"/> is null</exception>
public static bool IsExtern(this MemberDeclarationSyntax self)
{
return self is not null
? self.Modifiers.HasExtern()
: throw new ArgumentNullException(nameof(self));
}
/// <summary>Determines if a declaration has a partial modifier</summary>
/// <inheritdoc cref="IsExtern(MemberDeclarationSyntax)"/>
public static bool IsPartial(this MemberDeclarationSyntax self)
{
return self is not null
? self.Modifiers.HasPartialKeyword()
: throw new ArgumentNullException(nameof(self));
}
/// <summary>Determines if a declaration has a static modifier</summary>
/// <inheritdoc cref="IsExtern(MemberDeclarationSyntax)"/>
public static bool IsStatic(this MemberDeclarationSyntax self)
{
return self is not null
? self.Modifiers.HasStatic()
: throw new ArgumentNullException(nameof(self));
}
}
}