Skip to content

Indented line comments still introduce a paragraph break in diff output #16

Description

@sou1118

Summary

Follow-up to #9, which fixes paragraph splitting caused by line comments on their own line. The fix covers the common case, but an indented line comment still breaks the paragraph.

Reproduction

First sentence.
  // indented comment
Second sentence.

Typst compiles this source as one paragraph, but the generated diff output contains a whitespace-only line between the sentences:

First sentence.
␣␣
Second sentence.

Typst treats a line containing only whitespace as a paragraph break (verified against typst-syntax: "Alpha.\n \nBeta.\n" parses with a Parbreak node), so the compiled diff shows two paragraphs where the original had one.

Root cause

With an indented comment, the CST nodes around the dropped LineComment are Space("\n ") and Space("\n"). The guard added in #9 only skips a Space node whose text is exactly "\n" when the buffer already ends with '\n' — the first node ends the buffer with "\n " (trailing spaces, not a newline), so the second "\n" is appended and the output ends up with a whitespace-only line.

Suggested direction

Extend the skip condition to treat a buffer ending in '\n' plus trailing spaces/tabs as "already at a line break": when the next Space starts with '\n' and paragraph_buf.trim_end_matches([' ', '\t']).ends_with('\n'), truncate the trailing indent from the buffer and skip the newline. This also removes the dangling indent so no whitespace-only line can appear.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions