1
1
mirror of https://github.com/qvacua/vimr.git synced 2024-12-18 03:01:38 +03:00
vimr/bin/generate_autocmds.py
2023-12-24 22:12:37 +01:00

38 lines
1.1 KiB
Python
Executable File

#!/usr/bin/env python3
import os
import io
import re
from string import Template
# Assume that we're in $REPO_ROOT/Neovim
NVIM_AUEVENTS_ENUM_FILE = "./build/include/auevents_enum.generated.h"
SWIFT_TEMPLATE_FILE = "../resources/autocmds.template.swift"
def convert(line: str) -> tuple[str, str]:
result = re.match(r'^EVENT_(.*) = (.*)', line.replace(',', ''))
return result.group(1), result.group(2)
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())]
autocmds = [convert(line) for line in raw_auto_cmds]
template = Template(template_string)
return template.substitute(
event_cases="\n".join(
[f" case {event[0].lower()}" for event in autocmds]
),
version=version
)
if __name__ == '__main__':
version = os.environ['version']
with io.open(SWIFT_TEMPLATE_FILE, "r") as template:
print(swift_autocmds(version, template.read()))