|
| 1 | +package fr.adrienbrault.idea.symfony2plugin.config.yaml; |
| 2 | + |
| 3 | +import com.intellij.psi.PsiElement; |
| 4 | +import com.intellij.psi.PsiElementResolveResult; |
| 5 | +import com.intellij.psi.PsiPolyVariantReferenceBase; |
| 6 | +import com.intellij.psi.ResolveResult; |
| 7 | +import com.intellij.util.IncorrectOperationException; |
| 8 | +import fr.adrienbrault.idea.symfony2plugin.dic.container.util.ServiceContainerUtil; |
| 9 | +import org.apache.commons.lang.StringUtils; |
| 10 | +import org.jetbrains.annotations.NotNull; |
| 11 | +import org.jetbrains.yaml.psi.YAMLScalar; |
| 12 | + |
| 13 | +public class ConstantYamlReference extends PsiPolyVariantReferenceBase<YAMLScalar> { |
| 14 | + |
| 15 | + public ConstantYamlReference(@NotNull YAMLScalar element) { |
| 16 | + super(element); |
| 17 | + } |
| 18 | + |
| 19 | + @NotNull |
| 20 | + @Override |
| 21 | + public ResolveResult[] multiResolve(boolean incompleteCode) { |
| 22 | + var constantName = getElement().getTextValue(); |
| 23 | + if (StringUtils.isBlank(constantName)) { |
| 24 | + return ResolveResult.EMPTY_ARRAY; |
| 25 | + } |
| 26 | + |
| 27 | + return PsiElementResolveResult.createResults( |
| 28 | + ServiceContainerUtil.getTargetsForConstant(getElement().getProject(), constantName) |
| 29 | + ); |
| 30 | + } |
| 31 | + |
| 32 | + @Override |
| 33 | + public PsiElement handleElementRename(@NotNull String newElementName) throws IncorrectOperationException { |
| 34 | + var constantName = getValue(); |
| 35 | + if (isClassConst(constantName)) { |
| 36 | + newElementName = getClassName(constantName) + "::" + newElementName; |
| 37 | + } |
| 38 | + |
| 39 | + return super.handleElementRename(newElementName); |
| 40 | + } |
| 41 | + |
| 42 | + private boolean isClassConst(@NotNull String value) { |
| 43 | + return value.contains("::"); |
| 44 | + } |
| 45 | + |
| 46 | + @NotNull |
| 47 | + private String getClassName(@NotNull String value) { |
| 48 | + return value.substring(0, value.indexOf("::")); |
| 49 | + } |
| 50 | +} |
0 commit comments