提交 33bb4c41 编写于 作者: C Cyrus Najmabadi

Place '#pragma warning restore' in better location with multi-line statements.

上级 9c7e1ce0
......@@ -73,6 +73,33 @@ void Method()
}}");
}
[WorkItem(26015, "https://github.com/dotnet/roslyn/issues/26015")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsSuppression)]
public async Task TestPragmaWarningDirectiveAroundMultiLineStatement()
{
await TestAsync(
@"
class Class
{
void Method()
{
[|string x = @""multi
line"";|]
}
}",
$@"
class Class
{{
void Method()
{{
#pragma warning disable CS0219 // {CSharpResources.WRN_UnreferencedVarAssg_Title}
string x = @""multi
line"";
#pragma warning restore CS0219 // {CSharpResources.WRN_UnreferencedVarAssg_Title}
}}
}}");
}
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsSuppression)]
public async Task TestMultilineStatementPragmaWarningDirective()
{
......
......@@ -11,6 +11,7 @@
using Microsoft.CodeAnalysis.CSharp.Extensions;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Formatting;
using Microsoft.CodeAnalysis.Text;
namespace Microsoft.CodeAnalysis.CSharp.CodeFixes.Suppression
{
......@@ -74,14 +75,10 @@ protected override bool IsAttributeListWithAssemblyAttributes(SyntaxNode node)
}
protected override bool IsEndOfLine(SyntaxTrivia trivia)
{
return trivia.Kind() == SyntaxKind.EndOfLineTrivia;
}
=> trivia.Kind() == SyntaxKind.EndOfLineTrivia;
protected override bool IsEndOfFileToken(SyntaxToken token)
{
return token.Kind() == SyntaxKind.EndOfFileToken;
}
=> token.Kind() == SyntaxKind.EndOfFileToken;
protected override async Task<SyntaxNode> AddGlobalSuppressMessageAttributeAsync(SyntaxNode newRoot, ISymbol targetSymbol, Diagnostic diagnostic, Workspace workspace, CancellationToken cancellationToken)
{
......@@ -185,5 +182,24 @@ protected override SyntaxTrivia TogglePragmaDirective(SyntaxTrivia trivia)
var newPragmaWarning = pragmaWarning.WithDisableOrRestoreKeyword(toggledToken);
return SyntaxFactory.Trivia(newPragmaWarning);
}
protected override SyntaxToken GetAdjustedTokenForPragmaRestore(
SyntaxToken token, SyntaxNode root, TextLineCollection lines, int indexOfLine)
{
var nextToken = token.GetNextToken();
if (nextToken.Kind() == SyntaxKind.SemicolonToken &&
nextToken.Parent is StatementSyntax statement &&
statement.GetLastToken() == nextToken &&
token.Parent.FirstAncestorOrSelf<StatementSyntax>() == statement)
{
// both the current and next tokens belong to the same statement, and the next token
// is the final semicolon in a statement. Do not put the pragma before that
// semicolon. Place it after the semicolon so the statement stays whole.
return nextToken;
}
return token;
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册