IDEA-163049 "= ..." on selected item in completion list is confusing

This commit is contained in:
peter
2017-01-24 15:51:25 +01:00
parent ca52397881
commit 9fb3ec5fbe
5 changed files with 38 additions and 22 deletions
@@ -45,6 +45,7 @@ import java.awt.*;
* @author peter
*/
public class VariableLookupItem extends LookupItem<PsiVariable> implements TypedLookupItem, StaticallyImportable {
private static final String EQ = " = ";
@Nullable private final MemberLookupHelper myHelper;
private final Color myColor;
private final String myTailText;
@@ -77,7 +78,7 @@ public class VariableLookupItem extends LookupItem<PsiVariable> implements Typed
String initText = initializer == null ? null : initializer.getText();
if (StringUtil.isEmpty(initText)) return null;
String prefix = var instanceof PsiEnumConstant ? "" : " = ";
String prefix = var instanceof PsiEnumConstant ? "" : EQ;
String suffix = var instanceof PsiEnumConstant && ((PsiEnumConstant)var).getInitializingClass() != null ? " {...}" : "";
return StringUtil.trimLog(prefix + initText + suffix, 30);
}
@@ -159,7 +160,11 @@ public class VariableLookupItem extends LookupItem<PsiVariable> implements Typed
presentation.setTypeText(getType().getPresentableText());
}
if (myTailText != null && StringUtil.isEmpty(presentation.getTailText())) {
presentation.setTailText(myTailText, true);
if (myTailText.startsWith(EQ)) {
presentation.appendTailTextItalic(" (" + myTailText + ")", true);
} else {
presentation.setTailText(myTailText, true);
}
}
}
@@ -1448,7 +1448,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 == ' = 2 (foo)'
assert p.tailText == ' ( = 2) (foo)'
assert p.typeText == 'int'
type 'fo\n'
@@ -1707,7 +1707,8 @@ class Bar {
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'
assert LookupElementPresentation.renderElement(myFixture.lookup.items[3]).tailText == ' ( = 42)'
assert LookupElementPresentation.renderElement(myFixture.lookup.items[3]).tailFragments[0].italic
}
void testSuggestInterfaceArrayWhenObjectIsExpected() {
@@ -17,7 +17,6 @@ package com.intellij.codeInsight.lookup;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.ui.JBColor;
import com.intellij.util.Function;
import com.intellij.util.SmartList;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -67,7 +66,11 @@ public class LookupElementPresentation {
}
public void appendTailText(@NotNull String text, boolean grayed) {
appendTailText(new TextFragment(text, grayed, null));
appendTailText(new TextFragment(text, grayed, false, null));
}
public void appendTailTextItalic(@NotNull String text, boolean grayed) {
appendTailText(new TextFragment(text, grayed, true, null));
}
private void appendTailText(@NotNull TextFragment fragment) {
@@ -80,14 +83,14 @@ public class LookupElementPresentation {
public void setTailText(@Nullable String text, boolean grayed) {
clearTail();
if (text != null) {
appendTailText(new TextFragment(text, grayed, null));
appendTailText(new TextFragment(text, grayed, false, null));
}
}
public void setTailText(@Nullable String text, @Nullable Color foreground) {
clearTail();
if (text != null) {
appendTailText(new TextFragment(text, false, foreground));
appendTailText(new TextFragment(text, false, false, foreground));
}
}
@@ -101,7 +104,7 @@ public class LookupElementPresentation {
}
/**
* Is equivalent to instanceof {@link com.intellij.codeInsight.lookup.RealLookupElementPresentation} check.
* Is equivalent to instanceof {@link RealLookupElementPresentation} check.
*
* @return whether the presentation is requested to actually render lookup element on screen, or just to estimate its width.
* In the second, 'non-real' case, some heavy operations (e.g. getIcon()) can be omitted (only icon width is important)
@@ -127,7 +130,7 @@ public class LookupElementPresentation {
@NotNull
public List<TextFragment> getTailFragments() {
return myTail == null ? Collections.<TextFragment>emptyList() : Collections.unmodifiableList(myTail);
return myTail == null ? Collections.emptyList() : Collections.unmodifiableList(myTail);
}
@Nullable
@@ -147,13 +150,13 @@ public class LookupElementPresentation {
@Deprecated
public boolean isTailGrayed() {
return myTail != null && myTail.get(0).grayed;
return myTail != null && myTail.get(0).myGrayed;
}
@Nullable
@Deprecated
public Color getTailForeground() {
return myTail != null ? myTail.get(0).fgColor : null;
return myTail != null ? myTail.get(0).myFgColor : null;
}
public boolean isItemTextBold() {
@@ -217,31 +220,37 @@ public class LookupElementPresentation {
public static class TextFragment {
public final String text;
private final boolean grayed;
@Nullable private final Color fgColor;
private final boolean myGrayed;
private final boolean myItalic;
@Nullable private final Color myFgColor;
public TextFragment(String text, boolean grayed, @Nullable Color fgColor) {
private TextFragment(String text, boolean grayed, boolean italic, @Nullable Color fgColor) {
this.text = text;
this.grayed = grayed;
this.fgColor = fgColor;
myGrayed = grayed;
myItalic = italic;
myFgColor = fgColor;
}
@Override
public String toString() {
return "TextFragment{" +
"text='" + text + '\'' +
", grayed=" + grayed +
", fgColor=" + fgColor +
", grayed=" + myGrayed +
", fgColor=" + myFgColor +
'}';
}
public boolean isGrayed() {
return grayed;
return myGrayed;
}
public boolean isItalic() {
return myItalic;
}
@Nullable
public Color getForegroundColor() {
return fgColor;
return myFgColor;
}
}
}
@@ -266,7 +266,8 @@ public class LookupCellRenderer implements ListCellRenderer {
}
String trimmed = trimLabelText(fragment.text, allowedWidth, fontMetrics);
myTailComponent.append(trimmed, new SimpleTextAttributes(style, getTailTextColor(isSelected, fragment, foreground, nonFocusedSelection)));
int fragmentStyle = fragment.isItalic() ? style | SimpleTextAttributes.STYLE_ITALIC : style;
myTailComponent.append(trimmed, new SimpleTextAttributes(fragmentStyle, getTailTextColor(isSelected, fragment, foreground, nonFocusedSelection)));
allowedWidth -= RealLookupElementPresentation.getStringWidth(trimmed, fontMetrics);
}
}