diff --git a/src/System.Windows.Forms.Analyzers.CSharp/src/System/Windows/Forms/Generators/ApplicationConfigurationGenerator.cs b/src/System.Windows.Forms.Analyzers.CSharp/src/System/Windows/Forms/Generators/ApplicationConfigurationGenerator.cs index 36ef732c5aa..93b55e37548 100644 --- a/src/System.Windows.Forms.Analyzers.CSharp/src/System/Windows/Forms/Generators/ApplicationConfigurationGenerator.cs +++ b/src/System.Windows.Forms.Analyzers.CSharp/src/System/Windows/Forms/Generators/ApplicationConfigurationGenerator.cs @@ -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; @@ -14,12 +13,13 @@ internal class ApplicationConfigurationGenerator : IIncrementalGenerator { private static void Execute( SourceProductionContext context, - ImmutableArray syntaxNodes, + bool hasSupportedSyntaxNode, + string? projectNamespace, OutputKind outputKind, ApplicationConfig? applicationConfig, Diagnostic? applicationConfigDiagnostics) { - if (syntaxNodes.IsEmpty) + if (!hasSupportedSyntaxNode) { return; } @@ -44,7 +44,7 @@ private static void Execute( return; } - string? code = ApplicationConfigurationInitializeBuilder.GenerateInitialize(projectNamespace: GetUserProjectNamespace(syntaxNodes[0]), applicationConfig); + string? code = ApplicationConfigurationInitializeBuilder.GenerateInitialize(projectNamespace, applicationConfig); if (code is not null) { context.AddSource("ApplicationConfiguration.g.cs", code); @@ -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) diff --git a/src/System.Windows.Forms.Analyzers.CSharp/src/System/Windows/Forms/Generators/ProjectFileReader.cs b/src/System.Windows.Forms.Analyzers.CSharp/src/System/Windows/Forms/Generators/ProjectFileReader.cs index 8f26d2be324..99ba123daa6 100644 --- a/src/System.Windows.Forms.Analyzers.CSharp/src/System/Windows/Forms/Generators/ProjectFileReader.cs +++ b/src/System.Windows.Forms.Analyzers.CSharp/src/System/Windows/Forms/Generators/ProjectFileReader.cs @@ -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); }); diff --git a/src/System.Windows.Forms.Analyzers.CSharp/tests/UnitTests/System/Windows/Forms/Generators/ApplicationConfigurationInitializeBuilderTests.GenerateInitialize_DefaultFont=SansSerif.verified.txt b/src/System.Windows.Forms.Analyzers.CSharp/tests/UnitTests/System/Windows/Forms/Generators/ApplicationConfigurationInitializeBuilderTests.GenerateInitialize_DefaultFont=SansSerif.verified.txt index da42d2f7eac..f76e8661f6f 100644 --- a/src/System.Windows.Forms.Analyzers.CSharp/tests/UnitTests/System/Windows/Forms/Generators/ApplicationConfigurationInitializeBuilderTests.GenerateInitialize_DefaultFont=SansSerif.verified.txt +++ b/src/System.Windows.Forms.Analyzers.CSharp/tests/UnitTests/System/Windows/Forms/Generators/ApplicationConfigurationInitializeBuilderTests.GenerateInitialize_DefaultFont=SansSerif.verified.txt @@ -17,7 +17,7 @@ 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)); /// /// public static void Initialize() @@ -25,6 +25,6 @@ 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)); } } diff --git a/src/System.Windows.Forms.Analyzers.CSharp/tests/UnitTests/System/Windows/Forms/Generators/ApplicationConfigurationInitializeBuilderTests.GenerateInitialize_DefaultFont=Tahoma.verified.txt b/src/System.Windows.Forms.Analyzers.CSharp/tests/UnitTests/System/Windows/Forms/Generators/ApplicationConfigurationInitializeBuilderTests.GenerateInitialize_DefaultFont=Tahoma.verified.txt index 281c844b844..97184b57304 100644 --- a/src/System.Windows.Forms.Analyzers.CSharp/tests/UnitTests/System/Windows/Forms/Generators/ApplicationConfigurationInitializeBuilderTests.GenerateInitialize_DefaultFont=Tahoma.verified.txt +++ b/src/System.Windows.Forms.Analyzers.CSharp/tests/UnitTests/System/Windows/Forms/Generators/ApplicationConfigurationInitializeBuilderTests.GenerateInitialize_DefaultFont=Tahoma.verified.txt @@ -17,7 +17,7 @@ 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)); /// /// public static void Initialize() @@ -25,6 +25,6 @@ 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)); } } diff --git a/src/System.Windows.Forms.Analyzers.CSharp/tests/UnitTests/System/Windows/Forms/Generators/ApplicationConfigurationInitializeBuilderTests.GenerateInitialize_DefaultFont=default.verified.txt b/src/System.Windows.Forms.Analyzers.CSharp/tests/UnitTests/System/Windows/Forms/Generators/ApplicationConfigurationInitializeBuilderTests.GenerateInitialize_DefaultFont=default.verified.txt index 4ba59493dd5..f47084cb332 100644 --- a/src/System.Windows.Forms.Analyzers.CSharp/tests/UnitTests/System/Windows/Forms/Generators/ApplicationConfigurationInitializeBuilderTests.GenerateInitialize_DefaultFont=default.verified.txt +++ b/src/System.Windows.Forms.Analyzers.CSharp/tests/UnitTests/System/Windows/Forms/Generators/ApplicationConfigurationInitializeBuilderTests.GenerateInitialize_DefaultFont=default.verified.txt @@ -17,7 +17,7 @@ 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)); /// /// public static void Initialize() @@ -25,6 +25,6 @@ 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)); } } diff --git a/src/System.Windows.Forms.Analyzers.CSharp/tests/UnitTests/System/Windows/Forms/Generators/ApplicationConfigurationInitializeBuilderTests.cs b/src/System.Windows.Forms.Analyzers.CSharp/tests/UnitTests/System/Windows/Forms/Generators/ApplicationConfigurationInitializeBuilderTests.cs index 07b94220a89..5ddddc20ae9 100644 --- a/src/System.Windows.Forms.Analyzers.CSharp/tests/UnitTests/System/Windows/Forms/Generators/ApplicationConfigurationInitializeBuilderTests.cs +++ b/src/System.Windows.Forms.Analyzers.CSharp/tests/UnitTests/System/Windows/Forms/Generators/ApplicationConfigurationInitializeBuilderTests.cs @@ -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); } @@ -58,25 +57,23 @@ public static IEnumerable 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" }; @@ -84,25 +81,23 @@ public static IEnumerable GenerateInitializeData() 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" }; @@ -110,49 +105,45 @@ public static IEnumerable GenerateInitializeData() 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( + 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" }; } diff --git a/src/System.Windows.Forms.Analyzers.CSharp/tests/UnitTests/System/Windows/Forms/Generators/MockData/ApplicationConfigurationGeneratorTests.GenerateInitialize_user_settings_boilerplate.cs b/src/System.Windows.Forms.Analyzers.CSharp/tests/UnitTests/System/Windows/Forms/Generators/MockData/ApplicationConfigurationGeneratorTests.GenerateInitialize_user_settings_boilerplate.cs index 98b21f3e51a..7e3e726507a 100644 --- a/src/System.Windows.Forms.Analyzers.CSharp/tests/UnitTests/System/Windows/Forms/Generators/MockData/ApplicationConfigurationGeneratorTests.GenerateInitialize_user_settings_boilerplate.cs +++ b/src/System.Windows.Forms.Analyzers.CSharp/tests/UnitTests/System/Windows/Forms/Generators/MockData/ApplicationConfigurationGeneratorTests.GenerateInitialize_user_settings_boilerplate.cs @@ -18,7 +18,7 @@ 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)); /// /// public static void Initialize() @@ -26,7 +26,7 @@ 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)); } } } diff --git a/src/System.Windows.Forms.Analyzers.CSharp/tests/UnitTests/System/Windows/Forms/Generators/MockData/ApplicationConfigurationGeneratorTests.GenerateInitialize_user_top_level.cs b/src/System.Windows.Forms.Analyzers.CSharp/tests/UnitTests/System/Windows/Forms/Generators/MockData/ApplicationConfigurationGeneratorTests.GenerateInitialize_user_top_level.cs index 760d80e44b9..d030c8babd4 100644 --- a/src/System.Windows.Forms.Analyzers.CSharp/tests/UnitTests/System/Windows/Forms/Generators/MockData/ApplicationConfigurationGeneratorTests.GenerateInitialize_user_top_level.cs +++ b/src/System.Windows.Forms.Analyzers.CSharp/tests/UnitTests/System/Windows/Forms/Generators/MockData/ApplicationConfigurationGeneratorTests.GenerateInitialize_user_top_level.cs @@ -17,7 +17,7 @@ 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)); /// /// public static void Initialize() @@ -25,6 +25,6 @@ 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)); } } diff --git a/src/System.Windows.Forms.Analyzers/src/System/Windows/Forms/ApplicationConfig.FontDescriptor.cs b/src/System.Windows.Forms.Analyzers/src/System/Windows/Forms/ApplicationConfig.FontDescriptor.cs index f9041700ea0..97013181df8 100644 --- a/src/System.Windows.Forms.Analyzers/src/System/Windows/Forms/ApplicationConfig.FontDescriptor.cs +++ b/src/System.Windows.Forms.Analyzers/src/System/Windows/Forms/ApplicationConfig.FontDescriptor.cs @@ -7,7 +7,7 @@ namespace System.Windows.Forms.Analyzers { - internal partial class ApplicationConfig + internal partial record ApplicationConfig { public class FontDescriptor { @@ -30,10 +30,10 @@ public override string ToString() string name = Regex.Replace(Name, @"[^\w\d ]", string.Empty); string fontFamily = string.IsNullOrWhiteSpace(name) - ? "Control.DefaultFont.FontFamily" - : $"new FontFamily(\"{name}\")"; + ? "global::System.Windows.Forms.Control.DefaultFont.FontFamily" + : $"new global::System.Drawing.FontFamily(\"{name}\")"; - return $"new Font({fontFamily}, {Size.ToString(CultureInfo.InvariantCulture)}f, (FontStyle){(int)Style}, (GraphicsUnit){(int)Unit})"; + return $"new global::System.Drawing.Font({fontFamily}, {Size.ToString(CultureInfo.InvariantCulture)}f, (global::System.Drawing.FontStyle){(int)Style}, (global::System.Drawing.GraphicsUnit){(int)Unit})"; } } } diff --git a/src/System.Windows.Forms.Analyzers/src/System/Windows/Forms/ApplicationConfig.FontStyle.cs b/src/System.Windows.Forms.Analyzers/src/System/Windows/Forms/ApplicationConfig.FontStyle.cs index 97877a077fa..54eac898d13 100644 --- a/src/System.Windows.Forms.Analyzers/src/System/Windows/Forms/ApplicationConfig.FontStyle.cs +++ b/src/System.Windows.Forms.Analyzers/src/System/Windows/Forms/ApplicationConfig.FontStyle.cs @@ -1,10 +1,10 @@ -// Licensed to the .NET Foundation under one or more agreements. +// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. namespace System.Windows.Forms.Analyzers { - internal partial class ApplicationConfig + internal partial record ApplicationConfig { // Copied from https://github.com/dotnet/runtime/blob/00ee1c18715723e62484c9bc8a14f517455fc3b3/src/libraries/System.Drawing.Common/src/System/Drawing/FontStyle.cs [Flags] diff --git a/src/System.Windows.Forms.Analyzers/src/System/Windows/Forms/ApplicationConfig.GraphicsUnit.cs b/src/System.Windows.Forms.Analyzers/src/System/Windows/Forms/ApplicationConfig.GraphicsUnit.cs index a436eb2a341..f07d12832b9 100644 --- a/src/System.Windows.Forms.Analyzers/src/System/Windows/Forms/ApplicationConfig.GraphicsUnit.cs +++ b/src/System.Windows.Forms.Analyzers/src/System/Windows/Forms/ApplicationConfig.GraphicsUnit.cs @@ -1,10 +1,10 @@ -// Licensed to the .NET Foundation under one or more agreements. +// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. namespace System.Windows.Forms.Analyzers { - internal partial class ApplicationConfig + internal partial record ApplicationConfig { // Copied from https://github.com/dotnet/runtime/blob/00ee1c18715723e62484c9bc8a14f517455fc3b3/src/libraries/System.Drawing.Common/src/System/Drawing/GraphicsUnit.cs public enum GraphicsUnit diff --git a/src/System.Windows.Forms.Analyzers/src/System/Windows/Forms/ApplicationConfig.cs b/src/System.Windows.Forms.Analyzers/src/System/Windows/Forms/ApplicationConfig.cs index c901554407c..56dcd8e6fca 100644 --- a/src/System.Windows.Forms.Analyzers/src/System/Windows/Forms/ApplicationConfig.cs +++ b/src/System.Windows.Forms.Analyzers/src/System/Windows/Forms/ApplicationConfig.cs @@ -2,9 +2,20 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +namespace System.Runtime.CompilerServices +{ + internal static class IsExternalInit + { + } +} + namespace System.Windows.Forms.Analyzers { - internal partial class ApplicationConfig + internal partial record ApplicationConfig( + bool EnableVisualStyles, + string? DefaultFont, + HighDpiMode HighDpiMode, + bool UseCompatibleTextRendering) { public static class PropertyNameCSharp { @@ -27,10 +38,5 @@ public static class PropertyDefaultValue public const HighDpiMode DpiMode = HighDpiMode.SystemAware; public const bool UseCompatibleTextRendering = false; } - - public bool EnableVisualStyles { get; set; } - public FontDescriptor? DefaultFont { get; set; } - public HighDpiMode HighDpiMode { get; set; } - public bool UseCompatibleTextRendering { get; set; } } } diff --git a/src/System.Windows.Forms.Analyzers/tests/UnitTests/System/Windows/Forms/Analyzers/ApplicationConfigTests.FontDescriptor.cs b/src/System.Windows.Forms.Analyzers/tests/UnitTests/System/Windows/Forms/Analyzers/ApplicationConfigTests.FontDescriptor.cs index 5a4059c1fc1..a3c6d61a233 100644 --- a/src/System.Windows.Forms.Analyzers/tests/UnitTests/System/Windows/Forms/Analyzers/ApplicationConfigTests.FontDescriptor.cs +++ b/src/System.Windows.Forms.Analyzers/tests/UnitTests/System/Windows/Forms/Analyzers/ApplicationConfigTests.FontDescriptor.cs @@ -32,14 +32,14 @@ public void FontDescriptor_ctor() } [Theory] - [InlineData("", "new Font(Control.DefaultFont.FontFamily, 10f, (FontStyle)3, (GraphicsUnit)3)")] - [InlineData(" ", "new Font(Control.DefaultFont.FontFamily, 10f, (FontStyle)3, (GraphicsUnit)3)")] - [InlineData("\t", "new Font(Control.DefaultFont.FontFamily, 10f, (FontStyle)3, (GraphicsUnit)3)")] - [InlineData("fontName", "new Font(new FontFamily(\"fontName\"), 10f, (FontStyle)3, (GraphicsUnit)3)")] - [InlineData("\"fontName\"", "new Font(new FontFamily(\"fontName\"), 10f, (FontStyle)3, (GraphicsUnit)3)")] - [InlineData("Name with \tspaces", "new Font(new FontFamily(\"Name with spaces\"), 10f, (FontStyle)3, (GraphicsUnit)3)")] - [InlineData("Name with 'quotes'", "new Font(new FontFamily(\"Name with quotes\"), 10f, (FontStyle)3, (GraphicsUnit)3)")] - [InlineData("Name with \r\n lines", "new Font(new FontFamily(\"Name with lines\"), 10f, (FontStyle)3, (GraphicsUnit)3)")] + [InlineData("", "new global::System.Drawing.Font(global::System.Windows.Forms.Control.DefaultFont.FontFamily, 10f, (global::System.Drawing.FontStyle)3, (global::System.Drawing.GraphicsUnit)3)")] + [InlineData(" ", "new global::System.Drawing.Font(global::System.Windows.Forms.Control.DefaultFont.FontFamily, 10f, (global::System.Drawing.FontStyle)3, (global::System.Drawing.GraphicsUnit)3)")] + [InlineData("\t", "new global::System.Drawing.Font(global::System.Windows.Forms.Control.DefaultFont.FontFamily, 10f, (global::System.Drawing.FontStyle)3, (global::System.Drawing.GraphicsUnit)3)")] + [InlineData("fontName", "new global::System.Drawing.Font(new global::System.Drawing.FontFamily(\"fontName\"), 10f, (global::System.Drawing.FontStyle)3, (global::System.Drawing.GraphicsUnit)3)")] + [InlineData("\"fontName\"", "new global::System.Drawing.Font(new global::System.Drawing.FontFamily(\"fontName\"), 10f, (global::System.Drawing.FontStyle)3, (global::System.Drawing.GraphicsUnit)3)")] + [InlineData("Name with \tspaces", "new global::System.Drawing.Font(new global::System.Drawing.FontFamily(\"Name with spaces\"), 10f, (global::System.Drawing.FontStyle)3, (global::System.Drawing.GraphicsUnit)3)")] + [InlineData("Name with 'quotes'", "new global::System.Drawing.Font(new global::System.Drawing.FontFamily(\"Name with quotes\"), 10f, (global::System.Drawing.FontStyle)3, (global::System.Drawing.GraphicsUnit)3)")] + [InlineData("Name with \r\n lines", "new global::System.Drawing.Font(new global::System.Drawing.FontFamily(\"Name with lines\"), 10f, (global::System.Drawing.FontStyle)3, (global::System.Drawing.GraphicsUnit)3)")] public void FontDescriptor_ToString(string fontName, string expected) { FontDescriptor descriptor = new(fontName, 10f, FontStyle.Bold | FontStyle.Italic, GraphicsUnit.Point); @@ -65,7 +65,7 @@ public void FontDescriptor_ToString_culture_agnostic(string cultureName) FontDescriptor descriptor = new("Microsoft Sans Serif", 8.25f, FontStyle.Bold | FontStyle.Italic, GraphicsUnit.Point); _output.WriteLine(descriptor.ToString()); - Assert.Equal("new Font(new FontFamily(\"Microsoft Sans Serif\"), 8.25f, (FontStyle)3, (GraphicsUnit)3)", descriptor.ToString()); + Assert.Equal("new global::System.Drawing.Font(new global::System.Drawing.FontFamily(\"Microsoft Sans Serif\"), 8.25f, (global::System.Drawing.FontStyle)3, (global::System.Drawing.GraphicsUnit)3)", descriptor.ToString()); } } }