Skip to content
This repository has been archived by the owner on Jun 15, 2023. It is now read-only.

Allow indentation in multiline strings #35

Closed
jfrolich opened this issue Jul 9, 2020 · 5 comments
Closed

Allow indentation in multiline strings #35

jfrolich opened this issue Jul 9, 2020 · 5 comments
Labels
enhancement New feature or request stale

Comments

@jfrolich
Copy link

jfrolich commented Jul 9, 2020

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
Copy link
Author

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
Copy link
Author

More information in this Reason issue: reasonml/reason#2603

@yawaramin
Copy link

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

@yawaramin
Copy link

yawaramin commented Apr 8, 2021

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

@ryyppy ryyppy added bug Something isn't working enhancement New feature or request and removed bug Something isn't working labels May 2, 2022
@stale
Copy link

stale bot commented May 28, 2023

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 stale bot added the stale label May 28, 2023
@stale stale bot closed this as completed Jun 12, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request stale
Projects
None yet
Development

No branches or pull requests

3 participants