Created
June 18, 2025 01:19
-
-
Save sambacha/a06915c9b9d34588bdf072a4d3d0cf78 to your computer and use it in GitHub Desktop.
solidity kakoune
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Kakoune configuration for Solidity | |
# ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾ | |
# Detection | |
# ‾‾‾‾‾‾‾‾‾‾ | |
hook global BufCreate .*\.sol %{ | |
set-option buffer filetype solidity | |
} | |
# Initialization | |
# ‾‾‾‾‾‾‾‾‾‾‾‾‾‾ | |
hook global WinSetOption filetype=solidity %{ | |
require-module solidity | |
set-option window static_words %opt{solidity_static_words} | |
set-option window comment_line '//' | |
set-option window comment_block '/* ' ' */' | |
} | |
hook -group solidity-highlight global WinSetOption filetype=solidity %{ | |
add-highlighter window/solidity ref solidity | |
hook -once -always window WinSetOption filetype=.* %{ remove-highlighter window/solidity } | |
} | |
provide-module solidity %{ | |
# Highlighters | |
# ‾‾‾‾‾‾‾‾‾‾‾‾ | |
add-highlighter shared/solidity regions | |
add-highlighter shared/solidity/code default-region group | |
# General code quality highlighters | |
add-highlighter shared/solidity/code/trailing_whitespace regex \h+$ 0:trailing_whitespace | |
add-highlighter shared/solidity/code/tab regex \t+ 0:Error | |
# Comments (NatSpec is highlighted as documentation) | |
add-highlighter shared/solidity/natspec_line region '///' '\n' fill documentation | |
add-highlighter shared/solidity/natspec_block region '/\*\*' '\*/' regions | |
add-highlighter shared/solidity/line_comment region '//' '\n' fill comment | |
add-highlighter shared/solidity/block_comment region '/\*' '\*/' fill comment | |
# Highlight NatSpec tags inside documentation blocks | |
add-highlighter shared/solidity/natspec_block/ default-region fill documentation | |
add-highlighter shared/solidity/natspec_block/tag regex @(dev|notice|author|title|param|return)\b 0:attribute | |
# Strings | |
add-highlighter shared/solidity/string region %{(")} %{(?<!\\)(\\\\)*"} regions | |
add-highlighter shared/solidity/string/ default-region fill string | |
add-highlighter shared/solidity/string/escape regex %{\\["'\\nrt]} 0:meta | |
add-highlighter shared/solidity/string/hex regex %{\\x[0-9a-fA-F]{2}} 0:meta | |
add-highlighter shared/solidity/string/unicode regex %{\\u[0-9a-fA-F]{4}} 0:meta | |
# Pragma directive | |
add-highlighter shared/solidity/code/pragma regex \b(pragma)\h+(solidity)\h*([^\s;]+) 1:keyword 2:type 3:value | |
# Assembly blocks | |
add-highlighter shared/solidity/assembly region \b(assembly)\h*\{ \} regions | |
add-highlighter shared/solidity/assembly/ 1:keyword | |
add-highlighter shared/solidity/assembly/code default-region group | |
evaluate-commands %sh{ | |
# List of Yul/Assembly opcodes | |
opcodes="stop|add|mul|sub|div|sdiv|mod|smod|addmod|mulmod|exp|signextend|lt|gt|slt|sgt|eq|iszero|and|or|xor|not|byte|shl|shr|sar|keccak256" | |
opcodes="${opcodes}|address|balance|origin|caller|callvalue|calldataload|calldatasize|calldatacopy|codesize|codecopy|gasprice|extcodesize|extcodecopy" | |
opcodes="${opcodes}|returndatasize|returndatacopy|extcodehash|create|create2|call|callcode|delegatecall|staticcall|return|revert|selfdestruct|invalid" | |
opcodes="${opcodes}|log0|log1|log2|log3|log4|mload|mstore|mstore8|sload|sstore|msize|gas|jump|jumpi|pc|pop|dup1|dup2|dup3|dup4|dup5|dup6|dup7|dup8|dup9|dup10" | |
opcodes="${opcodes}|dup11|dup12|dup13|dup14|dup15|dup16|swap1|swap2|swap3|swap4|swap5|swap6|swap7|swap8|swap9|swap10|swap11|swap12|swap13|swap14|swap15|swap16" | |
opcodes="${opcodes}|let|if|switch|case|default|for|break|continue|function|leave" # Yul keywords | |
printf %s "add-highlighter shared/solidity/assembly/code/opcodes regex \b(${opcodes})\b 0:keyword" | |
} | |
# Assembly also has comments, numbers, and strings. Re-use main highlighters. | |
add-highlighter shared/solidity/assembly/code/ ref line_comment | |
add-highlighter shared/solidity/assembly/code/ ref block_comment | |
add-highlighter shared/solidity/assembly/code/ ref string | |
add-highlighter shared/solidity/assembly/code/hex_number regex \b0x[0-9a-fA-F]+\b 0:value | |
add-highlighter shared/solidity/assembly/code/dec_number regex \b[0-9]+\b 0:value | |
# Numbers | |
add-highlighter shared/solidity/code/hex_number regex \b0x[0-9a-fA-F_]+\b 0:value | |
add-highlighter shared/solidity/code/dec_number regex \b[0-9][0-9_]*(\s*(wei|gwei|ether))?\b 1:value 2:attribute | |
add-highlighter shared/solidity/code/scientific_number regex \b[0-9][0-9_]*e[0-9]+\b 0:value | |
# Operators and Punctuation | |
add-highlighter shared/solidity/code/operator regex (=>|\*\*|==|!=|<=|>=|&&|\|\||<<|>>|\+=|-=|\*=|/=|%=|&=|\|=|\^=|[\+\-\*/%&|\^<>=!~?:\.]) 0:operator | |
add-highlighter shared/solidity/code/punctuation regex [{}\[\]();,] 0:operator | |
# Identifiers (Order is important here) | |
# Highlight function calls like `myFunction(...)` | |
add-highlighter shared/solidity/code/function_call regex \b[a-z_][a-zA-Z0-9_]*\b(?=\s*\() 0:function | |
# Highlight user-defined types (Contract, Struct, Enum, Event, Error) by PascalCase convention | |
add-highlighter shared/solidity/code/type_identifier regex \b[A-Z][a-zA-Z0-9_]*\b 0:type | |
# Highlight constants by UPPER_CASE convention | |
add-highlighter shared/solidity/code/constant_identifier regex \b[A-Z][A-Z0-9_]*\b(?![a-z]) 0:value | |
# Keywords, Types, and Builtins | |
# ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾ | |
evaluate-commands %sh{ | |
keywords="abstract|after|alias|anonymous|apply|as|assembly|aut|break|case|catch|constant|constructor|continue|contract|copyof" | |
keywords="${keywords}|default|define|delete|do|else|enum|event|external|fallback|final|for|function|if|immutable|implements" | |
keywords="${keywords}|import|in|indexed|inline|internal|is|let|library|macro|match|modifier|mutable|new|of|override" | |
keywords="${keywords}|payable|private|promise|public|pure|receive|relocatable|return|returns|sealed|sizeof|static" | |
keywords="${keywords}|struct|supports|switch|try|type|typeof|unchecked|using|view|virtual|while" | |
types="address|bool|bytes|fixed|int|string|ufixed|uint|bytes[1-9]\d*|int(8|16|24|32|40|48|56|64|72|80|88|96|104|112|120|128|136|144|152|160|168|176|184|192|200|208|216|224|232|240|248|256)" | |
types="${types}|uint(8|16|24|32|40|48|56|64|72|80|88|96|104|112|120|128|136|144|152|160|168|176|184|192|200|208|216|224|232|240|248|256)|mapping|error" | |
builtins="assert|require|revert|keccak256|sha256|ripemd160|ecrecover|addmod|mulmod|selfdestruct|block|msg|tx|abi|super|this" | |
constants="true|false|wei|gwei|ether" | |
# Combine all for static completion, replacing '|' with spaces | |
all_words="${keywords}|${types}|${builtins}|${constants}" | |
printf %s\\n "declare-option str-list solidity_static_words ${all_words}" | tr '|' ' ' | |
# Create the highlighters | |
printf %s " | |
add-highlighter shared/solidity/code/keywords regex \b(${keywords})\b 0:keyword | |
add-highlighter shared/solidity/code/types regex \b(${types})\b 0:type | |
add-highlighter shared/solidity/code/builtins regex \b(${builtins})\b 0:builtin | |
add-highlighter shared/solidity/code/constants regex \b(${constants})\b 0:value | |
" | |
} | |
} | |
# Conveniences | |
# ‾‾‾‾‾‾‾‾‾‾‾‾ | |
# Expand '/*' to '/* */' in input mode. | |
hook global WinSetOption filetype=solidity %{ | |
hook window InsertChar '*' %{ | |
try %{ | |
execute-keys -draft 'h<a-k>/\*<ret>' | |
execute-keys ' */<left><left>' | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment