diff --git a/src/EditorFeatures/VisualBasicTest/ChangeSignature/ReorderParameters.InvocationErrors.vb b/src/EditorFeatures/VisualBasicTest/ChangeSignature/ReorderParameters.InvocationErrors.vb index eff669c12b5da4d0edf0c8af52e6fb42329cdf67..2502dc06fb3b0db0e7a78a0c9eb27cc6e900a26d 100644 --- a/src/EditorFeatures/VisualBasicTest/ChangeSignature/ReorderParameters.InvocationErrors.vb +++ b/src/EditorFeatures/VisualBasicTest/ChangeSignature/ReorderParameters.InvocationErrors.vb @@ -30,14 +30,14 @@ End Class]]>.NormalizedValue() End Function - Public Async Function TestReorderMethodParameters_InsufficientParameters_None() As Task + Public Async Function TestReorderMethodParameters_NoExistingParameters() As Task Dim markup = .NormalizedValue() - Await TestChangeSignatureViaCommandAsync(LanguageNames.VisualBasic, markup, expectedSuccess:=False, expectedErrorText:=FeaturesResources.This_signature_does_not_contain_parameters_that_can_be_changed) + Await TestChangeSignatureViaCommandAsync(LanguageNames.VisualBasic, markup, expectedSuccess:=True) End Function diff --git a/src/Features/CSharp/Portable/ChangeSignature/CSharpChangeSignatureService.cs b/src/Features/CSharp/Portable/ChangeSignature/CSharpChangeSignatureService.cs index 93bf069be57baeea9ef434a54966194c345ee623..850061d4627303ba6eb86ecb54b5224d7ea968ad 100644 --- a/src/Features/CSharp/Portable/ChangeSignature/CSharpChangeSignatureService.cs +++ b/src/Features/CSharp/Portable/ChangeSignature/CSharpChangeSignatureService.cs @@ -448,9 +448,12 @@ private SyntaxNode GetNodeContainingTargetNode(SyntaxNode matchingNode) return null; } - private SeparatedSyntaxList AddNewArgumentsToList(SeparatedSyntaxList newArguments, SignatureChange signaturePermutation) + private SeparatedSyntaxList AddNewArgumentsToList( + SeparatedSyntaxList newArguments, + SignatureChange signaturePermutation) { List fullList = new List(); + List separators = new List(); var updatedParameters = signaturePermutation.UpdatedConfiguration.ToListOfParameters(); @@ -468,6 +471,7 @@ private SeparatedSyntaxList AddNewArgumentsToList(SeparatedSynta seenNameEquals ? SyntaxFactory.NameColon(addedParameter.Name) : default, refKindKeyword: default, expression: SyntaxFactory.ParseExpression(addedParameter.CallsiteValue))); + separators.Add(SyntaxFactory.Token(SyntaxKind.CommaToken)); } } else @@ -479,12 +483,28 @@ private SeparatedSyntaxList AddNewArgumentsToList(SeparatedSynta seenNameEquals = true; } + if (indexInExistingList < newArguments.SeparatorCount) + { + separators.Add(newArguments.GetSeparator(indexInExistingList)); + } + fullList.Add(newArguments[indexInExistingList++]); } } } - return SyntaxFactory.SeparatedList(fullList); + // Add the rest of existing parameters, e.g. from the params argument. + while (indexInExistingList < newArguments.Count) + { + if (indexInExistingList < newArguments.SeparatorCount) + { + separators.Add(newArguments.GetSeparator(indexInExistingList)); + } + + fullList.Add(newArguments[indexInExistingList++]); + } + + return SyntaxFactory.SeparatedList(fullList, separators); } private SeparatedSyntaxList PermuteDeclaration(SeparatedSyntaxList list, SignatureChange updatedSignature) where T : SyntaxNode diff --git a/src/Features/VisualBasic/Portable/ChangeSignature/VisualBasicChangeSignatureService.vb b/src/Features/VisualBasic/Portable/ChangeSignature/VisualBasicChangeSignatureService.vb index 32b9c5f167201fbf45cd4c54c2c8281c36bf5976..0821ae14aea0082f1b770354a477d8e6a4b67b66 100644 --- a/src/Features/VisualBasic/Portable/ChangeSignature/VisualBasicChangeSignatureService.vb +++ b/src/Features/VisualBasic/Portable/ChangeSignature/VisualBasicChangeSignatureService.vb @@ -10,6 +10,7 @@ Imports Microsoft.CodeAnalysis.Formatting Imports Microsoft.CodeAnalysis.Host.Mef Imports Microsoft.CodeAnalysis.PooledObjects Imports System.Composition +Imports Microsoft.CodeAnalysis.Utilities Namespace Microsoft.CodeAnalysis.VisualBasic.ChangeSignature @@ -425,17 +426,20 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.ChangeSignature End Function Private Function PermuteDeclaration(Of T As SyntaxNode)(list As SeparatedSyntaxList(Of T), updatedSignature As SignatureChange) As SeparatedSyntaxList(Of T) - Dim originalParameters = updatedSignature.OriginalConfiguration.ToListOfParameters() + Dim originalParameterSymbols = updatedSignature.OriginalConfiguration.ToListOfParameters().Select(Function(p) p.Symbol).ToArray() Dim reorderedParameters = updatedSignature.UpdatedConfiguration.ToListOfParameters() Dim newParameters = New List(Of T) For Each newParam In reorderedParameters - Dim pos = originalParameters.IndexOf(newParam) - Dim param = list(pos) - newParameters.Add(param) + Dim existingParam = TryCast(newParam, ExistingParameter) + If existingParam IsNot Nothing Then + Dim pos = originalParameterSymbols.IndexOf(existingParam.Symbol) + Dim param = list(pos) + newParameters.Add(param) + End If Next - Dim numSeparatorsToSkip = originalParameters.Count - reorderedParameters.Count + Dim numSeparatorsToSkip = originalParameterSymbols.Length - reorderedParameters.Count Return SyntaxFactory.SeparatedList(newParameters, GetSeparators(list, numSeparatorsToSkip)) End Function