update copyright
[fedora-idea.git] / java / java-impl / src / com / intellij / codeInsight / template / macro / ExpectedTypeMacro.java
blob592caa55f0dbed40dd1e5e3d1749f2dda79001ef
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.template.macro;
18 import com.intellij.codeInsight.CodeInsightBundle;
19 import com.intellij.codeInsight.ExpectedTypeInfo;
20 import com.intellij.codeInsight.ExpectedTypesProvider;
21 import com.intellij.codeInsight.completion.CompletionUtil;
22 import com.intellij.codeInsight.lookup.LookupElement;
23 import com.intellij.codeInsight.template.*;
24 import com.intellij.codeInsight.template.impl.JavaTemplateUtil;
25 import com.intellij.openapi.command.WriteCommandAction;
26 import com.intellij.openapi.components.ServiceManager;
27 import com.intellij.openapi.project.Project;
28 import com.intellij.psi.*;
29 import com.intellij.psi.text.BlockSupport;
30 import org.jetbrains.annotations.NotNull;
31 import org.jetbrains.annotations.Nullable;
33 import java.util.LinkedHashSet;
34 import java.util.Set;
36 public class ExpectedTypeMacro implements Macro{
38 public String getName() {
39 return "expectedType";
42 public String getDescription() {
43 return CodeInsightBundle.message("macro.expected.type");
46 public String getDefaultValue() {
47 return "A";
50 public Result calculateResult(@NotNull Expression[] params, ExpressionContext context) {
51 PsiType[] types = getExpectedTypes(params, context);
52 if (types == null || types.length == 0) return null;
53 return new PsiTypeResult(types[0], context.getProject());
56 public Result calculateQuickResult(@NotNull Expression[] params, ExpressionContext context) {
57 return null;
60 public LookupElement[] calculateLookupItems(@NotNull Expression[] params, ExpressionContext context) {
61 PsiType[] types = getExpectedTypes(params, context);
62 if (types == null || types.length < 2) return null;
63 Set<LookupElement> set = new LinkedHashSet<LookupElement>();
64 for (PsiType type : types) {
65 JavaTemplateUtil.addTypeLookupItem(set, type);
67 return set.toArray(new LookupElement[set.size()]);
70 @Nullable
71 private static PsiType[] getExpectedTypes(Expression[] params, final ExpressionContext context) {
72 if (params.length != 0) return null;
74 final Project project = context.getProject();
75 PsiDocumentManager.getInstance(project).commitAllDocuments();
76 PsiType[] types = null;
78 final int offset = context.getTemplateStartOffset();
79 PsiFile file = PsiDocumentManager.getInstance(project).getPsiFile(context.getEditor().getDocument());
80 assert file != null;
81 final PsiFile fileCopy = (PsiFile)file.copy();
83 new WriteCommandAction(project) {
84 protected void run(com.intellij.openapi.application.Result result) throws Throwable {
85 final BlockSupport blockSupport = ServiceManager.getService(project, BlockSupport.class);
86 blockSupport.reparseRange(fileCopy, offset, offset, CompletionUtil.DUMMY_IDENTIFIER);
88 }.execute();
89 PsiElement element = fileCopy.findElementAt(offset);
91 if (element instanceof PsiIdentifier && element.getParent() instanceof PsiExpression) {
92 ExpectedTypeInfo[] infos = ExpectedTypesProvider.getInstance(project).getExpectedTypes((PsiExpression)element.getParent(), true);
93 if (infos.length > 0){
94 types = new PsiType[infos.length];
95 for(int i = 0; i < infos.length; i++) {
96 ExpectedTypeInfo info = infos[i];
97 types[i] = info.getType();
102 return types;