Skip to content

Commit cd84374

Browse files
authored
Fix highlighting for payable, immutable, receive, fallback, and enum values (intellij-solidity#325)
1 parent 236035a commit cd84374

File tree

4 files changed

+29
-2
lines changed

4 files changed

+29
-2
lines changed

src/main/kotlin/me/serce/solidity/ide/annotation/annotator.kt

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ class SolidityAnnotator : Annotator {
2323
when (element) {
2424
is SolNumberType -> applyColor(holder, element, SolColor.TYPE)
2525
is SolElementaryTypeName -> applyColor(holder, element, SolColor.TYPE)
26+
is SolStateMutability -> if (element.text == "payable") {
27+
applyColor(holder, element, SolColor.KEYWORD)
28+
}
29+
is SolEnumValue -> applyColor(holder, element, SolColor.ENUM_VALUE)
2630
is SolMemberAccessExpression -> when(element.expression.firstChild.text) {
2731
"super" -> applyColor(holder, element.expression.firstChild, SolColor.KEYWORD)
2832
"msg", "block", "abi" -> applyColor(holder, element.expression.firstChild, SolColor.GLOBAL)
@@ -58,7 +62,17 @@ class SolidityAnnotator : Annotator {
5862
applyColor(holder, element.identifier, SolColor.STATE_VARIABLE)
5963
}
6064
}
61-
is SolFunctionDefinition -> element.identifier?.let { applyColor(holder, it, SolColor.FUNCTION_DECLARATION) }
65+
is SolFunctionDefinition -> {
66+
val identifier = element.identifier
67+
if (identifier !== null) {
68+
applyColor(holder, identifier, SolColor.FUNCTION_DECLARATION)
69+
} else {
70+
val firstChildNode = element.node.firstChildNode
71+
if (firstChildNode.text == "receive" || firstChildNode.text == "fallback") {
72+
applyColor(holder, firstChildNode.textRange, SolColor.RECEIVE_FALLBACK_DECLARATION)
73+
}
74+
}
75+
}
6276
is SolModifierDefinition -> element.identifier?.let { applyColor(holder, it, SolColor.FUNCTION_DECLARATION) }
6377
is SolModifierInvocation -> applyColor(holder, element.varLiteral.identifier, SolColor.FUNCTION_CALL)
6478
is SolUserDefinedTypeName -> {
@@ -72,6 +86,7 @@ class SolidityAnnotator : Annotator {
7286
is SolFunctionCallElement -> when(element.firstChild.text) {
7387
"keccak256" -> applyColor(holder, element.firstChild, SolColor.GLOBAL_FUNCTION_CALL)
7488
"require" -> applyColor(holder, element.firstChild, SolColor.KEYWORD)
89+
"assert" -> applyColor(holder, element.firstChild, SolColor.KEYWORD)
7590
else -> when(SolResolver.resolveTypeNameUsingImports(element).firstOrNull()) {
7691
is SolErrorDefinition -> applyColor(holder, element.referenceNameElement, SolColor.ERROR_NAME)
7792
is SolEventDefinition -> applyColor(holder, element.referenceNameElement, SolColor.EVENT_NAME)

src/main/kotlin/me/serce/solidity/ide/colors/SolColor.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ enum class SolColor(humanName: String, default: TextAttributesKey) {
1313
ERROR_NAME("Types//Error name", Defaults.CLASS_NAME),
1414
EVENT_NAME("Types//Event name", Defaults.CLASS_NAME),
1515
ENUM_NAME("Types//Enum name", Defaults.CLASS_NAME),
16+
ENUM_VALUE("Types//Enum value", Defaults.STATIC_FIELD),
1617
TYPE("Types//Value type", Defaults.KEYWORD),
1718
USER_DEFINED_VALUE_TYPE("Types//User-defined value type", Defaults.CLASS_NAME),
1819

@@ -21,6 +22,7 @@ enum class SolColor(humanName: String, default: TextAttributesKey) {
2122
STATE_VARIABLE("Identifiers//State variable", Defaults.INSTANCE_FIELD),
2223

2324
FUNCTION_DECLARATION("Functions//Function declaration", Defaults.FUNCTION_DECLARATION),
25+
RECEIVE_FALLBACK_DECLARATION("Functions//Receive/Fallback declaration", Defaults.STATIC_METHOD),
2426
FUNCTION_CALL("Functions//Function call", Defaults.FUNCTION_CALL),
2527
GLOBAL_FUNCTION_CALL("Functions//Global function call", Defaults.GLOBAL_VARIABLE),
2628

src/main/kotlin/me/serce/solidity/ide/highlighter.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ object SolHighlighter : SyntaxHighlighterBase() {
5656
IF, ELSE, FOR, WHILE, DO, BREAK, CONTINUE, THROW, USING, RETURN, RETURNS,
5757
MAPPING, EVENT, /*ERROR,*/ ANONYMOUS, MODIFIER, ASSEMBLY,
5858
VAR, STORAGE, MEMORY, WEI, ETHER, GWEI, SZABO, FINNEY, SECONDS, MINUTES, HOURS,
59-
DAYS, WEEKS, YEARS, TYPE, VIRTUAL, OVERRIDE
59+
DAYS, WEEKS, YEARS, TYPE, VIRTUAL, OVERRIDE, IMMUTABLE, INDEXED
6060
)
6161

6262
private fun types() = setOf<IElementType>(

src/main/resources/me/serce/solidity/ide/colors/highlighter_example.sol

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@
44

55
<TYPE>uint8</TYPE> <KEYWORD>constant</KEYWORD> <CONSTANT>MASK</CONSTANT> = 0x01;
66

7+
<KEYWORD>enum</KEYWORD> <ENUM_NAME>Types</ENUM_NAME> {
8+
<ENUM_VALUE>Type1</ENUM_VALUE>,
9+
<ENUM_VALUE>Type2</ENUM_VALUE>
10+
}
11+
12+
<KEYWORD>type</KEYWORD> <USER_DEFINED_VALUE_TYPE>Arg</USER_DEFINED_VALUE_TYPE> <KEYWORD>is</KEYWORD> <TYPE>uint256</TYPE>;
13+
714
/**
815
* @title Claimable
916
* @dev Extension for the Ownable contract, where the ownership needs to be claimed.
@@ -28,6 +35,9 @@
2835
_;
2936
}
3037

38+
<RECEIVE_FALLBACK_DECLARATION>receive</RECEIVE_FALLBACK_DECLARATION>() <KEYWORD>external</KEYWORD> <KEYWORD>payable</KEYWORD> {}
39+
<RECEIVE_FALLBACK_DECLARATION>fallback</RECEIVE_FALLBACK_DECLARATION>() <KEYWORD>external</KEYWORD> {}
40+
3141
<KEYWORD>function</KEYWORD> <FUNCTION_DECLARATION>transferOwnership</FUNCTION_DECLARATION>(<TYPE>address</TYPE> newOwner) <FUNCTION_CALL>onlyOwner</FUNCTION_CALL> {
3242
pendingOwner = newOwner;
3343
}

0 commit comments

Comments
 (0)