From 6705bdc69e1682982832ebea24e791581c44d0d2 Mon Sep 17 00:00:00 2001 From: Alexander M Pickering Date: Mon, 3 Aug 2026 21:52:33 -0500 Subject: Allow comments to have tabs Currently, comments need to be left-aligned, even if they appear in an indented block. change the comment form to strip the same whitespace as the first line so that comments can be indented without embedding indents in the stdin. --- tests/fixtures/c/indented.c | 13 +++++++++++++ tests/fixtures/c/indented_tabs.c | 7 +++++++ tests/helpers/common.bash | 33 ++++++++++++++++++++++++++++++--- tests/unit/extraction.bats | 40 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 90 insertions(+), 3 deletions(-) create mode 100644 tests/fixtures/c/indented.c create mode 100644 tests/fixtures/c/indented_tabs.c (limited to 'tests') diff --git a/tests/fixtures/c/indented.c b/tests/fixtures/c/indented.c new file mode 100644 index 0000000..0b7a9f6 --- /dev/null +++ b/tests/fixtures/c/indented.c @@ -0,0 +1,13 @@ +enum my_enum { + /* md + @name my_enum/one + My explanation of type_one + */ + type_one = 1; + /* md + @name my_enum/two + My explanation of type_two + relative indented line + */ + type_two = 2; +} diff --git a/tests/fixtures/c/indented_tabs.c b/tests/fixtures/c/indented_tabs.c new file mode 100644 index 0000000..5340f83 --- /dev/null +++ b/tests/fixtures/c/indented_tabs.c @@ -0,0 +1,7 @@ +enum e { + /* md + @name e/a + Tab-indented body + */ + A = 1; +} diff --git a/tests/helpers/common.bash b/tests/helpers/common.bash index 779cc96..12501bc 100644 --- a/tests/helpers/common.bash +++ b/tests/helpers/common.bash @@ -64,9 +64,18 @@ BEGIN{} /\*\// { if(in_comment) {print "EOF"} in_comment=0 + indent="" +} +in_comment { + line=\$0 + if (indent != "" && index(line, indent) == 1) { + line=substr(line, length(indent)+1) + } + print line } -in_comment {print} /\/\* .+/ && !/\*\// { + indent=\$0 + sub(/[^[:space:]].*/, "", indent) print "chunkfile=\$(mktemp -p ${cache}/chunks)" print "cat > \$chunkfile << \"EOF\"\\n@file " name ":" NR ; if(\$2) {\$1=""; print "@exec " \$0} @@ -83,9 +92,18 @@ BEGIN{} /\]\]/ { if(in_comment) {print "EOF"} in_comment=0 + indent="" +} +in_comment { + line=\$0 + if (indent != "" && index(line, indent) == 1) { + line=substr(line, length(indent)+1) + } + print line } -in_comment {print} /--\[\[ .+/ && !/\]\]$/ { + indent=\$0 + sub(/[^[:space:]].*/, "", indent) print "chunkfile=\$(mktemp -p ${cache}/chunks)" print "cat > \$chunkfile << \"EOF\"\\n@file " name ":" NR ; if(\$2) {\$1=""; print "@exec " \$0} @@ -103,9 +121,18 @@ BEGIN{} /'''/ { if(in_comment) {print "EOF"} in_comment=0 + indent="" +} +in_comment { + line=$0 + if (indent != "" && index(line, indent) == 1) { + line=substr(line, length(indent)+1) + } + print line } -in_comment {print} /'''.+/ && !/'''$/ { + indent=$0 + sub(/[^[:space:]].*/, "", indent) print "chunkfile=$(mktemp -p __PY_CACHE__/chunks)" print "cat > $chunkfile << \"EOF\"\n@file " name ":" NR ; renderer = $0 diff --git a/tests/unit/extraction.bats b/tests/unit/extraction.bats index 21c6784..492d4e5 100644 --- a/tests/unit/extraction.bats +++ b/tests/unit/extraction.bats @@ -91,6 +91,46 @@ teardown() { chunk_contains "$CACHE" "Second chunk content" } +@test "C: opening-tag indent is stripped from body lines" { + local prog chunk line + prog="$(awk_prog_c "$CACHE")" + run_extraction "$FIXTURES/c/indented.c" "$prog" "$CACHE" + assert_equal "$(chunk_count "$CACHE")" "2" + chunk_contains "$CACHE" "@name my_enum/one" + chunk_contains "$CACHE" "My explanation of type_one" + # Body lines must not retain the 4-space indent from before /* + if grep -rlE '^ @name |^ My explanation' "$CACHE/chunks" >/dev/null 2>&1; then + echo "FAILED: opener indent was left on body lines" >&2 + grep -rn . "$CACHE/chunks" >&2 || true + return 1 + fi + # Relative extra indent inside the comment is preserved after stripping opener indent + chunk=$(grep -rl "relative indented line" "$CACHE/chunks") + line=$(grep -F "relative indented line" "$chunk") + # shellcheck disable=SC2016 + if [ "$line" != "$(printf '\trelative indented line')" ]; then + echo "FAILED: expected relative tab indent to remain after strip" >&2 + printf 'got: %q\n' "$line" >&2 + cat "$chunk" >&2 + return 1 + fi +} + +@test "C: tab indent before opening tag is stripped from body lines" { + local prog chunk line + prog="$(awk_prog_c "$CACHE")" + run_extraction "$FIXTURES/c/indented_tabs.c" "$prog" "$CACHE" + assert_equal "$(chunk_count "$CACHE")" "1" + chunk=$(grep -rl "Tab-indented body" "$CACHE/chunks") + line=$(grep -F "Tab-indented body" "$chunk") + if [ "$line" != "Tab-indented body" ]; then + echo "FAILED: expected tab opener indent stripped from body" >&2 + printf 'got: %q\n' "$line" >&2 + cat "$chunk" >&2 + return 1 + fi +} + # ── Lua (--[[ ... ]]) ────────────────────────────────────────────────────── @test "Lua: single doc comment produces one chunk" { -- cgit v1.2.3-70-g09d2