lishwist/core/scripts/strip_godoc_comments

56 lines
1.2 KiB
Python
Executable File

#!/usr/bin/env python3
import fileinput
from collections import deque
from typing import Iterator
from typing import TypeVar
T = TypeVar("T")
def window(it: Iterator[T], size: int = 2) -> Iterator[list[T]]:
if size < 2:
raise ValueError("Window size must be at least 2")
window: deque[T] = deque(maxlen=size)
for _ in range(size):
try:
window.append(next(it))
except StopIteration:
return
yield list(window)
for item in it:
window.append(item)
yield list(window)
struct = False
func = False
for line, next_line in window(fileinput.input()):
line = line.removesuffix("\n")
if line.startswith("type ") and line.endswith(" struct {"):
struct = True
elif struct and line.endswith("}"):
struct = False
elif line.startswith("func"):
func = True
elif next_line[:4].strip() != "":
func = False
if struct:
if (
line != ""
and not line.lstrip().startswith("//")
or "Has unexported fields" in line
):
print(line)
elif func:
if line != "" and not line.startswith(" "):
print(line)
else:
print(line)