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
.hint
.api
.impls
;
18 import com
.intellij
.lang
.parameterInfo
.*;
19 import com
.intellij
.psi
.*;
20 import com
.intellij
.util
.text
.CharArrayUtil
;
21 import com
.intellij
.codeInsight
.lookup
.LookupElement
;
22 import org
.jetbrains
.annotations
.NonNls
;
23 import org
.jetbrains
.annotations
.Nullable
;
24 import org
.jetbrains
.annotations
.NotNull
;
27 * Created by IntelliJ IDEA.
28 * User: Maxim.Mossienko
31 * To change this template use File | Settings | File Templates.
33 public class AnnotationParameterInfoHandler
implements ParameterInfoHandler
<PsiAnnotationParameterList
,PsiAnnotationMethod
> {
34 public @Nullable Object
[] getParametersForLookup(LookupElement item
, ParameterInfoContext context
) {
38 public Object
[] getParametersForDocumentation(final PsiAnnotationMethod p
, final ParameterInfoContext context
) {
39 return new Object
[] {p
};
42 public boolean couldShowInLookup() {
46 public PsiAnnotationParameterList
findElementForParameterInfo(final CreateParameterInfoContext context
) {
47 final PsiAnnotation annotation
= ParameterInfoUtils
.findParentOfType(context
.getFile(), context
.getOffset(), PsiAnnotation
.class);
49 if (annotation
!= null) {
50 final PsiJavaCodeReferenceElement nameReference
= annotation
.getNameReferenceElement();
52 if (nameReference
!= null) {
53 final PsiElement resolved
= nameReference
.resolve();
55 if (resolved
instanceof PsiClass
) {
56 final PsiClass aClass
= (PsiClass
)resolved
;
58 if (aClass
.isAnnotationType()) {
59 final PsiMethod
[] methods
= aClass
.getMethods();
61 if (methods
.length
!= 0) {
62 context
.setItemsToShow(methods
);
64 final PsiAnnotationMethod annotationMethod
= findAnnotationMethod(context
.getFile(), context
.getOffset());
65 if (annotationMethod
!= null) context
.setHighlightedElement(annotationMethod
);
67 return annotation
.getParameterList();
77 public void showParameterInfo(@NotNull final PsiAnnotationParameterList element
, final CreateParameterInfoContext context
) {
78 context
.showHint(element
, element
.getTextRange().getStartOffset() + 1, this);
81 public PsiAnnotationParameterList
findElementForUpdatingParameterInfo(final UpdateParameterInfoContext context
) {
82 final PsiAnnotation annotation
= ParameterInfoUtils
.findParentOfType(context
.getFile(), context
.getOffset(), PsiAnnotation
.class);
83 return annotation
!= null ? annotation
.getParameterList() : null;
86 public void updateParameterInfo(@NotNull final PsiAnnotationParameterList o
, final UpdateParameterInfoContext context
) {
87 CharSequence chars
= context
.getEditor().getDocument().getCharsSequence();
88 int offset1
= CharArrayUtil
.shiftForward(chars
, context
.getEditor().getCaretModel().getOffset(), " \t");
89 if (chars
.charAt(offset1
) == ',') {
90 offset1
= CharArrayUtil
.shiftBackward(chars
, offset1
- 1, " \t");
92 context
.setHighlightedParameter(findAnnotationMethod(context
.getFile(), offset1
));
95 public String
getParameterCloseChars() {
96 return ParameterInfoUtils
.DEFAULT_PARAMETER_CLOSE_CHARS
;
99 public boolean tracksParameterIndex() {
103 public void updateUI(final PsiAnnotationMethod p
, final ParameterInfoUIContext context
) {
104 @NonNls StringBuffer buffer
= new StringBuffer();
105 int highlightStartOffset
;
106 int highlightEndOffset
;
107 buffer
.append(p
.getReturnType().getPresentableText());
109 highlightStartOffset
= buffer
.length();
110 buffer
.append(p
.getName());
111 highlightEndOffset
= buffer
.length();
114 if (p
.getDefaultValue() != null) {
115 buffer
.append(" default ");
116 buffer
.append(p
.getDefaultValue().getText());
119 context
.setupUIComponentPresentation(buffer
.toString(), highlightStartOffset
, highlightEndOffset
, false, p
.isDeprecated(),
120 false, context
.getDefaultParameterColor());
123 private static PsiAnnotationMethod
findAnnotationMethod(PsiFile file
, int offset
) {
124 PsiNameValuePair pair
= ParameterInfoUtils
.findParentOfType(file
, offset
, PsiNameValuePair
.class);
125 if (pair
== null) return null;
126 final PsiElement resolved
= pair
.getReference().resolve();
127 return resolved
instanceof PsiAnnotationMethod ?
(PsiAnnotationMethod
)resolved
: null;