Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix generator incrementality #8388

Merged
merged 9 commits into from
Jan 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Collections.Immutable;
using System.Windows.Forms.Analyzers;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
Expand All @@ -14,12 +13,13 @@ internal class ApplicationConfigurationGenerator : IIncrementalGenerator
{
private static void Execute(
SourceProductionContext context,
ImmutableArray<SyntaxNode> syntaxNodes,
bool hasSupportedSyntaxNode,
string? projectNamespace,
OutputKind outputKind,
ApplicationConfig? applicationConfig,
Diagnostic? applicationConfigDiagnostics)
{
if (syntaxNodes.IsEmpty)
if (!hasSupportedSyntaxNode)
{
return;
}
Expand All @@ -44,7 +44,7 @@ private static void Execute(
return;
}

string? code = ApplicationConfigurationInitializeBuilder.GenerateInitialize(projectNamespace: GetUserProjectNamespace(syntaxNodes[0]), applicationConfig);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Collecting a bunch of nodes and using only the first node looks suspicious. I just kept the existing behavior, but definitely worth revisiting.

string? code = ApplicationConfigurationInitializeBuilder.GenerateInitialize(projectNamespace, applicationConfig);
if (code is not null)
{
context.AddSource("ApplicationConfiguration.g.cs", code);
Expand All @@ -65,24 +65,25 @@ private static void Execute(

public void Initialize(IncrementalGeneratorInitializationContext context)
{
var outputKindProvider = context.CompilationProvider.Select((compilation, _) => compilation.Options.OutputKind);
var syntaxProvider = context.SyntaxProvider.CreateSyntaxProvider(
predicate: (syntaxNode, _) => IsSupportedSyntaxNode(syntaxNode),
transform: (generatorSyntaxContext, _) => generatorSyntaxContext.Node);
predicate: static (syntaxNode, _) => IsSupportedSyntaxNode(syntaxNode),
transform: static (generatorSyntaxContext, _) => GetUserProjectNamespace(generatorSyntaxContext.Node));

var globalConfig = ProjectFileReader.ReadApplicationConfig(context.AnalyzerConfigOptionsProvider);

var inputs = context.CompilationProvider
var inputs = outputKindProvider
.Combine(syntaxProvider.Collect())
.Combine(globalConfig)
.Select((data, cancellationToken)
=> (Compilation: data.Left.Left,
Nodes: data.Left.Right,
=> (OutputKind: data.Left.Left,
ProjectNamespaces: data.Left.Right,
ApplicationConfig: data.Right.ApplicationConfig,
ApplicationConfigDiagnostics: data.Right.Diagnostic));

context.RegisterSourceOutput(
inputs,
(context, source) => Execute(context, source.Nodes, source.Compilation.Options.OutputKind, source.ApplicationConfig, source.ApplicationConfigDiagnostics));
(context, source) => Execute(context, source.ProjectNamespaces.Length > 0, source.ProjectNamespaces.Length > 0 ? source.ProjectNamespaces[0] : null, source.OutputKind, source.ApplicationConfig, source.ApplicationConfigDiagnostics));
}

public static bool IsSupportedSyntaxNode(SyntaxNode syntaxNode)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,7 @@ internal static partial class ProjectFileReader
return ((ApplicationConfig?)null, diagnostic);
}

ApplicationConfig projectConfig = new()
{
EnableVisualStyles = enableVisualStyles,
DefaultFont = font,
HighDpiMode = highDpiMode,
UseCompatibleTextRendering = useCompatibleTextRendering
};
ApplicationConfig projectConfig = new(enableVisualStyles, font?.ToString(), highDpiMode, useCompatibleTextRendering);
return (projectConfig, null);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ internal static partial class ApplicationConfiguration
/// global::System.Windows.Forms.Application.EnableVisualStyles();
/// global::System.Windows.Forms.Application.SetCompatibleTextRenderingDefault(true);
/// global::System.Windows.Forms.Application.SetHighDpiMode(HighDpiMode.SystemAware);
/// global::System.Windows.Forms.Application.SetDefaultFont(new Font(new FontFamily("Microsoft Sans Serif"), 8.25f, (FontStyle)0, (GraphicsUnit)3));
/// global::System.Windows.Forms.Application.SetDefaultFont(new global::System.Drawing.Font(new global::System.Drawing.FontFamily("Microsoft Sans Serif"), 8.25f, (global::System.Drawing.FontStyle)0, (global::System.Drawing.GraphicsUnit)3));
/// </code>
/// </summary>
public static void Initialize()
{
global::System.Windows.Forms.Application.EnableVisualStyles();
global::System.Windows.Forms.Application.SetCompatibleTextRenderingDefault(true);
global::System.Windows.Forms.Application.SetHighDpiMode(HighDpiMode.SystemAware);
global::System.Windows.Forms.Application.SetDefaultFont(new Font(new FontFamily("Microsoft Sans Serif"), 8.25f, (FontStyle)0, (GraphicsUnit)3));
global::System.Windows.Forms.Application.SetDefaultFont(new global::System.Drawing.Font(new global::System.Drawing.FontFamily("Microsoft Sans Serif"), 8.25f, (global::System.Drawing.FontStyle)0, (global::System.Drawing.GraphicsUnit)3));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ internal static partial class ApplicationConfiguration
/// global::System.Windows.Forms.Application.EnableVisualStyles();
/// global::System.Windows.Forms.Application.SetCompatibleTextRenderingDefault(true);
/// global::System.Windows.Forms.Application.SetHighDpiMode(HighDpiMode.SystemAware);
/// global::System.Windows.Forms.Application.SetDefaultFont(new Font(new FontFamily("Tahoma"), 12f, (FontStyle)0, (GraphicsUnit)3));
/// global::System.Windows.Forms.Application.SetDefaultFont(new global::System.Drawing.Font(new global::System.Drawing.FontFamily("Tahoma"), 12f, (global::System.Drawing.FontStyle)0, (global::System.Drawing.GraphicsUnit)3));
/// </code>
/// </summary>
public static void Initialize()
{
global::System.Windows.Forms.Application.EnableVisualStyles();
global::System.Windows.Forms.Application.SetCompatibleTextRenderingDefault(true);
global::System.Windows.Forms.Application.SetHighDpiMode(HighDpiMode.SystemAware);
global::System.Windows.Forms.Application.SetDefaultFont(new Font(new FontFamily("Tahoma"), 12f, (FontStyle)0, (GraphicsUnit)3));
global::System.Windows.Forms.Application.SetDefaultFont(new global::System.Drawing.Font(new global::System.Drawing.FontFamily("Tahoma"), 12f, (global::System.Drawing.FontStyle)0, (global::System.Drawing.GraphicsUnit)3));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ internal static partial class ApplicationConfiguration
/// global::System.Windows.Forms.Application.EnableVisualStyles();
/// global::System.Windows.Forms.Application.SetCompatibleTextRenderingDefault(true);
/// global::System.Windows.Forms.Application.SetHighDpiMode(HighDpiMode.SystemAware);
/// global::System.Windows.Forms.Application.SetDefaultFont(new Font(Control.DefaultFont.FontFamily, 12f, (FontStyle)3, (GraphicsUnit)6));
/// global::System.Windows.Forms.Application.SetDefaultFont(new global::System.Drawing.Font(global::System.Windows.Forms.Control.DefaultFont.FontFamily, 12f, (global::System.Drawing.FontStyle)3, (global::System.Drawing.GraphicsUnit)6));
/// </code>
/// </summary>
public static void Initialize()
{
global::System.Windows.Forms.Application.EnableVisualStyles();
global::System.Windows.Forms.Application.SetCompatibleTextRenderingDefault(true);
global::System.Windows.Forms.Application.SetHighDpiMode(HighDpiMode.SystemAware);
global::System.Windows.Forms.Application.SetDefaultFont(new Font(Control.DefaultFont.FontFamily, 12f, (FontStyle)3, (GraphicsUnit)6));
global::System.Windows.Forms.Application.SetDefaultFont(new global::System.Drawing.Font(global::System.Windows.Forms.Control.DefaultFont.FontFamily, 12f, (global::System.Drawing.FontStyle)3, (global::System.Drawing.GraphicsUnit)6));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,12 @@ public void ApplicationConfigurationInitializeBuilder_GenerateInitialize_can_han
string expected = File.ReadAllText($@"System\Windows\Forms\Generators\MockData\{GetType().Name}.{expectedFileName}.cs");

string output = ApplicationConfigurationInitializeBuilder.GenerateInitialize(ns,
new ApplicationConfig
{
DefaultFont = null,
EnableVisualStyles = PropertyDefaultValue.EnableVisualStyles,
HighDpiMode = PropertyDefaultValue.DpiMode,
UseCompatibleTextRendering = PropertyDefaultValue.UseCompatibleTextRendering
});
new ApplicationConfig(
EnableVisualStyles: PropertyDefaultValue.EnableVisualStyles,
DefaultFont: null,
HighDpiMode: PropertyDefaultValue.DpiMode,
UseCompatibleTextRendering: PropertyDefaultValue.UseCompatibleTextRendering
));

Assert.Equal(expected, output);
}
Expand All @@ -58,101 +57,93 @@ public static IEnumerable<object[]> GenerateInitializeData()
yield return new object[]
{
culture,
new ApplicationConfig
{
DefaultFont = null,
EnableVisualStyles = false,
HighDpiMode = PropertyDefaultValue.DpiMode,
UseCompatibleTextRendering = PropertyDefaultValue.UseCompatibleTextRendering
},
new ApplicationConfig(
EnableVisualStyles: false,
DefaultFont: null,
HighDpiMode: PropertyDefaultValue.DpiMode,
UseCompatibleTextRendering: PropertyDefaultValue.UseCompatibleTextRendering
),
"EnableVisualStyles=false"
};
yield return new object[]
{
culture,
new ApplicationConfig
{
DefaultFont = null,
EnableVisualStyles = true,
HighDpiMode = PropertyDefaultValue.DpiMode,
UseCompatibleTextRendering = PropertyDefaultValue.UseCompatibleTextRendering
},
new ApplicationConfig(
EnableVisualStyles: true,
DefaultFont: null,
HighDpiMode: PropertyDefaultValue.DpiMode,
UseCompatibleTextRendering: PropertyDefaultValue.UseCompatibleTextRendering
),
"EnableVisualStyles=true"
};

// UseCompatibleTextRendering: false, true
yield return new object[]
{
culture,
new ApplicationConfig
{
DefaultFont = null,
EnableVisualStyles = PropertyDefaultValue.EnableVisualStyles,
HighDpiMode = PropertyDefaultValue.DpiMode,
UseCompatibleTextRendering = false
},
new ApplicationConfig(
EnableVisualStyles: PropertyDefaultValue.EnableVisualStyles,
DefaultFont: null,
HighDpiMode: PropertyDefaultValue.DpiMode,
UseCompatibleTextRendering: false
),
"UseCompTextRendering=false"
};
yield return new object[]
{
culture,
new ApplicationConfig
{
DefaultFont = null,
EnableVisualStyles = PropertyDefaultValue.EnableVisualStyles,
HighDpiMode = PropertyDefaultValue.DpiMode,
UseCompatibleTextRendering = true
},
new ApplicationConfig(
EnableVisualStyles: PropertyDefaultValue.EnableVisualStyles,
DefaultFont: null,
HighDpiMode: PropertyDefaultValue.DpiMode,
UseCompatibleTextRendering: true
),
"UseCompTextRendering=true"
};

// DefaultFont: null, FontDescriptor
yield return new object[]
{
culture,
new ApplicationConfig
{
DefaultFont = null,
EnableVisualStyles = PropertyDefaultValue.EnableVisualStyles,
HighDpiMode = PropertyDefaultValue.DpiMode,
UseCompatibleTextRendering = false
},
new ApplicationConfig(
EnableVisualStyles: PropertyDefaultValue.EnableVisualStyles,
DefaultFont: null,
HighDpiMode: PropertyDefaultValue.DpiMode,
UseCompatibleTextRendering: false
),
"DefaultFont=null"
};
yield return new object[]
{
culture,
new ApplicationConfig
{
DefaultFont = new FontDescriptor(string.Empty, 12, FontStyle.Bold | FontStyle.Italic, GraphicsUnit.Millimeter),
EnableVisualStyles = PropertyDefaultValue.EnableVisualStyles,
HighDpiMode = PropertyDefaultValue.DpiMode,
UseCompatibleTextRendering = true
},
new ApplicationConfig(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Youssef1313 , any specific reason to opt to change order of prams in tests instead of in record constructor?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. It just happened that when I wrote the record, I matched the order here:

ApplicationConfig projectConfig = new()
{
EnableVisualStyles = enableVisualStyles,
DefaultFont = font,
HighDpiMode = highDpiMode,
UseCompatibleTextRendering = useCompatibleTextRendering
};
return (projectConfig, null);

as I was focusing more on the generator updates rather than tests.

EnableVisualStyles: PropertyDefaultValue.EnableVisualStyles,
DefaultFont: new FontDescriptor(string.Empty, 12, FontStyle.Bold | FontStyle.Italic, GraphicsUnit.Millimeter).ToString(),
HighDpiMode: PropertyDefaultValue.DpiMode,
UseCompatibleTextRendering: true
),
"DefaultFont=default"
};
yield return new object[]
{
culture,
new ApplicationConfig
{
DefaultFont = new FontDescriptor("Tahoma", 12, FontStyle.Regular, GraphicsUnit.Point),
EnableVisualStyles = PropertyDefaultValue.EnableVisualStyles,
HighDpiMode = PropertyDefaultValue.DpiMode,
UseCompatibleTextRendering = true
},
new ApplicationConfig(
EnableVisualStyles: PropertyDefaultValue.EnableVisualStyles,
DefaultFont: new FontDescriptor("Tahoma", 12, FontStyle.Regular, GraphicsUnit.Point).ToString(),
HighDpiMode: PropertyDefaultValue.DpiMode,
UseCompatibleTextRendering: true
),
"DefaultFont=Tahoma"
};
yield return new object[]
{
culture,
new ApplicationConfig
{
DefaultFont = new FontDescriptor("Microsoft Sans Serif", 8.25f, FontStyle.Regular, GraphicsUnit.Point),
EnableVisualStyles = PropertyDefaultValue.EnableVisualStyles,
HighDpiMode = PropertyDefaultValue.DpiMode,
UseCompatibleTextRendering = true
},
new ApplicationConfig(
EnableVisualStyles: PropertyDefaultValue.EnableVisualStyles,
DefaultFont: new FontDescriptor("Microsoft Sans Serif", 8.25f, FontStyle.Regular, GraphicsUnit.Point).ToString(),
HighDpiMode: PropertyDefaultValue.DpiMode,
UseCompatibleTextRendering: true
),
"DefaultFont=SansSerif"
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ internal static partial class ApplicationConfiguration
/// global::System.Windows.Forms.Application.EnableVisualStyles();
/// global::System.Windows.Forms.Application.SetCompatibleTextRenderingDefault(true);
/// global::System.Windows.Forms.Application.SetHighDpiMode(HighDpiMode.DpiUnawareGdiScaled);
/// global::System.Windows.Forms.Application.SetDefaultFont(new Font(new FontFamily("Microsoft Sans Serif"), 8.25f, (FontStyle)0, (GraphicsUnit)2));
/// global::System.Windows.Forms.Application.SetDefaultFont(new global::System.Drawing.Font(new global::System.Drawing.FontFamily("Microsoft Sans Serif"), 8.25f, (global::System.Drawing.FontStyle)0, (global::System.Drawing.GraphicsUnit)2));
/// </code>
/// </summary>
public static void Initialize()
{
global::System.Windows.Forms.Application.EnableVisualStyles();
global::System.Windows.Forms.Application.SetCompatibleTextRenderingDefault(true);
global::System.Windows.Forms.Application.{|CS0117:SetHighDpiMode|}({|CS0103:HighDpiMode|}.DpiUnawareGdiScaled);
global::System.Windows.Forms.Application.{|CS0117:SetDefaultFont|}(new Font(new FontFamily("Microsoft Sans Serif"), 8.25f, (FontStyle)0, (GraphicsUnit)2));
global::System.Windows.Forms.Application.{|CS0117:SetDefaultFont|}(new global::System.Drawing.Font(new global::System.Drawing.FontFamily("Microsoft Sans Serif"), 8.25f, (global::System.Drawing.FontStyle)0, (global::System.Drawing.GraphicsUnit)2));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ internal static partial class ApplicationConfiguration
/// global::System.Windows.Forms.Application.EnableVisualStyles();
/// global::System.Windows.Forms.Application.SetCompatibleTextRenderingDefault(true);
/// global::System.Windows.Forms.Application.SetHighDpiMode(HighDpiMode.DpiUnawareGdiScaled);
/// global::System.Windows.Forms.Application.SetDefaultFont(new Font(new FontFamily("Microsoft Sans Serif"), 8.25f, (FontStyle)0, (GraphicsUnit)2));
/// global::System.Windows.Forms.Application.SetDefaultFont(new global::System.Drawing.Font(new global::System.Drawing.FontFamily("Microsoft Sans Serif"), 8.25f, (global::System.Drawing.FontStyle)0, (global::System.Drawing.GraphicsUnit)2));
/// </code>
/// </summary>
public static void Initialize()
{
global::System.Windows.Forms.Application.EnableVisualStyles();
global::System.Windows.Forms.Application.SetCompatibleTextRenderingDefault(true);
global::System.Windows.Forms.Application.{|CS0117:SetHighDpiMode|}({|CS0103:HighDpiMode|}.DpiUnawareGdiScaled);
global::System.Windows.Forms.Application.{|CS0117:SetDefaultFont|}(new Font(new FontFamily("Microsoft Sans Serif"), 8.25f, (FontStyle)0, (GraphicsUnit)2));
global::System.Windows.Forms.Application.{|CS0117:SetDefaultFont|}(new global::System.Drawing.Font(new global::System.Drawing.FontFamily("Microsoft Sans Serif"), 8.25f, (global::System.Drawing.FontStyle)0, (global::System.Drawing.GraphicsUnit)2));
}
}
Loading