Skip to content

Commit

Permalink
[JavaScript] const variable highlighting (fixes #22)
Browse files Browse the repository at this point in the history
  • Loading branch information
ris58h committed Sep 13, 2024
1 parent 516bc86 commit 7fc7ef5
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 32 deletions.
18 changes: 11 additions & 7 deletions src/main/kotlin/ris58h/webcalm/css/CssSyntaxHighlighter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ class CssSyntaxHighlighter : SyntaxHighlighterBase() {
}

private fun tokenColor(tokenType: IElementType): TextAttributesKey? {
//TODO: other token types
return when (tokenType) {
CssTypes.NUMBER, CssTypes.DIMENSION, CssTypes.PERCENTAGE -> CssSyntaxHighlighterColors.NUMBER
CssTypes.HASH -> CssSyntaxHighlighterColors.CONSTANT
CssTypes.VARIABLE -> CssSyntaxHighlighterColors.VARIABLE
else -> tokenColorFromTokenSets(tokenType)
}
}

private fun tokenColorFromTokenSets(tokenType: IElementType): TextAttributesKey? {
if (CssTokenSets.COMMENTS.contains(tokenType)) {
return CssSyntaxHighlighterColors.COMMENT
}
Expand Down Expand Up @@ -49,12 +59,6 @@ class CssSyntaxHighlighter : SyntaxHighlighterBase() {
if (CssTokenSets.FUNCTIONS.contains(tokenType)) {
return CssSyntaxHighlighterColors.FUNCTION_CALL
}
//TODO: other token types
return when (tokenType) {
CssTypes.NUMBER, CssTypes.DIMENSION, CssTypes.PERCENTAGE -> CssSyntaxHighlighterColors.NUMBER
CssTypes.HASH -> CssSyntaxHighlighterColors.CONSTANT
CssTypes.VARIABLE -> CssSyntaxHighlighterColors.VARIABLE
else -> null
}
return null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.intellij.openapi.editor.DefaultLanguageHighlighterColors
import com.intellij.openapi.editor.colors.TextAttributesKey
import com.intellij.openapi.project.DumbAware
import com.intellij.psi.PsiElement
import com.intellij.psi.util.PsiTreeUtil
import ris58h.webcalm.javascript.psi.*

class JavaScriptHighlightingAnnotator : Annotator, DumbAware {
Expand All @@ -15,8 +16,33 @@ class JavaScriptHighlightingAnnotator : Annotator, DumbAware {
override fun annotate(element: PsiElement, holder: AnnotationHolder) {
if (element !is JavaScriptIdentifier) return

val color = when (val parent = element.parent) {
is JavaScriptVariableDeclaration -> DefaultLanguageHighlighterColors.LOCAL_VARIABLE
var color = colorFromElement(element)

if (color == null) {
val resolved = element.reference?.resolve()
if (resolved is JavaScriptIdentifier) {
color = colorFromElement(resolved)
}
}

if (color != null) {
holder.newSilentAnnotation(HighlightSeverity.INFORMATION)
.textAttributes(color)
.create()
}
}

private fun colorFromElement(element: JavaScriptIdentifier) =
when (val parent = element.parent) {
is JavaScriptVariableDeclaration -> {
val variableStatement = PsiTreeUtil.getParentOfType(parent, JavaScriptVariableStatement::class.java)
if (variableStatement != null
&& PsiTreeUtil.getParentOfType(variableStatement, JavaScriptStatementsOwner::class.java) is JavaScriptFile) {
DefaultLanguageHighlighterColors.CONSTANT
}
else DefaultLanguageHighlighterColors.LOCAL_VARIABLE
}

is JavaScriptFormalParameter -> DefaultLanguageHighlighterColors.PARAMETER
is JavaScriptIdentifierExpression -> colorWhenParentIsIdentifierExpression(parent, element)
is JavaScriptFunctionDeclaration -> {
Expand All @@ -39,21 +65,14 @@ class JavaScriptHighlightingAnnotator : Annotator, DumbAware {
else -> null
}

if (color != null) {
holder.newSilentAnnotation(HighlightSeverity.INFORMATION)
.textAttributes(color)
.create()
}
}

private fun colorWhenParentIsIdentifierExpression(parent: JavaScriptIdentifierExpression, element: JavaScriptIdentifier): TextAttributesKey? {
return when (val parent2 = parent.parent) {
is JavaScriptCallExpression -> DefaultLanguageHighlighterColors.FUNCTION_CALL
is JavaScriptUpdateExpression -> DefaultLanguageHighlighterColors.REASSIGNED_LOCAL_VARIABLE
is JavaScriptAssignmentExpression -> {
if (parent2.firstChild === parent) DefaultLanguageHighlighterColors.REASSIGNED_LOCAL_VARIABLE
else null
}
// is JavaScriptUpdateExpression -> DefaultLanguageHighlighterColors.REASSIGNED_LOCAL_VARIABLE
// is JavaScriptAssignmentExpression -> {
// if (parent2.firstChild === parent) DefaultLanguageHighlighterColors.REASSIGNED_LOCAL_VARIABLE
// else null
// }
is JavaScriptFormalRestParameter -> DefaultLanguageHighlighterColors.PARAMETER
else -> {
// TODO: check for a param with the same name that can override predefined globals
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,20 @@ class JavaScriptSyntaxHighlighter : SyntaxHighlighterBase() {
}

private fun tokenColor(tokenType: IElementType): TextAttributesKey? {
//TODO: other token types
return when (tokenType) {
JavaScriptTypes.NULL_LITERAL -> JavaScriptHighlighterColors.KEYWORD
JavaScriptTypes.BOOLEAN_LITERAL -> JavaScriptHighlighterColors.KEYWORD
JavaScriptTypes.IDENTIFIER -> JavaScriptHighlighterColors.IDENTIFIER
JavaScriptTypes.DOT, JavaScriptTypes.QUESTION_MARK_DOT -> JavaScriptHighlighterColors.DOT
JavaScriptTypes.SEMICOLON -> JavaScriptHighlighterColors.SEMICOLON
JavaScriptTypes.COMMA -> JavaScriptHighlighterColors.COMMA
JavaScriptTypes.REGEX_LITERAL -> JavaScriptHighlighterColors.STRING
else -> tokenColorFromTokenSets(tokenType)
}
}

private fun tokenColorFromTokenSets(tokenType: IElementType): TextAttributesKey? {
if (JavaScriptTokenSets.COMMENTS.contains(tokenType)) {
return JavaScriptHighlighterColors.COMMENT
}
Expand Down Expand Up @@ -49,16 +63,6 @@ class JavaScriptSyntaxHighlighter : SyntaxHighlighterBase() {
if (JavaScriptTokenSets.NUMBERS.contains(tokenType)) {
return JavaScriptHighlighterColors.NUMBER
}
//TODO: other token types
return when (tokenType) {
JavaScriptTypes.NULL_LITERAL -> JavaScriptHighlighterColors.KEYWORD
JavaScriptTypes.BOOLEAN_LITERAL -> JavaScriptHighlighterColors.KEYWORD
JavaScriptTypes.IDENTIFIER -> JavaScriptHighlighterColors.IDENTIFIER
JavaScriptTypes.DOT, JavaScriptTypes.QUESTION_MARK_DOT -> JavaScriptHighlighterColors.DOT
JavaScriptTypes.SEMICOLON -> JavaScriptHighlighterColors.SEMICOLON
JavaScriptTypes.COMMA -> JavaScriptHighlighterColors.COMMA
JavaScriptTypes.REGEX_LITERAL -> JavaScriptHighlighterColors.STRING
else -> null
}
return null
}
}
8 changes: 8 additions & 0 deletions src/test/testData/javascript/reassinged-variables.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
var a = 0;
a = a + 1;

let b = 0;
b = b + 1;

const c = 0;
c = c + 1;

0 comments on commit 7fc7ef5

Please sign in to comment.