-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathSampleHintElementAnalyzer.cs
61 lines (53 loc) · 2.05 KB
/
SampleHintElementAnalyzer.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
54
55
56
57
58
59
60
61
using System.Linq;
using JetBrains.Metadata.Reader.Impl;
using JetBrains.ReSharper.Feature.Services.Daemon;
using JetBrains.ReSharper.Psi;
using JetBrains.ReSharper.Psi.CSharp.Tree;
using JetBrains.ReSharper.Psi.Tree;
namespace ReSharperPlugin.InlayHints;
/*
using Xunit;
namespace ClassLibrary2;
public class Class1
{
[Theory]
[InlineData("a", 4)] // <-- on push-to-hint
public void Test(string a, int b)
{
}
}
*/
[ElementProblemAnalyzer(
typeof(IAttribute),
HighlightingTypes = new[] { typeof(SampleInlayHint) })]
public class SampleHintElementAnalyzer : ElementProblemAnalyzer<IAttribute>
{
private readonly ClrTypeName _theoryAttributeName = new("Xunit.TheoryAttribute");
private readonly ClrTypeName _inlineDataAttributeName = new("Xunit.InlineDataAttribute");
protected override void Run(
IAttribute element,
ElementProblemAnalyzerData data,
IHighlightingConsumer consumer)
{
var attributeElement = element.TypeReference?.Resolve().DeclaredElement as ITypeElement;
if (!Equals(attributeElement?.GetClrName(), _theoryAttributeName))
return;
var declaration = CSharpFunctionDeclarationNavigator.GetByAttribute(element);
var method = (IMethod)declaration?.DeclaredElement;
if (method == null)
return;
var parameterNames = method.Parameters.Select(x => x.ShortName).ToList();
var attributes = declaration.Attributes.Where(x => x.Name.ShortName == "InlineData");
// var attributes = method.GetAttributeInstances(_inlineDataAttributeName, inherit: false);
foreach (var attribute in attributes)
{
var parameters = attribute.ConstructorArgumentExpressions;
// var parameters = attribute.PositionParameters().ToList();
for (var i = 0; i < parameters.Count; i++)
{
var parameter = parameters[i];
consumer.AddHighlighting(new SampleInlayHint(parameterNames[i], parameter, parameter.GetDocumentStartOffset()));
}
}
}
}