Inlay hints: do not chop list in presence of comments: IDEA-218083

GitOrigin-RevId: e442ad27055bb59888f2173150ac95de75b7f87b
This commit is contained in:
Roman.Ivanov
2019-07-12 08:28:33 +03:00
committed by intellij-monorepo-bot
parent 3d3c719d7a
commit 5652d26604
5 changed files with 36 additions and 10 deletions
@@ -51,6 +51,10 @@ public abstract class AbstractChopListAction<L extends PsiElement, E extends Psi
return element.getTextRange().getStartOffset();
}
protected boolean canChop(List<E> elements) {
return true;
}
private static class Context<L extends PsiElement, E extends PsiElement> {
final @NotNull L list;
final @NotNull List<E> elements;
@@ -68,7 +72,9 @@ public abstract class AbstractChopListAction<L extends PsiElement, E extends Psi
List<E> elements = getElements(list);
if (elements == null) return null;
if (elements.size() < minElementCount()) return null;
if (!canChop(elements)) return null;
if (!hasElementsNotOnSeparateLines(elements)) return null;
canChop(elements);
return new Context<>(list, elements);
}
@@ -8,6 +8,8 @@ import com.intellij.psi.util.PsiTreeUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.List;
import static com.intellij.util.ObjectUtils.tryCast;
public abstract class AbstractJavaChopListAction<L extends PsiElement, E extends PsiElement> extends AbstractChopListAction<L, E> {
@@ -29,4 +31,9 @@ public abstract class AbstractJavaChopListAction<L extends PsiElement, E extends
if (token != null && token.getTokenType() == JavaTokenType.COMMA) return token.getTextRange().getEndOffset();
return element.getTextRange().getEndOffset();
}
@Override
protected boolean canChop(List<E> elements) {
return !JavaListUtils.containsEolComments(elements);
}
}
@@ -1,8 +1,6 @@
// 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.
package com.intellij.codeInsight.intention.impl.lists;
import com.intellij.psi.JavaTokenType;
import com.intellij.psi.PsiComment;
import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -24,12 +22,6 @@ public abstract class AbstractJavaJoinListAction<L extends PsiElement, E extends
@Override
protected boolean canJoin(@NotNull List<E> elements) {
PsiElement parent = elements.get(0).getParent();
for (PsiElement child : parent.getChildren()) {
if (child instanceof PsiComment && ((PsiComment)child).getTokenType() == JavaTokenType.END_OF_LINE_COMMENT) {
return false;
}
}
return true;
return !JavaListUtils.containsEolComments(elements);
}
}
@@ -6,6 +6,8 @@ import com.intellij.psi.util.PsiTreeUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.List;
class JavaListUtils {
private JavaListUtils() { }
@@ -20,7 +22,7 @@ class JavaListUtils {
}
@Nullable
static PsiElement nextBreak(@NotNull PsiElement element) {
static PsiElement nextBreak(@NotNull PsiElement element) {
PsiElement current = element.getNextSibling();
while (current != null && isValidIntermediateElement(current)) {
if (current instanceof PsiWhiteSpace && current.textContains('\n')) return current;
@@ -29,6 +31,17 @@ class JavaListUtils {
return null;
}
static boolean containsEolComments(@NotNull List<? extends PsiElement> elements) {
PsiElement parent = elements.get(0).getParent();
for (PsiElement child : parent.getChildren()) {
if (child instanceof PsiComment && ((PsiComment)child).getTokenType() == JavaTokenType.END_OF_LINE_COMMENT) {
return true;
}
}
return false;
}
private static boolean isValidIntermediateElement(@NotNull PsiElement element) {
return element instanceof PsiWhiteSpace ||
(element instanceof PsiJavaToken && ((PsiJavaToken)element).getTokenType() == JavaTokenType.COMMA);
@@ -0,0 +1,8 @@
// "Put arguments on separate lines" "false"
class A {
void foo(String s, String s1) {
foo("a", // !!!<caret>
"b");
}
}