Skip to content

Conversation

zaneduffield
Copy link
Collaborator

This PR adds support for formatting the interior contents of multi-line strings.

A setting has been added to control the behaviour (format_multiline_strings), and it defaults to true. I have enabled this by default because I'm confident in the initial implementation, and we should continue with our stated goal of "format the entire file".

Before implementing this, I had to do some testing to confirm some of the fine-grained details.
My findings are asserted in this script, which can be summarised as

  1. leading (insignificant) whitespace is determined by the whitespace before the trailing quotes
    • any blank char (U+00 -> U+20) is valid leading whitespace (but U+00 is erroneously rejected by the compiler — reported in RSS-3970)
  2. a blank internal line can use any prefix of the leading whitespace
  3. internal line endings can be mixed, and a lone \r is considered to end the line
    • the compiler normalises the line separators, based on the {$TEXTBLOCK} directive, so it is safe to change them
  4. trailing whitespace is significant

With these findings, I implemented the normalisation as follows

  1. Indentation is normalised to align the trailing quote with the leading quote. Lines in the middle are adjusted to retain their original significant leading whitespace.
  2. Internal line endings are normalised to match the configured line ending.
  3. Invalid multiline strings are left untouched.

With the interior of multiline strings be mutated, the cursor relocation was broken when inside such tokens. This has been fixed, and the new algorithm will maintain the number of line breaks relative to the end of the token as well as the number of bytes from the end of the relevant line.

Closes #238

@zaneduffield zaneduffield requested a review from jgardn3r August 26, 2025 01:23
Copy link

codecov bot commented Aug 26, 2025

Codecov Report

❌ Patch coverage is 97.96610% with 6 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
core/src/lang.rs 92.10% 1 Missing and 2 partials ⚠️
core/src/rules/optimising_line_formatter/mod.rs 91.30% 1 Missing and 1 partial ⚠️
...les/optimising_line_formatter/multiline_strings.rs 98.52% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

@zaneduffield zaneduffield added this to the Format token contents milestone Aug 26, 2025
1. Indentation is normalised to align the trailing quote with the
   leading quote. Lines in the middle are adjusted to retain their
   original significant leading whitespace.
2. Internal line endings are normalised to match the configured line
   ending.
3. Invalid multiline strings are left untouched.
@zaneduffield zaneduffield force-pushed the multiline-strings-indent branch from 55e6b15 to b1b5795 Compare August 28, 2025 04:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Framework support for mutating token contents
1 participant