dotnet / roslyn

The Roslyn .NET compiler provides C# and Visual Basic languages with rich code analysis APIs.
https://docs.microsoft.com/dotnet/csharp/roslyn-sdk/
MIT License
19.05k stars 4.03k forks source link

SourceGenerator How to distinguish SyntaxNode generated by intelliCode #62820

Closed 84819760 closed 2 years ago

84819760 commented 2 years ago

Intellicode will trigger the generation of a new tree,How to distinguish SyntaxNode generated by intelliCode ? QQ截图20220721150742

CyrusNajmabadi commented 2 years ago

There is no way to distinguish them. Intellicode just spits out normal text which becomes a standard part of the roslyn pipeline. Once emitted there is no way to distinguish it from anything the user may have written themselves.

84819760 commented 2 years ago

I found a way to distinguish. I hope there can be an official way。

CyrusNajmabadi commented 2 years ago

@84819760 what way did you find?

84819760 commented 2 years ago

@CyrusNajmabadi

Can only distinguish whether it is generated by intellicode,This code can also achieve my goal,I hope there can be an official way。

public void Execute(GeneratorExecutionContext context)
{
   var isPrediction = new StackTrace().GetFrames()
        .Select(stackFrame => stackFrame.GetMethod().DeclaringType.Name)
        .Any(typeName => typeName is "SemanticsModelPredictionModifier");
   if (isPrediction) return;
   // code ......
}