diff --git a/src/Analyzers/CSharp/Analyzers/CodeStyle/CSharpAnalyzerOptionsProvider.cs b/src/Analyzers/CSharp/Analyzers/CodeStyle/CSharpAnalyzerOptionsProvider.cs index e90703700a3e0..9ab3e53943d31 100644 --- a/src/Analyzers/CSharp/Analyzers/CodeStyle/CSharpAnalyzerOptionsProvider.cs +++ b/src/Analyzers/CSharp/Analyzers/CodeStyle/CSharpAnalyzerOptionsProvider.cs @@ -51,6 +51,10 @@ public CSharpAnalyzerOptionsProvider(AnalyzerConfigOptions options, AnalyzerOpti public CodeStyleOption2 NamespaceDeclarations => GetOption(CSharpCodeStyleOptions.NamespaceDeclarations, FallbackSyntaxFormattingOptions.NamespaceDeclarations); public CodeStyleOption2 PreferTopLevelStatements => GetOption(CSharpCodeStyleOptions.PreferTopLevelStatements, FallbackSyntaxFormattingOptions.PreferTopLevelStatements); + // AddImportPlacementOptions + + public CodeStyleOption2 UsingDirectivePlacement => GetOption(CSharpCodeStyleOptions.PreferredUsingDirectivePlacement, FallbackAddImportPlacementOptions.UsingDirectivePlacement); + // CodeStyleOptions public CodeStyleOption2 ImplicitObjectCreationWhenTypeIsApparent => GetOption(CSharpCodeStyleOptions.ImplicitObjectCreationWhenTypeIsApparent, FallbackCodeStyleOptions.ImplicitObjectCreationWhenTypeIsApparent); @@ -83,7 +87,6 @@ public CSharpAnalyzerOptionsProvider(AnalyzerConfigOptions options, AnalyzerOpti public CodeStyleOption2 PreferExpressionBodiedLambdas => GetOption(CSharpCodeStyleOptions.PreferExpressionBodiedLambdas, FallbackCodeStyleOptions.PreferExpressionBodiedLambdas); public CodeStyleOption2 PreferStaticLocalFunction => GetOption(CSharpCodeStyleOptions.PreferStaticLocalFunction, FallbackCodeStyleOptions.PreferStaticLocalFunction); - public CodeStyleOption2 PreferredUsingDirectivePlacement => GetOption(CSharpCodeStyleOptions.PreferredUsingDirectivePlacement, FallbackCodeStyleOptions.PreferredUsingDirectivePlacement); private TValue GetOption(Option2 option, TValue defaultValue) => _options.GetEditorConfigOption(option, defaultValue); @@ -97,6 +100,9 @@ private CSharpSimplifierOptions FallbackSimplifierOptions private CSharpSyntaxFormattingOptions FallbackSyntaxFormattingOptions => (CSharpSyntaxFormattingOptions?)_fallbackOptions.CleanupOptions?.FormattingOptions ?? CSharpSyntaxFormattingOptions.Default; + private AddImportPlacementOptions FallbackAddImportPlacementOptions + => _fallbackOptions.CleanupOptions?.AddImportOptions ?? AddImportPlacementOptions.Default; + public static explicit operator CSharpAnalyzerOptionsProvider(AnalyzerOptionsProvider provider) => new(provider.GetAnalyzerConfigOptions(), provider.GetFallbackOptions()); diff --git a/src/Analyzers/CSharp/Analyzers/MisplacedUsingDirectives/MisplacedUsingDirectivesDiagnosticAnalyzer.cs b/src/Analyzers/CSharp/Analyzers/MisplacedUsingDirectives/MisplacedUsingDirectivesDiagnosticAnalyzer.cs index 8926f53598e43..3ad7b5d86f483 100644 --- a/src/Analyzers/CSharp/Analyzers/MisplacedUsingDirectives/MisplacedUsingDirectivesDiagnosticAnalyzer.cs +++ b/src/Analyzers/CSharp/Analyzers/MisplacedUsingDirectives/MisplacedUsingDirectivesDiagnosticAnalyzer.cs @@ -56,7 +56,7 @@ protected override void InitializeWorker(AnalysisContext context) private void AnalyzeNamespaceNode(SyntaxNodeAnalysisContext context) { - var option = context.GetCSharpAnalyzerOptions().PreferredUsingDirectivePlacement; + var option = context.GetCSharpAnalyzerOptions().UsingDirectivePlacement; if (option.Value != AddImportPlacement.OutsideNamespace) return; @@ -66,7 +66,7 @@ private void AnalyzeNamespaceNode(SyntaxNodeAnalysisContext context) private static void AnalyzeCompilationUnitNode(SyntaxNodeAnalysisContext context) { - var option = context.GetCSharpAnalyzerOptions().PreferredUsingDirectivePlacement; + var option = context.GetCSharpAnalyzerOptions().UsingDirectivePlacement; var compilationUnit = (CompilationUnitSyntax)context.Node; if (option.Value != AddImportPlacement.InsideNamespace diff --git a/src/Analyzers/CSharp/CodeFixes/MisplacedUsingDirectives/MisplacedUsingDirectivesCodeFixProvider.cs b/src/Analyzers/CSharp/CodeFixes/MisplacedUsingDirectives/MisplacedUsingDirectivesCodeFixProvider.cs index 14ca40c4abe33..00e7438ad6932 100644 --- a/src/Analyzers/CSharp/CodeFixes/MisplacedUsingDirectives/MisplacedUsingDirectivesCodeFixProvider.cs +++ b/src/Analyzers/CSharp/CodeFixes/MisplacedUsingDirectives/MisplacedUsingDirectivesCodeFixProvider.cs @@ -64,7 +64,7 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context) // Read the preferred placement option and verify if it can be applied to this code file. // There are cases where we will not be able to fix the diagnostic and the user will need to resolve // it manually. - var (placement, preferPreservation) = DeterminePlacement(compilationUnit, options.PreferredUsingDirectivePlacement); + var (placement, preferPreservation) = DeterminePlacement(compilationUnit, options.UsingDirectivePlacement); if (preferPreservation) return; diff --git a/src/EditorFeatures/CSharp/AddImports/CSharpAddImportPlacementOptionsStorage.cs b/src/EditorFeatures/CSharp/AddImports/CSharpAddImportPlacementOptionsStorage.cs index ad0d107bfa788..619fbad20b679 100644 --- a/src/EditorFeatures/CSharp/AddImports/CSharpAddImportPlacementOptionsStorage.cs +++ b/src/EditorFeatures/CSharp/AddImports/CSharpAddImportPlacementOptionsStorage.cs @@ -31,6 +31,6 @@ public AddImportPlacementOptions GetOptions(IGlobalOptionService globalOptions) internal static AddImportPlacementOptions GetCSharpAddImportPlacementOptions(IGlobalOptionService globalOptions) => new( PlaceSystemNamespaceFirst: globalOptions.GetOption(GenerationOptions.PlaceSystemNamespaceFirst, LanguageNames.CSharp), - PlaceImportsInsideNamespaces: globalOptions.GetOption(CSharpCodeStyleOptions.PreferredUsingDirectivePlacement).Value == AddImportPlacement.InsideNamespace, - AllowInHiddenRegions: false); // no global option available); + UsingDirectivePlacement: globalOptions.GetOption(CSharpCodeStyleOptions.PreferredUsingDirectivePlacement), + AllowInHiddenRegions: AddImportPlacementOptions.Default.AllowInHiddenRegions); // no global option available); } diff --git a/src/EditorFeatures/CSharp/CodeStyle/CSharpCodeStyleOptionsStorage.cs b/src/EditorFeatures/CSharp/CodeStyle/CSharpCodeStyleOptionsStorage.cs index 945b10c050938..9b53c7d51c1e2 100644 --- a/src/EditorFeatures/CSharp/CodeStyle/CSharpCodeStyleOptionsStorage.cs +++ b/src/EditorFeatures/CSharp/CodeStyle/CSharpCodeStyleOptionsStorage.cs @@ -55,6 +55,5 @@ public static CSharpIdeCodeStyleOptions GetCSharpCodeStyleOptions(this IGlobalOp UnusedValueAssignment: globalOptions.GetOption(CSharpCodeStyleOptions.UnusedValueAssignment), PreferMethodGroupConversion: globalOptions.GetOption(CSharpCodeStyleOptions.PreferMethodGroupConversion), PreferExpressionBodiedLambdas: globalOptions.GetOption(CSharpCodeStyleOptions.PreferExpressionBodiedLambdas), - PreferStaticLocalFunction: globalOptions.GetOption(CSharpCodeStyleOptions.PreferStaticLocalFunction), - PreferredUsingDirectivePlacement: globalOptions.GetOption(CSharpCodeStyleOptions.PreferredUsingDirectivePlacement)); + PreferStaticLocalFunction: globalOptions.GetOption(CSharpCodeStyleOptions.PreferStaticLocalFunction)); } diff --git a/src/EditorFeatures/CSharpTest/Formatting/CodeCleanupTests.cs b/src/EditorFeatures/CSharpTest/Formatting/CodeCleanupTests.cs index bd01530d8ab0a..601c7c3278592 100644 --- a/src/EditorFeatures/CSharpTest/Formatting/CodeCleanupTests.cs +++ b/src/EditorFeatures/CSharpTest/Formatting/CodeCleanupTests.cs @@ -608,7 +608,7 @@ TestWorkspace GetTestWorkspaceForLanguage(string language) /// Indicates whether 'using' directives should be organized into separated groups. Default is true. /// The to test code cleanup. private protected static Task AssertCodeCleanupResult(string expected, string code, bool systemUsingsFirst = true, bool separateUsingGroups = false) - => AssertCodeCleanupResult(expected, code, CSharpCodeStyleOptions.PreferOutsidePlacementWithSilentEnforcement, systemUsingsFirst, separateUsingGroups); + => AssertCodeCleanupResult(expected, code, new(AddImportPlacement.OutsideNamespace, NotificationOption2.Silent), systemUsingsFirst, separateUsingGroups); /// /// Assert the expected code value equals the actual processed input . diff --git a/src/EditorFeatures/VisualBasic/AddImports/VisualBasicAddImportPlacementOptionsStorage.vb b/src/EditorFeatures/VisualBasic/AddImports/VisualBasicAddImportPlacementOptionsStorage.vb index 2718d8415b900..f51d13c7b38e7 100644 --- a/src/EditorFeatures/VisualBasic/AddImports/VisualBasicAddImportPlacementOptionsStorage.vb +++ b/src/EditorFeatures/VisualBasic/AddImports/VisualBasicAddImportPlacementOptionsStorage.vb @@ -31,8 +31,8 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.AddImport Public Function GetVisualBasicAddImportPlacementOptions(globalOptions As IGlobalOptionService) As AddImportPlacementOptions Return New AddImportPlacementOptions( PlaceSystemNamespaceFirst:=globalOptions.GetOption(GenerationOptions.PlaceSystemNamespaceFirst, LanguageNames.VisualBasic), - PlaceImportsInsideNamespaces:=False, ' VB does not support imports in namespace declarations - AllowInHiddenRegions:=False) ' no global option available + UsingDirectivePlacement:=AddImportPlacementOptions.Default.UsingDirectivePlacement, ' VB does not support imports in namespace declarations + AllowInHiddenRegions:=AddImportPlacementOptions.Default.AllowInHiddenRegions) ' no global option available End Function End Module End Namespace diff --git a/src/Features/CSharp/Portable/Formatting/CSharpOrganizeUsingsNewDocumentFormattingProvider.cs b/src/Features/CSharp/Portable/Formatting/CSharpOrganizeUsingsNewDocumentFormattingProvider.cs index 4bfbf176acb6c..611c15cb17e68 100644 --- a/src/Features/CSharp/Portable/Formatting/CSharpOrganizeUsingsNewDocumentFormattingProvider.cs +++ b/src/Features/CSharp/Portable/Formatting/CSharpOrganizeUsingsNewDocumentFormattingProvider.cs @@ -30,13 +30,15 @@ public async Task FormatNewDocumentAsync(Document document, Document? { var organizeImportsService = document.GetRequiredLanguageService(); - var organizeOptions = await OrganizeImportsOptions.FromDocumentAsync(document, cancellationToken).ConfigureAwait(false); - var documentOptions = await document.GetOptionsAsync(cancellationToken).ConfigureAwait(false); - var codeStyleOption = documentOptions.GetOption(CSharpCodeStyleOptions.PreferredUsingDirectivePlacement); + var organizeOptions = new OrganizeImportsOptions( + options.AddImportOptions.PlaceSystemNamespaceFirst, + options.FormattingOptions.SeparateImportDirectiveGroups, + options.FormattingOptions.LineFormatting.NewLine); var organizedDocument = await organizeImportsService.OrganizeImportsAsync(document, organizeOptions, cancellationToken).ConfigureAwait(false); - return await MisplacedUsingDirectivesCodeFixProvider.TransformDocumentIfRequiredAsync(organizedDocument, options.SimplifierOptions, codeStyleOption, cancellationToken).ConfigureAwait(false); + return await MisplacedUsingDirectivesCodeFixProvider.TransformDocumentIfRequiredAsync( + organizedDocument, options.SimplifierOptions, options.AddImportOptions.UsingDirectivePlacement, cancellationToken).ConfigureAwait(false); } } } diff --git a/src/VisualStudio/Core/Test/Snippets/CSharpSnippetExpansionClientTests.vb b/src/VisualStudio/Core/Test/Snippets/CSharpSnippetExpansionClientTests.vb index 23e7de87258a3..e876b8ba3ffed 100644 --- a/src/VisualStudio/Core/Test/Snippets/CSharpSnippetExpansionClientTests.vb +++ b/src/VisualStudio/Core/Test/Snippets/CSharpSnippetExpansionClientTests.vb @@ -396,8 +396,8 @@ using G= H.I; Dim document = workspace.CurrentSolution.Projects.Single().Documents.Single() Dim addImportOptions = New AddImportPlacementOptions( PlaceSystemNamespaceFirst:=placeSystemNamespaceFirst, - PlaceImportsInsideNamespaces:=False, - AllowInHiddenRegions:=False) + UsingDirectivePlacement:=AddImportPlacementOptions.Default.UsingDirectivePlacement, + AllowInHiddenRegions:=AddImportPlacementOptions.Default.AllowInHiddenRegions) Dim formattingOptions = CSharpSyntaxFormattingOptions.Default diff --git a/src/VisualStudio/Core/Test/Snippets/VisualBasicSnippetExpansionClientTests.vb b/src/VisualStudio/Core/Test/Snippets/VisualBasicSnippetExpansionClientTests.vb index 252e427a8422e..facf730ebaa11 100644 --- a/src/VisualStudio/Core/Test/Snippets/VisualBasicSnippetExpansionClientTests.vb +++ b/src/VisualStudio/Core/Test/Snippets/VisualBasicSnippetExpansionClientTests.vb @@ -415,8 +415,8 @@ End Class Dim addImportOptions = New AddImportPlacementOptions( PlaceSystemNamespaceFirst:=placeSystemNamespaceFirst, - PlaceImportsInsideNamespaces:=False, - AllowInHiddenRegions:=False) + UsingDirectivePlacement:=AddImportPlacementOptions.Default.UsingDirectivePlacement, + AllowInHiddenRegions:=AddImportPlacementOptions.Default.AllowInHiddenRegions) Dim formattingOptions = VisualBasicSyntaxFormattingOptions.Default diff --git a/src/Workspaces/CSharpTest/CodeGeneration/AddImportsTests.cs b/src/Workspaces/CSharpTest/CodeGeneration/AddImportsTests.cs index ad6f7fd99e0d4..48ddca8854cca 100644 --- a/src/Workspaces/CSharpTest/CodeGeneration/AddImportsTests.cs +++ b/src/Workspaces/CSharpTest/CodeGeneration/AddImportsTests.cs @@ -9,6 +9,7 @@ using System.Threading; using System.Threading.Tasks; using Microsoft.CodeAnalysis.AddImport; +using Microsoft.CodeAnalysis.CodeStyle; using Microsoft.CodeAnalysis.CSharp.Formatting; using Microsoft.CodeAnalysis.CSharp.Simplification; using Microsoft.CodeAnalysis.CSharp.Syntax; @@ -77,7 +78,7 @@ private static async Task TestAsync( var addImportOptions = new AddImportPlacementOptions( PlaceSystemNamespaceFirst: placeSystemNamespaceFirst, - PlaceImportsInsideNamespaces: placeImportsInsideNamespaces, + UsingDirectivePlacement: new CodeStyleOption2(placeImportsInsideNamespaces ? AddImportPlacement.InsideNamespace : AddImportPlacement.OutsideNamespace, NotificationOption2.None), AllowInHiddenRegions: false); var formattingOptions = CSharpSyntaxFormattingOptions.Default; diff --git a/src/Workspaces/Core/Portable/Formatting/Formatter.cs b/src/Workspaces/Core/Portable/Formatting/Formatter.cs index 6e098000f8da5..d4971f947c54c 100644 --- a/src/Workspaces/Core/Portable/Formatting/Formatter.cs +++ b/src/Workspaces/Core/Portable/Formatting/Formatter.cs @@ -8,6 +8,7 @@ using System.Threading; using System.Threading.Tasks; using Microsoft.CodeAnalysis.Diagnostics; +using Microsoft.CodeAnalysis.Editing; using Microsoft.CodeAnalysis.Formatting.Rules; using Microsoft.CodeAnalysis.Host; using Microsoft.CodeAnalysis.Options; diff --git a/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/CSharp/CodeStyle/CSharpCodeStyleOptions.cs b/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/CSharp/CodeStyle/CSharpCodeStyleOptions.cs index b2ebba4e0a29d..e279a051a6280 100644 --- a/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/CSharp/CodeStyle/CSharpCodeStyleOptions.cs +++ b/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/CSharp/CodeStyle/CSharpCodeStyleOptions.cs @@ -257,7 +257,7 @@ private static Option2> CreateUsingDirectiv new RoamingProfileStorageLocation($"TextEditor.CSharp.Specific.{optionName}")); public static readonly Option2> PreferredUsingDirectivePlacement = CreateUsingDirectivePlacementOption( - "PreferredUsingDirectivePlacement", defaultValue: PreferOutsidePlacementWithSilentEnforcement, "csharp_using_directive_placement"); + "PreferredUsingDirectivePlacement", AddImportPlacementOptions.Default.UsingDirectivePlacement, "csharp_using_directive_placement"); internal static readonly Option2> UnusedValueExpressionStatement = CodeStyleHelpers.CreateUnusedExpressionAssignmentOption( diff --git a/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/CSharp/CodeStyle/CSharpIdeCodeStyleOptions.cs b/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/CSharp/CodeStyle/CSharpIdeCodeStyleOptions.cs index 23f93122c786a..82922512b745c 100644 --- a/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/CSharp/CodeStyle/CSharpIdeCodeStyleOptions.cs +++ b/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/CSharp/CodeStyle/CSharpIdeCodeStyleOptions.cs @@ -67,7 +67,6 @@ internal sealed class CSharpIdeCodeStyleOptions : IdeCodeStyleOptions // the following are also used in code generation features, consider sharing: [DataMember(Order = BaseMemberCount + 25)] public readonly CodeStyleOption2 PreferStaticLocalFunction; [DataMember(Order = BaseMemberCount + 26)] public readonly CodeStyleOption2 PreferExpressionBodiedLambdas; - [DataMember(Order = BaseMemberCount + 27)] public readonly CodeStyleOption2 PreferredUsingDirectivePlacement; #pragma warning disable IDE1006 // Record naming style public CSharpIdeCodeStyleOptions( @@ -98,8 +97,7 @@ public CSharpIdeCodeStyleOptions( CodeStyleOption2? UnusedValueAssignment = null, CodeStyleOption2? PreferMethodGroupConversion = null, CodeStyleOption2? PreferExpressionBodiedLambdas = null, - CodeStyleOption2? PreferStaticLocalFunction = null, - CodeStyleOption2? PreferredUsingDirectivePlacement = null) + CodeStyleOption2? PreferStaticLocalFunction = null) #pragma warning restore : base(Common) { @@ -130,6 +128,5 @@ public CSharpIdeCodeStyleOptions( this.PreferMethodGroupConversion = PreferMethodGroupConversion ?? s_trueWithSilentEnforcement; this.PreferExpressionBodiedLambdas = PreferExpressionBodiedLambdas ?? s_whenPossibleWithSilentEnforcement; this.PreferStaticLocalFunction = PreferStaticLocalFunction ?? s_trueWithSuggestionEnforcement; - this.PreferredUsingDirectivePlacement = PreferredUsingDirectivePlacement ?? s_outsideNamespacePlacementWithSilentEnforcement; } } diff --git a/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/Core/AddImport/AddImportPlacementOptions.cs b/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/Core/AddImport/AddImportPlacementOptions.cs index e842cfd3c472f..aa9a6807f0a3f 100644 --- a/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/Core/AddImport/AddImportPlacementOptions.cs +++ b/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/Core/AddImport/AddImportPlacementOptions.cs @@ -5,22 +5,42 @@ using System.Runtime.Serialization; using System.Threading; using System.Threading.Tasks; +using Microsoft.CodeAnalysis.CodeStyle; using Microsoft.CodeAnalysis.Host; using Microsoft.CodeAnalysis.Shared.Extensions; namespace Microsoft.CodeAnalysis.AddImport; [DataContract] -internal readonly record struct AddImportPlacementOptions( - [property: DataMember(Order = 0)] bool PlaceSystemNamespaceFirst = true, - [property: DataMember(Order = 1)] bool PlaceImportsInsideNamespaces = false, - [property: DataMember(Order = 2)] bool AllowInHiddenRegions = false) +internal sealed record class AddImportPlacementOptions { - public AddImportPlacementOptions() - : this(PlaceSystemNamespaceFirst: true) + public static readonly CodeStyleOption2 s_outsideNamespacePlacementWithSilentEnforcement = + new(AddImportPlacement.OutsideNamespace, NotificationOption2.Silent); + + [property: DataMember(Order = 0)] + public bool PlaceSystemNamespaceFirst { get; init; } + + /// + /// Where to place C# usings relative to namespace declaration, ignored by VB. + /// + [property: DataMember(Order = 1)] + public CodeStyleOption2 UsingDirectivePlacement { get; init; } + + [property: DataMember(Order = 2)] + public bool AllowInHiddenRegions { get; init; } + + public AddImportPlacementOptions( + bool PlaceSystemNamespaceFirst = true, + CodeStyleOption2? UsingDirectivePlacement = null, + bool AllowInHiddenRegions = false) { + this.PlaceSystemNamespaceFirst = PlaceSystemNamespaceFirst; + this.UsingDirectivePlacement = UsingDirectivePlacement ?? s_outsideNamespacePlacementWithSilentEnforcement; + this.AllowInHiddenRegions = AllowInHiddenRegions; } + public bool PlaceImportsInsideNamespaces => UsingDirectivePlacement.Value == AddImportPlacement.InsideNamespace; + public static readonly AddImportPlacementOptions Default = new(); } diff --git a/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/CSharp/CodeActions/CSharpCodeFixOptionsProvider.cs b/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/CSharp/CodeActions/CSharpCodeFixOptionsProvider.cs index dd859692aa8cc..ac4e07637e628 100644 --- a/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/CSharp/CodeActions/CSharpCodeFixOptionsProvider.cs +++ b/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/CSharp/CodeActions/CSharpCodeFixOptionsProvider.cs @@ -64,15 +64,15 @@ public SimplifierOptions GetSimplifierOptions() internal SyntaxFormattingOptions GetFormattingOptions() => CSharpSyntaxFormattingOptions.Create(_options, FallbackSyntaxFormattingOptions); + // AddImportPlacementOptions + + public CodeStyleOption2 UsingDirectivePlacement => GetOption(CSharpCodeStyleOptions.PreferredUsingDirectivePlacement, FallbackAddImportPlacementOptions.UsingDirectivePlacement); + // CodeStyleOptions public CodeStyleOption2 PreferredModifierOrder => GetOption(CSharpCodeStyleOptions.PreferredModifierOrder, FallbackCodeStyleOptions.PreferredModifierOrder); public CodeStyleOption2 RequireAccessibilityModifiers => GetOption(CodeStyleOptions2.RequireAccessibilityModifiers, FallbackCodeStyleOptions.Common.RequireAccessibilityModifiers); - // CodeGenerationOptions - - public CodeStyleOption2 PreferredUsingDirectivePlacement => GetOption(CSharpCodeStyleOptions.PreferredUsingDirectivePlacement, FallbackCodeStyleOptions.PreferredUsingDirectivePlacement); - private TValue GetOption(Option2 option, TValue defaultValue) => _options.GetEditorConfigOption(option, defaultValue); @@ -80,7 +80,7 @@ private TValue GetOption(PerLanguageOption2 option, TValue defau => _options.GetEditorConfigOption(option, defaultValue); #if CODE_STYLE - private CSharpIdeCodeStyleOptions FallbackCodeStyleOptions + private static CSharpIdeCodeStyleOptions FallbackCodeStyleOptions => CSharpIdeCodeStyleOptions.Default; #else private CSharpIdeCodeStyleOptions FallbackCodeStyleOptions @@ -88,7 +88,7 @@ private CSharpIdeCodeStyleOptions FallbackCodeStyleOptions #endif #if CODE_STYLE - private CSharpSimplifierOptions FallbackSimplifierOptions + private static CSharpSimplifierOptions FallbackSimplifierOptions => CSharpSimplifierOptions.Default; #else private CSharpSimplifierOptions FallbackSimplifierOptions @@ -96,7 +96,7 @@ private CSharpSimplifierOptions FallbackSimplifierOptions #endif #if CODE_STYLE - private CSharpSyntaxFormattingOptions FallbackSyntaxFormattingOptions + private static CSharpSyntaxFormattingOptions FallbackSyntaxFormattingOptions => CSharpSyntaxFormattingOptions.Default; #else private CSharpSyntaxFormattingOptions FallbackSyntaxFormattingOptions @@ -104,12 +104,20 @@ private CSharpSyntaxFormattingOptions FallbackSyntaxFormattingOptions #endif #if CODE_STYLE - private LineFormattingOptions FallbackLineFormattingOptions + private static LineFormattingOptions FallbackLineFormattingOptions => LineFormattingOptions.Default; #else private LineFormattingOptions FallbackLineFormattingOptions => _fallbackOptions.GetOptions(_languageServices).CleanupOptions.FormattingOptions.LineFormatting; #endif + +#if CODE_STYLE + private static AddImportPlacementOptions FallbackAddImportPlacementOptions + => AddImportPlacementOptions.Default; +#else + private AddImportPlacementOptions FallbackAddImportPlacementOptions + => _fallbackOptions.GetOptions(_languageServices).CleanupOptions.AddImportOptions; +#endif } internal static class CSharpCodeFixOptionsProviders diff --git a/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/CSharp/LanguageServices/CSharpAddImportsService.cs b/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/CSharp/LanguageServices/CSharpAddImportsService.cs index 9da783cd3668b..2b6c36017e27b 100644 --- a/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/CSharp/LanguageServices/CSharpAddImportsService.cs +++ b/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/CSharp/LanguageServices/CSharpAddImportsService.cs @@ -17,12 +17,6 @@ using Microsoft.CodeAnalysis.Host.Mef; using Roslyn.Utilities; -#if CODE_STYLE -using OptionSet = Microsoft.CodeAnalysis.Diagnostics.AnalyzerConfigOptions; -#else -using OptionSet = Microsoft.CodeAnalysis.Options.OptionSet; -#endif - namespace Microsoft.CodeAnalysis.CSharp.AddImport { [ExportLanguageService(typeof(IAddImportsService), LanguageNames.CSharp), Shared] @@ -35,11 +29,8 @@ public CSharpAddImportsService() { } - public override bool PlaceImportsInsideNamespaces(AnalyzerConfigOptions configOptions, bool fallbackValue) - { - var placementStyle = configOptions.GetEditorConfigOption?>(CSharpCodeStyleOptions.PreferredUsingDirectivePlacement, defaultValue: null); - return (placementStyle != null) ? (placementStyle.Value == AddImportPlacement.InsideNamespace) : fallbackValue; - } + public override CodeStyleOption2 GetUsingDirectivePlacementCodeStyleOption(AnalyzerConfigOptions configOptions, CodeStyleOption2 fallbackValue) + => configOptions.GetEditorConfigOption(CSharpCodeStyleOptions.PreferredUsingDirectivePlacement, fallbackValue); // C# doesn't have global imports. protected override ImmutableArray GetGlobalImports(Compilation compilation, SyntaxGenerator generator) diff --git a/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/Core/LanguageServices/AddImports/AbstractAddImportsService.cs b/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/Core/LanguageServices/AddImports/AbstractAddImportsService.cs index a54b55a08c4b2..7485a06ec65f3 100644 --- a/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/Core/LanguageServices/AddImports/AbstractAddImportsService.cs +++ b/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/Core/LanguageServices/AddImports/AbstractAddImportsService.cs @@ -7,6 +7,7 @@ using System.Collections.Immutable; using System.Linq; using System.Threading; +using Microsoft.CodeAnalysis.CodeStyle; using Microsoft.CodeAnalysis.Diagnostics; using Microsoft.CodeAnalysis.Editing; using Microsoft.CodeAnalysis.Shared.Extensions; @@ -35,12 +36,12 @@ public AddImportPlacementOptions GetAddImportOptions(AnalyzerConfigOptions confi fallbackOptions ??= AddImportPlacementOptions.Default; return new( - PlaceSystemNamespaceFirst: configOptions.GetEditorConfigOption(GenerationOptions.PlaceSystemNamespaceFirst, fallbackOptions.Value.PlaceSystemNamespaceFirst), - PlaceImportsInsideNamespaces: PlaceImportsInsideNamespaces(configOptions, fallbackOptions.Value.PlaceImportsInsideNamespaces), + PlaceSystemNamespaceFirst: configOptions.GetEditorConfigOption(GenerationOptions.PlaceSystemNamespaceFirst, fallbackOptions.PlaceSystemNamespaceFirst), + UsingDirectivePlacement: GetUsingDirectivePlacementCodeStyleOption(configOptions, fallbackOptions.UsingDirectivePlacement), AllowInHiddenRegions: allowInHiddenRegions); } - public abstract bool PlaceImportsInsideNamespaces(AnalyzerConfigOptions configOptions, bool fallbackValue); + public abstract CodeStyleOption2 GetUsingDirectivePlacementCodeStyleOption(AnalyzerConfigOptions configOptions, CodeStyleOption2 fallbackValue); private bool IsSimpleUsing(TUsingOrAliasSyntax usingOrAlias) => !IsAlias(usingOrAlias) && !IsStaticUsing(usingOrAlias); private bool IsAlias(TUsingOrAliasSyntax usingOrAlias) => GetAlias(usingOrAlias) != null; diff --git a/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/Core/LanguageServices/AddImports/IAddImportsService.cs b/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/Core/LanguageServices/AddImports/IAddImportsService.cs index 1603ef153c6ee..9b9acf9ca9edd 100644 --- a/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/Core/LanguageServices/AddImports/IAddImportsService.cs +++ b/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/Core/LanguageServices/AddImports/IAddImportsService.cs @@ -20,8 +20,6 @@ internal interface IAddImportsService : ILanguageService { AddImportPlacementOptions GetAddImportOptions(AnalyzerConfigOptions configOptions, bool allowInHiddenRegions, AddImportPlacementOptions? fallbackOptions); - bool PlaceImportsInsideNamespaces(AnalyzerConfigOptions configOptions, bool fallbackValue); - /// /// Returns true if the tree already has an existing import syntactically equivalent to /// in scope at . This includes diff --git a/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/VisualBasic/LanguageServices/VisualBasicAddImportsService.vb b/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/VisualBasic/LanguageServices/VisualBasicAddImportsService.vb index bfe9370721f1f..0a1abe6113fb5 100644 --- a/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/VisualBasic/LanguageServices/VisualBasicAddImportsService.vb +++ b/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/VisualBasic/LanguageServices/VisualBasicAddImportsService.vb @@ -6,6 +6,7 @@ Imports System.Collections.Immutable Imports System.Composition Imports System.Threading Imports Microsoft.CodeAnalysis.AddImport +Imports Microsoft.CodeAnalysis.CodeStyle Imports Microsoft.CodeAnalysis.Diagnostics Imports Microsoft.CodeAnalysis.Editing Imports Microsoft.CodeAnalysis.Host.Mef @@ -13,12 +14,6 @@ Imports Microsoft.CodeAnalysis.PooledObjects Imports Microsoft.CodeAnalysis.VisualBasic.Syntax Imports Microsoft.CodeAnalysis.VisualBasic.Utilities -#If CODE_STYLE Then -Imports OptionSet = Microsoft.CodeAnalysis.Diagnostics.AnalyzerConfigOptions -#Else -Imports OptionSet = Microsoft.CodeAnalysis.Options.OptionSet -#End If - Namespace Microsoft.CodeAnalysis.VisualBasic.AddImports Friend Class VisualBasicAddImportsService @@ -64,10 +59,11 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.AddImports FirstOrDefault()?.Alias End Function - Public Overrides Function PlaceImportsInsideNamespaces(configOptions As AnalyzerConfigOptions, fallbackValue As Boolean) As Boolean + Public Overrides Function GetUsingDirectivePlacementCodeStyleOption(configOptions As AnalyzerConfigOptions, fallbackValue As CodeStyleOption2(Of AddImportPlacement)) As CodeStyleOption2(Of AddImportPlacement) ' Visual Basic doesn't support imports inside namespaces - Return False + Return fallbackValue End Function + Protected Overrides Function IsStaticUsing(usingOrAlias As ImportsStatementSyntax) As Boolean ' Visual Basic doesn't support static imports Return False diff --git a/src/Workspaces/VisualBasicTest/CodeGeneration/AddImportsTests.vb b/src/Workspaces/VisualBasicTest/CodeGeneration/AddImportsTests.vb index b14c8a58a0041..17ad929f552a4 100644 --- a/src/Workspaces/VisualBasicTest/CodeGeneration/AddImportsTests.vb +++ b/src/Workspaces/VisualBasicTest/CodeGeneration/AddImportsTests.vb @@ -4,6 +4,7 @@ Imports System.Threading Imports Microsoft.CodeAnalysis.AddImport +Imports Microsoft.CodeAnalysis.CodeStyle Imports Microsoft.CodeAnalysis.Editing Imports Microsoft.CodeAnalysis.Formatting Imports Microsoft.CodeAnalysis.Simplification @@ -85,8 +86,8 @@ End NameSpace" Dim addImportOptions = New AddImportPlacementOptions( PlaceSystemNamespaceFirst:=placeSystemNamespaceFirst, - PlaceImportsInsideNamespaces:=False, - AllowInHiddenRegions:=False) + UsingDirectivePlacement:=AddImportPlacementOptions.Default.UsingDirectivePlacement, + AllowInHiddenRegions:=AddImportPlacementOptions.Default.AllowInHiddenRegions) Dim formattingOptions = VisualBasicSyntaxFormattingOptions.Default Dim simplifierOptions = VisualBasicSimplifierOptions.Default