inline redundant array creation: remove trailing comma when followed by a comment (IDEA-241943)

GitOrigin-RevId: b5186892f33cd41ace02a2120be3c20bffce25c1
This commit is contained in:
Bas Leijdekkers
2020-05-29 16:06:49 +02:00
committed by intellij-monorepo-bot
parent b562263db5
commit 4d690a1566
2 changed files with 12 additions and 6 deletions

View File

@@ -1,4 +1,4 @@
// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.refactoring.util;
import com.intellij.codeInsight.BlockUtils;
@@ -218,18 +218,24 @@ public class InlineUtil {
PsiExpression[] initializers = arrayInitializer.getInitializers();
if (initializers.length > 0) {
PsiElement lastInitializerSibling = initializers[initializers.length - 1];
while (lastInitializerSibling != null) {
while (true) {
final PsiElement nextSibling = lastInitializerSibling.getNextSibling();
if (nextSibling == null) {
break;
}
if (nextSibling.getNode().getElementType() == JavaTokenType.RBRACE) break;
if (PsiUtil.isJavaToken(nextSibling, JavaTokenType.RBRACE)) break;
lastInitializerSibling = nextSibling;
}
if (lastInitializerSibling instanceof PsiWhiteSpace) {
lastInitializerSibling = PsiTreeUtil.skipWhitespacesBackward(lastInitializerSibling);
lastInitializerSibling = lastInitializerSibling.getPrevSibling();
}
if (lastInitializerSibling.getNode().getElementType() == JavaTokenType.COMMA) {
if (lastInitializerSibling instanceof PsiComment) {
final PsiElement possibleComma = PsiTreeUtil.skipWhitespacesAndCommentsBackward(lastInitializerSibling);
if (PsiUtil.isJavaToken(possibleComma, JavaTokenType.COMMA)) {
possibleComma.delete();
}
}
else if (PsiUtil.isJavaToken(lastInitializerSibling, JavaTokenType.COMMA)) {
lastInitializerSibling = lastInitializerSibling.getPrevSibling();
}
PsiElement firstElement = initializers[0];

View File

@@ -1,7 +1,7 @@
class Test {
void foo(String... strs){}
void bar() {
foo("edwqefwe", //my comment
foo("edwqefwe" //my comment
);
}
}