mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-10-06 10:07:41 +07:00
move class/type lookup element rendering to the corresponding lookup elements
This commit is contained in:
+95
-2
@@ -16,11 +16,18 @@
|
||||
package com.intellij.codeInsight.completion;
|
||||
|
||||
import com.intellij.codeInsight.TailType;
|
||||
import com.intellij.codeInsight.lookup.DefaultLookupItemRenderer;
|
||||
import com.intellij.codeInsight.lookup.LookupElementPresentation;
|
||||
import com.intellij.codeInsight.lookup.LookupItem;
|
||||
import com.intellij.codeInsight.lookup.impl.JavaElementLookupRenderer;
|
||||
import com.intellij.openapi.util.Comparing;
|
||||
import com.intellij.psi.PsiAnchor;
|
||||
import com.intellij.psi.PsiClass;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.codeStyle.CodeStyleSettingsManager;
|
||||
import com.intellij.psi.util.PsiUtilBase;
|
||||
import com.intellij.util.Function;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
/**
|
||||
* @author peter
|
||||
@@ -77,4 +84,90 @@ public class JavaPsiClassReferenceElement extends LookupItem<Object> {
|
||||
final String s = myQualifiedName;
|
||||
return s == null ? 239 : s.hashCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void renderElement(LookupElementPresentation presentation) {
|
||||
LookupItem item = this;
|
||||
PsiClass psiClass = getObject();
|
||||
renderClassItem(presentation, item, psiClass);
|
||||
}
|
||||
|
||||
public static void renderClassItem(LookupElementPresentation presentation, LookupItem item, PsiClass psiClass) {
|
||||
presentation.setIcon(DefaultLookupItemRenderer.getRawIcon(item, presentation.isReal()));
|
||||
|
||||
final boolean bold = item.getAttribute(LookupItem.HIGHLIGHTED_ATTR) != null;
|
||||
boolean strikeout = JavaElementLookupRenderer.isToStrikeout(item);
|
||||
presentation.setItemText(getName(psiClass, item));
|
||||
presentation.setStrikeout(strikeout);
|
||||
presentation.setItemTextBold(bold);
|
||||
|
||||
String tailText = StringUtil.notNullize((String) item.getAttribute(LookupItem.TAIL_TEXT_ATTR));
|
||||
PsiSubstitutor substitutor = (PsiSubstitutor)item.getAttribute(LookupItem.SUBSTITUTOR);
|
||||
|
||||
if (item.getAttribute(LookupItem.INDICATE_ANONYMOUS) != null &&
|
||||
(psiClass.isInterface() || psiClass.hasModifierProperty(PsiModifier.ABSTRACT))) {
|
||||
tailText = "{...}" + tailText;
|
||||
}
|
||||
if (substitutor == null && psiClass.getTypeParameters().length > 0) {
|
||||
tailText = "<" + StringUtil.join(psiClass.getTypeParameters(), new Function<PsiTypeParameter, String>() {
|
||||
public String fun(PsiTypeParameter psiTypeParameter) {
|
||||
return psiTypeParameter.getName();
|
||||
}
|
||||
}, "," + (showSpaceAfterComma(psiClass) ? " " : "")) + ">" + tailText;
|
||||
}
|
||||
presentation.setTailText(tailText, true);
|
||||
}
|
||||
|
||||
private static String getName(final PsiClass psiClass, final LookupItem<?> item) {
|
||||
String name = PsiUtilBase.getName(psiClass);
|
||||
|
||||
if (item.getAttribute(LookupItem.FORCE_QUALIFY) != null) {
|
||||
if (psiClass.getContainingClass() != null) {
|
||||
name = psiClass.getContainingClass().getName() + "." + name;
|
||||
}
|
||||
}
|
||||
|
||||
PsiSubstitutor substitutor = (PsiSubstitutor)item.getAttribute(LookupItem.SUBSTITUTOR);
|
||||
if (substitutor != null) {
|
||||
final PsiTypeParameter[] params = psiClass.getTypeParameters();
|
||||
if (params.length > 0) {
|
||||
return name + formatTypeParameters(substitutor, params);
|
||||
}
|
||||
}
|
||||
|
||||
return StringUtil.notNullize(name);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static String formatTypeParameters(@NotNull final PsiSubstitutor substitutor, final PsiTypeParameter[] params) {
|
||||
final boolean space = showSpaceAfterComma(params[0]);
|
||||
StringBuilder buffer = new StringBuilder();
|
||||
buffer.append("<");
|
||||
for(int i = 0; i < params.length; i++){
|
||||
final PsiTypeParameter param = params[i];
|
||||
final PsiType type = substitutor.substitute(param);
|
||||
if(type == null){
|
||||
return "";
|
||||
}
|
||||
if (type instanceof PsiClassType && ((PsiClassType)type).getParameters().length > 0) {
|
||||
buffer.append(((PsiClassType)type).rawType().getPresentableText()).append("<...>");
|
||||
} else {
|
||||
buffer.append(type.getPresentableText());
|
||||
}
|
||||
|
||||
if(i < params.length - 1) {
|
||||
buffer.append(",");
|
||||
if (space) {
|
||||
buffer.append(" ");
|
||||
}
|
||||
}
|
||||
}
|
||||
buffer.append(">");
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
private static boolean showSpaceAfterComma(PsiClass element) {
|
||||
return CodeStyleSettingsManager.getSettings(element.getProject()).SPACE_AFTER_COMMA;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@ package com.intellij.codeInsight.lookup;
|
||||
|
||||
import com.intellij.codeInsight.completion.DefaultInsertHandler;
|
||||
import com.intellij.codeInsight.completion.InsertionContext;
|
||||
import com.intellij.codeInsight.completion.JavaPsiClassReferenceElement;
|
||||
import com.intellij.psi.*;
|
||||
import org.jetbrains.annotations.NonNls;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
@@ -121,4 +122,24 @@ public class PsiTypeLookupItem extends LookupItem {
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void renderElement(LookupElementPresentation presentation) {
|
||||
final Object object = getObject();
|
||||
if (object instanceof PsiClass) {
|
||||
JavaPsiClassReferenceElement.renderClassItem(presentation, this, (PsiClass)object);
|
||||
} else {
|
||||
assert object instanceof PsiType;
|
||||
|
||||
presentation.setIcon(DefaultLookupItemRenderer.getRawIcon(this, presentation.isReal()));
|
||||
|
||||
presentation.setItemText(((PsiType)object).getCanonicalText());
|
||||
presentation.setItemTextBold(getAttribute(LookupItem.HIGHLIGHTED_ATTR) != null);
|
||||
|
||||
String tailText = (String)getAttribute(LookupItem.TAIL_TEXT_ATTR);
|
||||
if (tailText != null) {
|
||||
presentation.setTailText(tailText, getAttribute(LookupItem.TAIL_TEXT_SMALL_ATTR) != null);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+7
-104
@@ -21,12 +21,8 @@ import com.intellij.codeInsight.lookup.LookupElementPresentation;
|
||||
import com.intellij.codeInsight.lookup.LookupItem;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.codeStyle.CodeStyleSettingsManager;
|
||||
import com.intellij.psi.impl.beanProperties.BeanPropertyElement;
|
||||
import com.intellij.psi.util.PsiFormatUtil;
|
||||
import com.intellij.psi.util.PsiUtilBase;
|
||||
import com.intellij.util.Function;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.List;
|
||||
@@ -36,42 +32,21 @@ import java.util.List;
|
||||
*/
|
||||
public class JavaElementLookupRenderer implements ElementLookupRenderer {
|
||||
public boolean handlesItem(final Object element) {
|
||||
return element instanceof PsiClass || element instanceof PsiMember || element instanceof PsiVariable ||
|
||||
element instanceof PsiType || element instanceof PsiKeyword || element instanceof PsiExpression ||
|
||||
return element instanceof PsiVariable ||
|
||||
element instanceof PsiKeyword || element instanceof PsiExpression ||
|
||||
element instanceof PsiTypeElement || element instanceof BeanPropertyElement;
|
||||
}
|
||||
|
||||
public void renderElement(final LookupItem item, final Object element, final LookupElementPresentation presentation) {
|
||||
presentation.setIcon(DefaultLookupItemRenderer.getRawIcon(item, presentation.isReal()));
|
||||
|
||||
final boolean bold = item.getAttribute(LookupItem.HIGHLIGHTED_ATTR) != null;
|
||||
boolean strikeout = isToStrikeout(item);
|
||||
presentation.setItemText(getName(element, item));
|
||||
presentation.setStrikeout(strikeout);
|
||||
presentation.setItemTextBold(bold);
|
||||
presentation.setStrikeout(isToStrikeout(item));
|
||||
presentation.setItemTextBold(item.getAttribute(LookupItem.HIGHLIGHTED_ATTR) != null);
|
||||
|
||||
String tailText = StringUtil.notNullize(getTailText(element, item));
|
||||
boolean grayed = item.getAttribute(LookupItem.TAIL_TEXT_SMALL_ATTR) != null;
|
||||
PsiSubstitutor substitutor = (PsiSubstitutor)item.getAttribute(LookupItem.SUBSTITUTOR);
|
||||
if (element instanceof PsiClass) {
|
||||
final PsiClass psiClass = (PsiClass)element;
|
||||
if (item.getAttribute(LookupItem.INDICATE_ANONYMOUS) != null &&
|
||||
(psiClass.isInterface() || psiClass.hasModifierProperty(PsiModifier.ABSTRACT))) {
|
||||
tailText = "{...}" + tailText;
|
||||
}
|
||||
if (substitutor == null && psiClass.getTypeParameters().length > 0) {
|
||||
tailText = "<" + StringUtil.join(psiClass.getTypeParameters(), new Function<PsiTypeParameter, String>() {
|
||||
public String fun(PsiTypeParameter psiTypeParameter) {
|
||||
return psiTypeParameter.getName();
|
||||
}
|
||||
}, "," + (showSpaceAfterComma(psiClass) ? " " : "")) + ">" + tailText;
|
||||
}
|
||||
grayed = true;
|
||||
}
|
||||
presentation.setTailText(tailText, grayed);
|
||||
presentation.setTailText((String)item.getAttribute(LookupItem.TAIL_TEXT_ATTR), item.getAttribute(LookupItem.TAIL_TEXT_SMALL_ATTR) != null);
|
||||
|
||||
final String typeText = getTypeText(element, item);
|
||||
presentation.setTypeText(typeText != null ? typeText : "");
|
||||
presentation.setTypeText(getTypeText(element, item));
|
||||
}
|
||||
|
||||
private static String getName(final Object o, final LookupItem<?> item) {
|
||||
@@ -104,56 +79,16 @@ public class JavaElementLookupRenderer implements ElementLookupRenderer {
|
||||
}
|
||||
}
|
||||
|
||||
PsiSubstitutor substitutor = (PsiSubstitutor)item.getAttribute(LookupItem.SUBSTITUTOR);
|
||||
if (o instanceof PsiClass && substitutor != null) {
|
||||
final PsiClass psiClass = (PsiClass)o;
|
||||
final PsiTypeParameter[] params = psiClass.getTypeParameters();
|
||||
if (params.length > 0) {
|
||||
return name + formatTypeParameters(substitutor, params);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return StringUtil.notNullize(name);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static String getTailText(final Object o, final LookupItem item) {
|
||||
String text = null;
|
||||
if (o instanceof PsiElement) {
|
||||
final PsiElement element = (PsiElement)o;
|
||||
if (element.isValid() && element instanceof PsiMethod){
|
||||
PsiMethod method = (PsiMethod)element;
|
||||
final PsiSubstitutor substitutor = (PsiSubstitutor) item.getAttribute(LookupItem.SUBSTITUTOR);
|
||||
text = PsiFormatUtil.formatMethod(method,
|
||||
substitutor != null ? substitutor : PsiSubstitutor.EMPTY,
|
||||
PsiFormatUtil.SHOW_PARAMETERS,
|
||||
PsiFormatUtil.SHOW_NAME | PsiFormatUtil.SHOW_TYPE);
|
||||
}
|
||||
}
|
||||
|
||||
String tailText = (String)item.getAttribute(LookupItem.TAIL_TEXT_ATTR);
|
||||
if (tailText != null){
|
||||
if (text == null){
|
||||
text = tailText;
|
||||
}
|
||||
else{
|
||||
text += tailText;
|
||||
}
|
||||
}
|
||||
return text;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static String getTypeText(final Object o, final LookupItem item) {
|
||||
String text = null;
|
||||
if (o instanceof PsiElement) {
|
||||
final PsiElement element = (PsiElement)o;
|
||||
if (element.isValid()) {
|
||||
if (element instanceof PsiMethod){
|
||||
text = getTypeText(item, ((PsiMethod)element).getReturnType());
|
||||
}
|
||||
else if (element instanceof PsiVariable){
|
||||
if (element instanceof PsiVariable){
|
||||
PsiVariable variable = (PsiVariable)element;
|
||||
text = variable.getType().getPresentableText();
|
||||
}
|
||||
@@ -186,38 +121,6 @@ public class JavaElementLookupRenderer implements ElementLookupRenderer {
|
||||
return returnType.getPresentableText();
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static String formatTypeParameters(@NotNull final PsiSubstitutor substitutor, final PsiTypeParameter[] params) {
|
||||
final boolean space = showSpaceAfterComma(params[0]);
|
||||
StringBuilder buffer = new StringBuilder();
|
||||
buffer.append("<");
|
||||
for(int i = 0; i < params.length; i++){
|
||||
final PsiTypeParameter param = params[i];
|
||||
final PsiType type = substitutor.substitute(param);
|
||||
if(type == null){
|
||||
return "";
|
||||
}
|
||||
if (type instanceof PsiClassType && ((PsiClassType)type).getParameters().length > 0) {
|
||||
buffer.append(((PsiClassType)type).rawType().getPresentableText()).append("<...>");
|
||||
} else {
|
||||
buffer.append(type.getPresentableText());
|
||||
}
|
||||
|
||||
if(i < params.length - 1) {
|
||||
buffer.append(",");
|
||||
if (space) {
|
||||
buffer.append(" ");
|
||||
}
|
||||
}
|
||||
}
|
||||
buffer.append(">");
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
private static boolean showSpaceAfterComma(PsiClass element) {
|
||||
return CodeStyleSettingsManager.getSettings(element.getProject()).SPACE_AFTER_COMMA;
|
||||
}
|
||||
|
||||
public static boolean isToStrikeout(LookupItem<?> item) {
|
||||
final List<PsiMethod> allMethods = item.getUserData(JavaCompletionUtil.ALL_METHODS_ATTRIBUTE);
|
||||
if (allMethods != null){
|
||||
|
||||
Reference in New Issue
Block a user