update copyright
[fedora-idea.git] / java / java-impl / src / com / intellij / codeInsight / lookup / impl / JavaElementLookupRenderer.java
blobba9cee8a39c8b40264788d90781c209a5a2619bc
1 /*
2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.intellij.codeInsight.lookup.impl;
18 import com.intellij.codeInsight.completion.JavaCompletionUtil;
19 import com.intellij.codeInsight.lookup.DefaultLookupItemRenderer;
20 import com.intellij.codeInsight.lookup.LookupElementPresentation;
21 import com.intellij.codeInsight.lookup.LookupItem;
22 import com.intellij.openapi.util.text.StringUtil;
23 import com.intellij.psi.*;
24 import com.intellij.psi.impl.beanProperties.BeanPropertyElement;
25 import com.intellij.psi.util.PsiFormatUtil;
26 import com.intellij.psi.util.PsiUtilBase;
27 import org.jetbrains.annotations.Nullable;
29 import java.util.List;
31 /**
32 * @author yole
34 public class JavaElementLookupRenderer implements ElementLookupRenderer {
35 public boolean handlesItem(final Object element) {
36 return element instanceof PsiClass || element instanceof PsiMember || element instanceof PsiVariable ||
37 element instanceof PsiType || element instanceof PsiKeyword || element instanceof PsiExpression ||
38 element instanceof PsiTypeElement || element instanceof BeanPropertyElement;
41 public void renderElement(final LookupItem item, final Object element, final LookupElementPresentation presentation) {
42 presentation.setIcon(DefaultLookupItemRenderer.getRawIcon(item, presentation.isReal()));
44 final boolean bold = item.getAttribute(LookupItem.HIGHLIGHTED_ATTR) != null;
45 boolean strikeout = isToStrikeout(item);
46 presentation.setItemText(getName(element, item));
47 presentation.setStrikeout(strikeout);
48 presentation.setItemTextBold(bold);
50 String tailText = StringUtil.notNullize(getTailText(element, item));
51 boolean grayed = item.getAttribute(LookupItem.TAIL_TEXT_SMALL_ATTR) != null;
52 PsiSubstitutor substitutor = (PsiSubstitutor)item.getAttribute(LookupItem.SUBSTITUTOR);
53 if (element instanceof PsiClass) {
54 final PsiClass psiClass = (PsiClass)element;
55 if (item.getAttribute(LookupItem.INDICATE_ANONYMOUS) != null &&
56 (psiClass.isInterface() || psiClass.hasModifierProperty(PsiModifier.ABSTRACT))) {
57 tailText = "{...}" + tailText;
59 if (substitutor != null && psiClass.getTypeParameters().length > 0) {
60 tailText = "<...>" + tailText;
62 grayed = true;
64 presentation.setTailText(tailText, grayed);
66 final String typeText = getTypeText(element, item);
67 presentation.setTypeText(typeText != null ? typeText : "");
70 private static String getName(final Object o, final LookupItem<?> item) {
71 final String presentableText = item.getPresentableText();
72 if (presentableText != null) {
73 return presentableText;
76 String name = "";
77 if (o instanceof PsiElement) {
78 final PsiElement element = (PsiElement)o;
79 if (element.isValid()) {
80 if (element instanceof PsiKeyword || element instanceof PsiExpression || element instanceof PsiTypeElement) {
81 name = element.getText();
82 } else {
83 name = PsiUtilBase.getName(element);
87 else if (o instanceof PsiArrayType) {
88 name = ((PsiArrayType)o).getDeepComponentType().getPresentableText();
90 else if (o instanceof PsiType) {
91 name = ((PsiType)o).getPresentableText();
94 if (item.getAttribute(LookupItem.FORCE_QUALIFY) != null) {
95 if (o instanceof PsiMember && ((PsiMember)o).getContainingClass() != null) {
96 name = ((PsiMember)o).getContainingClass().getName() + "." + name;
100 return StringUtil.notNullize(name);
103 @Nullable
104 private static String getTailText(final Object o, final LookupItem item) {
105 String text = null;
106 if (o instanceof PsiElement) {
107 final PsiElement element = (PsiElement)o;
108 if (element.isValid() && element instanceof PsiMethod){
109 PsiMethod method = (PsiMethod)element;
110 final PsiSubstitutor substitutor = (PsiSubstitutor) item.getAttribute(LookupItem.SUBSTITUTOR);
111 text = PsiFormatUtil.formatMethod(method,
112 substitutor != null ? substitutor : PsiSubstitutor.EMPTY,
113 PsiFormatUtil.SHOW_PARAMETERS,
114 PsiFormatUtil.SHOW_NAME | PsiFormatUtil.SHOW_TYPE);
118 String tailText = (String)item.getAttribute(LookupItem.TAIL_TEXT_ATTR);
119 if (tailText != null){
120 if (text == null){
121 text = tailText;
123 else{
124 text += tailText;
127 return text;
130 @Nullable
131 private static String getTypeText(final Object o, final LookupItem item) {
132 String text = null;
133 if (o instanceof PsiElement) {
134 final PsiElement element = (PsiElement)o;
135 if (element.isValid()) {
136 if (element instanceof PsiMethod){
137 text = getTypeText(item, ((PsiMethod)element).getReturnType());
139 else if (element instanceof PsiVariable){
140 PsiVariable variable = (PsiVariable)element;
141 text = variable.getType().getPresentableText();
143 else if (element instanceof PsiExpression){
144 PsiExpression expression = (PsiExpression)element;
145 PsiType type = expression.getType();
146 if (type != null){
147 text = type.getPresentableText();
150 else if (element instanceof BeanPropertyElement) {
151 return getTypeText(item, ((BeanPropertyElement)element).getPropertyType());
156 return text;
159 @Nullable
160 private static String getTypeText(LookupItem item, @Nullable PsiType returnType) {
161 if (returnType == null) {
162 return null;
165 final PsiSubstitutor substitutor = (PsiSubstitutor)item.getAttribute(LookupItem.SUBSTITUTOR);
166 if (substitutor != null) {
167 return substitutor.substitute(returnType).getPresentableText();
169 return returnType.getPresentableText();
172 private static boolean isToStrikeout(LookupItem<?> item) {
173 final List<PsiMethod> allMethods = item.getUserData(JavaCompletionUtil.ALL_METHODS_ATTRIBUTE);
174 if (allMethods != null){
175 for (PsiMethod method : allMethods) {
176 if (!method.isValid()) { //?
177 return false;
179 if (!isDeprecated(method)) {
180 return false;
183 return true;
185 else if (item.getObject() instanceof PsiElement) {
186 final PsiElement element = (PsiElement)item.getObject();
187 if (element.isValid()) {
188 return isDeprecated(element);
191 return false;
194 private static boolean isDeprecated(PsiElement element) {
195 return element instanceof PsiDocCommentOwner && ((PsiDocCommentOwner)element).isDeprecated();