mirror of
https://github.com/headroomlabs-ai/headroom.git
synced 2026-11-22 13:29:19 +08:00
fix(wrap): ignore YAML document markers when finding Serena's root indent
A --- marker was counted as a column-0 mapping line, so an indented root kept its old value and a second key was appended at column 0. Skip markers and directives, and insert a missing key before a trailing document end marker. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
+36
-8
@@ -2269,15 +2269,26 @@ def _serena_inline_comment(rest: str) -> str:
|
||||
return " " + body
|
||||
|
||||
|
||||
# A document marker or directive is YAML syntax outside the mapping. It is
|
||||
# only a marker when it starts at column 0; an indented ``---`` inside a
|
||||
# block scalar is content.
|
||||
_YAML_STRUCTURE_LINE = re.compile(r"^(?:---|\.\.\.)(?:[ \t]|$)|^%")
|
||||
|
||||
|
||||
def _serena_root_indent(text: str) -> str:
|
||||
"""Return the indentation of the root mapping.
|
||||
|
||||
YAML allows every root key to share an indent, so column 0 is not the
|
||||
test. The root indent is the shortest leading whitespace on a non-empty,
|
||||
non-comment line. A deeper line belongs to a nested block.
|
||||
non-comment line. Document markers (``---``, ``...``) and ``%`` directives
|
||||
are skipped: they are YAML syntax outside the mapping, and counting them
|
||||
would treat an indented root as column 0. A deeper line belongs to a
|
||||
nested block.
|
||||
"""
|
||||
root: str | None = None
|
||||
for line in text.splitlines():
|
||||
if _YAML_STRUCTURE_LINE.match(line):
|
||||
continue
|
||||
stripped = line.lstrip(" \t")
|
||||
if not stripped or stripped.startswith("#"):
|
||||
continue
|
||||
@@ -2330,11 +2341,11 @@ def _ensure_serena_dashboard_disabled(*, verbose: bool = False) -> None:
|
||||
|
||||
The edit only touches the key at the root mapping's indent. YAML allows
|
||||
that mapping to be indented as a whole, so column 0 is not the test: the
|
||||
root indent is the shortest indent of a non-comment line. A deeper
|
||||
occurrence belongs to a nested mapping (e.g. an ``extension:`` block that
|
||||
reuses the name) and is left alone — both when rewriting the value and
|
||||
when dropping duplicates, so it can never be mistaken for, or merged
|
||||
with, Serena's real setting.
|
||||
root indent is the shortest indent of a non-comment line, ignoring
|
||||
document markers and directives. A deeper occurrence belongs to a nested
|
||||
mapping (e.g. an ``extension:`` block that reuses the name) and is left
|
||||
alone — both when rewriting the value and when dropping duplicates, so
|
||||
it can never be mistaken for, or merged with, Serena's real setting.
|
||||
"""
|
||||
import re
|
||||
|
||||
@@ -2395,8 +2406,25 @@ def _ensure_serena_dashboard_disabled(*, verbose: bool = False) -> None:
|
||||
appended.append(f"{indent}projects: []")
|
||||
|
||||
if appended:
|
||||
body = new.rstrip("\n")
|
||||
new = (f"{body}\n" if body.strip() else "") + "\n".join(appended) + "\n"
|
||||
addition = "\n".join(appended) + "\n"
|
||||
lines = new.splitlines(keepends=True)
|
||||
end_at: int | None = None
|
||||
for index in range(len(lines) - 1, -1, -1):
|
||||
if not lines[index].strip(" \t\r\n"):
|
||||
continue
|
||||
if lines[index].rstrip("\r\n") == "...":
|
||||
end_at = index
|
||||
break
|
||||
if end_at is None:
|
||||
body = new.rstrip("\n")
|
||||
new = (f"{body}\n" if body.strip() else "") + addition
|
||||
else:
|
||||
# A key after ``...`` starts a second document. Keep the addition
|
||||
# inside the one Serena loads.
|
||||
prefix = "".join(lines[:end_at])
|
||||
if prefix and not prefix.endswith("\n"):
|
||||
prefix += "\n"
|
||||
new = prefix + addition + "".join(lines[end_at:])
|
||||
|
||||
if new == text:
|
||||
return
|
||||
|
||||
@@ -237,6 +237,88 @@ def test_serena_dashboard_disabled_keeps_indented_root_mapping(tmp_path, monkeyp
|
||||
assert cfg.read_text() == first
|
||||
|
||||
|
||||
def test_serena_dashboard_disabled_keeps_indented_root_after_document_marker(
|
||||
tmp_path, monkeypatch
|
||||
) -> None:
|
||||
"""A ``---`` document marker is not a mapping line.
|
||||
|
||||
Counting it as column 0 makes the root indent empty, so the indented
|
||||
setting is left in place and a column-0 key is appended. YAML then
|
||||
rejects the file.
|
||||
"""
|
||||
import yaml
|
||||
|
||||
monkeypatch.setenv("HOME", str(tmp_path))
|
||||
cfg = tmp_path / ".serena" / "serena_config.yml"
|
||||
cfg.parent.mkdir(parents=True)
|
||||
cfg.write_text("---\n projects: []\n web_dashboard_open_on_launch: true\n")
|
||||
|
||||
wrap._ensure_serena_dashboard_disabled()
|
||||
|
||||
text = cfg.read_text()
|
||||
assert text.startswith("---\n")
|
||||
assert " web_dashboard_open_on_launch: false\n" in text
|
||||
assert "\nweb_dashboard_open_on_launch:" not in text
|
||||
parsed = yaml.safe_load(text)
|
||||
assert parsed["web_dashboard_open_on_launch"] is False
|
||||
assert parsed["projects"] == []
|
||||
|
||||
first = text
|
||||
wrap._ensure_serena_dashboard_disabled()
|
||||
assert cfg.read_text() == first
|
||||
|
||||
|
||||
def test_serena_dashboard_disabled_skips_yaml_directive(tmp_path, monkeypatch) -> None:
|
||||
"""A ``%YAML`` directive is outside the mapping and must not set the indent."""
|
||||
import yaml
|
||||
|
||||
monkeypatch.setenv("HOME", str(tmp_path))
|
||||
cfg = tmp_path / ".serena" / "serena_config.yml"
|
||||
cfg.parent.mkdir(parents=True)
|
||||
cfg.write_text("%YAML 1.2\n---\n projects: []\n web_dashboard_open_on_launch: true\n")
|
||||
|
||||
wrap._ensure_serena_dashboard_disabled()
|
||||
|
||||
text = cfg.read_text()
|
||||
assert text.startswith("%YAML 1.2\n---\n")
|
||||
assert " web_dashboard_open_on_launch: false\n" in text
|
||||
assert "\nweb_dashboard_open_on_launch:" not in text
|
||||
parsed = yaml.safe_load(text)
|
||||
assert parsed["web_dashboard_open_on_launch"] is False
|
||||
assert parsed["projects"] == []
|
||||
|
||||
first = text
|
||||
wrap._ensure_serena_dashboard_disabled()
|
||||
assert cfg.read_text() == first
|
||||
|
||||
|
||||
def test_serena_dashboard_disabled_appends_inside_document_end_marker(
|
||||
tmp_path, monkeypatch
|
||||
) -> None:
|
||||
"""A missing key is inserted before a trailing ``...``, not after it.
|
||||
|
||||
A key written after the document end marker starts a second document.
|
||||
"""
|
||||
import yaml
|
||||
|
||||
monkeypatch.setenv("HOME", str(tmp_path))
|
||||
cfg = tmp_path / ".serena" / "serena_config.yml"
|
||||
cfg.parent.mkdir(parents=True)
|
||||
cfg.write_text("---\n projects: []\n...\n")
|
||||
|
||||
wrap._ensure_serena_dashboard_disabled()
|
||||
|
||||
text = cfg.read_text()
|
||||
assert text.index(" web_dashboard_open_on_launch: false\n") < text.index("...\n")
|
||||
parsed = yaml.safe_load(text)
|
||||
assert parsed["web_dashboard_open_on_launch"] is False
|
||||
assert parsed["projects"] == []
|
||||
|
||||
first = text
|
||||
wrap._ensure_serena_dashboard_disabled()
|
||||
assert cfg.read_text() == first
|
||||
|
||||
|
||||
def test_serena_dashboard_disabled_ignores_nested_key(tmp_path, monkeypatch) -> None:
|
||||
"""A key nested under another mapping is not Serena's setting.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user