Advanced Php element pattern

Hello,
I need some help with an ElementPattern for my CompletionContributor.

class GoodsDetailCore
{
public static function Get($fields, $class = '')
{
return
GetTemplate('goods/GoodsDetail',
[
'fields' => $fields,
'class' => $class
]);
}

public static function Head($fields, $class = '')
{
return
GetTemplate('start code completion inside the quotes',
[
'fields' => $fields,
'class' => $class
]);
}

How do I write the pattern for that? I've tried using the same thing as I used for ReferenceContributor, but it does not work. I used this (kotlin):

val templateMethodPattern = object : PsiNamePatternCondition<PsiElement>("withFunctionName", StandardPatterns.string().matches("GetTemplate")) {
override fun getPropertyValue(o: Any): String? {
return if (o is FunctionReference) o.name else null
}
}

val elementPattern = psiElement().withElementType(PhpElementTypes.STRING).
withParent(psiElement().withElementType(PhpElementTypes.PARAMETER_LIST).withParent(psiElement().
withElementType(PhpElementTypes.FUNCTION_CALL).with(templateMethodPattern)))
extend(CompletionType.BASIC, elementPattern, CompletionProvider())


I tried many things, but nothing worked.

4 comments
Comment actions Permalink
Official comment

You need to provide completion inside a string, so instead of 

psiElement().withElementType(PhpElementTypes.STRING).

you need

psiElement().withParent(StringLiteralExpression.class). 
Comment actions Permalink

It still does not work, but I've made some progress:

val psiWithParentString = psiElement().withParent(StringLiteralExpression::class.java)
val psiWithTypeParamList = psiElement().withElementType(PhpElementTypes.PARAMETER_LIST)
val psiWithGetTemplateCall = psiElement().withElementType(PhpElementTypes.FUNCTION_CALL).with(templateMethodPattern)
val elementPattern = psiWithParentString.withSuperParent(2, psiWithTypeParamList).withParent(psiWithGetTemplateCall)

Using elementPattern without the last parent (psiWithGetTemplateCall) does trigger completion properly, however, I need it only inside the GetTemplate function call, and that does not work yet. Using it without this part:

.with(templateMethodPattern)

...does not work either. So what next?

0
Comment actions Permalink

In your pattern, you write that function call should be a parent of a string not a parent of a parameter list. It would be better to use:

psiWithParentString.withSuperParent(2, psiWithTypeParamList).withSuperParent(3, psiWithGetTemplateCall)

or even:

psiWithParentString.withSuperParent(3, psiWithGetTemplateCall)
0
Comment actions Permalink

Thank you, it works as it should!

0

Please sign in to leave a comment.