Skip to content

Commit

Permalink
Merge pull request #59397 from CyrusNajmabadi/simplifyWrapping
Browse files Browse the repository at this point in the history
Simplify the wrapping code for initializer-expression wrapping
  • Loading branch information
CyrusNajmabadi authored Feb 14, 2022
2 parents 51fc9ab + 9585377 commit c9d41c5
Show file tree
Hide file tree
Showing 20 changed files with 477 additions and 651 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable disable

using System.Collections.Immutable;
using System.Composition;
using System.Diagnostics.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeRefactorings;
using Microsoft.CodeAnalysis.CSharp.Wrapping.BinaryExpression;
using Microsoft.CodeAnalysis.CSharp.Wrapping.ChainedExpression;
using Microsoft.CodeAnalysis.CSharp.Wrapping.InitializerExpression;
using Microsoft.CodeAnalysis.CSharp.Wrapping.SeparatedSyntaxList;
using Microsoft.CodeAnalysis.Wrapping;

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +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.

#nullable disable

using Microsoft.CodeAnalysis.CSharp.Indentation;
using Microsoft.CodeAnalysis.Wrapping.SeparatedSyntaxList;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Linq;
using Microsoft.CodeAnalysis.CSharp.LanguageServices;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Options;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Text;
using Roslyn.Utilities;
Expand All @@ -23,6 +24,16 @@ internal partial class CSharpArgumentWrapper
protected override string Wrap_every_item => FeaturesResources.Wrap_every_argument;
protected override string Wrap_long_list => FeaturesResources.Wrap_long_argument_list;

public override bool Supports_UnwrapGroup_WrapFirst_IndentRest => true;
public override bool Supports_WrapEveryGroup_UnwrapFirst => true;
public override bool Supports_WrapLongGroup_UnwrapFirst => true;

protected override bool ShouldMoveOpenBraceToNewLine(OptionSet options)
=> false;

protected override bool ShouldMoveCloseBraceToNewLine
=> false;

protected override SeparatedSyntaxList<ArgumentSyntax> GetListItems(BaseArgumentListSyntax listSyntax)
=> listSyntax.Arguments;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// 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.

using Microsoft.CodeAnalysis.CSharp.Formatting;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Options;
using Roslyn.Utilities;

namespace Microsoft.CodeAnalysis.CSharp.Wrapping.SeparatedSyntaxList
{
internal sealed partial class CSharpInitializerExpressionWrapper
: AbstractCSharpSeparatedSyntaxListWrapper<InitializerExpressionSyntax, ExpressionSyntax>
{
protected override string Indent_all_items => FeaturesResources.Indent_all_elements;
protected override string Unwrap_all_items => FeaturesResources.Unwrap_all_elements;
protected override string Unwrap_list => FeaturesResources.Unwrap_initializer;
protected override string Wrap_every_item => FeaturesResources.Wrap_initializer;
protected override string Wrap_long_list => FeaturesResources.Wrap_long_initializer;

public override bool Supports_UnwrapGroup_WrapFirst_IndentRest => false;
public override bool Supports_WrapEveryGroup_UnwrapFirst => false;
public override bool Supports_WrapLongGroup_UnwrapFirst => false;

// unreachable as we explicitly declare that we don't support these scenarios.

protected override string Align_wrapped_items => throw ExceptionUtilities.Unreachable;
protected override string Indent_wrapped_items => throw ExceptionUtilities.Unreachable;
protected override string Unwrap_and_indent_all_items => throw ExceptionUtilities.Unreachable;

protected override bool ShouldMoveOpenBraceToNewLine(OptionSet options)
=> options.GetOption(CSharpFormattingOptions.NewLinesForBracesInObjectCollectionArrayInitializers);

protected override bool ShouldMoveCloseBraceToNewLine
=> true;

protected override SeparatedSyntaxList<ExpressionSyntax> GetListItems(InitializerExpressionSyntax listSyntax)
=> listSyntax.Expressions;

protected override InitializerExpressionSyntax? TryGetApplicableList(SyntaxNode node)
=> node as InitializerExpressionSyntax;

protected override bool PositionIsApplicable(SyntaxNode root, int position, SyntaxNode declaration, bool containsSyntaxError, InitializerExpressionSyntax listSyntax)
{
if (containsSyntaxError)
return false;

return listSyntax.Span.Contains(position);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
using System.Linq;
using Microsoft.CodeAnalysis.CSharp.CodeGeneration;
using Microsoft.CodeAnalysis.CSharp.Extensions;
using Microsoft.CodeAnalysis.CSharp.Formatting;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Options;
using Microsoft.CodeAnalysis.Text;
using Microsoft.CodeAnalysis.Utilities;
using Roslyn.Utilities;
Expand All @@ -24,6 +26,16 @@ internal partial class CSharpParameterWrapper
protected override string Wrap_every_item => FeaturesResources.Wrap_every_parameter;
protected override string Wrap_long_list => FeaturesResources.Wrap_long_parameter_list;

public override bool Supports_UnwrapGroup_WrapFirst_IndentRest => true;
public override bool Supports_WrapEveryGroup_UnwrapFirst => true;
public override bool Supports_WrapLongGroup_UnwrapFirst => true;

protected override bool ShouldMoveOpenBraceToNewLine(OptionSet options)
=> false;

protected override bool ShouldMoveCloseBraceToNewLine
=> false;

protected override SeparatedSyntaxList<ParameterSyntax> GetListItems(BaseParameterListSyntax listSyntax)
=> listSyntax.Parameters;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ public AbstractCodeActionComputer(

protected abstract Task<ImmutableArray<WrappingGroup>> ComputeWrappingGroupsAsync();

protected string GetSmartIndentationAfter(SyntaxNodeOrToken nodeOrToken)
=> GetIndentationAfter(nodeOrToken, FormattingOptions.IndentStyle.Smart);

protected string GetIndentationAfter(SyntaxNodeOrToken nodeOrToken, FormattingOptions.IndentStyle indentStyle)
{
var newSourceText = OriginalSourceText.WithChanges(new TextChange(new TextSpan(nodeOrToken.Span.End, 0), NewLine));
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public BinaryExpressionCodeActionComputer(
.CreateIndentationString(UseTabs, TabSize)));

_smartIndentTrivia = new SyntaxTriviaList(generator.Whitespace(
GetIndentationAfter(_exprsAndOperators[1], FormattingOptions.IndentStyle.Smart)));
GetSmartIndentationAfter(_exprsAndOperators[1])));
}

protected override async Task<ImmutableArray<WrappingGroup>> ComputeWrappingGroupsAsync()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public CallExpressionCodeActionComputer(
OriginalSourceText.GetOffset(firstPeriod.SpanStart).CreateIndentationString(UseTabs, TabSize)));

_smartIndentTrivia = new SyntaxTriviaList(generator.Whitespace(
GetIndentationAfter(firstPeriod, FormattingOptions.IndentStyle.Smart)));
GetSmartIndentationAfter(firstPeriod)));

_newlineBeforeOperatorTrivia = service.GetNewLineBeforeOperatorTrivia(NewLineTrivia);
}
Expand Down
Loading

0 comments on commit c9d41c5

Please sign in to comment.