|
| 1 | +/* |
| 2 | + * Copyright 2013-2026 consulo.io |
| 3 | + * |
| 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 |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 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. |
| 15 | + */ |
| 16 | + |
| 17 | +package consulo.spring.spel.language.impl.highlight; |
| 18 | + |
| 19 | +import com.intellij.java.language.psi.PsiField; |
| 20 | +import com.intellij.java.language.psi.PsiMethod; |
| 21 | +import consulo.annotation.access.RequiredReadAction; |
| 22 | +import consulo.codeEditor.DefaultLanguageHighlighterColors; |
| 23 | +import consulo.colorScheme.TextAttributesKey; |
| 24 | +import consulo.language.ast.ASTNode; |
| 25 | +import consulo.language.editor.annotation.AnnotationHolder; |
| 26 | +import consulo.language.editor.annotation.Annotator; |
| 27 | +import consulo.language.editor.annotation.HighlightSeverity; |
| 28 | +import consulo.language.psi.PsiElement; |
| 29 | +import consulo.spring.spel.language.SpELElementTypes; |
| 30 | +import consulo.spring.spel.language.SpELTokenTypes; |
| 31 | +import consulo.spring.spel.language.impl.psi.*; |
| 32 | + |
| 33 | +public class SpELSemanticHighlighter implements Annotator { |
| 34 | + private static final TextAttributesKey PROPERTY_ACCESS = TextAttributesKey.createTextAttributesKey( |
| 35 | + "SPEL_PROPERTY_ACCESS", DefaultLanguageHighlighterColors.INSTANCE_FIELD |
| 36 | + ); |
| 37 | + private static final TextAttributesKey METHOD_CALL = TextAttributesKey.createTextAttributesKey( |
| 38 | + "SPEL_METHOD_CALL", DefaultLanguageHighlighterColors.FUNCTION_CALL |
| 39 | + ); |
| 40 | + private static final TextAttributesKey BEAN_REFERENCE = TextAttributesKey.createTextAttributesKey( |
| 41 | + "SPEL_BEAN_REFERENCE", DefaultLanguageHighlighterColors.INSTANCE_FIELD |
| 42 | + ); |
| 43 | + private static final TextAttributesKey VARIABLE_REFERENCE = TextAttributesKey.createTextAttributesKey( |
| 44 | + "SPEL_VARIABLE_REFERENCE", DefaultLanguageHighlighterColors.LOCAL_VARIABLE |
| 45 | + ); |
| 46 | + private static final TextAttributesKey TYPE_REFERENCE = TextAttributesKey.createTextAttributesKey( |
| 47 | + "SPEL_TYPE_REFERENCE", DefaultLanguageHighlighterColors.CLASS_NAME |
| 48 | + ); |
| 49 | + private static final TextAttributesKey PLACEHOLDER_KEY_ATTR = TextAttributesKey.createTextAttributesKey( |
| 50 | + "SPEL_PLACEHOLDER_KEY", DefaultLanguageHighlighterColors.INSTANCE_FIELD |
| 51 | + ); |
| 52 | + private static final TextAttributesKey STATIC_METHOD_CALL = TextAttributesKey.createTextAttributesKey( |
| 53 | + "SPEL_STATIC_METHOD_CALL", DefaultLanguageHighlighterColors.STATIC_METHOD |
| 54 | + ); |
| 55 | + private static final TextAttributesKey STATIC_FIELD_ACCESS = TextAttributesKey.createTextAttributesKey( |
| 56 | + "SPEL_STATIC_FIELD_ACCESS", DefaultLanguageHighlighterColors.STATIC_FIELD |
| 57 | + ); |
| 58 | + |
| 59 | + @Override |
| 60 | + @RequiredReadAction |
| 61 | + public void annotate(PsiElement element, AnnotationHolder holder) { |
| 62 | + if (element instanceof SpELReferenceExpressionImpl refExpr) { |
| 63 | + highlightReferenceExpression(refExpr, holder); |
| 64 | + } |
| 65 | + else if (element instanceof SpELMethodCallExpressionImpl methodCall) { |
| 66 | + highlightMethodCall(methodCall, holder); |
| 67 | + } |
| 68 | + else if (element instanceof SpELBeanReferenceImpl beanRef) { |
| 69 | + highlightBeanReference(beanRef, holder); |
| 70 | + } |
| 71 | + else if (element instanceof SpELVariableReferenceImpl varRef) { |
| 72 | + highlightVariableReference(varRef, holder); |
| 73 | + } |
| 74 | + else if (element instanceof SpELTypeReferenceImpl typeRef) { |
| 75 | + highlightTypeReference(typeRef, holder); |
| 76 | + } |
| 77 | + else if (element instanceof SpELPlaceholderKeyImpl placeholderKey) { |
| 78 | + highlightPlaceholderKey(placeholderKey, holder); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + @RequiredReadAction |
| 83 | + private void highlightReferenceExpression(SpELReferenceExpressionImpl refExpr, AnnotationHolder holder) { |
| 84 | + ASTNode idNode = findLastIdentifier(refExpr); |
| 85 | + if (idNode == null) { |
| 86 | + return; |
| 87 | + } |
| 88 | + |
| 89 | + PsiElement resolved = refExpr.resolve(); |
| 90 | + if (resolved instanceof PsiMethod method) { |
| 91 | + TextAttributesKey key = method.hasModifierProperty("static") ? STATIC_METHOD_CALL : METHOD_CALL; |
| 92 | + holder.newSilentAnnotation(HighlightSeverity.INFORMATION) |
| 93 | + .range(idNode) |
| 94 | + .textAttributes(key) |
| 95 | + .create(); |
| 96 | + } |
| 97 | + else if (resolved instanceof PsiField field) { |
| 98 | + TextAttributesKey key = field.hasModifierProperty("static") ? STATIC_FIELD_ACCESS : PROPERTY_ACCESS; |
| 99 | + holder.newSilentAnnotation(HighlightSeverity.INFORMATION) |
| 100 | + .range(idNode) |
| 101 | + .textAttributes(key) |
| 102 | + .create(); |
| 103 | + } |
| 104 | + else if (refExpr.getQualifier() == null) { |
| 105 | + // unqualified reference = bean name, highlight like instance field |
| 106 | + holder.newSilentAnnotation(HighlightSeverity.INFORMATION) |
| 107 | + .range(idNode) |
| 108 | + .textAttributes(BEAN_REFERENCE) |
| 109 | + .create(); |
| 110 | + } |
| 111 | + else { |
| 112 | + // qualified but unresolved - still highlight as property |
| 113 | + holder.newSilentAnnotation(HighlightSeverity.INFORMATION) |
| 114 | + .range(idNode) |
| 115 | + .textAttributes(PROPERTY_ACCESS) |
| 116 | + .create(); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + @RequiredReadAction |
| 121 | + private void highlightMethodCall(SpELMethodCallExpressionImpl methodCall, AnnotationHolder holder) { |
| 122 | + // the method name is inside the REFERENCE_EXPRESSION child - handled there |
| 123 | + } |
| 124 | + |
| 125 | + @RequiredReadAction |
| 126 | + private void highlightBeanReference(SpELBeanReferenceImpl beanRef, AnnotationHolder holder) { |
| 127 | + ASTNode idNode = beanRef.getNode().findChildByType(SpELTokenTypes.IDENTIFIER); |
| 128 | + if (idNode != null) { |
| 129 | + holder.newSilentAnnotation(HighlightSeverity.INFORMATION) |
| 130 | + .range(idNode) |
| 131 | + .textAttributes(BEAN_REFERENCE) |
| 132 | + .create(); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + @RequiredReadAction |
| 137 | + private void highlightVariableReference(SpELVariableReferenceImpl varRef, AnnotationHolder holder) { |
| 138 | + ASTNode idNode = varRef.getNode().findChildByType(SpELTokenTypes.IDENTIFIER); |
| 139 | + if (idNode != null) { |
| 140 | + holder.newSilentAnnotation(HighlightSeverity.INFORMATION) |
| 141 | + .range(idNode) |
| 142 | + .textAttributes(VARIABLE_REFERENCE) |
| 143 | + .create(); |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + @RequiredReadAction |
| 148 | + private void highlightTypeReference(SpELTypeReferenceImpl typeRef, AnnotationHolder holder) { |
| 149 | + ASTNode qualifiedName = typeRef.getNode().findChildByType(SpELElementTypes.QUALIFIED_NAME); |
| 150 | + if (qualifiedName != null) { |
| 151 | + // highlight each identifier segment in the qualified name as class name |
| 152 | + for (ASTNode child = qualifiedName.getFirstChildNode(); child != null; child = child.getTreeNext()) { |
| 153 | + if (child.getElementType() == SpELTokenTypes.IDENTIFIER) { |
| 154 | + holder.newSilentAnnotation(HighlightSeverity.INFORMATION) |
| 155 | + .range(child) |
| 156 | + .textAttributes(TYPE_REFERENCE) |
| 157 | + .create(); |
| 158 | + } |
| 159 | + } |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + @RequiredReadAction |
| 164 | + private void highlightPlaceholderKey(SpELPlaceholderKeyImpl placeholderKey, AnnotationHolder holder) { |
| 165 | + holder.newSilentAnnotation(HighlightSeverity.INFORMATION) |
| 166 | + .range(placeholderKey) |
| 167 | + .textAttributes(PLACEHOLDER_KEY_ATTR) |
| 168 | + .create(); |
| 169 | + } |
| 170 | + |
| 171 | + private ASTNode findLastIdentifier(SpELReferenceExpressionImpl refExpr) { |
| 172 | + ASTNode lastId = null; |
| 173 | + for (ASTNode child = refExpr.getNode().getFirstChildNode(); child != null; child = child.getTreeNext()) { |
| 174 | + if (child.getElementType() == SpELTokenTypes.IDENTIFIER) { |
| 175 | + lastId = child; |
| 176 | + } |
| 177 | + } |
| 178 | + return lastId; |
| 179 | + } |
| 180 | +} |
0 commit comments