kdl icon indicating copy to clipboard operation
kdl copied to clipboard

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

Open zkat opened this issue 4 years ago • 2 comments

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.

zkat avatar Sep 28 '21 00:09 zkat

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).

CAD97 avatar Sep 28 '21 00:09 CAD97

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"

Lucretiel avatar Sep 29 '21 06:09 Lucretiel

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"

eugenesvk avatar May 25 '23 17:05 eugenesvk

This has been merged into the kdl-v2 branch

zkat avatar Dec 13 '23 05:12 zkat