Skip to content

provide incomplete "for" completion for Twig based on variable scope #1786

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 26, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@
import com.intellij.codeInsight.completion.*;
import com.intellij.codeInsight.lookup.LookupElement;
import com.intellij.codeInsight.lookup.LookupElementBuilder;
import com.intellij.codeInsight.lookup.LookupElementPresentation;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Pair;
import com.intellij.patterns.*;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.patterns.PatternCondition;
import com.intellij.patterns.PlatformPatterns;
import com.intellij.patterns.StandardPatterns;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiWhiteSpace;
Expand All @@ -16,6 +20,8 @@
import com.jetbrains.php.lang.psi.elements.Field;
import com.jetbrains.php.lang.psi.elements.Method;
import com.jetbrains.php.lang.psi.elements.PhpClass;
import com.jetbrains.php.lang.psi.elements.PhpTypedElement;
import com.jetbrains.php.lang.psi.resolve.types.PhpType;
import com.jetbrains.twig.TwigTokenTypes;
import com.jetbrains.twig.elements.TwigElementTypes;
import fr.adrienbrault.idea.symfony2plugin.Symfony2Icons;
Expand Down Expand Up @@ -427,6 +433,13 @@ public void addCompletions(@NotNull CompletionParameters parameters, ProcessingC
TwigPattern.getCompletablePattern(),
new IncompleteIncludePrintBlockCompletionProvider()
);

// {% for => "for flash in app.flashes"
extend(
CompletionType.BASIC,
PlatformPatterns.psiElement(TwigTokenTypes.TAG_NAME),
new IncompleteForCompletionProvider()
);
}

private boolean isCompletionStartingMatch(@NotNull String fullText, @NotNull CompletionParameters completionParameters, int minLength) {
Expand Down Expand Up @@ -801,5 +814,81 @@ public boolean accepts(@NotNull String s, ProcessingContext processingContext) {
}
}
}

/**
* {% for => "for flash in app.flashes"
*/
private class IncompleteForCompletionProvider extends CompletionProvider<CompletionParameters> {
@Override
protected void addCompletions(@NotNull CompletionParameters completionParameters, @NotNull ProcessingContext processingContext, @NotNull CompletionResultSet resultSet) {
if(!Symfony2ProjectComponent.isEnabled(completionParameters.getPosition())) {
return;
}

resultSet.restartCompletionOnPrefixChange(StandardPatterns.string().longerThan(1).with(new PatternCondition<>("for startsWith") {
@Override
public boolean accepts(@NotNull String s, ProcessingContext processingContext) {
return "for".startsWith(s);
}
}));

if (!isCompletionStartingMatch("for", completionParameters, 2)) {
return;
}

Set<Map.Entry<String, PsiVariable>> entries = TwigTypeResolveUtil.collectScopeVariables(completionParameters.getPosition()).entrySet();

Map<String, Pair<String, LookupElement>> arrays = new HashMap<>();

Project project = completionParameters.getPosition().getProject();

for(Map.Entry<String, PsiVariable> entry: entries) {
Collection<PhpClass> classFromPhpTypeSet = PhpElementsUtil.getClassFromPhpTypeSet(project, entry.getValue().getTypes());
for (PhpClass phpClass : classFromPhpTypeSet) {
for(Method method: phpClass.getMethods()) {
if(!(!method.getModifier().isPublic() || method.getName().startsWith("set") || method.getName().startsWith("__"))) {
if (PhpType.isArray(PhpIndex.getInstance(project).completeType(project, method.getType(), new HashSet<>()))) {
String propertyShortcutMethodName = TwigTypeResolveUtil.getPropertyShortcutMethodName(method);
arrays.put(entry.getKey() + "." + propertyShortcutMethodName, Pair.create(propertyShortcutMethodName, new PhpTwigMethodLookupElement(method)));
}
}
}

for(Field field: phpClass.getFields()) {
if(field.getModifier().isPublic()) {
if (PhpType.isArray(PhpIndex.getInstance(project).completeType(project, field.getType(), new HashSet<>()))) {
arrays.put(entry.getKey() + "." + field.getName(), Pair.create(field.getName(), new PhpTwigMethodLookupElement(field)));
}
}
}
}
}

for (Map.Entry<String, Pair<String, LookupElement>> entry : arrays.entrySet()) {
String var = entry.getValue().getFirst();
String unpluralize = StringUtil.unpluralize(var);
if (unpluralize != null) {
var = unpluralize;
}

LookupElementPresentation lookupElementPresentation = new LookupElementPresentation();
entry.getValue().getSecond().renderElement(lookupElementPresentation);

Set<String> types = new HashSet<>();
PsiElement psiElement = entry.getValue().getSecond().getPsiElement();
if (psiElement instanceof PhpTypedElement) {
types.addAll(((PhpTypedElement) psiElement).getType().getTypes());
}

String content = String.format("for %s in %s", var, entry.getKey());
LookupElementBuilder lookupElement = LookupElementBuilder.create(content)
.withIcon(lookupElementPresentation.getIcon())
.withStrikeoutness(lookupElementPresentation.isStrikeout())
.withTypeText(StringUtils.stripStart(TwigTypeResolveUtil.getTypeDisplayName(project, types), "\\"));

resultSet.addElement(lookupElement);
}
}
}
}