For use with https://github.com/SirVer/ultisnips
Note:
The multi-case regex won't handle all patterns correctly. Things like Just 123
or Just "abc"
etc. won't get picked up. Please tweak and share! 😇
For use with https://github.com/SirVer/ultisnips
Note:
The multi-case regex won't handle all patterns correctly. Things like Just 123
or Just "abc"
etc. won't get picked up. Please tweak and share! 😇
##################################################### | |
# Case lambda | |
# | |
# \case | |
# | |
# --> | |
# | |
# \x -> | |
# case x of | |
snippet "\\case" "Case lambda" br | |
\\${1:x} -> | |
case $1 of | |
$2 | |
endsnippet | |
##################################################### | |
# Multi-case | |
# | |
# A, B -> 1 | |
# | |
# --> | |
# | |
# A -> 1 | |
# B -> 1 | |
global !p | |
def gen_multicase(snip): | |
line = snip.buffer[snip.line] | |
indent = len(line) - len(line.lstrip()) | |
spaces = " " * indent | |
cases_joined, result = [x.strip() for x in line.strip().split("->")] | |
cases = [x.strip() for x in cases_joined.split(",")] | |
snip.buffer[snip.line] = "" # erase the line | |
case_lines = [f"{spaces}{case} -> {result}" for case in cases] | |
line_content = "\n".join(case_lines) | |
snip.expand_anon(line_content) | |
endglobal | |
post_jump "gen_multicase(snip)" | |
snippet "(([A-Za-z0-9]+, ?)+([A-Za-z0-9]+) ?->.*)" "Multi-case" br | |
`!p snip.rv = match.group(1)` | |
endsnippet | |
##################################################### | |
# Record punning | |
# | |
# {a,b,c} | |
# | |
# --> | |
# | |
# { a = a, b = b, c = c } | |
global !p | |
def gen_recordpunning(snip): | |
rec = match.group(1) | |
inner = rec[1:-1] | |
fields = [field.strip() for field in inner.split(",")] | |
new_rec = "{ " + ", ".join([f"{field} = {field}" for field in fields]) + " }" | |
snip.buffer[snip.line] = snip.buffer[snip.line][:-len(rec)] | |
snip.expand_anon(new_rec) | |
endglobal | |
post_jump "gen_recordpunning(snip)" | |
snippet "(\{ *[a-z][a-zA-Z0-9_]*( *, *[a-z][a-zA-Z0-9_]*)* *\})" "Record punning" br | |
`!p snip.rv = match.group(1)` | |
endsnippet | |
##################################################### | |
# Record constructor | |
# | |
# \->{a,b,c} | |
# | |
# --> | |
# | |
# \a b c -> { a = a, b = b, c = c } | |
global !p | |
def gen_recordconstructor(snip): | |
match_str = match.group(1) | |
inner = match_str[4:-1] | |
fields = [field.strip() for field in inner.split(",")] | |
field_names = " ".join(fields) | |
new_str = "\\" + field_names + " -> { " + ", ".join([f"{field} = {field}" for field in fields]) + " }" | |
snip.buffer[snip.line] = snip.buffer[snip.line][:-len(match_str)] | |
snip.expand_anon(new_str) | |
endglobal | |
post_jump "gen_recordconstructor(snip)" | |
snippet "(\\->\{ *[a-z][a-zA-Z0-9_]*( *, *[a-z][a-zA-Z0-9_]*)* *\})" "Record constructor" br | |
`!p snip.rv = match.group(1)` | |
endsnippet |