Skip to content

Commit 1e30936

Browse files
committed
feat: create custom element for block name
1 parent 69abc93 commit 1e30936

File tree

4 files changed

+80
-5
lines changed

4 files changed

+80
-5
lines changed

src/main/kotlin/com/github/xepozz/php_opcodes_language/language/parser/PHPOp.bnf

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,15 @@ phpOpFile ::= item_*
3333
private item_ ::= block | COMMENT | EOL
3434

3535
block ::= block_name COLON statements
36-
block_name ::= var_name | string_literal | IDENTIFIER COLON COLON IDENTIFIER | IDENTIFIER
36+
block_name ::= var_name | string_literal | method_name | function_name
37+
{
38+
implements=["com.intellij.psi.NavigatablePsiElement" "com.intellij.psi.PsiNamedElement" "com.intellij.psi.PsiLiteralValue"]
39+
extends="com.github.xepozz.php_opcodes_language.language.psi.impl.PHPOpBlockNameBaseImpl"
40+
methods=[isFunction isClass isClassMethod isMain]
41+
}
42+
43+
private method_name ::= IDENTIFIER COLON COLON IDENTIFIER
44+
private function_name ::= IDENTIFIER
3745

3846
statements ::= (statement | COMMENT | EOL)+
3947
statement ::= NUMBER (assignment_instruction | instruction) | live_range
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.github.xepozz.php_opcodes_language.language.psi.impl
2+
3+
import com.github.xepozz.php_opcodes_language.language.psi.PHPOpBlock
4+
import com.github.xepozz.php_opcodes_language.language.psi.PHPOpBlockName
5+
import com.intellij.icons.AllIcons
6+
import com.intellij.ide.projectView.PresentationData
7+
import com.intellij.lang.ASTNode
8+
import com.intellij.openapi.util.NlsSafe
9+
import com.intellij.psi.PsiElement
10+
import com.intellij.psi.search.LocalSearchScope
11+
import com.intellij.psi.search.SearchScope
12+
import com.jetbrains.php.lang.psi.PhpPsiUtil
13+
14+
abstract class PHPOpBlockNameBaseImpl : PHPOpBlockName, PHPOpElementImpl {
15+
constructor(node: ASTNode) : super(node)
16+
17+
override fun getValue(): Any? {
18+
return text
19+
}
20+
21+
override fun getName(): String = text
22+
23+
override fun setName(name: @NlsSafe String): PsiElement? {
24+
TODO("Not yet implemented")
25+
}
26+
27+
override fun getPresentation() = PresentationData(text, null, getIcon(0), null)
28+
29+
override fun getIcon(flags: Int) = AllIcons.Nodes.Property
30+
31+
override fun getUseScope(): SearchScope {
32+
val block = PhpPsiUtil.getParentOfClass(this, PHPOpBlock::class.java) ?: return super.getUseScope()
33+
return LocalSearchScope(block)
34+
}
35+
}

src/main/kotlin/com/github/xepozz/php_opcodes_language/language/psi/impl/PHPOpParameterBaseImpl.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ abstract class PHPOpParameterBaseImpl : PHPOpParameter, PHPOpElementImpl {
4444

4545
override fun isReferenceTo(psiElement: PsiElement) = when (psiElement) {
4646
!is PHPOpParameter -> false
47-
else -> when (true) {
48-
!psiElement.isVariable -> false
47+
else -> when {
48+
!this.isVariable || !psiElement.isVariable -> false
4949
else -> this.text == psiElement.text
5050
}
5151
}
Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,43 @@
11
package com.github.xepozz.php_opcodes_language.language.psi.impl
22

3+
import com.github.xepozz.php_opcodes_language.language.psi.PHPOpBlockName
34
import com.github.xepozz.php_opcodes_language.language.psi.PHPOpParameter
5+
import com.github.xepozz.php_opcodes_language.language.psi.PHPOpTypes
6+
import com.intellij.lang.tree.util.children
47

58
class PHPOpPsiImplUtil {
69
companion object {
710
@JvmStatic
8-
fun isVariable(element: PHPOpParameter): Boolean = element.text.startsWith("$")
911
fun isVariable(element: PHPOpParameter): Boolean = element.text.matches(Regex("[TV]\\d+"))
12+
13+
@JvmStatic
14+
fun isFunction(element: PHPOpBlockName): Boolean = element.node.let {
15+
val children = it.children().toList()
16+
children.size == 1
17+
&& children[0].elementType == PHPOpTypes.IDENTIFIER
18+
&& !children[0].text.contains("\\")
19+
}
20+
21+
// todo: check for class in a different way
22+
@JvmStatic
23+
fun isClass(element: PHPOpBlockName): Boolean = element.node.let {
24+
val children = it.children().toList()
25+
children.size == 1
26+
&& children[0].elementType == PHPOpTypes.IDENTIFIER
27+
&& children[0].text.contains("\\")
28+
}
29+
30+
@JvmStatic
31+
fun isClassMethod(element: PHPOpBlockName): Boolean = element.node.let {
32+
val children = it.children().toList()
33+
children.size == 4
34+
&& children[0].elementType == PHPOpTypes.IDENTIFIER
35+
&& children[1].elementType == PHPOpTypes.COLON
36+
&& children[2].elementType == PHPOpTypes.COLON
37+
&& children[3].elementType == PHPOpTypes.IDENTIFIER
38+
}
39+
40+
@JvmStatic
41+
fun isMain(element: PHPOpBlockName): Boolean = element.text == $$"$_main"
1042
}
11-
}
43+
}

0 commit comments

Comments
 (0)