Skip to content

Commit 77d4a63

Browse files
committed
feat: route parameters references #27
1 parent e4331b2 commit 77d4a63

File tree

5 files changed

+102
-0
lines changed

5 files changed

+102
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.github.tempest.framework.common.utils
2+
3+
object StringUtils {
4+
fun findTextBetweenParenthesis(text: String): List<MatchResult> =
5+
Regex("\\{([^}/]+)(?:[}/]|$)")
6+
.findAll(text)
7+
.toList()
8+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.github.tempest.framework.php.patterns
2+
3+
import com.intellij.patterns.ElementPattern
4+
import com.intellij.patterns.PropertyPatternCondition
5+
import com.jetbrains.php.lang.psi.elements.PhpAttribute
6+
7+
class AttributeFqnCondition<T : PhpAttribute>(namePattern: ElementPattern<String>) :
8+
PropertyPatternCondition<T?, String?>("withFqn", namePattern) {
9+
override fun getPropertyValue(o: Any): String? {
10+
return if (o is PhpAttribute) o.fqn else null
11+
}
12+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.github.tempest.framework.router.references
2+
3+
import com.intellij.openapi.util.TextRange
4+
import com.intellij.psi.PsiElement
5+
import com.intellij.psi.PsiReferenceBase
6+
import com.jetbrains.php.completion.PhpLookupElement
7+
import com.jetbrains.php.lang.psi.elements.Parameter
8+
import com.jetbrains.php.lang.psi.elements.StringLiteralExpression
9+
10+
class ParameterReference(
11+
element: StringLiteralExpression,
12+
textRange: TextRange,
13+
val parameters: Array<Parameter>,
14+
val resolvedParameter: Parameter? = null
15+
) : PsiReferenceBase<PsiElement>(element, textRange) {
16+
override fun resolve() = resolvedParameter
17+
18+
override fun getVariants() =
19+
parameters
20+
.map { PhpLookupElement(it) }
21+
.toTypedArray()
22+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.github.tempest.framework.router.references
2+
3+
import com.github.tempest.framework.TempestFrameworkClasses
4+
import com.github.tempest.framework.common.utils.StringUtils
5+
import com.github.tempest.framework.php.patterns.AttributeFqnCondition
6+
import com.intellij.openapi.util.TextRange
7+
import com.intellij.patterns.PlatformPatterns
8+
import com.intellij.patterns.StandardPatterns
9+
import com.intellij.psi.PsiElement
10+
import com.intellij.psi.PsiReference
11+
import com.intellij.psi.PsiReferenceContributor
12+
import com.intellij.psi.PsiReferenceProvider
13+
import com.intellij.psi.PsiReferenceRegistrar
14+
import com.intellij.util.ProcessingContext
15+
import com.jetbrains.php.lang.psi.elements.Method
16+
import com.jetbrains.php.lang.psi.elements.ParameterList
17+
import com.jetbrains.php.lang.psi.elements.PhpAttribute
18+
import com.jetbrains.php.lang.psi.elements.StringLiteralExpression
19+
20+
class RouteParametersReferenceContributor : PsiReferenceContributor() {
21+
override fun registerReferenceProviders(registrar: PsiReferenceRegistrar) {
22+
registrar.registerReferenceProvider(
23+
PlatformPatterns.psiElement(StringLiteralExpression::class.java)
24+
.withParent(PlatformPatterns.psiElement(ParameterList::class.java))
25+
.withSuperParent(
26+
2,
27+
PlatformPatterns.psiElement(PhpAttribute::class.java)
28+
.with(AttributeFqnCondition(StandardPatterns.string().oneOf(TempestFrameworkClasses.ROUTES)))
29+
),
30+
object : PsiReferenceProvider() {
31+
override fun getReferencesByElement(
32+
element: PsiElement,
33+
context: ProcessingContext
34+
): Array<out PsiReference> {
35+
val element = element as? StringLiteralExpression ?: return emptyArray()
36+
val attribute = element.parent.parent as? PhpAttribute ?: return emptyArray()
37+
val method = attribute.owner as? Method ?: return emptyArray()
38+
val parameters = method.parameters
39+
if (parameters.isEmpty()) return emptyArray()
40+
41+
return StringUtils.findTextBetweenParenthesis(element.text)
42+
.map { alias ->
43+
val rangeInElement = TextRange(
44+
alias.range.first + 1,
45+
alias.range.last
46+
)
47+
48+
val parameter = method.getParameter(alias.groupValues[1])
49+
ParameterReference(element, rangeInElement, parameters, parameter)
50+
}
51+
.toTypedArray()
52+
}
53+
54+
}
55+
)
56+
}
57+
}

src/main/resources/META-INF/plugin.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
order="first"
3131
language="HTML"
3232
implementation="com.github.tempest.framework.views.references.ComponentReferenceContributor"/>
33+
<psi.referenceContributor
34+
language="PHP"
35+
implementation="com.github.tempest.framework.router.references.RouteParametersReferenceContributor"/>
3336
<spellchecker.bundledDictionaryProvider
3437
implementation="com.github.tempest.framework.SpellcheckingDictionaryProvider"/>
3538
<lang.inspectionSuppressor

0 commit comments

Comments
 (0)