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
;
18 import com
.intellij
.codeInsight
.TailType
;
19 import com
.intellij
.codeInsight
.completion
.CompletionUtil
;
20 import com
.intellij
.codeInsight
.completion
.JavaPsiClassReferenceElement
;
21 import com
.intellij
.codeInsight
.completion
.PrefixMatcher
;
22 import com
.intellij
.codeInsight
.completion
.JavaMethodCallElement
;
23 import com
.intellij
.codeInsight
.completion
.impl
.CamelHumpMatcher
;
24 import com
.intellij
.codeInsight
.template
.Template
;
25 import com
.intellij
.openapi
.diagnostic
.Logger
;
26 import com
.intellij
.openapi
.util
.text
.StringUtil
;
27 import com
.intellij
.psi
.*;
28 import com
.intellij
.psi
.meta
.PsiMetaData
;
29 import com
.intellij
.psi
.util
.PsiUtilBase
;
30 import com
.intellij
.util
.containers
.ContainerUtil
;
31 import org
.jetbrains
.annotations
.NotNull
;
32 import org
.jetbrains
.annotations
.Nullable
;
34 import java
.util
.ArrayList
;
35 import java
.util
.Collection
;
36 import java
.util
.List
;
40 * Created by IntelliJ IDEA.
44 * To change this template use Options | File Templates.
46 public class LookupItemUtil
{
47 private static final Logger LOG
= Logger
.getInstance("#com.intellij.codeInsight.lookup.LookupItemUtil");
49 private LookupItemUtil() {
53 public static LookupElement
addLookupItem(Collection
<LookupElement
> set
, @NotNull Object object
) {
54 return addLookupItem(set
, object
, new CamelHumpMatcher(""));
58 public static LookupElement
addLookupItem(Collection
<LookupElement
> set
, @NotNull Object object
, PrefixMatcher matcher
) {
59 if (object
instanceof PsiType
) {
60 PsiType psiType
= (PsiType
)object
;
61 for (final LookupElement lookupItem
: set
) {
62 Object o
= lookupItem
.getObject();
63 if (o
.equals(psiType
)) {
69 for (LookupElement lookupItem
: set
) {
70 if(lookupItem
.getObject().equals(lookupItem
)) return null;
72 LookupElement item
= objectToLookupItem(object
);
73 if (matcher
.prefixMatches(item
)) {
74 return set
.add(item
) ? item
: null;
79 public static List
<LookupElement
> addLookupItems(Set
<LookupElement
> set
, Object
[] objects
, PrefixMatcher matcher
) {
80 final ArrayList
<LookupElement
> list
= new ArrayList
<LookupElement
>(objects
.length
);
81 for (Object object
: objects
) {
82 LOG
.assertTrue(object
!= null, "Lookup item can't be null!");
83 ContainerUtil
.addIfNotNull(addLookupItem(set
, object
, matcher
), list
);
90 * @see LookupElementBuilder
92 public static LookupElement
objectToLookupItem(Object object
) {
93 if (object
instanceof LookupElement
) return (LookupElement
)object
;
94 if (object
instanceof PsiClass
) {
95 return new JavaPsiClassReferenceElement((PsiClass
)object
).setTailType(TailType
.NONE
);
97 if (object
instanceof PsiMethod
) {
98 return new JavaMethodCallElement((PsiMethod
)object
);
102 LookupItem item
= new LookupItem(object
, "");
103 if (object
instanceof PsiElement
){
104 s
= PsiUtilBase
.getName((PsiElement
) object
);
106 if (object
instanceof PsiEnumConstant
) {
107 item
.addLookupStrings(((PsiEnumConstant
)object
).getName());
109 TailType tailType
= TailType
.NONE
;
110 if (object
instanceof PsiPackage
) {
111 tailType
= TailType
.DOT
;
112 s
= StringUtil
.notNullize(s
);
114 else if (object
instanceof PsiKeyword
) {
115 return new KeywordLookupItem((PsiKeyword
)object
).setTailType(tailType
);
117 else if (object
instanceof PsiExpression
) {
118 return new ExpressionLookupItem((PsiExpression
) object
).setTailType(tailType
);
120 else if (object
instanceof PsiType
) {
121 return PsiTypeLookupItem
.createLookupItem((PsiType
)object
);
123 else if (object
instanceof PsiMetaData
) {
124 s
= ((PsiMetaData
)object
).getName();
126 else if (object
instanceof String
) {
129 else if (object
instanceof Template
) {
130 s
= ((Template
) object
).getKey();
132 else if (object
instanceof PresentableLookupValue
) {
133 s
= ((PresentableLookupValue
)object
).getPresentation();
136 if (object
instanceof LookupValueWithUIHint
&& ((LookupValueWithUIHint
) object
).isBold() || object
instanceof PsiKeyword
) {
141 LOG
.assertTrue(false, "Null string for object: " + object
+ " of class " + (object
!= null ?object
.getClass():null));
143 if (object
instanceof LookupValueWithTail
) {
144 item
.setAttribute(LookupItem
.TAIL_TEXT_ATTR
, " " + ((LookupValueWithTail
)object
).getTailText());
146 item
.setLookupString(s
);
148 item
.setAttribute(CompletionUtil
.TAIL_TYPE_ATTR
, tailType
);