Skip to content

Draft of Eunoia format script#213

Open
ajreynol wants to merge 8 commits into
mainfrom
eoFormat
Open

Draft of Eunoia format script#213
ajreynol wants to merge 8 commits into
mainfrom
eoFormat

Conversation

@ajreynol

@ajreynol ajreynol commented May 12, 2026

Copy link
Copy Markdown
Member

There is a need to give a common format to Eunoia files, similar to clang-format for C++.

Co-Authored-By: ChatGPT 5.5 noreply@openai.com

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR introduces an initial formatter for Eunoia (.eo) files, intended to provide a consistent canonical layout (akin to clang-format) by parsing Eunoia as S-expressions, preserving comments, and applying syntax-directed indentation/alignment rules.

Changes:

  • Adds a new contrib/eo_format.py CLI tool that lexes/parses .eo files and re-emits formatted output with fixed indentation and line-width rules.
  • Implements special formatting for program blocks, including inline-case alignment logic and comment preservation.
  • Adds recursive formatting over include dependencies, plus --check and --diff modes.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread contrib/eo_format.py
Comment on lines +288 to +291
target = insert_before if insert_before is not None else len(lines) - 1
lines[target:target] = self.format_comment_with_indent(
node, self.leading_whitespace(lines[target])
)
Comment thread contrib/eo_format.py
import sys
from dataclasses import dataclass
from pathlib import Path
from typing import Iterable, Optional
Comment thread contrib/eo_format.py
class Node:
kind: str
text: str = ""
children: list["Node"] | None = None
Comment thread contrib/eo_format.py
Comment on lines +414 to +417
def program_case_info(
self, children: list[Node], case_level: int
) -> dict[int, Optional[dict[str, int | str]]]:
info: dict[int, Optional[dict[str, int | str]]] = {}
Comment thread contrib/eo_format.py
Comment on lines +514 to +518
def trailing_program_case_comments(
self,
children: list[Node],
case_info: dict[int, Optional[dict[str, int | str]]],
) -> dict[int, Node]:
Comment thread contrib/eo_format.py
Comment on lines +994 to +997
if not resolved.exists():
raise FormatError(f"{path}: file does not exist")
text = resolved.read_text()
nodes = parse_eunoia(text, str(resolved))
Comment thread contrib/eo_format.py
Comment on lines +1017 to +1020
def format_file(path: Path, formatter: Formatter) -> tuple[str, str]:
original = path.read_text()
formatted = formatter.format_document(parse_eunoia(original, str(path)))
return original, formatted
Comment thread contrib/eo_format.py
Comment on lines +1071 to +1074
if args.diff:
print(unified_diff(path, original, formatted), end="")
elif not args.check:
path.write_text(formatted)
Comment thread contrib/eo_format.py
Comment on lines +440 to +444
def inline_program_cases(
self,
children: list[Node],
case_info: dict[int, Optional[dict[str, int | str]]],
) -> dict[int, int]:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants