1
1
mirror of https://github.com/qvacua/vimr.git synced 2024-12-01 01:32:04 +03:00
vimr/bin/generate_autocmds.py

38 lines
1.1 KiB
Python
Raw Normal View History

2018-01-21 16:16:07 +03:00
#!/usr/bin/env python3
import os
import io
import re
from string import Template
2019-12-26 12:47:09 +03:00
# Assume that we're in $REPO_ROOT/NvimView
2020-12-20 17:53:11 +03:00
NVIM_AUEVENTS_ENUM_FILE = "./build/include/auevents_enum.generated.h"
2019-12-22 23:45:42 +03:00
SWIFT_TEMPLATE_FILE = "../resources/autocmds.template.swift"
def convert(line: str) -> (str, str):
result = re.match(r'^EVENT_(.*) = (.*)', line.replace(',', ''))
return result.group(1), result.group(2)
2019-12-22 23:45:42 +03:00
def swift_autocmds(version: str, template_string: str) -> str:
with io.open(NVIM_AUEVENTS_ENUM_FILE, "r") as auto_cmds_file:
raw_auto_cmds = [line.strip() for line in auto_cmds_file.readlines() if re.match(r'^EVENT_', line.strip())]
2019-12-22 23:45:42 +03:00
autocmds = [convert(line) for line in raw_auto_cmds]
template = Template(template_string)
2019-12-22 23:45:42 +03:00
return template.substitute(
event_cases="\n".join(
[f" case {event[0].lower()} = {event[1]}" for event in autocmds]
),
2018-01-27 14:29:49 +03:00
version=version
)
if __name__ == '__main__':
2019-12-22 23:45:42 +03:00
version = os.environ['version']
2019-12-26 12:47:09 +03:00
with io.open(SWIFT_TEMPLATE_FILE, "r") as template:
print(swift_autocmds(version, template.read()))