mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-04-19 04:51:24 +07:00
[java][switch completion] IDEA-271902 Fix the completion tail in case labels
Eliminate the insertion handler for JavaPsiClassReferenceElement because it might be confusing to the users not to be able to set the variable name themselves. In addition to that the patch also reverts the test data that was changed earlier because it was a perfectly fine piece of code and should not have been modified in the first place GitOrigin-RevId: 7046e638ed8f4c1cae72aca600fdfd06eaa3a502
This commit is contained in:
committed by
intellij-monorepo-bot
parent
00ac31059d
commit
be5828b0ce
@@ -675,7 +675,7 @@ public final class JavaCompletionContributor extends CompletionContributor imple
|
||||
}
|
||||
|
||||
LookupItem<?> item = element.as(LookupItem.CLASS_CONDITION_KEY);
|
||||
if (forcedTail != null) {
|
||||
if (forcedTail != null && !(element instanceof JavaPsiClassReferenceElement)) {
|
||||
element = TailTypeDecorator.withTail(element, forcedTail);
|
||||
}
|
||||
if (inSwitchLabel && !smart) {
|
||||
@@ -719,7 +719,7 @@ public final class JavaCompletionContributor extends CompletionContributor imple
|
||||
if (psiPackage == null) return lookupElements;
|
||||
for (PsiClass psiClass : psiPackage.getClasses(referenceElement.getResolveScope())) {
|
||||
CompletionElement completionElement = new CompletionElement(psiClass, PsiSubstitutor.EMPTY);
|
||||
JavaCompletionUtil.createLookupElements(completionElement, referenceElement).forEach(e -> lookupElements.add(e));
|
||||
JavaCompletionUtil.createLookupElements(completionElement, referenceElement).forEach(lookupElements::add);
|
||||
}
|
||||
}
|
||||
else {
|
||||
@@ -772,11 +772,7 @@ public final class JavaCompletionContributor extends CompletionContributor imple
|
||||
return false;
|
||||
}
|
||||
|
||||
if (JavaKeywordCompletion.isAfterPrimitiveOrArrayType(position)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
return !JavaKeywordCompletion.isAfterPrimitiveOrArrayType(position);
|
||||
}
|
||||
|
||||
public static boolean mayStartClassName(CompletionResultSet result) {
|
||||
@@ -1006,7 +1002,7 @@ public final class JavaCompletionContributor extends CompletionContributor imple
|
||||
static PsiJavaCodeReferenceElement getAnnotationNameIfInside(@Nullable PsiElement position) {
|
||||
PsiAnnotation anno = PsiTreeUtil.getParentOfType(position, PsiAnnotation.class);
|
||||
PsiJavaCodeReferenceElement ref = anno == null ? null : anno.getNameReferenceElement();
|
||||
return ref != null && PsiTreeUtil.isAncestor(ref, position, false) ? ref : null;
|
||||
return position != null && PsiTreeUtil.isAncestor(ref, position, false) ? ref : null;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@@ -1213,7 +1209,7 @@ public final class JavaCompletionContributor extends CompletionContributor imple
|
||||
private static class EnumStaticFieldsFilter implements ElementFilter {
|
||||
private final PsiClass myEnumClass;
|
||||
|
||||
public EnumStaticFieldsFilter(PsiClass enumClass) {myEnumClass = enumClass;}
|
||||
private EnumStaticFieldsFilter(PsiClass enumClass) { myEnumClass = enumClass;}
|
||||
|
||||
@Override
|
||||
public boolean isAcceptable(Object element, @Nullable PsiElement context) {
|
||||
|
||||
@@ -3,42 +3,30 @@ package com.intellij.codeInsight.completion;
|
||||
|
||||
import com.intellij.application.options.CodeStyle;
|
||||
import com.intellij.codeInsight.TailType;
|
||||
import com.intellij.codeInsight.daemon.impl.analysis.HighlightingFeature;
|
||||
import com.intellij.codeInsight.lookup.*;
|
||||
import com.intellij.codeInsight.lookup.impl.JavaElementLookupRenderer;
|
||||
import com.intellij.lang.java.JavaLanguage;
|
||||
import com.intellij.openapi.diagnostic.Logger;
|
||||
import com.intellij.openapi.editor.CaretModel;
|
||||
import com.intellij.openapi.util.ClassConditionKey;
|
||||
import com.intellij.openapi.util.Comparing;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.codeStyle.JavaCodeStyleManager;
|
||||
import com.intellij.psi.codeStyle.SuggestedNameInfo;
|
||||
import com.intellij.psi.codeStyle.VariableKind;
|
||||
import com.intellij.psi.impl.source.resolve.JavaResolveUtil;
|
||||
import com.intellij.psi.util.PsiFormatUtil;
|
||||
import com.intellij.psi.util.PsiUtilCore;
|
||||
import com.intellij.util.containers.ContainerUtil;
|
||||
import com.siyeh.ig.psiutils.TypeUtils;
|
||||
import org.jetbrains.annotations.Contract;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Locale;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* @author peter
|
||||
*/
|
||||
public class JavaPsiClassReferenceElement extends LookupItem<Object> implements TypedLookupItem {
|
||||
private static final Logger LOGGER = Logger.getInstance(JavaPsiClassReferenceElement.class.getName());
|
||||
|
||||
public static final ClassConditionKey<JavaPsiClassReferenceElement> CLASS_CONDITION_KEY = ClassConditionKey.create(JavaPsiClassReferenceElement.class);
|
||||
private final SmartPsiElementPointer<PsiClass> myClass;
|
||||
private final String myQualifiedName;
|
||||
private final String myClassName;
|
||||
private String myForcedPresentableName;
|
||||
private final String myPackageDisplayName;
|
||||
private PsiSubstitutor mySubstitutor = PsiSubstitutor.EMPTY;
|
||||
@@ -46,7 +34,6 @@ public class JavaPsiClassReferenceElement extends LookupItem<Object> implements
|
||||
public JavaPsiClassReferenceElement(PsiClass psiClass) {
|
||||
super(psiClass.getName(), psiClass.getName());
|
||||
myQualifiedName = psiClass.getQualifiedName();
|
||||
myClassName = psiClass.getName();
|
||||
myClass = SmartPointerManager.getInstance(psiClass.getProject()).createSmartPsiElementPointer(psiClass);
|
||||
setInsertHandler(AllClassesGetter.TRY_SHORTENING);
|
||||
setTailType(TailType.NONE);
|
||||
@@ -231,47 +218,4 @@ public class JavaPsiClassReferenceElement extends LookupItem<Object> implements
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleInsert(@NotNull InsertionContext context) {
|
||||
final PsiElement element = context.getFile().findElementAt(context.getStartOffset());
|
||||
if (element != null &&
|
||||
HighlightingFeature.PATTERNS_IN_SWITCH.isAvailable(element) &&
|
||||
JavaCompletionContributor.IN_SWITCH_LABEL.accepts(element)) {
|
||||
|
||||
final String variableName = getVariableName();
|
||||
if (!variableName.isEmpty()) {
|
||||
context.getDocument().insertString(context.getTailOffset(), " " + variableName);
|
||||
|
||||
final CaretModel model = context.getEditor().getCaretModel();
|
||||
model.moveToOffset(context.getTailOffset());
|
||||
}
|
||||
}
|
||||
|
||||
super.handleInsert(context);
|
||||
}
|
||||
|
||||
@Contract(value = "-> !null", pure = true)
|
||||
private String getVariableName() {
|
||||
final PsiElement psi = getPsiElement();
|
||||
LOGGER.assertTrue(psi instanceof PsiClass, "Mismatched element type");
|
||||
|
||||
final String[] strings = getPossibleVariableNames((PsiClass)psi);
|
||||
|
||||
if (strings.length > 0) return strings[0];
|
||||
|
||||
if (myClassName.isEmpty()) return "";
|
||||
|
||||
return myClassName.substring(0, 1).toLowerCase(Locale.ROOT);
|
||||
}
|
||||
|
||||
@Contract(pure = true)
|
||||
private static String@NotNull [] getPossibleVariableNames(@NotNull PsiClass psi) {
|
||||
final PsiClassType type = TypeUtils.getType(psi);
|
||||
|
||||
final JavaCodeStyleManager instance = JavaCodeStyleManager.getInstance(psi.getProject());
|
||||
final SuggestedNameInfo info = instance.suggestVariableName(VariableKind.LOCAL_VARIABLE, null, null, type);
|
||||
|
||||
return JavaCompletionUtil.completeVariableNameForRefactoring(instance, type, VariableKind.LOCAL_VARIABLE, info);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
|
||||
class Main {
|
||||
private static final int LEVEL = 0;
|
||||
int f(Object o) {
|
||||
return switch(o) {
|
||||
case LEVE<caret>
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
|
||||
class Main {
|
||||
private static final int LEVEL = 0;
|
||||
int f(Object o) {
|
||||
return switch(o) {
|
||||
case LEVEL -> <caret>
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
|
||||
class Main {
|
||||
private static final int LEVEL = 0;
|
||||
void f(Object o) {
|
||||
switch(o) {
|
||||
case LEVE<caret>
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
|
||||
class Main {
|
||||
private static final int LEVEL = 0;
|
||||
void f(Object o) {
|
||||
switch(o) {
|
||||
case LEVEL:<caret>
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,7 @@
|
||||
class Main {
|
||||
int f(Object o) {
|
||||
return switch(o) {
|
||||
case Integer integer -> <caret>
|
||||
case Integer<caret>
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,7 @@
|
||||
class Main {
|
||||
void f(Object o) {
|
||||
switch(o) {
|
||||
case Integer integer:<caret>
|
||||
case Integer<caret>
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
|
||||
package com.intellij.java.codeInsight.completion
|
||||
|
||||
import com.intellij.testFramework.LightProjectDescriptor
|
||||
@@ -26,4 +26,7 @@ class NormalSwitchCompletionTest extends NormalCompletionTestCase {
|
||||
void testCompletePatternVariableInSwitchExpr() { doTest() }
|
||||
void testCompletePatternVariableInSwitchStmt() { doTest() }
|
||||
|
||||
void testCompleteConstantInSwitchExpr() { doTest() }
|
||||
void testCompleteConstantInSwitchStmt() { doTest() }
|
||||
|
||||
}
|
||||
@@ -6,10 +6,12 @@ class BeforeDefault {
|
||||
|
||||
String test(X x) {
|
||||
return switch (x) {
|
||||
case A -> "foo";
|
||||
case B -> null;
|
||||
case C -> null;
|
||||
default -> "bar";
|
||||
case A: yield "foo";
|
||||
case B:
|
||||
yield null;
|
||||
case C:
|
||||
yield null;
|
||||
default: yield "bar";
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -6,8 +6,8 @@ class BeforeDefault {
|
||||
|
||||
String test(X x) {
|
||||
return <caret>switch (x) {
|
||||
case A -> "foo";
|
||||
default -> "bar";
|
||||
case A: yield "foo";
|
||||
default: yield "bar";
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user