Skip to content

Commit de66044

Browse files
committed
add yaml "requirements" completion on pattern/path keys
1 parent acabcca commit de66044

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

src/fr/adrienbrault/idea/symfony2plugin/config/yaml/YamlCompletionContributor.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@
4040
import org.jetbrains.yaml.psi.YAMLKeyValue;
4141
import org.jetbrains.yaml.psi.YAMLSequence;
4242

43+
import java.util.regex.Matcher;
44+
import java.util.regex.Pattern;
45+
4346
/**
4447
* @author Daniel Espendiller <daniel@espendiller.net>
4548
*/
@@ -116,6 +119,7 @@ public void addCompletions(@NotNull CompletionParameters parameters,
116119

117120
extend(CompletionType.BASIC, YamlElementPatternHelper.getSuperParentArrayKey("services"), new YamlCompletionProvider(new String[] {"class", "public", "tags", "calls", "arguments", "scope"}));
118121
extend(CompletionType.BASIC, YamlElementPatternHelper.getWithFirstRootKey(), new RouteKeyNameYamlCompletionProvider(new String[] {"pattern", "defaults", "path", "requirements", "methods", "condition", "resource", "prefix"}));
122+
extend(CompletionType.BASIC, YamlElementPatternHelper.getParentKeyName("requirements"), new RouteRequirementsCompletion());
119123

120124
extend(CompletionType.BASIC, StandardPatterns.and(
121125
YamlElementPatternHelper.getInsideKeyValue("tags"),
@@ -405,5 +409,34 @@ public void addCompletions(@NotNull CompletionParameters parameters, ProcessingC
405409
super.addCompletions(parameters, context, resultSet);
406410
}
407411
}
412+
413+
/**
414+
* "requirements" on "path/pattern: /hello/{name}"
415+
*/
416+
private class RouteRequirementsCompletion extends CompletionProvider<CompletionParameters> {
417+
@Override
418+
protected void addCompletions(@NotNull CompletionParameters completionParameters, ProcessingContext processingContext, @NotNull CompletionResultSet completionResultSet) {
419+
YAMLKeyValue yamlKeyValue = PsiTreeUtil.getParentOfType(completionParameters.getOriginalPosition(), YAMLKeyValue.class);
420+
if(yamlKeyValue != null) {
421+
PsiElement compoundValue = yamlKeyValue.getParent();
422+
if(compoundValue instanceof YAMLCompoundValue) {
423+
424+
// path and pattern are valid
425+
String pattern = YamlHelper.getYamlKeyValueAsString((YAMLCompoundValue) compoundValue, "path", false);
426+
if(pattern == null) {
427+
pattern = YamlHelper.getYamlKeyValueAsString((YAMLCompoundValue) compoundValue, "pattern", false);
428+
}
429+
430+
if(pattern != null) {
431+
Matcher matcher = Pattern.compile("\\{(\\w+)}").matcher(pattern);
432+
while(matcher.find()){
433+
completionResultSet.addElement(LookupElementBuilder.create(matcher.group(1)));
434+
}
435+
}
436+
437+
}
438+
}
439+
}
440+
}
408441
}
409442

src/fr/adrienbrault/idea/symfony2plugin/config/yaml/YamlElementPatternHelper.java

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,66 @@ public static ElementPattern<PsiElement> getOrmParentLookup(String keyName) {
115115
);
116116
}
117117

118+
/**
119+
* provides auto complete on
120+
*
121+
* keyName:
122+
* refer|
123+
* refer|: xxx
124+
* refer|
125+
*
126+
* @param keyName key name
127+
*/
128+
public static ElementPattern<PsiElement> getParentKeyName(String keyName) {
129+
return PlatformPatterns.or(
130+
// match
131+
//
132+
// keyName:
133+
// refer|: xxx
134+
PlatformPatterns
135+
.psiElement(YAMLTokenTypes.SCALAR_KEY)
136+
.withParent(PlatformPatterns
137+
.psiElement(YAMLKeyValue.class)
138+
.withParent(PlatformPatterns
139+
.psiElement(YAMLElementTypes.COMPOUND_VALUE)
140+
.withParent(PlatformPatterns
141+
.psiElement(YAMLKeyValue.class)
142+
.withName(
143+
PlatformPatterns.string().equalTo(keyName)
144+
)
145+
)
146+
)
147+
)
148+
.withLanguage(YAMLLanguage.INSTANCE),
149+
150+
// match
151+
//
152+
// keyName:
153+
// xxx: xxx
154+
// refer|
155+
PlatformPatterns
156+
.psiElement(YAMLTokenTypes.TEXT)
157+
.withParent(PlatformPatterns
158+
.psiElement(YAMLElementTypes.COMPOUND_VALUE)
159+
.withParent(PlatformPatterns
160+
.psiElement(YAMLKeyValue.class)
161+
.withName(
162+
PlatformPatterns.string().equalTo(keyName)
163+
)
164+
)
165+
)
166+
.withLanguage(YAMLLanguage.INSTANCE),
167+
168+
// match
169+
//
170+
// keyName:
171+
// refer|
172+
// xxx: xxx
173+
getKeyPattern(keyName)
174+
.withLanguage(YAMLLanguage.INSTANCE)
175+
);
176+
}
177+
118178
/**
119179
* provides auto complete on
120180
*

0 commit comments

Comments
 (0)