11package fr .adrienbrault .idea .symfony2plugin .doctrine .querybuilder ;
22
33import com .intellij .codeInsight .completion .*;
4+ import com .intellij .codeInsight .lookup .LookupElement ;
45import com .intellij .codeInsight .lookup .LookupElementBuilder ;
6+ import com .intellij .openapi .editor .Editor ;
57import com .intellij .openapi .project .Project ;
6- import com .intellij .patterns . PatternCondition ;
8+ import com .intellij .openapi . util . TextRange ;
79import com .intellij .patterns .PlatformPatterns ;
8- import com .intellij .patterns .StandardPatterns ;
910import com .intellij .psi .PsiElement ;
1011import com .intellij .psi .util .PsiTreeUtil ;
1112import 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}
0 commit comments