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
.lookup
.AutoCompletionPolicy
;
19 import com
.intellij
.codeInsight
.lookup
.LookupElement
;
20 import com
.intellij
.codeInsight
.lookup
.LookupItem
;
21 import com
.intellij
.openapi
.application
.ApplicationManager
;
22 import com
.intellij
.patterns
.ElementPattern
;
23 import static com
.intellij
.patterns
.PlatformPatterns
.psiElement
;
24 import com
.intellij
.patterns
.PsiJavaPatterns
;
25 import static com
.intellij
.patterns
.StandardPatterns
.or
;
26 import com
.intellij
.psi
.*;
27 import com
.intellij
.util
.ProcessingContext
;
28 import gnu
.trove
.THashSet
;
29 import org
.jetbrains
.annotations
.NotNull
;
36 public class JavaMemberNameCompletionContributor
extends CompletionContributor
{
37 public static final ElementPattern
<PsiElement
> INSIDE_TYPE_PARAMS_PATTERN
=
38 psiElement().afterLeaf(psiElement().withText("?").afterLeaf("<", ","));
40 public JavaMemberNameCompletionContributor() {
43 psiElement(PsiIdentifier
.class).andNot(INSIDE_TYPE_PARAMS_PATTERN
).withParent(
44 or(psiElement(PsiLocalVariable
.class), psiElement(PsiParameter
.class))),
45 new CompletionProvider
<CompletionParameters
>() {
46 public void addCompletions(@NotNull final CompletionParameters parameters
, final ProcessingContext matchingContext
, @NotNull final CompletionResultSet result
) {
47 final Set
<LookupElement
> lookupSet
= new THashSet
<LookupElement
>();
48 ApplicationManager
.getApplication().runReadAction(new Runnable() {
50 JavaCompletionUtil
.completeLocalVariableName(lookupSet
, result
.getPrefixMatcher(), (PsiVariable
)parameters
.getPosition().getParent());
53 for (final LookupElement item
: lookupSet
) {
54 if (item
instanceof LookupItem
) {
55 ((LookupItem
)item
).setAutoCompletionPolicy(AutoCompletionPolicy
.GIVE_CHANCE_TO_OVERWRITE
);
57 result
.addElement(item
);
63 psiElement(PsiIdentifier
.class).withParent(PsiField
.class).andNot(INSIDE_TYPE_PARAMS_PATTERN
),
64 new CompletionProvider
<CompletionParameters
>() {
65 public void addCompletions(@NotNull final CompletionParameters parameters
, final ProcessingContext matchingContext
, @NotNull final CompletionResultSet result
) {
66 final Set
<LookupElement
> lookupSet
= new THashSet
<LookupElement
>();
67 ApplicationManager
.getApplication().runReadAction(new Runnable() {
69 final PsiVariable variable
= (PsiVariable
)parameters
.getPosition().getParent();
70 JavaCompletionUtil
.completeFieldName(lookupSet
, variable
, result
.getPrefixMatcher());
71 JavaCompletionUtil
.completeMethodName(lookupSet
, variable
, result
.getPrefixMatcher());
74 for (final LookupElement item
: lookupSet
) {
75 result
.addElement(item
);
80 CompletionType
.BASIC
, PsiJavaPatterns
.psiElement().nameIdentifierOf(PsiJavaPatterns
.psiMethod().withParent(PsiClass
.class)),
81 new CompletionProvider
<CompletionParameters
>() {
82 public void addCompletions(@NotNull final CompletionParameters parameters
, final ProcessingContext matchingContext
, @NotNull final CompletionResultSet result
) {
83 final Set
<LookupElement
> lookupSet
= new THashSet
<LookupElement
>();
84 ApplicationManager
.getApplication().runReadAction(new Runnable() {
86 JavaCompletionUtil
.completeMethodName(lookupSet
, parameters
.getPosition().getParent(), result
.getPrefixMatcher());
89 for (final LookupElement item
: lookupSet
) {
90 result
.addElement(item
);