Skip to content

Commit 460052f

Browse files
committed
将调用方法的信息写进注释里
1 parent 7163762 commit 460052f

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

src/DotNetCampus.CommandLine.Analyzer/Generators/InterceptorGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ public InterceptsLocationAttribute(int version, string data)
130130
private string GenerateInterceptsLocationCode(InterceptorGeneratingModel model)
131131
{
132132
return $"""
133-
[global::System.Runtime.CompilerServices.InterceptsLocation({model.InterceptableLocation.Version}, "{model.InterceptableLocation.Data}")]
133+
[global::System.Runtime.CompilerServices.InterceptsLocation({model.InterceptableLocation.Version}, /* {model.InvocationInfo} */ "{model.InterceptableLocation.Data}")]
134134
""";
135135
}
136136

src/DotNetCampus.CommandLine.Analyzer/Generators/ModelProviding/InterceptorModelProvider.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,13 @@ public static IncrementalValuesProvider<InterceptorGeneratingModel> SelectMethod
102102
.ConstructorArguments.FirstOrDefault() is { Kind: TypedConstantKind.Primitive } verbArgument
103103
? verbArgument.Value?.ToString()
104104
: null;
105-
return new InterceptorGeneratingModel(interceptableLocation, symbol, verbName);
105+
// 获取调用代码所在的类和方法。
106+
var methodDeclaration = node.FirstAncestorOrSelf<MethodDeclarationSyntax>();
107+
var classDeclaration = methodDeclaration?.FirstAncestorOrSelf<BaseTypeDeclarationSyntax>();
108+
var invocationFileName = Path.GetFileName(node.SyntaxTree.FilePath);
109+
var invocationInfo = $"{classDeclaration?.Identifier.ToString()}.{methodDeclaration?.Identifier.ToString()} @{invocationFileName}";
110+
111+
return new InterceptorGeneratingModel(interceptableLocation, symbol, verbName, invocationInfo);
106112
})
107113
.Where(model => model is not null)
108114
.Select((model, ct) => model!);
@@ -112,7 +118,8 @@ public static IncrementalValuesProvider<InterceptorGeneratingModel> SelectMethod
112118
internal record InterceptorGeneratingModel(
113119
InterceptableLocation InterceptableLocation,
114120
INamedTypeSymbol CommandObjectType,
115-
string? VerbName
121+
string? VerbName,
122+
string InvocationInfo
116123
)
117124
{
118125
public string GetBuilderTypeName() => CommandObjectGeneratingModel.GetBuilderTypeName(CommandObjectType);

0 commit comments

Comments
 (0)