1 package com
.intellij
.codeInspection
.i18n
.folding
;
3 import com
.intellij
.lang
.folding
.FoldingBuilder
;
4 import com
.intellij
.lang
.folding
.FoldingDescriptor
;
5 import com
.intellij
.lang
.ASTNode
;
6 import com
.intellij
.openapi
.editor
.Document
;
7 import com
.intellij
.openapi
.util
.Ref
;
8 import com
.intellij
.psi
.*;
9 import com
.intellij
.psi
.impl
.source
.SourceTreeToPsiMap
;
10 import com
.intellij
.codeInsight
.AnnotationUtil
;
11 import com
.intellij
.codeInspection
.i18n
.JavaI18nUtil
;
16 * @author Konstantin Bulenkov
18 public class PropertyFoldingBuilder
implements FoldingBuilder
{
19 public FoldingDescriptor
[] buildFoldRegions(ASTNode node
, Document document
) {
20 final PsiElement element
= node
.getPsi();
21 if (!(element
instanceof PsiJavaFile
)) {
22 return FoldingDescriptor
.EMPTY
;
24 final PsiJavaFile file
= (PsiJavaFile
) element
;
25 final List
<FoldingDescriptor
> result
= new ArrayList
<FoldingDescriptor
>();
27 file
.accept(new JavaRecursiveElementWalkingVisitor() {
29 public void visitLiteralExpression(PsiLiteralExpression expression
) {
30 final Map
<String
, Object
> annotationParams
= new HashMap
<String
, Object
>();
31 annotationParams
.put(AnnotationUtil
.PROPERTY_KEY_RESOURCE_BUNDLE_PARAMETER
, null);
32 final PsiElement parent
= expression
.getParent();
33 if (JavaI18nUtil
.mustBePropertyKey(expression
, annotationParams
) && parent
instanceof PsiExpressionList
) {
34 if (((PsiExpressionList
)parent
).getExpressions().length
== 1 && parent
.getParent() instanceof PsiMethodCallExpression
) {
35 final PsiMethodCallExpression expr
= (PsiMethodCallExpression
)parent
.getParent();
36 result
.add(new FoldingDescriptor(expr
.getNode(), expr
.getTextRange()));
42 return result
.toArray(new FoldingDescriptor
[result
.size()]);
47 public String
getPlaceholderText(ASTNode node
) {
48 final PsiElement element
= SourceTreeToPsiMap
.treeElementToPsi(node
);
49 if (element
instanceof PsiMethodCallExpression
) {
50 return getI18nMessage((PsiMethodCallExpression
)element
);
52 return element
.getText();
55 private static String
getI18nMessage(PsiMethodCallExpression methodCallExpression
) {
56 final PsiExpression
[] expressions
= methodCallExpression
.getArgumentList().getExpressions();
57 if (expressions
.length
== 1 && expressions
[0] instanceof PsiLiteralExpression
) {
58 final PsiLiteralExpression literalExpr
= (PsiLiteralExpression
)expressions
[0];
59 final Object value
= literalExpr
.getValue();
60 if (value
instanceof String
) {
61 final String key
= (String
)value
;
62 final Ref
<String
> ref
= new Ref
<String
>();
63 if (JavaI18nUtil
.isValidPropertyReference(literalExpr
, key
, ref
)) {
64 final ResourceBundle bundle
= ResourceBundle
.getBundle(ref
.get());
65 final String property
= (bundle
== null) ?
null : bundle
.getString(key
);
66 if (property
!= null) {
67 return "\"" + property
+ "\"";
72 return methodCallExpression
.getText();
75 public boolean isCollapsedByDefault(ASTNode node
) {