fix: go syntax checker

This commit is contained in:
clelia
2026-08-12 16:52:00 +02:00
parent aafd630440
commit c52cfd06dd
+42 -28
View File
@@ -13,6 +13,35 @@ from .base import (
trim_commonpath, trim_commonpath,
) )
_RE_HEADER_LINE = re.compile(r"^(import|type|func)\b")
def _split_header_body(contents: str) -> tuple[str, str]:
"""Split shortened Go code into leading package-level declarations
(imports, types, helper funcs) and the remaining statements that belong
inside `func Main()`.
`shorten()`/`generic_shorten()` flatten these together and dedent
everything to column 0, so indentation alone can no longer tell them
apart. Helper declarations always come first (`shorten()`'s `RE_CODE`
requires `func Main()` to be the last top-level declaration), so this
scans for the first line, outside of any brackets, that isn't the start
of an `import`/`type`/`func` declaration and treats everything from
there onward as the body.
"""
lines = contents.splitlines(keepends=True)
depth = 0
header_end = 0
for i, line in enumerate(lines):
if depth == 0:
stripped = line.strip()
if stripped and _RE_HEADER_LINE.match(stripped) is None:
break
depth += line.count("{") + line.count("(")
depth -= line.count("}") + line.count(")")
header_end = i + 1
return "".join(lines[:header_end]), "".join(lines[header_end:])
class LanguageGo(Language): class LanguageGo(Language):
NAME = "go" NAME = "go"
@@ -105,35 +134,20 @@ class LanguageGo(Language):
def format(cls, fnames: list[str]) -> None: def format(cls, fnames: list[str]) -> None:
subprocess.run(["gofmt", "-w", *fnames], check=True) subprocess.run(["gofmt", "-w", *fnames], check=True)
RE_RENDERED = re.compile(
r"""
(?P<imports>
(?: import\s*\([^)]+\)\n
| import\s+"[^"]+"\n
| \n
)*
)
(?P<body> .* )
$
""",
re.DOTALL | re.VERBOSE,
)
@classmethod @classmethod
def unshorten(cls, contents: str) -> str: def unshorten(cls, contents: str) -> str:
if m := LanguageGo.RE_RENDERED.match(contents): header, body = _split_header_body(contents)
return textwrap.dedent( return textwrap.dedent(
"""\ """\
package snippet package snippet
{imports} {header}
func Main() {{ func Main() {{
{body} {body}
}} }}
""" """
).format( ).format(
imports=m["imports"].strip(), header=header.strip(),
body=textwrap.indent(m["body"].strip(), "\t"), body=textwrap.indent(body.strip(), "\t"),
) )
return contents