1
1
package com.github.dinbtechit.vscodetheme.annotators
2
2
3
+
3
4
import com.intellij.openapi.editor.DefaultLanguageHighlighterColors
4
5
import com.intellij.openapi.editor.colors.TextAttributesKey
5
6
import com.intellij.psi.PsiElement
@@ -9,6 +10,7 @@ import com.jetbrains.python.psi.*
9
10
import com.jetbrains.python.psi.impl.PyImportedModule
10
11
import com.jetbrains.python.psi.impl.references.PyImportReference
11
12
import com.jetbrains.python.psi.impl.stubs.PyClassElementType
13
+ import org.jetbrains.plugins.notebooks.jupyter.python.JupyterPyDialect
12
14
13
15
14
16
class PyAnnotator : BaseAnnotator () {
@@ -25,26 +27,43 @@ class PyAnnotator : BaseAnnotator() {
25
27
" PY.SECONDARY_KEYWORD_WITH_BG" ,
26
28
DEFAULT_KEYWORD
27
29
)
30
+ val SECONDARY_KEYWORD_WITH_BG_JUPYTER : TextAttributesKey = TextAttributesKey .createTextAttributesKey(
31
+ " PY.Jupyter.SECONDARY_KEYWORD_WITH_BG" ,
32
+ DEFAULT_KEYWORD
33
+ )
28
34
29
35
val FUNCTION_WITH_BG : TextAttributesKey = TextAttributesKey .createTextAttributesKey(
30
36
" PY.FUNCTION_CALL" ,
31
37
DefaultLanguageHighlighterColors .FUNCTION_CALL
32
38
)
39
+ val FUNCTION_WITH_BG_JUPYTER : TextAttributesKey = TextAttributesKey .createTextAttributesKey(
40
+ " PY.Jupyter.FUNCTION_CALL" ,
41
+ DefaultLanguageHighlighterColors .FUNCTION_CALL
42
+ )
33
43
34
44
val CLASS_REFERENCE_WITH_BG : TextAttributesKey = TextAttributesKey .createTextAttributesKey(
35
45
" PY.CLASS_REFERENCE" ,
36
46
DefaultLanguageHighlighterColors .CLASS_REFERENCE
37
47
)
48
+ val CLASS_REFERENCE_WITH_BG_JUPYTER : TextAttributesKey = TextAttributesKey .createTextAttributesKey(
49
+ " PY.Jupyter.CLASS_REFERENCE" ,
50
+ DefaultLanguageHighlighterColors .CLASS_REFERENCE
51
+ )
38
52
39
53
val IMPORT_REFERENCE_WITH_BG : TextAttributesKey = TextAttributesKey .createTextAttributesKey(
40
54
" PY.IMPORTS" ,
41
55
DefaultLanguageHighlighterColors .CLASS_REFERENCE
42
56
)
57
+ val IMPORT_REFERENCE_WITH_BG_JUPYTER : TextAttributesKey = TextAttributesKey .createTextAttributesKey(
58
+ " PY.Jupyter.IMPORTS" ,
59
+ DefaultLanguageHighlighterColors .CLASS_REFERENCE
60
+ )
43
61
}
44
62
45
63
override fun getKeywordType (element : PsiElement ): TextAttributesKey ? {
46
64
var type: TextAttributesKey ? = null
47
65
66
+
48
67
if ((element.elementType is PyElementType
49
68
&& (element.parent is PyTargetExpression
50
69
|| element.parent is PyReferenceExpression )) && (element.parent !is PyFunction )
@@ -55,21 +74,23 @@ class PyAnnotator : BaseAnnotator() {
55
74
if ((element.parent is PyClass && element.nextSibling is PyArgumentList )
56
75
|| element.parent.reference?.resolve().elementType is PyClassElementType
57
76
) {
58
- type = CLASS_REFERENCE_WITH_BG
77
+ type = if (isJupyterNoteBook(element)) CLASS_REFERENCE_WITH_BG_JUPYTER else CLASS_REFERENCE_WITH_BG
78
+
59
79
}
60
80
61
81
if ((element.parent is PyFunction && element.text != " def" )
62
82
|| (element.parent.reference?.resolve() is PyFunction && element.text != " def" )
63
83
) {
64
- type = FUNCTION_WITH_BG
84
+ type = if (isJupyterNoteBook(element)) FUNCTION_WITH_BG_JUPYTER else FUNCTION_WITH_BG
65
85
}
66
86
67
87
68
88
if (element.parent.reference is PyImportReference
69
89
|| element.parent.reference?.resolve() is PyImportedModule
70
90
|| element.parent.reference?.resolve() is PyFile
71
91
) {
72
- type = IMPORT_REFERENCE_WITH_BG
92
+ type = if (isJupyterNoteBook(element)) IMPORT_REFERENCE_WITH_BG_JUPYTER else IMPORT_REFERENCE_WITH_BG
93
+
73
94
}
74
95
75
96
@@ -79,11 +100,18 @@ class PyAnnotator : BaseAnnotator() {
79
100
" elif" , " else" , " if" , " except" , " pass" , " raise" , " return" , " try" , " while" ,
80
101
" with" -> type = SECONDARY_KEYWORD
81
102
" self" -> type = DEFAULT_KEYWORD
82
- " async" , " await" -> type = SECONDARY_KEYWORD_WITH_BG
103
+ " async" , " await" -> type = if (isJupyterNoteBook(element)) SECONDARY_KEYWORD_WITH_BG_JUPYTER
104
+ else SECONDARY_KEYWORD_WITH_BG
83
105
else -> {}
84
106
}
85
107
86
108
return type
87
109
}
88
110
111
+ private fun isJupyterNoteBook (element : PsiElement ): Boolean {
112
+ return element.containingFile.name.contains(" .ipynb" )
113
+ || element.containingFile.language.displayName == JupyterPyDialect .displayName
114
+
115
+ }
116
+
89
117
}
0 commit comments