syntax icon indicating copy to clipboard operation
syntax copied to clipboard

Allow indentation in multiline strings

Open jfrolich opened this issue 4 years ago • 4 comments

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.

jfrolich avatar Jul 09 '20 06:07 jfrolich

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>

jfrolich avatar Jul 10 '20 04:07 jfrolich

More information in this Reason issue: https://github.com/facebook/reason/issues/2603

jfrolich avatar Jul 10 '20 06:07 jfrolich

Adding a few more keywords for searchability: strip leading spaces from multiline strings.

yawaramin avatar Apr 08 '21 18:04 yawaramin

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

yawaramin avatar Apr 08 '21 21:04 yawaramin

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.

stale[bot] avatar May 28 '23 19:05 stale[bot]