diff --git a/Src/Workspaces/CSharp/Formatting/FormattingHelpers.cs b/Src/Workspaces/CSharp/Formatting/FormattingHelpers.cs index 4914eb89ced2d14d14d503c2f05e2c251c328aee..e688123b174c563896d5778a899d42ea86d8cd57 100644 --- a/Src/Workspaces/CSharp/Formatting/FormattingHelpers.cs +++ b/Src/Workspaces/CSharp/Formatting/FormattingHelpers.cs @@ -514,12 +514,6 @@ public static bool IsBlockBody(this SyntaxNode node) } } - public static bool BlockContainsOnlyLabel(this SyntaxNode node) - { - var block = node as BlockSyntax; - return block != null && block.Statements.Count == 1 && block.Statements[0] is LabeledStatementSyntax; - } - public static bool IsPlusOrMinusExpression(this SyntaxToken token) { if (token.CSharpKind() != SyntaxKind.PlusToken && token.CSharpKind() != SyntaxKind.MinusToken) diff --git a/Src/Workspaces/CSharp/Formatting/Rules/IndentBlockFormattingRule.cs b/Src/Workspaces/CSharp/Formatting/Rules/IndentBlockFormattingRule.cs index 6b0ec9e6aaa02f06ffa90267097dc7a9a544cd61..e00ce09a0e21153e944dec09e857a196662fd6b2 100644 --- a/Src/Workspaces/CSharp/Formatting/Rules/IndentBlockFormattingRule.cs +++ b/Src/Workspaces/CSharp/Formatting/Rules/IndentBlockFormattingRule.cs @@ -91,35 +91,14 @@ private void AddLabelIndentationOperation(List list, Synta if (labeledStatement != null) { var labelPositioningOption = optionSet.GetOption(CSharpFormattingOptions.LabelPositioning); - var startToken = labeledStatement.GetFirstToken().GetNextToken().GetNextToken(); - // make sure that label is not the only statement inside of a block - if (labeledStatement.Parent.BlockContainsOnlyLabel() && startToken.CSharpKind() != SyntaxKind.None) + if (labelPositioningOption == LabelPositionOptions.OneLess) { - // Nothing to do for NoIndent - if (labelPositioningOption == LabelPositionOptions.OneLess) - { - AddIndentBlockOperation(list, startToken, labeledStatement.GetLastToken()); - } - else if (labelPositioningOption == LabelPositionOptions.LeftMost) - { - AddAbsoluteZeroIndentBlockOperation(list, labeledStatement.GetFirstToken(includeZeroWidth: true), labeledStatement.GetFirstToken().GetNextToken()); - if (optionSet.GetOption(CSharpFormattingOptions.IndentBlock)) - { - AddIndentBlockOperation(list, labeledStatement.Parent.GetFirstToken(), startToken, labeledStatement.GetLastToken()); - } - } + AddUnindentBlockOperation(list, labeledStatement.Identifier, labeledStatement.ColonToken); } - else + else if (labelPositioningOption == LabelPositionOptions.LeftMost) { - if (labelPositioningOption == LabelPositionOptions.OneLess) - { - AddUnindentBlockOperation(list, labeledStatement.GetFirstToken(includeZeroWidth: true), labeledStatement.GetFirstToken().GetNextToken()); - } - else if (labelPositioningOption == LabelPositionOptions.LeftMost) - { - AddAbsoluteZeroIndentBlockOperation(list, labeledStatement.GetFirstToken(includeZeroWidth: true), labeledStatement.GetFirstToken().GetNextToken()); - } + AddAbsoluteZeroIndentBlockOperation(list, labeledStatement.Identifier, labeledStatement.ColonToken); } } } @@ -129,7 +108,7 @@ private void AddBlockIndentationOperation(List list, Synta var bracePair = node.GetBracePair(); // don't put block indentation operation if the block only contains label statement - if (node.BlockContainsOnlyLabel() || !bracePair.IsValidBracePair()) + if (!bracePair.IsValidBracePair()) { return; } @@ -148,6 +127,18 @@ private void AddBlockIndentationOperation(List list, Synta return; } + if (node is BlockSyntax && !optionSet.GetOption(CSharpFormattingOptions.IndentBlock)) + { + // do not add indent operation for block + return; + } + + if (node is SwitchStatementSyntax && !optionSet.GetOption(CSharpFormattingOptions.IndentSwitchSection)) + { + // do not add indent operation for switch statement + return; + } + AddIndentBlockOperation(list, bracePair.Item1.GetNextToken(includeZeroWidth: true), bracePair.Item2.GetPreviousToken(includeZeroWidth: true)); } diff --git a/Src/Workspaces/CSharp/Formatting/Rules/IndentUserSettingsFormattingRule.cs b/Src/Workspaces/CSharp/Formatting/Rules/IndentUserSettingsFormattingRule.cs index 2cf84e8d4435f2eb149c2107cc50299a92f59f25..ffb3091bcc6055ff8945bfc7747095620b2a7dac 100644 --- a/Src/Workspaces/CSharp/Formatting/Rules/IndentUserSettingsFormattingRule.cs +++ b/Src/Workspaces/CSharp/Formatting/Rules/IndentUserSettingsFormattingRule.cs @@ -1,16 +1,9 @@ // Copyright (c) Microsoft Open Technologies, Inc. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. -using System; using System.Collections.Generic; -using System.Text; -using Microsoft.CodeAnalysis; -using Microsoft.CodeAnalysis.CSharp; -using Microsoft.CodeAnalysis.CSharp.Symbols; using Microsoft.CodeAnalysis.CSharp.Syntax; using Microsoft.CodeAnalysis.Formatting.Rules; using Microsoft.CodeAnalysis.Options; -using Microsoft.CodeAnalysis.Shared.Utilities; -using Microsoft.CodeAnalysis.Text; namespace Microsoft.CodeAnalysis.CSharp.Formatting { @@ -20,62 +13,19 @@ public override void AddIndentBlockOperations(List list, S { nextOperation.Invoke(list); - AddAndRemoveBlockIndentationOperation(list, node, optionSet); - } - - private void AddAndRemoveBlockIndentationOperation(List list, SyntaxNode node, OptionSet optionSet) - { var bracePair = node.GetBracePair(); - // don't put block indentation operation if the block only contains label statement or it is lambda expression body block - if (node.IsLambdaBodyBlock() || node.BlockContainsOnlyLabel() || !bracePair.IsValidBracePair()) + // don't put block indentation operation if the block only contains lambda expression body block + if (node.IsLambdaBodyBlock() || !bracePair.IsValidBracePair()) { return; } - if (node is BlockSyntax && optionSet.GetOption(CSharpFormattingOptions.OpenCloseBracesIndent)) + if (optionSet.GetOption(CSharpFormattingOptions.OpenCloseBracesIndent)) { AddIndentBlockOperation(list, bracePair.Item1, bracePair.Item1, bracePair.Item1.Span); AddIndentBlockOperation(list, bracePair.Item2, bracePair.Item2, bracePair.Item2.Span); } - - if (node is BlockSyntax && !optionSet.GetOption(CSharpFormattingOptions.IndentBlock)) - { - var startToken = bracePair.Item1.GetNextToken(includeZeroWidth: true); - var endToken = bracePair.Item2.GetPreviousToken(includeZeroWidth: true); - - RemoveIndentBlockOperation(list, startToken, endToken); - } - - if (node is SwitchStatementSyntax && !optionSet.GetOption(CSharpFormattingOptions.IndentSwitchSection)) - { - var startToken = bracePair.Item1.GetNextToken(includeZeroWidth: true); - var endToken = bracePair.Item2.GetPreviousToken(includeZeroWidth: true); - - RemoveIndentBlockOperation(list, startToken, endToken); - } - } - - protected void RemoveIndentBlockOperation( - List list, - SyntaxToken startToken, - SyntaxToken endToken) - { - if (startToken.CSharpKind() == SyntaxKind.None || endToken.CSharpKind() == SyntaxKind.None) - { - return; - } - - var span = CommonFormattingHelpers.GetSpanIncludingTrailingAndLeadingTriviaOfAdjacentTokens(startToken, endToken); - - for (int i = 0; i < list.Count; i++) - { - if (list[i] != null && list[i].TextSpan == span) - { - list[i] = null; - return; - } - } } } }