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
.LookupItem
;
20 import com
.intellij
.codeInsight
.lookup
.LookupElement
;
21 import com
.intellij
.codeInsight
.template
.*;
22 import com
.intellij
.psi
.PsiArrayType
;
23 import com
.intellij
.psi
.PsiDocumentManager
;
24 import com
.intellij
.psi
.PsiExpression
;
25 import com
.intellij
.psi
.PsiType
;
26 import org
.jetbrains
.annotations
.NotNull
;
28 public class ComponentTypeOfMacro
implements Macro
{
29 public String
getName() {
30 return "componentTypeOf";
33 public String
getDescription() {
34 return CodeInsightBundle
.message("macro.component.type.of.array");
37 public String
getDefaultValue() {
41 public LookupElement
[] calculateLookupItems(@NotNull Expression
[] params
, ExpressionContext context
) {
42 if (params
.length
!= 1) return null;
43 LookupElement
[] lookupItems
= params
[0].calculateLookupItems(context
);
44 if (lookupItems
== null) return null;
46 for (LookupElement element
: lookupItems
) {
47 if (element
instanceof LookupItem
) {
48 final LookupItem item
= (LookupItem
)element
;
49 Integer bracketsCount
= (Integer
)item
.getUserData(LookupItem
.BRACKETS_COUNT_ATTR
);
50 if (bracketsCount
== null) return null;
51 item
.putUserData(LookupItem
.BRACKETS_COUNT_ATTR
, new Integer(bracketsCount
.intValue() - 1));
58 public Result
calculateQuickResult(@NotNull Expression
[] params
, ExpressionContext context
) {
62 public Result
calculateResult(@NotNull Expression
[] params
, final ExpressionContext context
) {
63 if (params
.length
!= 1) return null;
64 final Result result
= params
[0].calculateResult(context
);
65 if (result
== null) return null;
67 PsiDocumentManager
.getInstance(context
.getProject()).commitAllDocuments();
68 if (result
instanceof PsiTypeResult
) {
69 PsiType type
= ((PsiTypeResult
) result
).getType();
70 if (type
instanceof PsiArrayType
) {
71 return new PsiTypeResult(((PsiArrayType
) type
).getComponentType(), context
.getProject());
75 PsiExpression expr
= MacroUtil
.resultToPsiExpression(result
, context
);
78 type
= MacroUtil
.resultToPsiType(result
, context
);
81 type
= expr
.getType();
83 if (type
instanceof PsiArrayType
) {
84 return new PsiTypeResult(((PsiArrayType
) type
).getComponentType(), context
.getProject());
87 return new PsiElementResult(null);