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
.ExpectedTypeInfo
;
19 import com
.intellij
.codeInsight
.lookup
.LookupElement
;
20 import com
.intellij
.psi
.PsiClass
;
21 import com
.intellij
.psi
.PsiType
;
22 import org
.jetbrains
.annotations
.NotNull
;
27 public class PreferExpectedTypeWeigher
extends CompletionWeigher
{
29 private enum MyResult
{
35 public MyResult
weigh(@NotNull final LookupElement item
, final CompletionLocation location
) {
36 if (location
.getCompletionType() != CompletionType
.BASIC
) return MyResult
.normal
;
37 if (item
.getObject() instanceof PsiClass
) return MyResult
.normal
;
39 ExpectedTypeInfo
[] expectedInfos
= JavaCompletionUtil
.EXPECTED_TYPES
.getValue(location
);
40 if (expectedInfos
== null) return MyResult
.normal
;
42 PsiType itemType
= JavaCompletionUtil
.getLookupElementType(item
);
43 if (itemType
== null || !itemType
.isValid()) return MyResult
.normal
;
45 for (final ExpectedTypeInfo expectedInfo
: expectedInfos
) {
46 final PsiType defaultType
= expectedInfo
.getDefaultType();
47 final PsiType expectedType
= expectedInfo
.getType();
48 if (!expectedType
.isValid()) {
49 return MyResult
.normal
;
52 if (defaultType
!= expectedType
&& defaultType
.isAssignableFrom(itemType
)) {
53 return MyResult
.ofDefaultType
;
55 if (expectedType
.isAssignableFrom(itemType
)) {
56 return MyResult
.expected
;
60 return MyResult
.normal
;