|
3 | 3 |
|
4 | 4 | import com.intellij.java.language.codeInsight.AnnotationTargetUtil; |
5 | 5 | import com.intellij.java.language.psi.*; |
| 6 | +import consulo.annotation.access.RequiredReadAction; |
6 | 7 | import consulo.application.util.CachedValueProvider; |
7 | 8 | import consulo.content.scope.SearchScope; |
8 | 9 | import consulo.language.psi.PsiElement; |
|
15 | 16 |
|
16 | 17 | import jakarta.annotation.Nonnull; |
17 | 18 | import jakarta.annotation.Nullable; |
| 19 | + |
18 | 20 | import java.util.Arrays; |
19 | 21 | import java.util.Objects; |
20 | 22 |
|
21 | 23 | public class LightRecordField extends LightField implements LightRecordMember { |
22 | | - private final |
23 | | - @Nonnull |
24 | | - PsiRecordComponent myRecordComponent; |
25 | | - |
26 | | - public LightRecordField(@Nonnull PsiManager manager, |
27 | | - @Nonnull PsiField field, |
28 | | - @Nonnull PsiClass containingClass, |
29 | | - @Nonnull PsiRecordComponent component) { |
30 | | - super(manager, field, containingClass); |
31 | | - myRecordComponent = component; |
32 | | - } |
33 | | - |
34 | | - @Override |
35 | | - @Nonnull |
36 | | - public PsiRecordComponent getRecordComponent() { |
37 | | - return myRecordComponent; |
38 | | - } |
39 | | - |
40 | | - @Override |
41 | | - public int getTextOffset() { |
42 | | - return myRecordComponent.getTextOffset(); |
43 | | - } |
44 | | - |
45 | | - @Nonnull |
46 | | - @Override |
47 | | - public PsiElement getNavigationElement() { |
48 | | - return myRecordComponent.getNavigationElement(); |
49 | | - } |
50 | | - |
51 | | - @Override |
52 | | - public boolean isWritable() { |
53 | | - return true; |
54 | | - } |
55 | | - |
56 | | - @Override |
57 | | - public PsiFile getContainingFile() { |
58 | | - PsiClass containingClass = getContainingClass(); |
59 | | - if (containingClass == null) { |
60 | | - return null; |
61 | | - } |
62 | | - return containingClass.getContainingFile(); |
63 | | - } |
64 | | - |
65 | | - @Override |
66 | | - public |
67 | | - @Nonnull |
68 | | - PsiType getType() { |
69 | | - if (DumbService.isDumb(myRecordComponent.getProject())) { |
70 | | - return myRecordComponent.getType(); |
71 | | - } |
72 | | - return LanguageCachedValueUtil.getCachedValue(this, () -> { |
73 | | - PsiType type = myRecordComponent.getType() |
74 | | - .annotate(() -> Arrays.stream(myRecordComponent.getAnnotations()) |
75 | | - .filter(LightRecordField::hasApplicableAnnotationTarget) |
76 | | - .toArray(PsiAnnotation[]::new) |
77 | | - ); |
78 | | - return CachedValueProvider.Result.create(type, this); |
79 | | - }); |
80 | | - } |
81 | | - |
82 | | - @Override |
83 | | - @Nonnull |
84 | | - public PsiAnnotation[] getAnnotations() { |
85 | | - return getType().getAnnotations(); |
86 | | - } |
87 | | - |
88 | | - @Override |
89 | | - public boolean hasAnnotation(@Nonnull String fqn) { |
90 | | - PsiType type = getType(); |
91 | | - return type.hasAnnotation(fqn); |
92 | | - } |
93 | | - |
94 | | - @Override |
95 | | - @Nullable |
96 | | - public PsiAnnotation getAnnotation(@Nonnull String fqn) { |
97 | | - return getType().findAnnotation(fqn); |
98 | | - } |
| 24 | + private final |
| 25 | + @Nonnull |
| 26 | + PsiRecordComponent myRecordComponent; |
| 27 | + |
| 28 | + public LightRecordField( |
| 29 | + @Nonnull PsiManager manager, |
| 30 | + @Nonnull PsiField field, |
| 31 | + @Nonnull PsiClass containingClass, |
| 32 | + @Nonnull PsiRecordComponent component |
| 33 | + ) { |
| 34 | + super(manager, field, containingClass); |
| 35 | + myRecordComponent = component; |
| 36 | + } |
| 37 | + |
| 38 | + @Override |
| 39 | + @Nonnull |
| 40 | + public PsiRecordComponent getRecordComponent() { |
| 41 | + return myRecordComponent; |
| 42 | + } |
| 43 | + |
| 44 | + @Override |
| 45 | + public int getTextOffset() { |
| 46 | + return myRecordComponent.getTextOffset(); |
| 47 | + } |
| 48 | + |
| 49 | + @Nonnull |
| 50 | + @Override |
| 51 | + public PsiElement getNavigationElement() { |
| 52 | + return myRecordComponent.getNavigationElement(); |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public boolean isWritable() { |
| 57 | + return true; |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public PsiFile getContainingFile() { |
| 62 | + PsiClass containingClass = getContainingClass(); |
| 63 | + if (containingClass == null) { |
| 64 | + return null; |
| 65 | + } |
| 66 | + return containingClass.getContainingFile(); |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public |
| 71 | + @Nonnull |
| 72 | + PsiType getType() { |
| 73 | + if (DumbService.isDumb(myRecordComponent.getProject())) { |
| 74 | + return myRecordComponent.getType(); |
| 75 | + } |
| 76 | + return LanguageCachedValueUtil.getCachedValue(this, () -> { |
| 77 | + PsiType type = myRecordComponent.getType() |
| 78 | + .annotate(() -> Arrays.stream(myRecordComponent.getAnnotations()) |
| 79 | + .filter(LightRecordField::hasApplicableAnnotationTarget) |
| 80 | + .toArray(PsiAnnotation[]::new) |
| 81 | + ); |
| 82 | + return CachedValueProvider.Result.create(type, this); |
| 83 | + }); |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + @Nonnull |
| 88 | + public PsiAnnotation[] getAnnotations() { |
| 89 | + return getType().getAnnotations(); |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public boolean hasAnnotation(@Nonnull String fqn) { |
| 94 | + PsiType type = getType(); |
| 95 | + return type.hasAnnotation(fqn); |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + @Nullable |
| 100 | + public PsiAnnotation getAnnotation(@Nonnull String fqn) { |
| 101 | + return getType().findAnnotation(fqn); |
| 102 | + } |
99 | 103 |
|
100 | 104 | // @Override |
101 | | -// public Icon getElementIcon(final int flags) |
102 | | -// { |
| 105 | +// public Icon getElementIcon(final int flags) { |
103 | 106 | // final RowIcon baseIcon = |
104 | | -// IconManager.getInstance().createLayeredIcon(this, PlatformIcons.FIELD_ICON, ElementPresentationUtil.getFlags(this, false)); |
105 | | -// if(BitUtil.isSet(flags, ICON_FLAG_VISIBILITY)) |
106 | | -// { |
| 107 | +// IconManager.getInstance().createLayeredIcon(this, PlatformIconGroup.nodesField(), ElementPresentationUtil.getFlags(this, false)); |
| 108 | +// if (BitUtil.isSet(flags, ICON_FLAG_VISIBILITY)) { |
107 | 109 | // VisibilityIcons.setVisibilityIcon(PsiUtil.ACCESS_LEVEL_PRIVATE, baseIcon); |
108 | 110 | // } |
109 | 111 | // return baseIcon; |
110 | 112 | // } |
111 | 113 |
|
112 | | - @Override |
113 | | - public PsiElement getContext() { |
114 | | - return getContainingClass(); |
115 | | - } |
116 | | - |
117 | | - @Override |
118 | | - public |
119 | | - @Nonnull |
120 | | - SearchScope getUseScope() { |
121 | | - PsiClass aClass = Objects.requireNonNull(getContainingClass()); |
122 | | - PsiClass containingClass = aClass.getContainingClass(); |
123 | | - while (containingClass != null) { |
124 | | - aClass = containingClass; |
125 | | - containingClass = containingClass.getContainingClass(); |
126 | | - } |
127 | | - return new LocalSearchScope(aClass); |
128 | | - } |
129 | | - |
130 | | - private static boolean hasApplicableAnnotationTarget(PsiAnnotation annotation) { |
131 | | - return AnnotationTargetUtil.findAnnotationTarget(annotation, PsiAnnotation.TargetType.TYPE_USE, PsiAnnotation.TargetType.FIELD) != null; |
132 | | - } |
133 | | - |
134 | | - @Override |
135 | | - public void normalizeDeclaration() throws IncorrectOperationException { |
136 | | - // no-op |
137 | | - } |
138 | | - |
139 | | - @Override |
140 | | - public boolean equals(Object o) { |
141 | | - if (this == o) { |
142 | | - return true; |
143 | | - } |
144 | | - return o instanceof LightRecordField && |
145 | | - myRecordComponent.equals(((LightRecordField) o).myRecordComponent); |
146 | | - } |
147 | | - |
148 | | - @Override |
149 | | - public int hashCode() { |
150 | | - return Objects.hash(myRecordComponent); |
151 | | - } |
| 114 | + @Override |
| 115 | + public PsiElement getContext() { |
| 116 | + return getContainingClass(); |
| 117 | + } |
| 118 | + |
| 119 | + @Override |
| 120 | + public |
| 121 | + @Nonnull |
| 122 | + SearchScope getUseScope() { |
| 123 | + PsiClass aClass = Objects.requireNonNull(getContainingClass()); |
| 124 | + PsiClass containingClass = aClass.getContainingClass(); |
| 125 | + while (containingClass != null) { |
| 126 | + aClass = containingClass; |
| 127 | + containingClass = containingClass.getContainingClass(); |
| 128 | + } |
| 129 | + return new LocalSearchScope(aClass); |
| 130 | + } |
| 131 | + |
| 132 | + @RequiredReadAction |
| 133 | + private static boolean hasApplicableAnnotationTarget(PsiAnnotation annotation) { |
| 134 | + return AnnotationTargetUtil.findAnnotationTarget( |
| 135 | + annotation, |
| 136 | + PsiAnnotation.TargetType.TYPE_USE, |
| 137 | + PsiAnnotation.TargetType.FIELD |
| 138 | + ) != null; |
| 139 | + } |
| 140 | + |
| 141 | + @Override |
| 142 | + public void normalizeDeclaration() throws IncorrectOperationException { |
| 143 | + // no-op |
| 144 | + } |
| 145 | + |
| 146 | + @Override |
| 147 | + public boolean equals(Object o) { |
| 148 | + if (this == o) { |
| 149 | + return true; |
| 150 | + } |
| 151 | + return o instanceof LightRecordField && |
| 152 | + myRecordComponent.equals(((LightRecordField) o).myRecordComponent); |
| 153 | + } |
| 154 | + |
| 155 | + @Override |
| 156 | + public int hashCode() { |
| 157 | + return Objects.hash(myRecordComponent); |
| 158 | + } |
152 | 159 |
|
153 | 160 | } |
0 commit comments