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
.lookup
.LookupElement
;
20 import com
.intellij
.codeInsight
.template
.*;
21 import com
.intellij
.codeInsight
.template
.impl
.JavaTemplateUtil
;
22 import com
.intellij
.openapi
.project
.Project
;
23 import com
.intellij
.openapi
.util
.TextRange
;
24 import com
.intellij
.psi
.*;
25 import com
.intellij
.psi
.util
.PsiTreeUtil
;
26 import org
.jetbrains
.annotations
.NotNull
;
27 import org
.jetbrains
.annotations
.Nullable
;
29 import java
.util
.ArrayList
;
30 import java
.util
.Arrays
;
31 import java
.util
.LinkedHashSet
;
34 public class VariableOfTypeMacro
implements Macro
{
36 public String
getName() {
37 return "variableOfType";
40 public String
getDescription() {
41 return CodeInsightBundle
.message("macro.variable.of.type");
44 public String
getDefaultValue() {
48 public Result
calculateResult(@NotNull Expression
[] params
, ExpressionContext context
) {
49 final PsiElement
[] vars
= getVariables(params
, context
);
50 if (vars
== null || vars
.length
== 0) return null;
51 return new JavaPsiElementResult(vars
[0]);
54 public Result
calculateQuickResult(@NotNull Expression
[] params
, ExpressionContext context
) {
58 public LookupElement
[] calculateLookupItems(@NotNull Expression
[] params
, final ExpressionContext context
) {
59 final PsiElement
[] vars
= getVariables(params
, context
);
60 if (vars
== null || vars
.length
< 2) return null;
61 final Set
<LookupElement
> set
= new LinkedHashSet
<LookupElement
>();
62 for (PsiElement var
: vars
) {
63 JavaTemplateUtil
.addElementLookupItem(set
, var
);
65 return set
.toArray(new LookupElement
[set
.size()]);
69 private static PsiElement
[] getVariables(Expression
[] params
, final ExpressionContext context
) {
70 if (params
.length
!= 1) return null;
71 final Result result
= params
[0].calculateResult(context
);
72 if (result
== null) return null;
74 Project project
= context
.getProject();
75 final int offset
= context
.getStartOffset();
77 PsiDocumentManager
.getInstance(project
).commitAllDocuments();
79 final ArrayList
<PsiElement
> array
= new ArrayList
<PsiElement
>();
80 PsiType type
= MacroUtil
.resultToPsiType(result
, context
);
81 PsiFile file
= PsiDocumentManager
.getInstance(project
).getPsiFile(context
.getEditor().getDocument());
82 PsiElement place
= file
.findElementAt(offset
);
84 PsiVariable
[] variables
= MacroUtil
.getVariablesVisibleAt(place
, "");
85 PsiManager manager
= PsiManager
.getInstance(project
);
86 for (PsiVariable var
: variables
) {
87 if (var
instanceof PsiField
&& var
.hasModifierProperty(PsiModifier
.STATIC
)) {
88 PsiClass varClass
= ((PsiField
)var
).getContainingClass();
89 PsiClass placeClass
= PsiTreeUtil
.getParentOfType(place
, PsiClass
.class);
90 if (!manager
.areElementsEquivalent(varClass
, placeClass
)) continue;
92 else if (var
instanceof PsiLocalVariable
) {
93 final TextRange range
= var
.getNameIdentifier().getTextRange();
94 if (range
!= null && range
.contains(offset
)) {
99 PsiType type1
= var
.getType();
100 if (type
== null || type
.isAssignableFrom(type1
)) {
105 PsiExpression
[] expressions
= MacroUtil
.getStandardExpressionsOfType(place
, type
);
106 array
.addAll(Arrays
.asList(expressions
));
107 return array
.toArray(new PsiElement
[array
.size()]);