DeviceDependentGenerator added to tasks.json. It can now generate the files during VS Code builds. Ignore list updated.
This commit is contained in:
1 parent
af7c9491e4
commit
8ea975f2ed
4 files changed
+72
-6
No files matched your search
@@ -54,9 +54,12 @@ public sealed class DeviceDependentGenerator : IIncrementalGenerator
|
||||
IncrementalValueProvider<GeneratorOptions> options = context.AnalyzerConfigOptionsProvider.Select(
|
||||
static (provider, _) => new GeneratorOptions(provider.GlobalOptions));
|
||||
|
||||
context.RegisterSourceOutput(templates.Combine(options), static (productionContext, pair) =>
|
||||
IncrementalValueProvider<(Compilation Compilation, GeneratorOptions Options)> compilationAndOptions =
|
||||
context.CompilationProvider.Combine(options).Select(static (pair, _) => (pair.Left, pair.Right));
|
||||
|
||||
context.RegisterSourceOutput(templates.Combine(compilationAndOptions), static (productionContext, pair) =>
|
||||
{
|
||||
GenerateForTemplate(productionContext, pair.Left, pair.Right);
|
||||
GenerateForTemplate(productionContext, pair.Left, pair.Right.Compilation, pair.Right.Options);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -119,7 +122,7 @@ public sealed class DeviceDependentGenerator : IIncrementalGenerator
|
||||
return TemplateInfo.CreateValid(templateSymbol, enumTypeSymbol, attributeLocation, enumValues, suffix);
|
||||
}
|
||||
|
||||
private static void GenerateForTemplate(SourceProductionContext context, TemplateInfo template, GeneratorOptions options)
|
||||
private static void GenerateForTemplate(SourceProductionContext context, TemplateInfo template, Compilation compilation, GeneratorOptions options)
|
||||
{
|
||||
if (!template.Valid)
|
||||
{
|
||||
@@ -146,6 +149,7 @@ public sealed class DeviceDependentGenerator : IIncrementalGenerator
|
||||
SourceText fileText = templateSyntax.SyntaxTree.GetText(context.CancellationToken);
|
||||
string filePath = templateSyntax.SyntaxTree.FilePath;
|
||||
CSharpParseOptions baseParseOptions = (CSharpParseOptions)templateSyntax.SyntaxTree.Options;
|
||||
SyntaxTree originalTree = templateSyntax.SyntaxTree;
|
||||
|
||||
foreach (string enumValueName in template.EnumValues)
|
||||
{
|
||||
@@ -166,6 +170,8 @@ public sealed class DeviceDependentGenerator : IIncrementalGenerator
|
||||
continue;
|
||||
}
|
||||
|
||||
ReportDeviceSpecificTemplateErrors(context, compilation, originalTree, deviceTree, templateSyntax.Identifier.GetLocation(), enumValueName);
|
||||
|
||||
string generatedTypeName = baseName + enumValueName + template.Suffix;
|
||||
TypeDeclarationSyntax generatedTypeSyntax = TransformTemplateType(deviceTemplateSyntax, generatedTypeName);
|
||||
|
||||
@@ -186,6 +192,45 @@ public sealed class DeviceDependentGenerator : IIncrementalGenerator
|
||||
}
|
||||
}
|
||||
|
||||
private static void ReportDeviceSpecificTemplateErrors(
|
||||
SourceProductionContext context,
|
||||
Compilation compilation,
|
||||
SyntaxTree originalTree,
|
||||
SyntaxTree deviceTree,
|
||||
Location fallbackLocation,
|
||||
string enumValueName)
|
||||
{
|
||||
foreach (Diagnostic diagnostic in deviceTree.GetDiagnostics(context.CancellationToken))
|
||||
{
|
||||
if (diagnostic.Severity == DiagnosticSeverity.Error)
|
||||
{
|
||||
context.ReportDiagnostic(diagnostic);
|
||||
}
|
||||
}
|
||||
|
||||
if (compilation is not CSharpCompilation csharpCompilation)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
CSharpCompilation deviceCompilation = csharpCompilation.ReplaceSyntaxTree(originalTree, deviceTree);
|
||||
foreach (Diagnostic diagnostic in deviceCompilation.GetDiagnostics(context.CancellationToken))
|
||||
{
|
||||
if (diagnostic.Severity != DiagnosticSeverity.Error)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (diagnostic.Location is { Kind: LocationKind.SourceFile } location
|
||||
&& location.SourceTree != deviceTree)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
context.ReportDiagnostic(diagnostic);
|
||||
}
|
||||
}
|
||||
|
||||
private static bool IsTopLevel(TypeDeclarationSyntax typeDeclaration)
|
||||
=> typeDeclaration.Parent is NamespaceDeclarationSyntax
|
||||
|| typeDeclaration.Parent is FileScopedNamespaceDeclarationSyntax
|
||||
|
||||
Reference in new issue
Block a user