Preserve whitespace trivia when applying code fix

This commit is contained in:
Patrik Svensson 2021-06-23 23:42:05 +02:00 committed by Phil Scott
parent 6b5b31957c
commit a186fd94ac
2 changed files with 43 additions and 2 deletions

View File

@ -107,8 +107,9 @@ namespace Spectre.Console.Analyzer.CodeActions
SyntaxKind.SimpleMemberAccessExpression, SyntaxKind.SimpleMemberAccessExpression,
IdentifierName(ansiConsoleIdentifier), IdentifierName(ansiConsoleIdentifier),
IdentifierName(originalCaller))) IdentifierName(originalCaller)))
.WithArgumentList( .WithArgumentList(_originalInvocation.ArgumentList)
_originalInvocation.ArgumentList)) .WithTrailingTrivia(_originalInvocation.GetTrailingTrivia())
.WithLeadingTrivia(_originalInvocation.GetLeadingTrivia()))
.Expression; .Expression;
} }
} }

View File

@ -47,6 +47,46 @@ class TestClass
.ConfigureAwait(false); .ConfigureAwait(false);
} }
[Fact]
public async Task Static_call_replaced_with_field_call_Should_Preserve_Trivia()
{
const string Source = @"
using Spectre.Console;
class TestClass
{
IAnsiConsole _ansiConsole = AnsiConsole.Console;
void TestMethod()
{
var foo = 1;
AnsiConsole.Write(""this is fine"");
_ansiConsole.Write(""Hello, World"");
}
}";
const string FixedSource = @"
using Spectre.Console;
class TestClass
{
IAnsiConsole _ansiConsole = AnsiConsole.Console;
void TestMethod()
{
var foo = 1;
_ansiConsole.Write(""this is fine"");
_ansiConsole.Write(""Hello, World"");
}
}";
await SpectreAnalyzerVerifier<FavorInstanceAnsiConsoleOverStaticAnalyzer>
.VerifyCodeFixAsync(Source, _expectedDiagnostic.WithLocation(12, 9), FixedSource)
.ConfigureAwait(false);
}
[Fact] [Fact]
public async Task Static_call_replaced_with_parameter_call() public async Task Static_call_replaced_with_parameter_call()
{ {