mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
IDEA-158094 Auto-completion popup could display values when autocompleting constants
This commit is contained in:
@@ -15,12 +15,14 @@
|
||||
*/
|
||||
package com.intellij.codeInsight.completion;
|
||||
|
||||
import com.intellij.codeInsight.lookup.*;
|
||||
import com.intellij.codeInsight.lookup.LookupElement;
|
||||
import com.intellij.codeInsight.lookup.LookupElementDecorator;
|
||||
import com.intellij.codeInsight.lookup.LookupElementPresentation;
|
||||
import com.intellij.codeInsight.lookup.TypedLookupItem;
|
||||
import com.intellij.openapi.diagnostic.Logger;
|
||||
import com.intellij.openapi.editor.Document;
|
||||
import com.intellij.openapi.util.ClassConditionKey;
|
||||
import com.intellij.openapi.util.Key;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.codeStyle.CodeStyleManager;
|
||||
import com.intellij.psi.codeStyle.CodeStyleSettingsManager;
|
||||
@@ -99,8 +101,7 @@ public class JavaChainLookupElement extends LookupElementDecorator<LookupElement
|
||||
presentation.setItemText(qualifierText + "." + presentation.getItemText());
|
||||
|
||||
if (myQualifier instanceof JavaPsiClassReferenceElement) {
|
||||
String locationString = ((JavaPsiClassReferenceElement)myQualifier).getLocationString();
|
||||
presentation.setTailText(StringUtil.notNullize(presentation.getTailText()) + locationString);
|
||||
presentation.appendTailText(((JavaPsiClassReferenceElement)myQualifier).getLocationString(), false);
|
||||
}
|
||||
if (qualifierPresentation.isStrikeout()) {
|
||||
presentation.setStrikeout(true);
|
||||
|
||||
@@ -22,15 +22,18 @@ import com.intellij.codeInsight.daemon.impl.JavaColorProvider;
|
||||
import com.intellij.codeInsight.daemon.impl.analysis.HighlightControlFlowUtil;
|
||||
import com.intellij.codeInsight.lookup.impl.JavaElementLookupRenderer;
|
||||
import com.intellij.featureStatistics.FeatureUsageTracker;
|
||||
import com.intellij.lang.ASTNode;
|
||||
import com.intellij.openapi.editor.Document;
|
||||
import com.intellij.openapi.editor.RangeMarker;
|
||||
import com.intellij.openapi.util.Computable;
|
||||
import com.intellij.openapi.util.RecursionManager;
|
||||
import com.intellij.openapi.util.registry.Registry;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.codeStyle.JavaCodeStyleManager;
|
||||
import com.intellij.psi.controlFlow.ControlFlowUtil;
|
||||
import com.intellij.psi.impl.source.PostprocessReformattingAspect;
|
||||
import com.intellij.psi.impl.source.PsiFieldImpl;
|
||||
import com.intellij.psi.impl.source.SourceTreeToPsiMap;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.psi.util.PsiUtil;
|
||||
import com.intellij.util.containers.HashMap;
|
||||
@@ -47,12 +50,14 @@ import java.util.Collection;
|
||||
public class VariableLookupItem extends LookupItem<PsiVariable> implements TypedLookupItem, StaticallyImportable {
|
||||
@Nullable private final MemberLookupHelper myHelper;
|
||||
private final Color myColor;
|
||||
private final String myTailText;
|
||||
private PsiSubstitutor mySubstitutor = PsiSubstitutor.EMPTY;
|
||||
|
||||
public VariableLookupItem(PsiVariable var) {
|
||||
super(var, var.getName());
|
||||
myHelper = null;
|
||||
myColor = getInitializerColor(var);
|
||||
myTailText = getInitializerText(var);
|
||||
}
|
||||
|
||||
public VariableLookupItem(PsiField field, boolean shouldImport) {
|
||||
@@ -64,27 +69,39 @@ public class VariableLookupItem extends LookupItem<PsiVariable> implements Typed
|
||||
}
|
||||
}
|
||||
myColor = getInitializerColor(field);
|
||||
myTailText = getInitializerText(field);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static Color getInitializerColor(@NotNull PsiVariable var) {
|
||||
if (!JavaColorProvider.isColorType(var.getType())) {
|
||||
return null;
|
||||
}
|
||||
private String getInitializerText(PsiVariable var) {
|
||||
if (myColor != null || !var.hasModifierProperty(PsiModifier.FINAL) || !var.hasModifierProperty(PsiModifier.STATIC)) return null;
|
||||
|
||||
PsiElement initializer = var instanceof PsiEnumConstant ? ((PsiEnumConstant)var).getArgumentList() : getInitializer(var);
|
||||
String initText = initializer == null ? null : initializer.getText();
|
||||
if (StringUtil.isEmpty(initText)) return null;
|
||||
|
||||
String prefix = var instanceof PsiEnumConstant ? "" : " = ";
|
||||
String suffix = var instanceof PsiEnumConstant && ((PsiEnumConstant)var).getInitializingClass() != null ? " {...}" : "";
|
||||
return StringUtil.trimLog(prefix + initText + suffix, 30);
|
||||
}
|
||||
|
||||
private static PsiExpression getInitializer(@NotNull PsiVariable var) {
|
||||
PsiElement navigationElement = var.getNavigationElement();
|
||||
if (navigationElement instanceof PsiVariable) {
|
||||
var = (PsiVariable)navigationElement;
|
||||
}
|
||||
return getExpressionColor(var.getInitializer());
|
||||
return var instanceof PsiFieldImpl ? ((PsiFieldImpl)var).getDetachedInitializer() : var.getInitializer();
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static Color getExpressionColor(@Nullable PsiExpression expression) {
|
||||
private static Color getInitializerColor(@NotNull PsiVariable var) {
|
||||
if (!JavaColorProvider.isColorType(var.getType())) return null;
|
||||
|
||||
PsiExpression expression = getInitializer(var);
|
||||
if (expression instanceof PsiReferenceExpression) {
|
||||
final PsiElement target = ((PsiReferenceExpression)expression).resolve();
|
||||
if (target instanceof PsiVariable) {
|
||||
return RecursionManager.doPreventingRecursion(expression, true, () -> getExpressionColor(((PsiVariable)target).getInitializer()));
|
||||
return RecursionManager.doPreventingRecursion(expression, true, () -> getInitializerColor((PsiVariable)target));
|
||||
}
|
||||
}
|
||||
return JavaColorProvider.getJavaColorFromExpression(expression);
|
||||
@@ -144,6 +161,9 @@ public class VariableLookupItem extends LookupItem<PsiVariable> implements Typed
|
||||
} else {
|
||||
presentation.setTypeText(getType().getPresentableText());
|
||||
}
|
||||
if (myTailText != null && StringUtil.isEmpty(presentation.getTailText())) {
|
||||
presentation.setTailText(myTailText, true);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
class X {
|
||||
{
|
||||
E.FIELD<caret>
|
||||
}
|
||||
}
|
||||
|
||||
enum E {
|
||||
FIELD1( "x"),
|
||||
FIELD2("y") {
|
||||
public String toString() {
|
||||
return super.toString();
|
||||
}
|
||||
},
|
||||
FIELD3 {};
|
||||
|
||||
E(String s) {
|
||||
}
|
||||
|
||||
public static final int FIELD4 = 42;
|
||||
}
|
||||
+1
-1
@@ -1427,7 +1427,7 @@ class Foo {{
|
||||
def constant = myFixture.lookupElements.find { it.lookupString == 'Util.CONSTANT' }
|
||||
LookupElementPresentation p = ApplicationManager.application.runReadAction ({ LookupElementPresentation.renderElement(constant) } as Computable<LookupElementPresentation>)
|
||||
assert p.itemText == 'Util.CONSTANT'
|
||||
assert p.tailText == ' (foo)'
|
||||
assert p.tailText == ' = 2 (foo)'
|
||||
assert p.typeText == 'int'
|
||||
|
||||
type 'fo\n'
|
||||
|
||||
+8
@@ -1589,4 +1589,12 @@ class Bar {
|
||||
configure()
|
||||
assert 'B' == LookupElementPresentation.renderElement(myFixture.lookup.items[0]).typeText
|
||||
}
|
||||
|
||||
public void testShowVarInitializers() {
|
||||
configure()
|
||||
assert LookupElementPresentation.renderElement(myFixture.lookup.items[0]).tailText == '( "x")'
|
||||
assert LookupElementPresentation.renderElement(myFixture.lookup.items[1]).tailText == '("y") {...}'
|
||||
assert !LookupElementPresentation.renderElement(myFixture.lookup.items[2]).tailText
|
||||
assert LookupElementPresentation.renderElement(myFixture.lookup.items[3]).tailText == ' = 42'
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user