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
.intention
.impl
;
18 import com
.intellij
.codeInsight
.lookup
.LookupElement
;
19 import com
.intellij
.codeInsight
.lookup
.PsiTypeLookupItem
;
20 import com
.intellij
.codeInsight
.template
.Expression
;
21 import com
.intellij
.codeInsight
.template
.ExpressionContext
;
22 import com
.intellij
.codeInsight
.template
.PsiTypeResult
;
23 import com
.intellij
.codeInsight
.template
.Result
;
24 import com
.intellij
.openapi
.project
.Project
;
25 import com
.intellij
.psi
.PsiDocumentManager
;
26 import com
.intellij
.psi
.PsiType
;
27 import com
.intellij
.psi
.SmartTypePointer
;
28 import com
.intellij
.psi
.SmartTypePointerManager
;
30 import java
.util
.ArrayList
;
31 import java
.util
.LinkedHashSet
;
32 import java
.util
.List
;
35 public class TypeExpression
extends Expression
{
36 private final Set
<SmartTypePointer
> myItems
;
38 public TypeExpression(final Project project
, PsiType
[] types
) {
39 final SmartTypePointerManager manager
= SmartTypePointerManager
.getInstance(project
);
40 myItems
= new LinkedHashSet
<SmartTypePointer
>();
41 for (final PsiType type
: types
) {
42 myItems
.add(manager
.createSmartTypePointer(type
));
46 public Result
calculateResult(ExpressionContext context
) {
47 final Project project
= context
.getProject();
48 PsiDocumentManager
.getInstance(project
).commitAllDocuments();
49 if (myItems
.isEmpty()) return null;
51 final PsiType type
= myItems
.iterator().next().getType();
52 return type
== null?
null : new PsiTypeResult(type
, project
);
55 public Result
calculateQuickResult(ExpressionContext context
) {
56 return calculateResult(context
);
59 public LookupElement
[] calculateLookupItems(ExpressionContext context
) {
60 if (myItems
.size() <= 1) return null;
61 PsiDocumentManager
.getInstance(context
.getProject()).commitAllDocuments();
63 List
<LookupElement
> result
= new ArrayList
<LookupElement
>(myItems
.size());
64 for (final SmartTypePointer item
: myItems
) {
65 final PsiType type
= item
.getType();
67 result
.add(PsiTypeLookupItem
.createLookupItem(type
));
70 return result
.toArray(new LookupElement
[result
.size()]);