[java] Preserve comments in convert to varargs method fix

#IDEA-376822 Fixed

GitOrigin-RevId: 9e1340bd15e54e3a2f3fda1017ce11aa2433cba2
This commit is contained in:
Bart van Helvert
2025-08-04 23:00:44 +00:00
committed by intellij-monorepo-bot
parent 6a0303a4cd
commit 11a35fb07e
4 changed files with 15 additions and 16 deletions
@@ -7,6 +7,7 @@ import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.intellij.psi.search.searches.ReferencesSearch;
import com.siyeh.InspectionGadgetsBundle;
import com.siyeh.ig.psiutils.CommentTracker;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
@@ -56,7 +57,10 @@ public class ConvertToVarargsMethodFix extends PsiUpdateModCommandQuickFix {
final PsiTypeElement newTypeElement = factory.createTypeElement(ellipsisType);
final PsiTypeElement typeElement = lastParameter.getTypeElement();
if (typeElement != null) {
typeElement.replace(newTypeElement);
CommentTracker ct = new CommentTracker();
ct.grabComments(typeElement);
PsiElement result = typeElement.replace(newTypeElement);
ct.insertCommentsBefore(result.getLastChild()); // Swap comments, example: String /* Foo */ [] -> String/* Foo */...
}
}
@@ -0,0 +1,4 @@
class Test {
public void foo(/* Before */ String/* Middle */... a<caret>rg) {
}
}
@@ -0,0 +1,4 @@
class Test {
public void foo(/* Before */ String /* Middle */ [] a<caret>rg) {
}
}
@@ -1,18 +1,4 @@
/*
* Copyright 2000-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Copyright 2000-2025 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.siyeh.ig.fixes.migration;
import com.siyeh.InspectionGadgetsBundle;
@@ -25,6 +11,7 @@ import com.siyeh.ig.migration.MethodCanBeVariableArityMethodInspection;
public class ConvertToVarargsMethodFixTest extends IGQuickFixesTestCase {
public void testDeepArray() { doTest(); }
public void testComment() { doTest(); }
public void testFinal() { doTest(); }
public void testCStyle() { doTest(); }