Skip to content

Commit 35862e0

Browse files
committed
feat: highlight tokens
1 parent 7a05dc0 commit 35862e0

File tree

5 files changed

+68
-73
lines changed

5 files changed

+68
-73
lines changed
Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,30 @@
11
package com.github.xepozz.php_opcodes_language.language
22

3+
import com.github.xepozz.php_opcodes_language.language.psi.PHPOpInstructionName
4+
import com.github.xepozz.php_opcodes_language.language.psi.PHPOpVarName
35
import com.intellij.lang.annotation.AnnotationHolder
46
import com.intellij.lang.annotation.Annotator
5-
import com.intellij.openapi.editor.DefaultLanguageHighlighterColors
6-
import com.intellij.openapi.editor.colors.TextAttributesKey
7+
import com.intellij.lang.annotation.HighlightSeverity
78
import com.intellij.psi.PsiElement
9+
import com.jetbrains.php.lang.highlighter.PhpHighlightingData
810

911
class PHPOpAnnotator : Annotator {
1012
override fun annotate(element: PsiElement, holder: AnnotationHolder) {
11-
}
13+
// println("Annotating $element: ${element.text}")
14+
when (element) {
15+
is PHPOpVarName -> {
16+
holder.newSilentAnnotation(HighlightSeverity.INFORMATION)
17+
.range(element)
18+
.textAttributes(PhpHighlightingData.VAR)
19+
.create()
20+
}
1221

13-
companion object {
14-
val PATTERN_HIGHLIGHT = TextAttributesKey.createTextAttributesKey(
15-
"PHP_OPCODES_LANGUAGE_PATTERN",
16-
DefaultLanguageHighlighterColors.STRING,
17-
)
18-
val REFERENCE_DECLARATION_HIGHLIGHT = TextAttributesKey.createTextAttributesKey(
19-
"PHP_OPCODES_LANGUAGE_DIRECTIVE",
20-
DefaultLanguageHighlighterColors.FUNCTION_DECLARATION,
21-
)
22-
val REFERENCE_USAGE_HIGHLIGHT = TextAttributesKey.createTextAttributesKey(
23-
"PHP_OPCODES_LANGUAGE_ARGUMENT",
24-
DefaultLanguageHighlighterColors.FUNCTION_CALL,
25-
)
26-
val BLOCK_NAME_HIGHLIGHT = TextAttributesKey.createTextAttributesKey(
27-
"PHP_OPCODES_LANGUAGE_BLOCK_NAME",
28-
DefaultLanguageHighlighterColors.CLASS_NAME,
29-
)
30-
private val IDENTIFIER_HIGHLIGHT = TextAttributesKey.createTextAttributesKey(
31-
"PHP_OPCODES_LANGUAGE_IDENTIFIER",
32-
DefaultLanguageHighlighterColors.KEYWORD,
33-
)
22+
is PHPOpInstructionName -> {
23+
holder.newSilentAnnotation(HighlightSeverity.INFORMATION)
24+
.range(element)
25+
.textAttributes(PhpHighlightingData.FUNCTION_CALL)
26+
.create()
27+
}
28+
}
3429
}
3530
}

src/main/kotlin/com/github/xepozz/php_opcodes_language/language/PHPOpSyntaxHighlighter.kt

Lines changed: 0 additions & 45 deletions
This file was deleted.
Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,51 @@
11
package com.github.xepozz.php_opcodes_language.language
22

3+
import com.github.xepozz.php_opcodes_language.language.parser.PHPOpLexerAdapter
4+
import com.github.xepozz.php_opcodes_language.language.psi.PHPOpTypes
5+
import com.intellij.openapi.editor.DefaultLanguageHighlighterColors
6+
import com.intellij.openapi.editor.HighlighterColors
7+
import com.intellij.openapi.editor.colors.TextAttributesKey
8+
import com.intellij.openapi.fileTypes.SyntaxHighlighterBase
39
import com.intellij.openapi.fileTypes.SyntaxHighlighterFactory
410
import com.intellij.openapi.project.Project
511
import com.intellij.openapi.vfs.VirtualFile
12+
import com.intellij.psi.TokenType
13+
import com.intellij.psi.tree.IElementType
614

715
internal class PHPOpSyntaxHighlighterFactory : SyntaxHighlighterFactory() {
8-
override fun getSyntaxHighlighter(project: Project?, virtualFile: VirtualFile?) = PHPOpSyntaxHighlighter()
16+
override fun getSyntaxHighlighter(project: Project?, virtualFile: VirtualFile?) = object : SyntaxHighlighterBase() {
17+
override fun getHighlightingLexer() = PHPOpLexerAdapter()
18+
19+
override fun getTokenHighlights(tokenType: IElementType) = when (tokenType) {
20+
PHPOpTypes.NUMBER -> NUMBER_KEYS
21+
PHPOpTypes.TEXT -> TEXT_KEYS
22+
PHPOpTypes.COMMENT -> COMMENT_KEYS
23+
TokenType.BAD_CHARACTER -> BAD_CHAR_KEYS
24+
else -> EMPTY_KEYS
25+
}
26+
}
27+
28+
companion object {
29+
private val BAD_CHAR_KEYS = arrayOf(
30+
HighlighterColors.BAD_CHARACTER,
31+
)
32+
33+
private val COMMENT_KEYS = arrayOf(
34+
DefaultLanguageHighlighterColors.DOC_COMMENT
35+
)
36+
private val TEXT_KEYS = arrayOf(
37+
DefaultLanguageHighlighterColors.STRING
38+
)
39+
private val NUMBER_KEYS = arrayOf(
40+
DefaultLanguageHighlighterColors.NUMBER
41+
)
42+
private val HEREDOC_MARKER_KEYS = arrayOf(
43+
DefaultLanguageHighlighterColors.KEYWORD
44+
)
45+
private val HEREDOC_CONTENT_KEYS = arrayOf(
46+
DefaultLanguageHighlighterColors.STRING
47+
)
48+
private val EMPTY_KEYS = emptyArray<TextAttributesKey>()
49+
}
50+
951
}

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,14 @@ statement ::= NUMBER (assignment_instruction | instruction)
4747

4848
assignment_instruction ::= IDENTIFIER EQUALS_SIGN instruction
4949

50-
instruction ::= IDENTIFIER argument? argument?
50+
instruction ::= instruction_name argument? argument?
51+
52+
instruction_name ::= IDENTIFIER
5153

5254
argument ::= expr
5355

54-
private expr ::= var_name | TEXT | NUMBER | IDENTIFIER paren_expr | IDENTIFIER
56+
expr ::= var_name | TEXT | NUMBER | IDENTIFIER paren_expr | IDENTIFIER
5557

5658
paren_expr ::= LPAREN expr RPAREN
5759

58-
private var_name ::= DOLLAR_SIGN IDENTIFIER
60+
var_name ::= DOLLAR_SIGN IDENTIFIER

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<vendor>xepozz</vendor>
66

77
<depends>com.intellij.modules.platform</depends>
8+
<depends>com.jetbrains.php</depends>
89

910
<resource-bundle>messages.MyBundle</resource-bundle>
1011

0 commit comments

Comments
 (0)