Skip to content

Commit

Permalink
[GH-138] - trailing trivia
Browse files Browse the repository at this point in the history
  • Loading branch information
tpodolak committed Mar 18, 2020
1 parent 16938f0 commit 759860f
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeRefactorings;
using Microsoft.CodeAnalysis.CSharp;
Expand Down Expand Up @@ -26,13 +27,33 @@ protected override ObjectCreationExpressionSyntax UpdateObjectCreationExpression
IReadOnlyList<ArgumentSyntax> updatedArguments)
{
var originalArgumentList = objectCreationExpressionSyntax.ArgumentList;
var fullString = originalArgumentList.ToFullString();
var updatedArgumentList = originalArgumentList.Update(
originalArgumentList.OpenParenToken,
SeparatedList(updatedArguments),
Token(SyntaxKind.CloseParenToken));
originalArgumentList.CloseParenToken);

updatedArgumentList = UpdateArgumentListTrivia(originalArgumentList, updatedArgumentList);

return objectCreationExpressionSyntax.WithArgumentList(updatedArgumentList);
}

private static ArgumentListSyntax UpdateArgumentListTrivia(
ArgumentListSyntax originalArgumentList,
ArgumentListSyntax updatedArgumentList)
{
if (originalArgumentList.CloseParenToken.IsMissing == false)
{
return updatedArgumentList;
}

var token = originalArgumentList.ChildTokens().LastOrDefault(innerToken => innerToken.IsMissing == false);

if (token.Kind() != SyntaxKind.None && token.HasTrailingTrivia)
{
updatedArgumentList = updatedArgumentList.WithTrailingTrivia(token.TrailingTrivia);
}

return updatedArgumentList;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ internal sealed class IntroduceSubstituteCodeRefactoringProvider : AbstractIntro
{
protected override int ReadonlySubstituteDeclarationContainerRawKind { get; } = (int)SyntaxKind.ClassBlock;

protected override int LocalSubstituteDeclarationContainerRawKind { get; } = (int)SyntaxKind.ClassBlock;
protected override int LocalSubstituteDeclarationContainerRawKind { get; } = (int)SyntaxKind.FunctionBlock;

protected override IReadOnlyList<ArgumentSyntax> GetArgumentSyntaxNodes(ArgumentListSyntax argumentListSyntax, TextSpan span)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ public class IntroduceSubstituteCodeRefactoringProviderTests : CSharpCodeRefacto
[InlineData("new Foo([||]);")]
[InlineData("new Foo(, [||]);")]
[InlineData("new Foo([||],);")]
[InlineData("new Foo([||],")]
public async Task GeneratesConstructorArgumentListWithLocalVariables_WhenAllArgumentsMissing(string creationExpression)
{
var source = $@"using NSubstitute;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,32 @@ public class IntroduceSubstituteCodeRefactoringProviderTests : VisualBasicCodeRe
protected override CodeRefactoringProvider CodeRefactoringProvider { get; } = new IntroduceSubstituteCodeRefactoringProvider();

[Theory]
[InlineData("new Foo([||]);")]
[InlineData("new Foo(, [||]);")]
[InlineData("new Foo([||],);")]
[InlineData("New Foo([||]);")]
[InlineData("New Foo(, [||]);")]
[InlineData("New Foo([||],);")]
public async Task GeneratesConstructorArgumentListWithLocalVariables_WhenAllArgumentsMissing(string creationExpression)
{
var source = $@"using NSubstitute;
var source = $@"Imports NSubstitute
namespace MyNamespace
{{
public interface IService {{ }}
public interface IOtherService {{ }}
public class Foo
{{
public Foo(IService service, IOtherService otherService)
{{
}}
}}
Namespace MyNamespace
Interface IService
End Interface
Interface IOtherService
End Interface
Public Class Foo
Public Sub New(ByVal service As IService, ByVal otherService As IOtherService)
End Sub
End Class
Public Class FooTests
Public Sub Test()
Dim target = {creationExpression}
End Sub
End Class
End Namespace";

public class FooTests
{{
public void Test()
{{
var target = {creationExpression}
}}
}}
}}";
var newSource = @"using NSubstitute;
namespace MyNamespace
Expand Down

0 comments on commit 759860f

Please sign in to comment.