syntax
syntax copied to clipboard
Allow indentation in multiline strings
It was discussed before but I am afraid that adding this later might introduce a breaking change. So making an issue here so the choice can be made to implement it early or there is a solution that is non-breaking.
Context: if multiline strings cannot be indented, multiline strings are not very friendly to use at all, as generally code is indented.
Proposal:
let str = `
this is a multiline string
`
//^ the last backtick is determining the indent the string follows
// this string will actually be "this is a multiline string"
This means that in a multiline string there should always be a newline after the first backtick, with the last backtick indicating the indentation. Also pretty-printing can now shift strings in the right indentation.
Prior art is Elixir, Swift and Python, all adopting this style of multiline strings. Best docs here.
The only difference is that in some languages by default a newline is added on the last line. So that
let str = `
this is a multiline string
`
Will actually be this is a multiline string\n
(Elixir). I actually think this is not great because we can always explicitly add a newline if we want.
Of course
let str = `this is a single line string`
is still valid if the backticks are both on the same line.
Some ideas after a conversation with @jordwalke:
- Allowing multiline string as a way for documentation comments with an attribute. (I created a ocamldoc printer for markdown btw, this could allow documentation comments to be written in markdown).
@doc `
This is the documentation for this let binding
`
let a = "ah"
- We could allow prose-wrapping of strings (strings are automatically wrapped by the formatter to the line-width, but parsed as longer width string by the parser). This will also be great in combination with doc-comments.
let str = ``
This is a prose-wrapped string, it will automatically be formatted to the right line-width
``
- We could allow the same semantics in JSX (including prosewrapping)
<div>
hello this is a multiline string
${aString}
#{anElement}
</div>
More information in this Reason issue: https://github.com/facebook/reason/issues/2603
Adding a few more keywords for searchability: strip leading spaces from multiline strings.
Btw, in case it makes this easier to implement, I think this style would also be great:
let str =
// Leading backtick character determines how many leading spaces to trim.
`
This is a
multiline string.
`
The rescript-lang/syntax repo is obsolete and will be archived soon. If this issue is still relevant, please reopen in the compiler repo (https://github.com/rescript-lang/rescript-compiler) or comment here to ask for it to be moved. Thank you for your contributions.