From 12a96b80c1da0879b0f3b13505c5f757eea4be9f Mon Sep 17 00:00:00 2001 From: MartyIX <203266+MartyIX@users.noreply.github.com> Date: Sun, 22 Dec 2024 21:11:51 +0100 Subject: [PATCH] First attempt --- ...IntroduceVariableService_IntroduceLocal.cs | 16 +++++- .../IntroduceVariableTests.cs | 50 +++++++++++++++++++ ...ableService.IntroduceVariableCodeAction.cs | 2 +- .../AbstractIntroduceVariableService.cs | 3 +- ...IntroduceVariableService_IntroduceLocal.vb | 2 + .../Simplification/CSharpSimplifierOptions.cs | 2 + 6 files changed, 71 insertions(+), 4 deletions(-) diff --git a/src/Features/CSharp/Portable/IntroduceVariable/CSharpIntroduceVariableService_IntroduceLocal.cs b/src/Features/CSharp/Portable/IntroduceVariable/CSharpIntroduceVariableService_IntroduceLocal.cs index 441cc9139af93..384d0dabe404b 100644 --- a/src/Features/CSharp/Portable/IntroduceVariable/CSharpIntroduceVariableService_IntroduceLocal.cs +++ b/src/Features/CSharp/Portable/IntroduceVariable/CSharpIntroduceVariableService_IntroduceLocal.cs @@ -9,10 +9,12 @@ using System.Diagnostics; using System.Linq; using System.Threading; -using System.Threading.Tasks; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CodeActions; +using Microsoft.CodeAnalysis.CodeCleanup; +using Microsoft.CodeAnalysis.CSharp.CodeStyle.TypeStyle; using Microsoft.CodeAnalysis.CSharp.Extensions; +using Microsoft.CodeAnalysis.CSharp.Simplification; using Microsoft.CodeAnalysis.CSharp.Syntax; using Microsoft.CodeAnalysis.Editing; using Microsoft.CodeAnalysis.Formatting; @@ -28,6 +30,7 @@ internal sealed partial class CSharpIntroduceVariableService { protected override Document IntroduceLocal( SemanticDocument document, + CodeCleanupOptions options, ExpressionSyntax expression, bool allOccurrences, bool isConstant, @@ -47,6 +50,15 @@ protected override Document IntroduceLocal( ? TokenList(ConstKeyword) : default; + var updatedExpression = expression; + + if (options.SimplifierOptions is CSharpSimplifierOptions csOptions && csOptions.ImplicitObjectCreationWhenTypeIsApparent.Value + && csOptions.GetUseVarPreference() == UseVarPreference.None) + { + if (expression is ObjectCreationExpressionSyntax oce) + updatedExpression = ImplicitObjectCreationExpression(oce.ArgumentList, oce.Initializer); + } + var declarationStatement = LocalDeclarationStatement( modifiers, VariableDeclaration( @@ -54,7 +66,7 @@ protected override Document IntroduceLocal( [VariableDeclarator( newLocalNameToken.WithAdditionalAnnotations(RenameAnnotation.Create()), argumentList: null, - EqualsValueClause(expression.WithoutTrivia()))])); + EqualsValueClause(updatedExpression).WithoutTrivia())])); switch (containerToGenerateInto) { diff --git a/src/Features/CSharpTest/IntroduceVariable/IntroduceVariableTests.cs b/src/Features/CSharpTest/IntroduceVariable/IntroduceVariableTests.cs index fdb0e55ba394c..0485bac3d4db3 100644 --- a/src/Features/CSharpTest/IntroduceVariable/IntroduceVariableTests.cs +++ b/src/Features/CSharpTest/IntroduceVariable/IntroduceVariableTests.cs @@ -4283,6 +4283,56 @@ public T this[int i] await TestInRegularAndScriptAsync(code, expected, options: ImplicitTypingEverywhere()); } + [Fact] + public async Task TestIntroduceLocalWithTargetTypedNew() + { + var code = +""" +using System; +class SampleType +{ + public SampleType() + { + int sum = Sum([|new Numbers()|]); + } + + private int Sum(Numbers numbers) + { + return 42; + } + + private class Numbers {} +} +"""; + + var expected = +""" +using System; +class SampleType +{ + public SampleType() + { + Numbers {|Rename:numbers|} = new(); + int sum = Sum(numbers); + } + + private int Sum(Numbers numbers) + { + return 42; + } + + private class Numbers {} +} +"""; + + OptionsCollection optionsCollection = new(GetLanguage()) + { + { CSharpCodeStyleOptions.ImplicitObjectCreationWhenTypeIsApparent, new CodeStyleOption2(true, NotificationOption2.Warning) }, + }; + + await TestInRegularAndScriptAsync(code, expected, options: optionsCollection); + } + [Fact] public async Task TestIntroduceFieldInExpressionBodiedPropertyGetter() { diff --git a/src/Features/Core/Portable/IntroduceVariable/AbstractIntroduceVariableService.IntroduceVariableCodeAction.cs b/src/Features/Core/Portable/IntroduceVariable/AbstractIntroduceVariableService.IntroduceVariableCodeAction.cs index 9c3ee952630c7..d8ac6491a39ce 100644 --- a/src/Features/Core/Portable/IntroduceVariable/AbstractIntroduceVariableService.IntroduceVariableCodeAction.cs +++ b/src/Features/Core/Portable/IntroduceVariable/AbstractIntroduceVariableService.IntroduceVariableCodeAction.cs @@ -64,7 +64,7 @@ private async Task GetChangedDocumentCoreAsync(CancellationToken cance } else if (_isLocal) { - return _service.IntroduceLocal(_semanticDocument, _expression, _allOccurrences, _isConstant, cancellationToken); + return _service.IntroduceLocal(_semanticDocument, Options, _expression, _allOccurrences, _isConstant, cancellationToken); } else { diff --git a/src/Features/Core/Portable/IntroduceVariable/AbstractIntroduceVariableService.cs b/src/Features/Core/Portable/IntroduceVariable/AbstractIntroduceVariableService.cs index d35f83520e00d..90a727ecad74c 100644 --- a/src/Features/Core/Portable/IntroduceVariable/AbstractIntroduceVariableService.cs +++ b/src/Features/Core/Portable/IntroduceVariable/AbstractIntroduceVariableService.cs @@ -13,6 +13,7 @@ using Microsoft.CodeAnalysis.CodeActions; using Microsoft.CodeAnalysis.CodeCleanup; using Microsoft.CodeAnalysis.Editing; +using Microsoft.CodeAnalysis.Diagnostics; using Microsoft.CodeAnalysis.Formatting; using Microsoft.CodeAnalysis.Internal.Log; using Microsoft.CodeAnalysis.LanguageService; @@ -50,7 +51,7 @@ internal abstract partial class AbstractIntroduceVariableService IntroduceFieldAsync(SemanticDocument document, TExpressionSyntax expression, bool allOccurrences, bool isConstant, CancellationToken cancellationToken); protected abstract int DetermineFieldInsertPosition(TTypeDeclarationSyntax oldDeclaration, TTypeDeclarationSyntax newDeclaration); diff --git a/src/Features/VisualBasic/Portable/IntroduceVariable/VisualBasicIntroduceVariableService_IntroduceLocal.vb b/src/Features/VisualBasic/Portable/IntroduceVariable/VisualBasicIntroduceVariableService_IntroduceLocal.vb index 0e9fb641e4a79..5c0a1606b4fce 100644 --- a/src/Features/VisualBasic/Portable/IntroduceVariable/VisualBasicIntroduceVariableService_IntroduceLocal.vb +++ b/src/Features/VisualBasic/Portable/IntroduceVariable/VisualBasicIntroduceVariableService_IntroduceLocal.vb @@ -6,6 +6,7 @@ Imports System.Threading Imports Microsoft.CodeAnalysis Imports Microsoft.CodeAnalysis.CodeActions Imports Microsoft.CodeAnalysis.Formatting +Imports Microsoft.CodeAnalysis.CodeCleanup Imports Microsoft.CodeAnalysis.VisualBasic Imports Microsoft.CodeAnalysis.VisualBasic.Syntax @@ -13,6 +14,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.IntroduceVariable Partial Friend Class VisualBasicIntroduceVariableService Protected Overrides Function IntroduceLocal( document As SemanticDocument, + options As CodeCleanupOptions, expression As ExpressionSyntax, allOccurrences As Boolean, isConstant As Boolean, diff --git a/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/CSharp/Simplification/CSharpSimplifierOptions.cs b/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/CSharp/Simplification/CSharpSimplifierOptions.cs index e52fe6aa0b0eb..bd616fa208148 100644 --- a/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/CSharp/Simplification/CSharpSimplifierOptions.cs +++ b/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/CSharp/Simplification/CSharpSimplifierOptions.cs @@ -28,6 +28,7 @@ internal sealed record class CSharpSimplifierOptions : SimplifierOptions, IEquat [DataMember] public CodeStyleOption2 AllowEmbeddedStatementsOnSameLine { get; init; } = CodeStyleOption2.TrueWithSilentEnforcement; [DataMember] public CodeStyleOption2 PreferBraces { get; init; } = s_defaultPreferBraces; [DataMember] public CodeStyleOption2 PreferThrowExpression { get; init; } = CodeStyleOption2.TrueWithSuggestionEnforcement; + [DataMember] public CodeStyleOption2 ImplicitObjectCreationWhenTypeIsApparent { get; init; } = CodeStyleOption2.FalseWithSilentEnforcement; public CSharpSimplifierOptions() { @@ -43,6 +44,7 @@ public CSharpSimplifierOptions(IOptionsReader options) AllowEmbeddedStatementsOnSameLine = options.GetOption(CSharpCodeStyleOptions.AllowEmbeddedStatementsOnSameLine); PreferBraces = options.GetOption(CSharpCodeStyleOptions.PreferBraces); PreferThrowExpression = options.GetOption(CSharpCodeStyleOptions.PreferThrowExpression); + ImplicitObjectCreationWhenTypeIsApparent = options.GetOption(CSharpCodeStyleOptions.ImplicitObjectCreationWhenTypeIsApparent); } public UseVarPreference GetUseVarPreference()