Skip to content

Commit d58c1f4

Browse files
committed
ci: Update style.py to work with visibility on enums
E.g. the following can now be formatted correctly: c_enum! { #[repr(c_uint)] pub enum #anon { pub PTHREAD_INTROSPECTION_THREAD_CREATE = 1, pub PTHREAD_INTROSPECTION_THREAD_START, pub PTHREAD_INTROSPECTION_THREAD_TERMINATE, pub PTHREAD_INTROSPECTION_THREAD_DESTROY, } }
1 parent ba6357e commit d58c1f4

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

ci/style.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,22 @@ def fmt_one(fpath: Path, check_only: bool):
8787
# syntax. Replace it with a dummy name.
8888
text = re.sub(r"enum #anon\b", r"enum _fmt_anon", text)
8989

90+
# If enum variants are annotated with `pub`, rustfmt erases the visibility. To get
91+
# around this we first match on all enums to extract their bodies, then look for `pub`
92+
# visibility indicators. If found, these get stashed in a comment on the preceding
93+
# line.
94+
def enum_sub(m: re.Match) -> str:
95+
enum_body = m.group(0)
96+
rep = re.sub(
97+
r"^(.*)\b(pub\s*?(\(.*?\))?)\s*",
98+
r"\1/* FMT-VIS \2 END-FMT-VIS */\n\1",
99+
enum_body,
100+
flags=re.MULTILINE,
101+
)
102+
return rep
103+
104+
text = re.sub(r"\benum.*\{\n?(?:\s*[^}]*\n)+\s*\}", enum_sub, text)
105+
90106
# Invoke rustfmt passing via stdin/stdout so we don't need to write the file. Exits
91107
# on failure.
92108
cmd = ["rustfmt", "--config-path=.rustfmt.toml"]
@@ -109,6 +125,7 @@ def fmt_one(fpath: Path, check_only: bool):
109125
text = re.sub(r"fn (\w+)_fmt_tmp\(\)", r"\1!", text)
110126
text = re.sub(r"cfg_tmp!\(\[(.*?)\]\)", r"#[cfg(\1)]", text, flags=re.DOTALL)
111127
text = re.sub(r"enum _fmt_anon", r"enum #anon", text)
128+
text = re.sub(r"/\* FMT-VIS (.*) END-FMT-VIS \*/\n\s*", r"\1 ", text)
112129

113130
# And write the formatted file back
114131
fpath.write_text(text)

0 commit comments

Comments
 (0)