2 * Copyright 2006 Sascha Weinreuter
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 org
.intellij
.plugins
.intelliLang
.util
;
18 import com
.intellij
.openapi
.application
.ApplicationManager
;
19 import com
.intellij
.openapi
.editor
.Document
;
20 import com
.intellij
.openapi
.editor
.impl
.DocumentImpl
;
21 import com
.intellij
.openapi
.project
.Project
;
22 import com
.intellij
.openapi
.roots
.ProjectFileIndex
;
23 import com
.intellij
.openapi
.roots
.ProjectRootManager
;
24 import com
.intellij
.openapi
.util
.Comparing
;
25 import com
.intellij
.openapi
.vfs
.VirtualFile
;
26 import com
.intellij
.psi
.*;
27 import com
.intellij
.ui
.JavaReferenceEditorUtil
;
28 import org
.jetbrains
.annotations
.NotNull
;
29 import org
.jetbrains
.annotations
.Nullable
;
31 public class PsiUtilEx
{
36 public static boolean isInSourceContent(PsiElement e
) {
37 final VirtualFile file
= e
.getContainingFile().getVirtualFile();
38 if (file
== null) return false;
39 final ProjectFileIndex index
= ProjectRootManager
.getInstance(e
.getProject()).getFileIndex();
40 return index
.isInContent(file
);
44 public static PsiParameter
getParameterForArgument(PsiExpression element
) {
45 PsiElement p
= element
.getParent();
46 if (!(p
instanceof PsiExpressionList
)) return null;
47 PsiExpressionList list
= (PsiExpressionList
)p
;
48 PsiElement parent
= list
.getParent();
49 if (!(parent
instanceof PsiCallExpression
)) return null;
50 PsiExpression
[] arguments
= list
.getExpressions();
51 for (int i
= 0; i
< arguments
.length
; i
++) {
52 PsiExpression argument
= arguments
[i
];
53 if (argument
== element
) {
54 final PsiCallExpression call
= (PsiCallExpression
)parent
;
55 final PsiMethod method
= call
.resolveMethod();
57 final PsiParameter
[] parameters
= method
.getParameterList().getParameters();
58 if (parameters
.length
> i
) {
61 else if (parameters
.length
> 0) {
62 final PsiParameter lastParam
= parameters
[parameters
.length
- 1];
63 if (lastParam
.getType() instanceof PsiEllipsisType
) {
74 public static boolean isStringLiteral(PsiElement value
) {
75 if (value
instanceof PsiLiteralExpression
) {
76 final PsiLiteralExpression expression
= (PsiLiteralExpression
)value
;
77 final PsiType type
= expression
.getType();
78 if (type
!= null && isString(type
)) {
85 public static boolean isString(@NotNull PsiType type
) {
86 if (type
instanceof PsiClassType
) {
87 // optimization. doesn't require resolve
88 final String shortName
= ((PsiClassType
)type
).getClassName();
89 if (!Comparing
.equal(shortName
, CommonClassNames
.JAVA_LANG_STRING_SHORT
)) return false;
91 return CommonClassNames
.JAVA_LANG_STRING
.equals(type
.getCanonicalText());
94 public static boolean isStringOrStringArray(@NotNull PsiType type
) {
95 if (type
instanceof PsiArrayType
) {
96 return isString(((PsiArrayType
)type
).getComponentType());
99 return isString(type
);
103 public static Document
createDocument(final String s
, final Project project
) {
104 if (ApplicationManager
.getApplication().isUnitTestMode() || project
.isDefault()) {
105 return new DocumentImpl(s
);
108 return JavaReferenceEditorUtil
.createTypeDocument(s
, PsiManager
.getInstance(project
));
112 public static boolean isLanguageAnnotationTarget(final PsiModifierListOwner owner
) {
113 if (owner
instanceof PsiMethod
) {
114 final PsiType returnType
= ((PsiMethod
)owner
).getReturnType();
115 if (returnType
== null || !isStringOrStringArray(returnType
)) {
119 else if (owner
instanceof PsiVariable
) {
120 final PsiType type
= ((PsiVariable
)owner
).getType();
121 if (!isStringOrStringArray(type
)) {