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
.CodeInsightUtil
;
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
.util
.Condition
;
23 import com
.intellij
.psi
.PsiDocumentManager
;
24 import com
.intellij
.psi
.PsiElement
;
25 import com
.intellij
.psi
.PsiFile
;
26 import com
.intellij
.psi
.PsiType
;
27 import com
.intellij
.util
.containers
.HashSet
;
28 import org
.jetbrains
.annotations
.NotNull
;
30 import java
.util
.LinkedHashSet
;
33 public class SubtypesMacro
implements Macro
{
34 public String
getName() {
38 public String
getDescription() {
39 return "subtypes(TYPE)";
42 public String
getDefaultValue() {
46 public Result
calculateResult(@NotNull Expression
[] params
, ExpressionContext context
) {
47 if (params
.length
== 0) return null;
48 return params
[0].calculateQuickResult(context
);
51 public Result
calculateQuickResult(@NotNull Expression
[] params
, ExpressionContext context
) {
52 return calculateResult(params
, context
);
55 public LookupElement
[] calculateLookupItems(@NotNull Expression
[] params
, ExpressionContext context
) {
56 if (params
.length
== 0) return LookupElement
.EMPTY_ARRAY
;
57 Result paramResult
= params
[0].calculateQuickResult(context
);
58 if (paramResult
instanceof PsiTypeResult
) {
59 final PsiType type
= ((PsiTypeResult
)paramResult
).getType();
60 Set
<PsiType
> types
= new HashSet
<PsiType
>();
62 final PsiFile file
= PsiDocumentManager
.getInstance(context
.getProject()).getPsiFile(context
.getEditor().getDocument());
63 final PsiElement element
= file
.findElementAt(context
.getStartOffset());
64 types
.addAll(CodeInsightUtil
.addSubtypes(type
, element
, false, Condition
.TRUE
));
65 final Set
<LookupElement
> set
= new LinkedHashSet
<LookupElement
>();
66 for (PsiType t
: types
) {
67 JavaTemplateUtil
.addTypeLookupItem(set
, t
);
69 return set
.toArray(new LookupElement
[set
.size()]);
71 return LookupElement
.EMPTY_ARRAY
;