Skip to content

Commit 060f3bb

Browse files
authored
Merge pull request #2294 from Haehnchen/feature/querybuilder-dot-duplicate
add workaround to remove duplicate element for querybuilder after dot…
2 parents 842f17b + 0b3c58f commit 060f3bb

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

src/main/java/fr/adrienbrault/idea/symfony2plugin/doctrine/querybuilder/QueryBuilderCompletionContributor.java

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package fr.adrienbrault.idea.symfony2plugin.doctrine.querybuilder;
22

33
import com.intellij.codeInsight.completion.*;
4+
import com.intellij.codeInsight.lookup.LookupElement;
45
import com.intellij.codeInsight.lookup.LookupElementBuilder;
6+
import com.intellij.openapi.editor.Editor;
57
import com.intellij.openapi.project.Project;
6-
import com.intellij.patterns.PatternCondition;
8+
import com.intellij.openapi.util.TextRange;
79
import com.intellij.patterns.PlatformPatterns;
8-
import com.intellij.patterns.StandardPatterns;
910
import com.intellij.psi.PsiElement;
1011
import com.intellij.psi.util.PsiTreeUtil;
1112
import com.intellij.util.ProcessingContext;
@@ -534,6 +535,8 @@ private void buildLookupElements(CompletionResultSet completionResultSet, QueryB
534535
lookup = lookup.withBoldness(true);
535536
}
536537

538+
lookup = lookup.withInsertHandler(new DottedClearWorkoutInsertHandler());
539+
537540
completionResultSet.addElement(lookup);
538541
}
539542
}
@@ -548,4 +551,35 @@ public static QueryBuilderMethodReferenceParser getQueryBuilderParser(MethodRefe
548551
addAll(processor.getQueryBuilderMethodReferences());
549552
}});
550553
}
554+
555+
/**
556+
* Workaround to fix duplicated elements after a dot sign
557+
*
558+
* "https://blog.jetbrains.com/phpstorm/2023/09/phpstorm-public-roadmap-whats-coming-in-2023-3/#doctrine-query-language-support-inside-querybuilder"
559+
*
560+
* Some methods provide language injection from PhpStorm itself, and therefore have supported for dotted prefix ".":
561+
* - $qb->select('fooBar.id', 'fooBar.');
562+
* - $qb->select('fooBar.fooBar.id', 'fooBar.id');
563+
*/
564+
private static class DottedClearWorkoutInsertHandler implements InsertHandler<LookupElement> {
565+
@Override
566+
public void handleInsert(@NotNull InsertionContext context, @NotNull LookupElement item) {
567+
Editor editor = context.getEditor();
568+
String insertedText = editor.getDocument().getText(new TextRange(context.getStartOffset(), context.getTailOffset()));
569+
String lookupString = item.getLookupString();
570+
571+
int substring = lookupString.indexOf(".");
572+
if (substring > 0) {
573+
String beforeDot = lookupString.substring(0, substring);
574+
TextRange rangeBeforeInserted = new TextRange(context.getStartOffset() - beforeDot.length() - 1, context.getStartOffset());
575+
String textBeforeDot = editor.getDocument().getText(rangeBeforeInserted);
576+
577+
// if final inserted lookup string result in duplication remove it
578+
if (insertedText.startsWith(textBeforeDot)) {
579+
context.getDocument().deleteString(rangeBeforeInserted.getStartOffset(), rangeBeforeInserted.getEndOffset());
580+
context.commitDocument();
581+
}
582+
}
583+
}
584+
}
551585
}

src/main/java/fr/adrienbrault/idea/symfony2plugin/doctrine/querybuilder/util/MatcherUtil.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@
1111
*/
1212
public class MatcherUtil {
1313

14-
private static MethodMatcher.CallToSignature[] SELECT_FIELDS = new MethodMatcher.CallToSignature[] {
14+
private static final MethodMatcher.CallToSignature[] SELECT_FIELDS = new MethodMatcher.CallToSignature[] {
1515
new MethodMatcher.CallToSignature("\\Doctrine\\ORM\\QueryBuilder", "orderBy"),
1616
new MethodMatcher.CallToSignature("\\Doctrine\\ORM\\QueryBuilder", "addOrderBy"),
1717
new MethodMatcher.CallToSignature("\\Doctrine\\ORM\\QueryBuilder", "set"),
1818
};
1919

20-
private static MethodMatcher.CallToSignature[] SELECT_FIELDS_VARIADIC = new MethodMatcher.CallToSignature[] {
20+
private static final MethodMatcher.CallToSignature[] SELECT_FIELDS_VARIADIC = new MethodMatcher.CallToSignature[] {
2121
new MethodMatcher.CallToSignature("\\Doctrine\\ORM\\QueryBuilder", "select"),
2222
new MethodMatcher.CallToSignature("\\Doctrine\\ORM\\QueryBuilder", "addSelect"),
2323
new MethodMatcher.CallToSignature("\\Doctrine\\ORM\\QueryBuilder", "groupBy"),

0 commit comments

Comments
 (0)