update copyright
[fedora-idea.git] / java / java-impl / src / com / intellij / codeInsight / completion / JavaAwareCompletionData.java
blob073e3cf18d1cb31a0d74c598fb76c64c843a3b1a
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.completion;
18 import com.intellij.codeInsight.TailType;
19 import com.intellij.codeInsight.lookup.LookupElement;
20 import com.intellij.codeInsight.lookup.LookupItem;
21 import com.intellij.codeInsight.lookup.LookupItemUtil;
22 import com.intellij.psi.*;
23 import com.intellij.psi.filters.ContextGetter;
24 import com.intellij.util.IncorrectOperationException;
25 import org.jetbrains.annotations.NotNull;
27 import java.util.Map;
28 import java.util.Set;
30 /**
31 * @author peter
33 public class JavaAwareCompletionData extends CompletionData{
35 protected void addLookupItem(Set<LookupElement> set, TailType tailType, @NotNull Object completion, final PsiFile file, final CompletionVariant variant) {
36 if (completion instanceof LookupElement && !(completion instanceof LookupItem)) {
37 set.add((LookupElement)completion);
38 return;
41 LookupElement _ret = LookupItemUtil.objectToLookupItem(completion);
42 if(_ret == null || !(_ret instanceof LookupItem)) return;
44 LookupItem ret = (LookupItem)_ret;
45 final InsertHandler insertHandler = variant.getInsertHandler();
46 if(insertHandler != null && ret.getInsertHandler() == null) {
47 ret.setInsertHandler(insertHandler);
48 ret.setTailType(TailType.UNKNOWN);
50 else if (tailType != TailType.NONE) {
51 ret.setTailType(tailType);
54 final Map<Object, Object> itemProperties = variant.getItemProperties();
55 for (final Object key : itemProperties.keySet()) {
56 ret.setAttribute(key, itemProperties.get(key));
58 set.add(ret);
61 protected void addKeywords(final Set<LookupElement> set, final PsiElement position, final PrefixMatcher matcher, final PsiFile file,
62 final CompletionVariant variant, final Object comp, final TailType tailType) {
63 final PsiElementFactory factory = JavaPsiFacade.getInstance(file.getProject()).getElementFactory();
64 if (comp instanceof String) {
65 addKeyword(factory, set, tailType, comp, matcher, file, variant);
67 else {
68 final CompletionContext context = position.getUserData(CompletionContext.COMPLETION_CONTEXT_KEY);
69 if (comp instanceof ContextGetter) {
70 final Object[] elements = ((ContextGetter)comp).get(position, context);
71 for (Object element : elements) {
72 addLookupItem(set, tailType, element, file, variant);
75 // TODO: KeywordChooser -> ContextGetter
76 else if (comp instanceof KeywordChooser) {
77 final String[] keywords = ((KeywordChooser)comp).getKeywords(context, position);
78 for (String keyword : keywords) {
79 addKeyword(factory, set, tailType, keyword, matcher, file, variant);
85 private void addKeyword(PsiElementFactory factory, Set<LookupElement> set, final TailType tailType, final Object comp, final PrefixMatcher matcher,
86 final PsiFile file,
87 final CompletionVariant variant) {
88 for (final LookupElement item : set) {
89 if (item.getObject().toString().equals(comp.toString())) {
90 return;
93 if(factory == null){
94 addLookupItem(set, tailType, comp, file, variant);
96 else{
97 try{
98 final PsiKeyword keyword = factory.createKeyword((String)comp);
99 addLookupItem(set, tailType, keyword, file, variant);
101 catch(IncorrectOperationException e){
102 addLookupItem(set, tailType, comp, file, variant);
107 public void fillCompletions(CompletionParameters parameters, CompletionResultSet result) {