|
16 | 16 | package com.intellij.java.language.impl.psi.presentation.java; |
17 | 17 |
|
18 | 18 | import com.intellij.java.language.psi.*; |
19 | | -import consulo.language.psi.PsiBundle; |
| 19 | +import consulo.annotation.access.RequiredReadAction; |
| 20 | +import consulo.language.localize.LanguageLocalize; |
20 | 21 | import consulo.language.psi.PsiElement; |
21 | 22 | import consulo.language.psi.PsiFile; |
22 | 23 | import consulo.language.psi.util.PsiTreeUtil; |
| 24 | +import consulo.localize.LocalizeValue; |
| 25 | +import consulo.util.lang.StringUtil; |
23 | 26 |
|
24 | 27 | public class ClassPresentationUtil { |
25 | | - private ClassPresentationUtil() { |
26 | | - } |
27 | | - |
28 | | - public static String getNameForClass(PsiClass aClass, boolean qualified) { |
29 | | - if (aClass instanceof PsiAnonymousClass) { |
30 | | - if (aClass instanceof PsiEnumConstantInitializer) { |
31 | | - PsiEnumConstant enumConstant = ((PsiEnumConstantInitializer) aClass).getEnumConstant(); |
32 | | - String name = enumConstant.getName(); |
33 | | - return PsiBundle.message("enum.constant.context", name, getContextName(enumConstant, qualified)); |
34 | | - } |
35 | | - return PsiBundle.message("anonymous.class.context.display", getContextName(aClass, qualified)); |
36 | | - } |
37 | | - if (qualified) { |
38 | | - String qName = aClass.getQualifiedName(); |
39 | | - if (qName != null) { |
40 | | - return qName; |
41 | | - } |
| 28 | + private ClassPresentationUtil() { |
42 | 29 | } |
43 | 30 |
|
44 | | - String className = aClass.getName(); |
45 | | - String contextName = getContextName(aClass, qualified); |
46 | | - return contextName != null ? PsiBundle.message("class.context.display", className, contextName) : className; |
47 | | - } |
| 31 | + @RequiredReadAction |
| 32 | + public static LocalizeValue getNameForClass(PsiClass aClass, boolean qualified) { |
| 33 | + if (aClass instanceof PsiAnonymousClass) { |
| 34 | + if (aClass instanceof PsiEnumConstantInitializer enumInitializer) { |
| 35 | + PsiEnumConstant enumConstant = enumInitializer.getEnumConstant(); |
| 36 | + return LanguageLocalize.enumConstantContext( |
| 37 | + StringUtil.notNullize(enumConstant.getName()), |
| 38 | + StringUtil.notNullize(getContextName(enumConstant, qualified)) |
| 39 | + ); |
| 40 | + } |
| 41 | + return LanguageLocalize.anonymousClassContextDisplay(StringUtil.notNullize(getContextName(aClass, qualified))); |
| 42 | + } |
| 43 | + if (qualified) { |
| 44 | + String qName = aClass.getQualifiedName(); |
| 45 | + if (qName != null) { |
| 46 | + return LocalizeValue.of(qName); |
| 47 | + } |
| 48 | + } |
48 | 49 |
|
49 | | - private static String getNameForElement(PsiElement element, boolean qualified) { |
50 | | - if (element instanceof PsiClass) { |
51 | | - return getNameForClass((PsiClass) element, qualified); |
52 | | - } else if (element instanceof PsiMethod) { |
53 | | - PsiMethod method = (PsiMethod) element; |
54 | | - String methodName = method.getName(); |
55 | | - return PsiBundle.message("method.context.display", methodName, getContextName(method, qualified)); |
56 | | - } else if (element instanceof PsiClassOwner) { |
57 | | - return null; |
58 | | - } else if (element instanceof PsiFile) { |
59 | | - return ((PsiFile) element).getName(); |
60 | | - } else if (element instanceof PsiField) { |
61 | | - return ((PsiField) element).getName(); |
62 | | - } else { |
63 | | - return null; |
| 50 | + String className = aClass.getName(); |
| 51 | + String contextName = getContextName(aClass, qualified); |
| 52 | + return contextName != null |
| 53 | + ? LanguageLocalize.classContextDisplay(StringUtil.notNullize(className), contextName) |
| 54 | + : LocalizeValue.ofNullable(className); |
64 | 55 | } |
65 | | - } |
66 | 56 |
|
67 | | - public static String getContextName(PsiElement element, boolean qualified) { |
68 | | - PsiElement parent = PsiTreeUtil.getStubOrPsiParentOfType(element, PsiMember.class); |
69 | | - if (parent == null) { |
70 | | - parent = element.getContainingFile(); |
| 57 | + @RequiredReadAction |
| 58 | + private static LocalizeValue getNameForElement(PsiElement element, boolean qualified) { |
| 59 | + if (element instanceof PsiClass psiClass) { |
| 60 | + return getNameForClass(psiClass, qualified); |
| 61 | + } |
| 62 | + else if (element instanceof PsiMethod method) { |
| 63 | + return LanguageLocalize.methodContextDisplay( |
| 64 | + StringUtil.notNullize(method.getName()), |
| 65 | + StringUtil.notNullize(getContextName(method, qualified)) |
| 66 | + ); |
| 67 | + } |
| 68 | + else if (element instanceof PsiClassOwner) { |
| 69 | + return LocalizeValue.empty(); |
| 70 | + } |
| 71 | + else if (element instanceof PsiFile file) { |
| 72 | + return LocalizeValue.of(file.getName()); |
| 73 | + } |
| 74 | + else if (element instanceof PsiField field) { |
| 75 | + return LocalizeValue.ofNullable(field.getName()); |
| 76 | + } |
| 77 | + else { |
| 78 | + return LocalizeValue.empty(); |
| 79 | + } |
71 | 80 | } |
72 | | - while (true) { |
73 | | - if (parent == null) { |
74 | | - return null; |
75 | | - } |
76 | | - String name = getNameForElement(parent, qualified); |
77 | | - if (name != null) { |
78 | | - return name; |
79 | | - } |
80 | | - if (parent instanceof PsiFile) { |
81 | | - return null; |
82 | | - } |
83 | | - parent = PsiTreeUtil.getStubOrPsiParent(parent); |
| 81 | + |
| 82 | + @RequiredReadAction |
| 83 | + public static String getContextName(PsiElement element, boolean qualified) { |
| 84 | + PsiElement parent = PsiTreeUtil.getStubOrPsiParentOfType(element, PsiMember.class); |
| 85 | + if (parent == null) { |
| 86 | + parent = element.getContainingFile(); |
| 87 | + } |
| 88 | + while (true) { |
| 89 | + if (parent == null) { |
| 90 | + return null; |
| 91 | + } |
| 92 | + LocalizeValue name = getNameForElement(parent, qualified); |
| 93 | + if (name.isNotEmpty()) { |
| 94 | + return name.getNullIfEmpty(); |
| 95 | + } |
| 96 | + if (parent instanceof PsiFile) { |
| 97 | + return null; |
| 98 | + } |
| 99 | + parent = PsiTreeUtil.getStubOrPsiParent(parent); |
| 100 | + } |
84 | 101 | } |
85 | | - } |
86 | 102 |
|
87 | | - public static String getFunctionalExpressionPresentation(PsiFunctionalExpression functionalExpression, boolean qualified) { |
88 | | - return "Functional expression in " + getContextName(functionalExpression, qualified); |
89 | | - } |
| 103 | + @RequiredReadAction |
| 104 | + public static LocalizeValue getFunctionalExpressionPresentation(PsiFunctionalExpression functionalExpression, boolean qualified) { |
| 105 | + return LocalizeValue.localizeTODO("Functional expression in " + getContextName(functionalExpression, qualified)); |
| 106 | + } |
90 | 107 | } |
0 commit comments