Skip to content

Commit 75e0170

Browse files
committed
feat: differentiate math operators from keyword operators
1 parent 070451e commit 75e0170

File tree

3 files changed

+34
-15
lines changed

3 files changed

+34
-15
lines changed

.idea/codeStyles/Project.xml

-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main/kotlin/com/github/lppedd/kotlin/KotlinAnnotator.kt

+26-9
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import com.intellij.openapi.editor.colors.TextAttributesKey
77
import com.intellij.openapi.editor.markup.TextAttributes
88
import com.intellij.psi.PsiElement
99
import org.jetbrains.kotlin.idea.highlighter.KotlinHighlightingColors
10+
import org.jetbrains.kotlin.lexer.KtKeywordToken
11+
import org.jetbrains.kotlin.lexer.KtSingleValueToken
1012
import org.jetbrains.kotlin.psi.KtOperationReferenceExpression
1113

1214
internal val KOTLIN_INFIX_FUN: TextAttributesKey =
@@ -15,28 +17,43 @@ internal val KOTLIN_INFIX_FUN: TextAttributesKey =
1517
KotlinHighlightingColors.FUNCTION_CALL
1618
)
1719

18-
internal val KOTLIN_OPERATOR_FUN: TextAttributesKey =
20+
internal val KOTLIN_KEYWORD_OPERATOR_FUN: TextAttributesKey =
1921
TextAttributesKey.createTextAttributesKey(
20-
"CUSTOM_KOTLIN_OPERATOR_FUN",
22+
"CUSTOM_KOTLIN_KEYWORD_OPERATOR_FUN",
2123
KotlinHighlightingColors.KEYWORD
2224
)
2325

26+
internal val KOTLIN_MATH_OPERATOR_FUN: TextAttributesKey =
27+
TextAttributesKey.createTextAttributesKey(
28+
"CUSTOM_KOTLIN_MATH_OPERATOR_FUN",
29+
KotlinHighlightingColors.OPERATOR_SIGN
30+
)
31+
2432
/**
2533
* @author Edoardo Luppi
2634
*/
2735
private class KotlinAnnotator : Annotator {
2836
private val globalScheme = EditorColorsManager.getInstance().globalScheme
2937
private val infixFunAttributes = globalScheme.getAttributes(KOTLIN_INFIX_FUN)
30-
private val operatorFunAttributes = globalScheme.getAttributes(KOTLIN_OPERATOR_FUN)
38+
private val keywordOperatorFunAttributes = globalScheme.getAttributes(KOTLIN_KEYWORD_OPERATOR_FUN)
39+
private val mathOperatorFunAttributes = globalScheme.getAttributes(KOTLIN_MATH_OPERATOR_FUN)
3140

3241
override fun annotate(element: PsiElement, annotationHolder: AnnotationHolder) {
33-
if (element !is KtOperationReferenceExpression) return
34-
35-
val textAttributes =
36-
if (element.isConventionOperator()) operatorFunAttributes
37-
else infixFunAttributes
42+
if (element is KtOperationReferenceExpression) {
43+
getTextAttributes(element)?.let {
44+
annotationHolder.setTextAttributes(element, it)
45+
}
46+
}
47+
}
3848

39-
annotationHolder.setTextAttributes(element, textAttributes)
49+
private fun getTextAttributes(expr: KtOperationReferenceExpression): TextAttributes? {
50+
val operationSignTokenType = expr.operationSignTokenType
51+
return when {
52+
!expr.isConventionOperator() -> infixFunAttributes
53+
operationSignTokenType is KtKeywordToken -> keywordOperatorFunAttributes
54+
operationSignTokenType is KtSingleValueToken -> mathOperatorFunAttributes
55+
else -> null
56+
}
4057
}
4158

4259
private fun AnnotationHolder.setTextAttributes(

src/main/kotlin/com/github/lppedd/kotlin/KotlinColorSettingsPage.kt

+8-5
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,22 @@ private class KotlinColorSettingsPage : ColorSettingsPage {
2525
override fun getAttributeDescriptors(): Array<AttributesDescriptor> =
2626
arrayOf(
2727
AttributesDescriptor("Infix function call", KOTLIN_INFIX_FUN),
28-
AttributesDescriptor("Operator function call", KOTLIN_OPERATOR_FUN)
28+
AttributesDescriptor("Keyword operator function call", KOTLIN_KEYWORD_OPERATOR_FUN),
29+
AttributesDescriptor("Math operator function call", KOTLIN_MATH_OPERATOR_FUN),
2930
)
3031

3132
override fun getAdditionalHighlightingTagToDescriptorMap(): Map<String, TextAttributesKey> =
3233
mapOf(
33-
"infix" to KOTLIN_INFIX_FUN,
34-
"operator" to KOTLIN_OPERATOR_FUN,
34+
"k-infix" to KOTLIN_INFIX_FUN,
35+
"k-kw-operator" to KOTLIN_KEYWORD_OPERATOR_FUN,
36+
"k-math-operator" to KOTLIN_MATH_OPERATOR_FUN,
3537
)
3638

3739
override fun getDemoText(): String = """
3840
|fun myFunction(myValue: String, myValues: List<String>) {
39-
| if (myValue <infix>owns</infix> 'c') { }
40-
| if (myValue <operator>!in</operator> myValues) { }
41+
| if (myValue <k-infix>owns</k-infix> 'c') { }
42+
| if (myValue <k-kw-operator>!in</k-kw-operator> myValues) { }
43+
| if (myValue <k-math-operator>></k-math-operator> myValues.first()) { }
4144
|}
4245
|
4346
|infix fun String.owns(char: Char): Boolean = contains(char)

0 commit comments

Comments
 (0)