|
13 | 13 | * See the License for the specific language governing permissions and |
14 | 14 | * limitations under the License. |
15 | 15 | */ |
16 | | - |
17 | | -/* |
18 | | - * @author max |
19 | | - */ |
20 | 16 | package com.intellij.java.impl.codeInsight.daemon.impl; |
21 | 17 |
|
22 | | -import java.text.MessageFormat; |
23 | | -import java.util.Arrays; |
24 | | -import java.util.LinkedHashSet; |
25 | | -import java.util.Set; |
26 | | - |
| 18 | +import com.intellij.java.language.impl.psi.presentation.java.ClassPresentationUtil; |
27 | 19 | import com.intellij.java.language.psi.PsiClass; |
| 20 | +import com.intellij.java.language.psi.PsiMethod; |
| 21 | +import consulo.annotation.access.RequiredReadAction; |
| 22 | +import consulo.application.util.HtmlBuilder; |
| 23 | +import consulo.application.util.HtmlChunk; |
28 | 24 | import consulo.language.psi.PsiElement; |
29 | 25 | import consulo.language.psi.PsiFile; |
30 | | -import com.intellij.java.language.psi.PsiMethod; |
31 | | -import com.intellij.java.language.impl.psi.presentation.java.ClassPresentationUtil; |
| 26 | +import consulo.localize.LocalizeValue; |
32 | 27 |
|
33 | | -public class GutterIconTooltipHelper |
34 | | -{ |
35 | | - private GutterIconTooltipHelper() |
36 | | - { |
37 | | - } |
| 28 | +import java.util.Arrays; |
| 29 | +import java.util.LinkedHashSet; |
| 30 | +import java.util.Set; |
| 31 | +import java.util.function.BiFunction; |
| 32 | + |
| 33 | +/** |
| 34 | + * @author max |
| 35 | + */ |
| 36 | +public class GutterIconTooltipHelper { |
| 37 | + private GutterIconTooltipHelper() { |
| 38 | + } |
38 | 39 |
|
39 | | - public static String composeText(PsiElement[] elements, String start, String pattern) |
40 | | - { |
41 | | - return composeText(Arrays.asList(elements), start, pattern); |
42 | | - } |
| 40 | + @RequiredReadAction |
| 41 | + public static HtmlChunk.Element composeText( |
| 42 | + PsiElement[] elements, |
| 43 | + String startHtml, |
| 44 | + BiFunction<String, String, LocalizeValue> pattern |
| 45 | + ) { |
| 46 | + return composeText(Arrays.asList(elements), startHtml, pattern); |
| 47 | + } |
43 | 48 |
|
44 | | - public static String composeText(Iterable<? extends PsiElement> elements, String start, String pattern) |
45 | | - { |
46 | | - return composeText(elements, start, pattern, ""); |
47 | | - } |
| 49 | + @RequiredReadAction |
| 50 | + public static HtmlChunk.Element composeText( |
| 51 | + Iterable<? extends PsiElement> elements, |
| 52 | + String startHtml, |
| 53 | + BiFunction<String, String, LocalizeValue> pattern |
| 54 | + ) { |
| 55 | + return composeText(elements, startHtml, pattern, ""); |
| 56 | + } |
48 | 57 |
|
49 | | - public static String composeText(Iterable<? extends PsiElement> elements, String start, String pattern, String postfix) |
50 | | - { |
51 | | - StringBuilder result = new StringBuilder(); |
52 | | - result.append("<html><body>"); |
53 | | - result.append(start); |
54 | | - Set<String> names = new LinkedHashSet<String>(); |
55 | | - for(PsiElement element : elements) |
56 | | - { |
57 | | - String descr = ""; |
58 | | - if(element instanceof PsiClass) |
59 | | - { |
60 | | - String className = ClassPresentationUtil.getNameForClass((PsiClass) element, true); |
61 | | - descr = MessageFormat.format(pattern, className); |
62 | | - } |
63 | | - else if(element instanceof PsiMethod) |
64 | | - { |
65 | | - String methodName = ((PsiMethod) element).getName(); |
66 | | - PsiClass aClass = ((PsiMethod) element).getContainingClass(); |
67 | | - String className = aClass == null ? "" : ClassPresentationUtil.getNameForClass(aClass, true); |
68 | | - descr = MessageFormat.format(pattern, methodName, className); |
69 | | - } |
70 | | - else if(element instanceof PsiFile) |
71 | | - { |
72 | | - descr = MessageFormat.format(pattern, ((PsiFile) element).getName()); |
73 | | - } |
74 | | - names.add(descr); |
75 | | - } |
| 58 | + @RequiredReadAction |
| 59 | + public static HtmlChunk.Element composeText( |
| 60 | + Iterable<? extends PsiElement> elements, |
| 61 | + String startHtml, |
| 62 | + BiFunction<String, String, LocalizeValue> pattern, |
| 63 | + String endHtml |
| 64 | + ) { |
| 65 | + Set<LocalizeValue> names = new LinkedHashSet<>(); |
| 66 | + for (PsiElement element : elements) { |
| 67 | + if (element instanceof PsiClass psiClass) { |
| 68 | + String className = ClassPresentationUtil.getNameForClass(psiClass, true); |
| 69 | + names.add(pattern.apply("", className)); |
| 70 | + } |
| 71 | + else if (element instanceof PsiMethod method) { |
| 72 | + String methodName = method.getName(); |
| 73 | + PsiClass aClass = method.getContainingClass(); |
| 74 | + String className = aClass == null ? "" : ClassPresentationUtil.getNameForClass(aClass, true); |
| 75 | + names.add(pattern.apply(methodName, className)); |
| 76 | + } |
| 77 | + else if (element instanceof PsiFile file) { |
| 78 | + names.add(pattern.apply("", file.getName())); |
| 79 | + } |
| 80 | + } |
76 | 81 |
|
77 | | - String sep = ""; |
78 | | - for(String name : names) |
79 | | - { |
80 | | - result.append(sep); |
81 | | - sep = "<br>"; |
82 | | - result.append(name); |
83 | | - } |
84 | | - result.append(postfix); |
85 | | - result.append("</body></html>"); |
86 | | - return result.toString(); |
87 | | - } |
| 82 | + HtmlBuilder result = new HtmlBuilder().appendRaw(startHtml); |
| 83 | + boolean first = true; |
| 84 | + for (LocalizeValue name : names) { |
| 85 | + if (!first) { |
| 86 | + result.append(HtmlChunk.br()); |
| 87 | + } |
| 88 | + first = false; |
| 89 | + result.append(name); |
| 90 | + } |
| 91 | + result.appendRaw(endHtml); |
| 92 | + return result.wrapWith(HtmlChunk.body()).wrapWith(HtmlChunk.html()); |
| 93 | + } |
88 | 94 | } |
0 commit comments