Skip to content

Commit 20c0bfa

Browse files
committed
add completion for parameter in doctrine querybuilder "where" context
1 parent d63749f commit 20c0bfa

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import org.jetbrains.annotations.Nullable;
2525

2626
import java.util.*;
27+
import java.util.regex.Matcher;
28+
import java.util.regex.Pattern;
2729

2830
public class QueryBuilderCompletionContributor extends CompletionContributor {
2931

@@ -171,6 +173,9 @@ protected void addCompletions(@NotNull CompletionParameters completionParameters
171173
return;
172174
}
173175

176+
// $qb->andWhere('foo.id = ":foo_id"')
177+
addParameterNameCompletion(completionParameters, completionResultSet, psiElement);
178+
174179
// querybuilder parser is too slow longer values, and that dont make sense here at all
175180
// user can fire a manual completion event, when needed...
176181
if(completionParameters.isAutoPopup()) {
@@ -199,6 +204,35 @@ protected void addCompletions(@NotNull CompletionParameters completionParameters
199204

200205
}
201206

207+
private void addParameterNameCompletion(CompletionParameters completionParameters, CompletionResultSet completionResultSet, PsiElement psiElement) {
208+
209+
PsiElement literalExpr = psiElement.getParent();
210+
if(!(literalExpr instanceof StringLiteralExpression)) {
211+
return;
212+
}
213+
214+
String content = PsiElementUtils.getStringBeforeCursor((StringLiteralExpression) literalExpr, completionParameters.getOffset() - literalExpr.getTextOffset());
215+
if(content == null) {
216+
return;
217+
}
218+
219+
Matcher matcher = Pattern.compile("(\\w+)\\.(\\w+)[\\s+]*[=><]+[\\s+]$").matcher(content);
220+
if (matcher.find()) {
221+
final String complete = matcher.group(1) + "_" + matcher.group(2);
222+
223+
// fill underscore and underscore completion
224+
Set<String> strings = new HashSet<String>() {{
225+
add(complete);
226+
add(fr.adrienbrault.idea.symfony2plugin.util.StringUtils.camelize(complete, true));
227+
}};
228+
229+
for(String string: strings) {
230+
completionResultSet.addElement(LookupElementBuilder.create(":" + string).withIcon(Symfony2Icons.DOCTRINE));
231+
}
232+
233+
}
234+
}
235+
202236
});
203237

204238
// $qb->join('test.foo', 'foo');

src/fr/adrienbrault/idea/symfony2plugin/util/PsiElementUtils.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,4 +303,11 @@ public static <T extends PsiElement> List<T> getPrevSiblingsOfType(@Nullable Psi
303303
return elements;
304304
}
305305

306+
@Nullable
307+
public static String getStringBeforeCursor(StringLiteralExpression literal, int cursorOffset) {
308+
int cursorOffsetClean = cursorOffset - 1;
309+
String content = literal.getContents();
310+
return content.length() >= cursorOffsetClean ? content.substring(0, cursorOffsetClean) : null;
311+
}
312+
306313
}

0 commit comments

Comments
 (0)