Skip to content

Commit c838cdc

Browse files
authored
Merge pull request Haehnchen#1202 from cedricziel/yaml-const
Add YAML const GoTo Target for Symfony 3.2+ style constants
2 parents ba7904f + fba2ded commit c838cdc

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

src/main/java/fr/adrienbrault/idea/symfony2plugin/config/yaml/YamlGoToDeclarationHandler.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ public PsiElement[] getGotoDeclarationTargets(PsiElement psiElement, int i, Edit
6969
targets.addAll(constantGoto(psiElement, psiText));
7070
}
7171

72+
// mind the whitespace
73+
if(hasNewConst(psiElement)) {
74+
targets.addAll(newConstantGoto(psiElement, psiText));
75+
}
76+
7277
if(psiText.contains("\\")) {
7378
targets.addAll(classGoToDeclaration(psiElement, psiText)) ;
7479
}
@@ -162,6 +167,22 @@ public PsiElement[] getGotoDeclarationTargets(PsiElement psiElement, int i, Edit
162167
return targets.toArray(new PsiElement[targets.size()]);
163168
}
164169

170+
private boolean hasNewConst(@NotNull PsiElement psiElement) {
171+
PsiElement prevSibling = psiElement.getPrevSibling();
172+
while (prevSibling != null) {
173+
IElementType elementType = prevSibling.getNode().getElementType();
174+
if (elementType == YAMLTokenTypes.TEXT || elementType == YAMLTokenTypes.SCALAR_DSTRING || elementType == YAMLTokenTypes.SCALAR_STRING || elementType == YAMLTokenTypes.TAG) {
175+
String psiText = PsiElementUtils.getText(prevSibling);
176+
177+
return psiText.equals("!php/const");
178+
}
179+
180+
prevSibling = prevSibling.getPrevSibling();
181+
}
182+
183+
return false;
184+
}
185+
165186
@NotNull
166187
private Collection<PsiElement> classGoToDeclaration(@NotNull PsiElement psiElement, @NotNull String className) {
167188

@@ -234,6 +255,14 @@ private Collection<PsiElement> constantGoto(@NotNull PsiElement psiElement, @Not
234255
return ServiceContainerUtil.getTargetsForConstant(psiElement.getProject(), constantName);
235256
}
236257

258+
@NotNull
259+
private Collection<PsiElement> newConstantGoto(@NotNull PsiElement psiElement, @NotNull String constantName) {
260+
if(StringUtils.isBlank(constantName)) {
261+
return Collections.emptyList();
262+
}
263+
264+
return ServiceContainerUtil.getTargetsForConstant(psiElement.getProject(), constantName);
265+
}
237266

238267
@NotNull
239268
private Collection<PsiElement> visitConfigKey(@NotNull PsiElement psiElement) {

src/test/java/fr/adrienbrault/idea/symfony2plugin/tests/config/yaml/YamlGoToDeclarationHandlerTest.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
import org.jetbrains.annotations.NotNull;
1111
import org.jetbrains.yaml.YAMLFileType;
1212

13-
import java.io.File;
14-
1513
/**
1614
* @author Daniel Espendiller <daniel@espendiller.net>
1715
*
@@ -94,6 +92,15 @@ public void testPhpConstantNavigation() {
9492
assertNavigationMatch(YAMLFileType.YML, "bar: '!php/const:\\YAML_<caret>FOO_BAR'");
9593
}
9694

95+
public void testPhpConstantNavigation34() {
96+
assertNavigationMatch(YAMLFileType.YML, "bar: !php/const \\YAML_<caret>FOO_BAR");
97+
assertNavigationMatch(YAMLFileType.YML, "bar: !php/const YAML_<caret>FOO_BAR");
98+
assertNavigationMatch(YAMLFileType.YML, "bar: !php/const Yaml\\Foo\\Bar::YAML_FOO_BAR<caret>_CLASS");
99+
assertNavigationMatch(YAMLFileType.YML, "bar: !php/const Yaml\\Foo\\Bar::::YAML_FOO_BAR<caret>_CLASS");
100+
assertNavigationMatch(YAMLFileType.YML, "bar: !php/const Yaml\\Foo\\Bar:YAML_FOO_BAR<caret>_CLASS");
101+
assertNavigationMatch(YAMLFileType.YML, "bar: !php/const \\Yaml\\Foo\\Bar:YAML_FOO_BAR<caret>_CLASS");
102+
}
103+
97104
public void testParameter() {
98105
assertNavigationMatch(YAMLFileType.YML, "bar: %foo_p<caret>arameter%");
99106
}

0 commit comments

Comments
 (0)