Skip to content

Format multi-line string contents#270

Merged
zaneduffield merged 3 commits into
integrated-application-development:masterfrom
zaneduffield:multiline-strings-indent
Sep 29, 2025
Merged

Format multi-line string contents#270
zaneduffield merged 3 commits into
integrated-application-development:masterfrom
zaneduffield:multiline-strings-indent

Conversation

@zaneduffield

Copy link
Copy Markdown
Contributor

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
@codecov

codecov Bot commented Aug 26, 2025

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 98.23322% with 5 lines in your changes missing coverage. Please review.

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

📢 Thoughts on this report? Let us know!

@zaneduffield zaneduffield added this to the Format token contents milestone Aug 26, 2025
Comment thread core/src/rules/optimising_line_formatter/multiline_strings.rs
@zaneduffield zaneduffield force-pushed the multiline-strings-indent branch from 55e6b15 to b1b5795 Compare August 28, 2025 04:42
Comment thread core/CHANGELOG.md Outdated
Comment thread core/src/lang.rs Outdated
Comment thread core/src/lang.rs Outdated
Comment thread core/src/lang.rs Outdated
Comment thread core/src/defaults/reconstructor.rs Outdated
Comment thread core/CHANGELOG.md Outdated
Comment thread core/datatests/generators/optimising_line_formatter.rs
Comment thread core/datatests/generators/optimising_line_formatter.rs Outdated
Comment thread core/src/rules/optimising_line_formatter/multiline_strings.rs
Comment thread core/src/rules/optimising_line_formatter/multiline_strings.rs
@zaneduffield zaneduffield force-pushed the multiline-strings-indent branch from b1b5795 to 97cf22d Compare September 10, 2025 05:03
@zaneduffield zaneduffield force-pushed the multiline-strings-indent branch 2 times, most recently from 040b85f to ec60448 Compare September 17, 2025 00:48

@jgardn3r jgardn3r left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Getting really close, I think.

Comment thread core/src/defaults/reconstructor.rs Outdated
Comment thread core/src/rules/optimising_line_formatter/multiline_strings.rs Outdated
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 merged commit 9fb4f05 into integrated-application-development:master Sep 29, 2025
7 checks passed
@zaneduffield zaneduffield deleted the multiline-strings-indent branch September 29, 2025 05:14
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

2 participants