From 759860f3a18764b8c14f7ccb7d00cdf9d3601fbd Mon Sep 17 00:00:00 2001 From: tpodolak Date: Wed, 18 Mar 2020 20:35:20 +0100 Subject: [PATCH] [GH-138] - trailing trivia --- ...roduceSubstituteCodeRefactoringProvider.cs | 25 ++++++++++- ...roduceSubstituteCodeRefactoringProvider.cs | 2 +- ...eSubstituteCodeRefactoringProviderTests.cs | 1 - ...eSubstituteCodeRefactoringProviderTests.cs | 44 +++++++++---------- 4 files changed, 46 insertions(+), 26 deletions(-) diff --git a/src/NSubstitute.Analyzers.CSharp/CodeRefactoringProviders/IntroduceSubstituteCodeRefactoringProvider.cs b/src/NSubstitute.Analyzers.CSharp/CodeRefactoringProviders/IntroduceSubstituteCodeRefactoringProvider.cs index e0104ff6..4f15fa54 100644 --- a/src/NSubstitute.Analyzers.CSharp/CodeRefactoringProviders/IntroduceSubstituteCodeRefactoringProvider.cs +++ b/src/NSubstitute.Analyzers.CSharp/CodeRefactoringProviders/IntroduceSubstituteCodeRefactoringProvider.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using System.Linq; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CodeRefactorings; using Microsoft.CodeAnalysis.CSharp; @@ -26,13 +27,33 @@ protected override ObjectCreationExpressionSyntax UpdateObjectCreationExpression IReadOnlyList 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; + } } } \ No newline at end of file diff --git a/src/NSubstitute.Analyzers.VisualBasic/CodeRefactoringProviders/IntroduceSubstituteCodeRefactoringProvider.cs b/src/NSubstitute.Analyzers.VisualBasic/CodeRefactoringProviders/IntroduceSubstituteCodeRefactoringProvider.cs index ef04a020..98d1144d 100644 --- a/src/NSubstitute.Analyzers.VisualBasic/CodeRefactoringProviders/IntroduceSubstituteCodeRefactoringProvider.cs +++ b/src/NSubstitute.Analyzers.VisualBasic/CodeRefactoringProviders/IntroduceSubstituteCodeRefactoringProvider.cs @@ -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 GetArgumentSyntaxNodes(ArgumentListSyntax argumentListSyntax, TextSpan span) { diff --git a/tests/NSubstitute.Analyzers.Tests.CSharp/CodeRefactoringProvidersTests/IntroduceSubstituteRefactoringProviderTests/IntroduceSubstituteCodeRefactoringProviderTests.cs b/tests/NSubstitute.Analyzers.Tests.CSharp/CodeRefactoringProvidersTests/IntroduceSubstituteRefactoringProviderTests/IntroduceSubstituteCodeRefactoringProviderTests.cs index 49478ab2..69f5aca3 100644 --- a/tests/NSubstitute.Analyzers.Tests.CSharp/CodeRefactoringProvidersTests/IntroduceSubstituteRefactoringProviderTests/IntroduceSubstituteCodeRefactoringProviderTests.cs +++ b/tests/NSubstitute.Analyzers.Tests.CSharp/CodeRefactoringProvidersTests/IntroduceSubstituteRefactoringProviderTests/IntroduceSubstituteCodeRefactoringProviderTests.cs @@ -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; diff --git a/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeRefactoringProvidersTests/IntroduceSubstituteRefactoringProviderTests/IntroduceSubstituteCodeRefactoringProviderTests.cs b/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeRefactoringProvidersTests/IntroduceSubstituteRefactoringProviderTests/IntroduceSubstituteCodeRefactoringProviderTests.cs index ff3186f6..b4bc4ce7 100644 --- a/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeRefactoringProvidersTests/IntroduceSubstituteRefactoringProviderTests/IntroduceSubstituteCodeRefactoringProviderTests.cs +++ b/tests/NSubstitute.Analyzers.Tests.VisualBasic/CodeRefactoringProvidersTests/IntroduceSubstituteRefactoringProviderTests/IntroduceSubstituteCodeRefactoringProviderTests.cs @@ -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