提交 5ac384f5 编写于 作者: B Balaji

Merge pull request #654 from basoundr/fix285

Format hash directives inside other hash directives of complex trivia
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Diagnostics;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Extensions;
using Microsoft.CodeAnalysis.CSharp.Symbols;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Formatting;
using Microsoft.CodeAnalysis.Options;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Text;
using Roslyn.Utilities;
namespace Microsoft.CodeAnalysis.CSharp.Formatting
......@@ -291,7 +287,8 @@ private bool ShouldFormat()
OnComment(trivia, index) ||
OnSkippedTokensOrText(trivia) ||
OnRegion(trivia, index) ||
OnPreprocessor(trivia, index))
OnPreprocessor(trivia, index) ||
OnDisabledTextTrivia(trivia, index))
{
return true;
}
......@@ -300,6 +297,20 @@ private bool ShouldFormat()
return false;
}
private bool OnDisabledTextTrivia(SyntaxTrivia trivia, int index)
{
if (trivia.IsKind(SyntaxKind.DisabledTextTrivia))
{
var triviaString = trivia.ToString();
if (!string.IsNullOrEmpty(triviaString) && SyntaxFacts.IsNewLine(triviaString.Last()))
{
ResetStateAfterNewLine(index);
}
}
return false;
}
private static bool ShouldFormatSingleLineDocumentationComment(int indentation, int tabSize, SyntaxTrivia trivia)
{
var xmlComment = (DocumentationCommentTriviaSyntax)trivia.GetStructure();
......
......@@ -5910,6 +5910,101 @@ void M()
}
}
}";
AssertFormat(expected, code);
}
[WorkItem(285)]
[WorkItem(1089196)]
[Fact, Trait(Traits.Feature, Traits.Features.Formatting)]
public void FormatHashInBadDirectiveToZeroColumnAnywhereInsideIfDef()
{
const string code = @"class MyClass
{
static void Main(string[] args)
{
#if false
#
#endif
}
}";
const string expected = @"class MyClass
{
static void Main(string[] args)
{
#if false
#
#endif
}
}";
AssertFormat(expected, code);
}
[WorkItem(285)]
[WorkItem(1089196)]
[Fact, Trait(Traits.Feature, Traits.Features.Formatting)]
public void FormatHashElseToZeroColumnAnywhereInsideIfDef()
{
const string code = @"class MyClass
{
static void Main(string[] args)
{
#if false
#else
Appropriate indentation should be here though #
#endif
}
}";
const string expected = @"class MyClass
{
static void Main(string[] args)
{
#if false
#else
Appropriate indentation should be here though #
#endif
}
}";
AssertFormat(expected, code);
}
[WorkItem(285)]
[WorkItem(1089196)]
[Fact, Trait(Traits.Feature, Traits.Features.Formatting)]
public void FormatHashsToZeroColumnAnywhereInsideIfDef()
{
const string code = @"class MyClass
{
static void Main(string[] args)
{
#if false
#else
#
#endif
}
}";
const string expected = @"class MyClass
{
static void Main(string[] args)
{
#if false
#else
#
#endif
}
}";
AssertFormat(expected, code);
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册