kdl-org / kdl

the kdl document language specifications
https://kdl.dev
Other
1.1k stars 61 forks source link

`\` at EOL should escape all whitespace after it and treat it as a single one #213

Closed zkat closed 9 months ago

zkat commented 2 years ago

in one of their streams, @lucretiel pointed out that Rust strings let you do this:

let foo = "x\
    y\
    z\
    ";

I think this is a GREAT compromise for not automatically collecting indentation? I think we should do this too.

My one question is that I'm not 100% sure what Rust itself does, but I think whatever that is, we should probably just copy it.

CAD97 commented 2 years ago

Rust escapes all whitespace until the next non-whitespace character.

Specifically,

fn main() {
    dbg!("x\
    y\
    z\
    ");
}
[src/main.rs:2] "x\
    y\
    z\
    " = "xyz"

In strings, this makes sense, but outside of strings, specifically for the purpose of KDL, I think treating \\\n\p{White_Space}*regex as one unit of whitespace (not linespace) is better from a practicality of use standpoint (no splitting lexer atoms over multiple lines).

Lucretiel commented 2 years ago

Just to be clear, this proposal only applies to quoted string literals in KDL; it doesn't change the behavior of linespace / splitting nodes over multiple lines or anything about how KDL handles syntactic whitespace.

The Rust behavior is specifically that a backslash preceding a line break (newline or CRLF) consumes itself, the line break, and all succeeding literal whitespace at the beginning of the next line. I'd propose the simpler, more general rule, which is simply that a backslash consume all literal whitespace that succeeds it. In either cases, they notably only consume literal whitespace; they do not consume escaped whitespace. This means we can write things like:

node "\
    Header\n\
    \tIndented\n\
    \tIndented again\
"

This is exactly equivalent to:

node "Header
\tIndented
\tIndented again"
eugenesvk commented 1 year ago

Would a better rule be to consume only the same amount of whitespace, so to cut out the prefix whitespace in vertically aligned columns, but also allow non-prefix whitespace at line beginning

node "\
  Header\n\
    - item 1 with 2-spaces indent\n\
    - item 2 with 2-spaces indent\n\
  Continue\
"
node "Header
  - item 1 with 2-spaces indent
  - item 2 with 2-spaces indent
Continue"
zkat commented 9 months ago

This has been merged into the kdl-v2 branch