提交 7a599fa3 编写于 作者: H heejaechang 提交者: RoslynTeam

some clean up on C# label formatting rule.

previously, it was a bit more complex than needed. I cleaned/simplified it a bit. (changeset 1213970)
上级 0b18ba43
......@@ -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)
......
......@@ -91,35 +91,14 @@ private void AddLabelIndentationOperation(List<IndentBlockOperation> 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<IndentBlockOperation> 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<IndentBlockOperation> 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));
}
......
// 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<IndentBlockOperation> list, S
{
nextOperation.Invoke(list);
AddAndRemoveBlockIndentationOperation(list, node, optionSet);
}
private void AddAndRemoveBlockIndentationOperation(List<IndentBlockOperation> 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<IndentBlockOperation> 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;
}
}
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册