diff --git a/RELEASE.md b/RELEASE.md index 633f8d3d..c3977e48 100644 --- a/RELEASE.md +++ b/RELEASE.md @@ -134,7 +134,7 @@ python tools/check_release.py --libxr ../libxr --xrobot . \ ```json { - "xr-syntax": "0.2.0", + "xr-syntax": "0.3.0", "repositories": { "libxr": {"commit": "<40-hex>"}, "xrobot": {"commit": "<40-hex>", "version": "1.0.0"}, diff --git a/pyproject.toml b/pyproject.toml index 9eb64a6c..0b90b935 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -21,7 +21,7 @@ dependencies = [ "pyyaml", "requests", "ruamel.yaml>=0.17.21", - "xr-syntax==0.2.0" + "xr-syntax==0.3.0" ] classifiers = [ diff --git a/src/xrobot/cpp_layout.py b/src/xrobot/cpp_layout.py deleted file mode 100644 index 6a54649e..00000000 --- a/src/xrobot/cpp_layout.py +++ /dev/null @@ -1,373 +0,0 @@ -"""生成的 C++ 的排版:include 块、调用、花括号初始化器和宏,与 LibXR 的 .clang-format -(Google 风格、列宽 90、Allman 花括号、IncludeBlocks: Regroup)一致,不调用 clang-format。 -Layout of the generated C++: the include block, calls, brace initializers and macros, matching -LibXR's .clang-format (Google based, column limit 90, Allman braces, IncludeBlocks: Regroup) -without running clang-format. - -调用的实参放得下就写在一行,否则填满每一行并续行对齐到左括号之后;一个标识符都放不下时左括号 -后换行,缩进 4 格。花括号初始化器放得下就写在一行,否则每项一行、每项带逗号,右括号回到初始化器 -所在行的缩进。生成器的测试用固定版本的 clang-format 检查输出无需改动。 -Arguments of a call share one line when they fit, otherwise every line is filled and -continued aligned after the opening parenthesis; when that leaves no room the call breaks after -the parenthesis and indents by four. A brace initializer that fits stays on one line, otherwise -it has one entry per line, each followed by a comma, and the closing brace returns to the -indent of the line the initializer starts on. The tests of the generator check with a pinned -clang-format that the output needs no changes. -""" - -from __future__ import annotations - -import re -from dataclasses import dataclass, field - -from xrobot.source_syntax import close_token, code_tokens, split_arguments - -COLUMN_LIMIT = 90 -INDENT = 2 -CONTINUATION = 4 - -# 花括号或括号前可以出现的类型写法:限定名,可带模板实参。 -# What may precede a brace or a parenthesis: a qualified name with optional template arguments. -_TYPE_PREFIX = re.compile( - r"[A-Za-z_][A-Za-z_0-9]*(?:\s*::\s*[A-Za-z_][A-Za-z_0-9]*)*(?:\s*<.*>)?", re.S -) -_DESIGNATOR = re.compile(r"\.\s*([A-Za-z_][A-Za-z_0-9]*)\s*=(?!=)") - - -@dataclass -class Node: - """初始化器或实参的结构:花括号列表、调用、指定初始化项,或原样保留的文本。 - The structure of an initializer or argument: a brace list, a call, a designated entry, or - text kept as written. - """ - - kind: str # 'text' | 'brace' | 'call' | 'designated' - text: str = "" # text 的内容;brace 和 call 的前缀;designated 的字段名 - items: list[Node] = field(default_factory=list) - - -def parse_node(text: str) -> Node: - """把一段 C++ 文本拆成 Node;只拆顶层的花括号列表、调用和指定初始化项,其余原样保留。 - Split a piece of C++ text into a Node; only top-level brace lists, calls and designated - entries are split, everything else is kept as written. - """ - text = text.strip() - items = code_tokens(text) - if not items: - return Node("text", text) - designator = _DESIGNATOR.match(text) - if designator and items[0].text == ".": - return Node("designated", designator.group(1), [parse_node(text[designator.end() :])]) - for opening, kind in (("{", "brace"), ("(", "call")): - start = next( - (i for i, t in enumerate(items) if t.text in ("{", "(", "[") and _top_level(items, i)), - None, - ) - if start is None or items[start].text != opening: - continue - if close_token(items, start) != len(items) - 1: - continue - prefix = text[: items[start].start].strip() - if prefix and not _TYPE_PREFIX.fullmatch(prefix): - continue - if kind == "call" and not prefix: - continue - body = text[items[start].end : items[-1].start] - parts = [p for p in split_arguments(body) if p.strip()] - return Node(kind, prefix, [parse_node(p) for p in parts]) - return Node("text", text) - - -def _top_level(items: list, index: int) -> bool: - """items[index] 前面没有未配对的括号(模板尖括号不算)。 - Whether no bracket is open before items[index] (template angle brackets do not count). - """ - depth = 0 - for token in items[:index]: - if token.text in ("{", "(", "["): - depth += 1 - elif token.text in ("}", ")", "]"): - depth -= 1 - return depth == 0 - - -def flat(node: Node) -> str: - """node 写在一行的文本。 - The text of node on one line. - """ - if node.kind == "text": - return node.text - if node.kind == "designated": - return f".{node.text} = {flat(node.items[0])}" - opening, closing = ("{", "}") if node.kind == "brace" else ("(", ")") - return node.text + opening + ", ".join(flat(item) for item in node.items) + closing - - -def _fits(head: str, text: str, trailer: str) -> bool: - """head、text 和 trailer 接在一起是否不超过列宽,且 text 没有换行。 - Whether head, text and trailer together stay within the column limit and text has no - line break. - """ - return "\n" not in text and len(head) + len(text) + len(trailer) <= COLUMN_LIMIT - - -def layout_value(head: str, text: str, trailer: str, indent: int) -> list[str]: - """把一个值接在 head 之后排版;返回的第一行以 head 开头,最后一行以 trailer 结尾。 - Lay out a value after head; the first returned line starts with head and the last ends - with trailer. - - Args: - head: 值之前已在这一行上的文本(含缩进)。 - The text already on the line before the value, indentation included. - indent: 这一行的缩进,右花括号回到这里。 - The indentation of the line; the closing brace returns to it. - - 全部是指定初始化项的最外层花括号列表每项一行,放得下也一样。 - An outermost brace list of designated entries has one entry per line even when it fits. - """ - return _layout(parse_node(text), head, trailer, indent, 0, False) - - -def _layout(node: Node, head: str, trailer: str, indent: int, level: int, force: bool) -> list[str]: - """layout_value 对解析后的 Node 的递归;level 是花括号的嵌套层数,force 要求展开。 - The recursion of layout_value over a parsed Node; level is the brace nesting depth and - force requires the node to be expanded. - """ - text = flat(node) - designated = ( - node.kind == "brace" - and bool(node.items) - and all(item.kind == "designated" for item in node.items) - ) - if not (force or (level == 0 and designated)) and _fits(head, text, trailer): - return [head + text + trailer] - if node.kind == "designated": - child = node.items[0] - if child.kind == "brace" and child.items: - # Allman 花括号:字段名后换行,花括号另起一行并多缩进一级,每项一行。 - # Allman braces: break after the field name, and the brace starts a line one - # level deeper with one entry per line. - inner = indent + CONTINUATION - lines = [head + f".{node.text} ="] - return lines + _layout(child, " " * inner, trailer, inner, level + 1, True) - return _layout(child, head + f".{node.text} = ", trailer, indent, level, False) - if node.kind == "brace" and node.items: - inner = indent + CONTINUATION - lines = [head + node.text + "{"] - rows = _columns([flat(item) for item in node.items], inner, level, node.items) - if rows is None: - for item in node.items: - lines += _layout(item, " " * inner, ",", inner, level + 1, False) - else: - lines += rows - lines.append(" " * indent + "}" + trailer) - return lines - if node.kind == "call" and node.items: - lead = len(head) - len(head.lstrip(" ")) - return layout_call( - head.lstrip(" ") + node.text + "(", [flat(i) for i in node.items], ")" + trailer, lead - ) - return [head + text + trailer] - - -def _columns(items: list[str], indent: int, level: int, nodes: list[Node]) -> list[str] | None: - """花括号列表的表格排版:clang-format 对有尾随逗号的列表按列对齐,选行数最少的列数。 - The table layout of a brace list: clang-format aligns a list with a trailing comma in - columns and picks the column count with the fewest lines. - - 只有最外层列表有 5 项以上、内层列表有 19 项以上,且每项都能放在一行时才用;返回 None 时 - 每项一行。 - It applies only to an outermost list of five items or more, or a nested list of nineteen - or more, whose items each fit on one line; None means one item per line. - """ - if len(items) < (5 if level == 0 else 19): - return None - if any( - "\n" in text or node.kind == "designated" for text, node in zip(items, nodes, strict=True) - ): - return None - lengths = [len(text) + 1 for text in items] # 含逗号 / the comma included - remaining = COLUMN_LIMIT - indent - formats = [] - for columns in range(1, COLUMN_LIMIT // 3 + 1): - sizes = [0] * columns - smallest = [10**9] * columns - count = 1 - for i, length in enumerate(lengths): - column = i % columns - if i and column == 0: - count += 1 - sizes[column] = max(sizes[column], length) - smallest[column] = min(smallest[column], length) - if columns > len(items): - break - width = columns - 1 + sum(sizes) - if any(sizes[c] - smallest[c] > 10 for c in range(columns - 1)): - continue - if width > COLUMN_LIMIT and columns > 1: - continue - formats.append((columns, sizes, width, count)) - best = None - for columns, sizes, width, count in reversed(formats): - if width <= remaining or columns == 1: - if best is not None and count > best[3]: - break - best = (columns, sizes, width, count) - if best is None or best[0] == 1: - return None - columns, sizes = best[0], best[1] - rows = [] - for start in range(0, len(items), columns): - cells = [text + "," for text in items[start : start + columns]] - padded = [cell.ljust(sizes[c]) for c, cell in enumerate(cells[:-1])] + cells[-1:] - rows.append(" " * indent + " ".join(padded)) - return rows - - -def layout_declaration( - declaration: str, name: str, value: str, trailer: str, indent: int -) -> list[str]: - """一个带初始值的变量声明:第一行放不下时,在类型和变量名之间换行。 - A variable declaration with an initial value: when its first line is too long, it breaks - between the type and the name. - - Args: - declaration: 变量名之前的部分,如 "static const T"。 - The part before the name, such as "static const T". - """ - prefix = " " * indent - lines = layout_value(f"{prefix}{declaration} {name} = ", value, trailer, indent) - if len(lines[0]) <= COLUMN_LIMIT: - return lines - inner = indent + CONTINUATION - first = f"{prefix}{declaration} {name} =" - if len(first) <= COLUMN_LIMIT: - # 变量名放得下:值另起一行。 - # The name fits: the value starts a line of its own. - return [first] + layout_value(" " * inner, value, trailer, inner) - return [prefix + declaration] + layout_value(" " * inner + f"{name} = ", value, trailer, inner) - - -def wrap_comment(text: str, indent: int = 0) -> list[str]: - """一行 // 注释;超过列宽时在空格处折成多行,与 clang-format 的 ReflowComments 一致。 - A // comment line; one longer than the column limit is broken at spaces into several - lines, as clang-format's ReflowComments does. - """ - prefix = " " * indent + "// " - lines, current = [], prefix.rstrip() - for word in text.split(" "): - if current != prefix.rstrip() and len(current) + 1 + len(word) > COLUMN_LIMIT: - lines.append(current) - current = prefix.rstrip() - current += " " + word - lines.append(current) - return lines - - -def layout_call( - head: str, - arguments: list[str], - tail: str, - indent: int = 0, - extra: int = 0, - hang_margin: int = 1, -) -> list[str]: - """一个调用:实参放得下就在一行,否则按 clang-format 填满每一行。 - A call: the arguments share one line when they fit, otherwise every line is filled as - clang-format does. - - Args: - head: 到左括号为止的文本,不含缩进;整行的缩进是 indent。 - The text up to and including the opening parenthesis, without indentation. - arguments: 每个实参的单行文本。 - The one-line text of each argument. - tail: 最后一个实参之后的文本,如 ")" 或 ");"。 - The text after the last argument, such as ")" or ");". - extra: 每行末尾之后要占用的宽度(宏的续行符)。 - Width reserved after every line (the line continuation of a macro). - hang_margin: 左括号后换行比对齐少几行时才换行;函数声明比调用更不愿意换行。 - How many lines breaking after the parenthesis must save before it is chosen; a - function declaration is more reluctant than a call. - """ - prefix = " " * indent - one_line = prefix + head + ", ".join(arguments) + tail - if len(one_line) <= COLUMN_LIMIT and "\n" not in one_line: - return [one_line] - aligned = _pack(prefix + head, arguments, tail, len(prefix + head), extra) - hanging = [prefix + head] + _pack( - " " * (indent + CONTINUATION), arguments, tail, indent + CONTINUATION, extra, True - ) - limit = COLUMN_LIMIT - extra - if any(len(line) > limit for line in aligned) or len(aligned) - len(hanging) >= hang_margin: - return hanging - return aligned - - -def _pack( - first: str, arguments: list[str], tail: str, column: int, extra: int, fresh: bool = False -) -> list[str]: - """把实参填进行:first 是第一行已有的文本,续行缩进 column 格。 - Fill lines with the arguments: first is the text already on the first line, and - continuation lines are indented by column. - - fresh 为真时 first 只是缩进,第一个实参不加空格。 - With fresh, first is only indentation and the first argument gets no space before it. - """ - limit = COLUMN_LIMIT - extra - lines, current = [], first - start = first - for i, argument in enumerate(arguments): - piece = argument + (tail if i + 1 == len(arguments) else ",") - if current == start: - current += piece - elif len(current) + 1 + len(piece) <= limit: - current += " " + piece - else: - lines.append(current) - start = " " * column - current = start + piece - lines.append(current) - return lines - - -def layout_macro(name: str, call: str, arguments: list[str]) -> list[str]: - """#define name() call(arguments...);放不下时调用另起一行并用续行符对齐。 - #define name() call(arguments...); when it does not fit the call starts on its own line and - the line continuations are aligned. - """ - one_line = f"#define {name}() {call}({', '.join(arguments)})" - if len(one_line) <= COLUMN_LIMIT: - return [one_line] - body = layout_call(call + "(", arguments, ")", INDENT, extra=2) - lines = [f"#define {name}()"] + body - width = max(len(line) for line in lines[:-1]) + 1 - return [line.ljust(width) + "\\" for line in lines[:-1]] + [lines[-1]] - - -def sort_includes(names: list[str]) -> list[str]: - """按 clang-format 的 Regroup 排好的 #include 行:先 <*.h>,再其他 <...>,再 "...", - 组之间空一行,组内按名字的字节序排序并去重。 - The #include lines grouped as clang-format's Regroup does: <*.h>, other <...>, then "...", - a blank line between groups, each group sorted bytewise by name without repeats. - - Args: - names: 每个 include 的写法,如 "libxr.hpp"(带引号)或 。 - Each include as written, such as "libxr.hpp" with its quotes or . - """ - groups: dict[int, list[str]] = {1: [], 2: [], 3: []} - for name in sorted(set(names), key=lambda n: n.strip('"<>')): - if re.match(r"", name): - groups[2].append(name) - elif re.match(r"<.*\.h>", name): - groups[1].append(name) - elif name.startswith("<"): - groups[2].append(name) - else: - groups[3].append(name) - lines: list[str] = [] - for group in groups.values(): - if group: - if lines: - lines.append("") - lines += [f"#include {name}" for name in group] - return lines diff --git a/src/xrobot/generate_main.py b/src/xrobot/generate_main.py index 7735b7ff..74266e08 100644 --- a/src/xrobot/generate_main.py +++ b/src/xrobot/generate_main.py @@ -11,6 +11,16 @@ import re from pathlib import Path +from xr_syntax.cpp import CppFileBuilder +from xr_syntax.format import ( + INDENT, + layout_call, + layout_declaration, + layout_macro, + layout_value, + sort_includes, + wrap_comment, +) from xr_syntax.i18n import tr from xrobot.config import ( @@ -35,15 +45,6 @@ template_bindings, type_shape, ) -from xrobot.cpp_layout import ( - INDENT, - layout_call, - layout_declaration, - layout_macro, - layout_value, - sort_includes, - wrap_comment, -) from xrobot.module_parser import discover_modules, module_interface, select_module from xrobot.project import Project, atomic_write, header_banner, header_relative from xrobot.source_syntax import ( @@ -934,9 +935,17 @@ def _monitor_checks(names: list[str]) -> list[str]: @staticmethod def _numbered(lines: list, back: object, header_name: str | None) -> str: - """把占位换成指回头文件自身的 #line,并拼成文本。 - Replace the placeholders with #line directives that point back into the header - itself, and join the text. + """把占位换成指回头文件自身的 #line,再交给 CppFileBuilder 拼成整份文件。 + Replace the placeholders with #line directives that point back into the header itself and + hand the lines to CppFileBuilder. + + #line 的编号只由它之前已经输出的行数决定,因此先算出全部行再拼装。 + The number of a #line directive depends only on how many lines were emitted before it, so + every line is settled before the file is assembled. + + 最后一行是空行时,它就是文件末尾的那个换行,不再补第二个;否则补一个换行,让文件以换行结束。 + An empty last line is the newline at the end of the file and gets no second one; otherwise + one is added, so the file ends with a newline. """ result, number = [], 1 # 下一行输出的行号 / line number of the next emitted line for line in lines: @@ -944,7 +953,15 @@ def _numbered(lines: list, back: object, header_name: str | None) -> str: line = f'#line {number + 1} "{_c_string(header_name)}"' result.append(line) number += line.count("\n") + 1 - return "\n".join(result) + ("" if result and result[-1] == "" else "\n") + if result and result[-1] == "": + # build() 逐片段原样拼接:末尾的空片段是一个空行,比 "\n".join(lines) 多出一个换行。 + # build() joins the chunks as written, so a trailing empty chunk is one blank line more + # than "\n".join(lines) gives. + result.pop() + builder = CppFileBuilder() + for line in result: + builder.raw(line) + return builder.build(require_clean=True).render() def _c_string(path: str) -> str: diff --git a/src/xrobot/lock.py b/src/xrobot/lock.py index 242405f4..2f1ca4aa 100644 --- a/src/xrobot/lock.py +++ b/src/xrobot/lock.py @@ -31,6 +31,7 @@ from urllib.request import url2pathname import yaml +from xr_syntax.cmake import CMakeFileBuilder from xr_syntax.i18n import tr from xrobot.git import checkout_state, git, head_commit, origin_url @@ -1246,18 +1247,26 @@ def write_cmake(modules_dir: Path, records: dict) -> None: """写出 Modules/CMakeLists.txt:模块自带 CMakeLists.txt 时 include 它,否则加 include 目录。 Write Modules/CMakeLists.txt: include a Module's own CMakeLists.txt, else add its folder to the include path. + + 文件由 CMakeFileBuilder 生成,它在 build() 时把整份源码解析一次,因此写出的 CMake 一定是 + xr-syntax 自己能读的。 + CMakeFileBuilder writes the file and parses the whole source once in build(), so the CMake it + writes is always something xr-syntax can read back. """ - lines = ["# Generated by `xrobot setup` from xrobot.lock; do not edit or commit.", ""] + builder = CMakeFileBuilder() + builder.comment("Generated by `xrobot setup` from xrobot.lock; do not edit or commit.") + builder.raw("") for identity in sorted(records): validate_id(identity) folder = Path(modules_dir) / identity if (folder / "CMakeLists.txt").exists(): - lines.append(f'include("${{CMAKE_CURRENT_LIST_DIR}}/{identity}/CMakeLists.txt")') + builder.command("include", [f'"${{CMAKE_CURRENT_LIST_DIR}}/{identity}/CMakeLists.txt"']) else: - lines.append( - f'target_include_directories(xr PUBLIC "${{CMAKE_CURRENT_LIST_DIR}}/{identity}")' + builder.command( + "target_include_directories", + ["xr", "PUBLIC", f'"${{CMAKE_CURRENT_LIST_DIR}}/{identity}"'], ) - atomic_write(Path(modules_dir) / "CMakeLists.txt", "\n".join(lines) + "\n") + atomic_write(Path(modules_dir) / "CMakeLists.txt", builder.build(require_clean=True).render()) def _load_lock(lock_path: Path) -> dict: diff --git a/src/xrobot/module_creator.py b/src/xrobot/module_creator.py index a025d4de..fb35dd79 100644 --- a/src/xrobot/module_creator.py +++ b/src/xrobot/module_creator.py @@ -14,6 +14,7 @@ from pathlib import Path import yaml +from xr_syntax.cpp import CppFileBuilder from xr_syntax.i18n import tr from xrobot.config import identifier_problem @@ -119,29 +120,28 @@ def _header( sort_keys=False, allow_unicode=True, ).rstrip() - lines = [ - "#pragma once", - "", - "// clang-format off", - "/* === MODULE MANIFEST V2 ===", - manifest, - "=== END MANIFEST === */", - "// clang-format on", - "", - *_include_lines(constructors + templates, includes), - "", - ] + builder = CppFileBuilder(header=True) + builder.raw("") + # manifest 由 clang-format 关闭保护,它的排版由 yaml.safe_dump 决定。 + # The manifest is protected by clang-format off; yaml.safe_dump decides its layout. + builder.format_region( + "\n/* === MODULE MANIFEST V2 ===\n" + manifest + "\n=== END MANIFEST === */\n" + ) + builder.raw("") + for line in _include_lines(constructors + templates, includes): + builder.raw(line) + builder.raw("") if templates: - lines.append(f"template <{', '.join(templates)}>") - lines += [ + builder.raw(f"template <{', '.join(templates)}>") + for line in ( f"class {class_name}", "{", " public:", f" {class_name}({', '.join(constructors)}) {{}}", "};", - "", - ] - return "\n".join(lines) + ): + builder.raw(line) + return builder.build(require_clean=True).render() def _interface(text: str, class_name: str, constructors: list[str], templates: list[str]) -> dict: diff --git a/src/xrobot/project.py b/src/xrobot/project.py index 5ec19b74..1c4ac42e 100644 --- a/src/xrobot/project.py +++ b/src/xrobot/project.py @@ -23,10 +23,9 @@ from typing import NamedTuple from xr_syntax.cpp import identifier_occurrences +from xr_syntax.format import wrap_comment from xr_syntax.i18n import tr -from xrobot.cpp_layout import wrap_comment - MODULES_YAML = Path("Modules/modules.yaml") SOURCE_SUFFIXES = (".c", ".cc", ".cpp", ".cxx") HEADER_LINE = re.compile(r'^// xrobot: (config|entry|depends) "([^"]+)"$') diff --git a/tests/test_cpp_layout.py b/tests/test_cpp_layout.py deleted file mode 100644 index c7306fdf..00000000 --- a/tests/test_cpp_layout.py +++ /dev/null @@ -1,265 +0,0 @@ -"""生成的 C++ 的排版(xrobot.cpp_layout):值、声明、调用、宏、注释和 include。 -Layout of the generated C++ (xrobot.cpp_layout): values, declarations, calls, macros, comments -and includes. - -期望的文本与固定版本的 clang-format 对 LibXR 风格的输出相同;test_clang_format.py 对生成的 -头文件逐一核对这一点。 -The expected text is what the pinned clang-format gives in LibXR's style; test_clang_format.py -checks that on generated headers. -""" - -from fixtures import TestCase - -from xrobot.cpp_layout import ( - flat, - layout_call, - layout_declaration, - layout_macro, - layout_value, - parse_node, - sort_includes, - wrap_comment, -) - - -class Parsing(TestCase): - """把 C++ 文本拆成花括号列表、调用和指定初始化项。 - Splitting C++ text into brace lists, calls and designated entries. - """ - - def test_brace_lists_calls_and_designated_entries_are_split(self): - node = parse_node('{.a = 1, .b = {2, "x, y"}, .c = Foo(4, {5})}') - self.assertEqual(node.kind, "brace") - self.assertEqual( - [(i.kind, i.text) for i in node.items], - [("designated", "a"), ("designated", "b"), ("designated", "c")], - ) - self.assertEqual(node.items[1].items[0].kind, "brace") - self.assertEqual(node.items[2].items[0].kind, "call") - self.assertEqual(node.items[2].items[0].text, "Foo") - self.assertEqual(flat(node), '{.a = 1, .b = {2, "x, y"}, .c = Foo(4, {5})}') - - def test_typed_braces_keep_their_type(self): - node = parse_node("std::array{1, 2}") - self.assertEqual((node.kind, node.text), ("brace", "std::array")) - self.assertEqual(flat(node), "std::array{1, 2}") - - def test_anything_else_is_text_as_written(self): - for text in ("a + f(x)", "cond ? {1} : {2}", "values[0]", "(a + b) * c", "-f(1)", "x"): - with self.subTest(text=text): - node = parse_node(text) - self.assertEqual((node.kind, node.text), ("text", text)) - - -class Values(TestCase): - """值接在声明之后的排版。 - Laying out a value after a declaration. - """ - - HEAD = " static const T x = " - - def lay(self, text): - """HEAD 之后的一个值的排版结果。 - The layout of a value after HEAD. - """ - return layout_value(self.HEAD, text, ";", 2) - - def test_a_value_that_fits_stays_on_one_line(self): - self.assertEqual(self.lay("{1, 2}"), [" static const T x = {1, 2};"]) - self.assertEqual(self.lay("Param{1, 2}"), [" static const T x = Param{1, 2};"]) - - def test_an_outermost_struct_has_one_field_per_line_even_when_it_fits(self): - self.assertEqual( - self.lay("{.a = 1, .b = {.c = 2}}"), - [" static const T x = {", " .a = 1,", " .b = {.c = 2},", " };"], - ) - self.assertEqual(self.lay("{}"), [" static const T x = {};"]) - - def test_a_nested_struct_that_does_not_fit_breaks_after_its_field_name(self): - text = ( - '{.name = "n", .inner = {.first_long_field = 1000000, ' - ".second_long_field = 2000000, .third_long_field = 3}}" - ) - self.assertEqual( - self.lay(text), - [ - " static const T x = {", - ' .name = "n",', - " .inner =", - " {", - " .first_long_field = 1000000,", - " .second_long_field = 2000000,", - " .third_long_field = 3,", - " },", - " };", - ], - ) - - def test_a_list_of_five_or_more_fills_columns(self): - text = "{" + ", ".join(f'"name_{i}"' for i in range(12)) + "}" - self.assertEqual( - self.lay(text), - [ - " static const T x = {", - ' "name_0", "name_1", "name_2", "name_3", "name_4", "name_5",', - ' "name_6", "name_7", "name_8", "name_9", "name_10", "name_11",', - " };", - ], - ) - - def test_a_list_of_fewer_items_has_one_per_line(self): - text = "{" + ", ".join(f"Some::Long::Qualified::Name{i}" for i in range(4)) + "}" - lines = self.lay(text) - self.assertEqual(len(lines), 6) - self.assertEqual(lines[1], " Some::Long::Qualified::Name0,") - - def test_a_call_fills_lines_aligned_after_its_parenthesis(self): - text = "Type(" + ", ".join(f"argument_number_{i}" for i in range(8)) + ")" - self.assertEqual( - self.lay(text), - [ - " static const T x = Type(argument_number_0, argument_number_1, argument_number_2,", - " argument_number_3, argument_number_4, argument_number_5,", - " argument_number_6, argument_number_7);", - ], - ) - - def test_text_that_cannot_be_split_overflows_instead_of_breaking(self): - long_text = "a_very_long_name_" * 8 - self.assertEqual(self.lay(long_text), [f"{self.HEAD}{long_text};"]) - - -class Declarations(TestCase): - """第一行放不下的声明。 - Declarations whose first line is too long. - """ - - def test_a_long_name_goes_to_the_next_line_after_the_type(self): - self.assertEqual( - layout_declaration( - "static const std::initializer_list", - "xr_shared_topic_topic_configs", - '{"a", "b"}', - ";", - 2, - ), - [ - " static const std::initializer_list", - ' xr_shared_topic_topic_configs = {"a", "b"};', - ], - ) - - def test_a_brace_that_does_not_fit_after_the_name_starts_its_own_line(self): - items = ", ".join(f"Entry::kValue{i}" for i in range(7)) - self.assertEqual( - layout_declaration( - "static const std::initializer_list", - "xr_jftjjislhhdxmel_ltqslbq", - "{" + items + "}", - ";", - 2, - ), - [ - " static const std::initializer_list " - "xr_jftjjislhhdxmel_ltqslbq =", - " {", - " Entry::kValue0, Entry::kValue1, Entry::kValue2, Entry::kValue3,", - " Entry::kValue4, Entry::kValue5, Entry::kValue6,", - " };", - ], - ) - - -class Calls(TestCase): - """调用、函数声明和宏的实参排版。 - The argument layout of calls, function declarations and macros. - """ - - def test_arguments_that_fit_share_one_line(self): - self.assertEqual( - layout_call("static CMD cmd(", ['"a"', "b"], ");", 2), [' static CMD cmd("a", b);'] - ) - - def test_a_call_aligns_after_the_parenthesis(self): - args = [f"motor_{i}" for i in range(9)] - self.assertEqual( - layout_call("static Chassis chassis(", args, ");", 2), - [ - " static Chassis chassis(motor_0, motor_1, motor_2, motor_3, motor_4, " - "motor_5,", - " motor_6, motor_7, motor_8);", - ], - ) - - def test_a_call_breaks_after_the_parenthesis_when_that_saves_a_line(self): - args = [f"argument_{i:02d}" for i in range(14)] - lines = layout_call("static Something_long_name instance_with_long_name(", args, ");", 2) - self.assertEqual(lines[0], " static Something_long_name instance_with_long_name(") - self.assertEqual(lines[1][:10], " argu") - self.assertTrue(all(len(line) <= 90 for line in lines)) - - def test_a_function_declaration_is_more_reluctant_to_break_after_the_parenthesis(self): - params = [f"LibXR::GPIO& gpio_{i}" for i in range(6)] - head = "[[noreturn]] static inline void XRobotMain(" - aligned = layout_call(head, params, ")", 0, hang_margin=3) - self.assertEqual(len(aligned), 3) - self.assertTrue(aligned[1].startswith(" " * 43 + "LibXR::GPIO& gpio_2")) - many = [f"LibXR::GPIO& gpio_{i}" for i in range(30)] - hanging = layout_call(head, many, ")", 0, hang_margin=3) - self.assertEqual(hanging[0], head) - self.assertTrue(hanging[1].startswith(" LibXR::GPIO& gpio_0, ")) - - def test_a_short_macro_is_one_line_and_a_long_one_aligns_its_continuations(self): - self.assertEqual( - layout_macro("XROBOT_MAIN", "::XRobotMain", ["a", "b"]), - ["#define XROBOT_MAIN() ::XRobotMain(a, b)"], - ) - lines = layout_macro("XROBOT_MAIN", "::XRobotMain", [f"gpio_{i}" for i in range(14)]) - self.assertEqual( - lines, - [ - "#define XROBOT_MAIN()" + " " * 66 + "\\", - " ::XRobotMain(gpio_0, gpio_1, gpio_2, gpio_3, gpio_4, gpio_5, gpio_6, gpio_7, " - "gpio_8, \\", - " gpio_9, gpio_10, gpio_11, gpio_12, gpio_13)", - ], - ) - - -class Text(TestCase): - """注释和 include。 - Comments and includes. - """ - - def test_a_long_comment_is_broken_at_spaces(self): - text = ( - "Generated by `xrobot gen` from User/products/a_rather_long_product_name_xxxx.yaml; " - "do not edit by hand." - ) - self.assertEqual( - wrap_comment(text), - [ - "// Generated by `xrobot gen` from User/products/a_rather_long_product_name_xxxx" - ".yaml; do", - "// not edit by hand.", - ], - ) - self.assertEqual(wrap_comment("short", 2), [" // short"]) - - def test_includes_are_grouped_and_sorted_like_clang_format_regroups_them(self): - self.assertEqual( - sort_includes( - ['"libxr.hpp"', "", '"Led.hpp"', "", '"sub/Pins.h"', '"Led.hpp"'] - + [""] - ), - [ - "#include ", - "", - "#include ", - "#include ", - "", - '#include "Led.hpp"', - '#include "libxr.hpp"', - '#include "sub/Pins.h"', - ], - ) diff --git a/tests/test_lock.py b/tests/test_lock.py index 0b9902d8..a7964a9f 100644 --- a/tests/test_lock.py +++ b/tests/test_lock.py @@ -1165,6 +1165,43 @@ def test_module_list_includes_or_adds_the_folder(self): ) self.assertIn('include("${CMAKE_CURRENT_LIST_DIR}/team/WithCMake/CMakeLists.txt")', text) + def test_a_long_module_identity_keeps_each_command_on_one_line(self): + # 仓库名不受我们控制:QDU-Robomaster 下已有 39 个字符的名字,这里用 60 个字符。生成的 CMake + # 没有排版工具,命令必须原样写成一行,与 xrobot 1.0.1 的输出逐字节相同。 + # A repository name is not ours to choose: QDU-Robomaster already has one of 39 characters, + # and this one has 60. Generated CMake has no formatter, so a command stays on one line + # exactly as written, byte for byte as xrobot 1.0.1 writes it. + identity = "QDU-Robomaster/" + "W" * 45 + self.assertEqual(len(identity), 60) + (self.modules / identity).mkdir(parents=True) + write_cmake(self.modules, {identity: {}}) + self.assertEqual( + (self.modules / "CMakeLists.txt").read_text(encoding="utf-8"), + "# Generated by `xrobot setup` from xrobot.lock; do not edit or commit.\n" + "\n" + 'target_include_directories(xr PUBLIC "${CMAKE_CURRENT_LIST_DIR}/' + identity + '")\n', + ) + self.write(self.modules / identity / "CMakeLists.txt", "") + write_cmake(self.modules, {identity: {}}) + self.assertEqual( + (self.modules / "CMakeLists.txt").read_text(encoding="utf-8"), + "# Generated by `xrobot setup` from xrobot.lock; do not edit or commit.\n" + "\n" + 'include("${CMAKE_CURRENT_LIST_DIR}/' + identity + '/CMakeLists.txt")\n', + ) + + def test_an_empty_module_list_writes_the_same_two_lines(self): + # 没有任何模块时,Modules/CMakeLists.txt 是说明行加一个空行;build() 原样拼接各片段,不吞 + # 掉末尾的空行(xrobot 1.0.1 写出 72 字节)。 + # Without any module, Modules/CMakeLists.txt is the comment line and one blank line; + # build() joins the chunks as written and does not swallow the trailing blank line + # (xrobot 1.0.1 writes 72 bytes). + write_cmake(self.modules, {}) + self.assertEqual( + (self.modules / "CMakeLists.txt").read_bytes(), + b"# Generated by `xrobot setup` from xrobot.lock; do not edit or commit.\n\n", + ) + class ReleaseGate(UpstreamTestCase): """发布线检查(--release-ref)。