Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Oct 22, 2025

  • Explore repository structure and understand the issue
  • Locate the relevant formatting code in private/syntax-replacement.rkt
  • Run existing tests to understand current state
  • Create a test case that verifies the fix
  • Modify string-replacement-format to account for trailing text on single-line changes
  • Verify the fix with tests
  • Run broader test suite to ensure no regressions
  • Test with a realistic example to ensure the fix works as expected
  • Run full test suite to verify no regressions (862 tests passed)
  • Address code review feedback
  • Run security checks
  • Final verification
  • Add #lang resyntax/test test case as requested
  • Update test expectations to match formatter output

Summary

Fixed the issue where single-line code replacements would sometimes exceed the line length limit. The problem was in string-replacement-format which didn't account for trailing text after the replacement end position when calculating the allowed width for formatting.

Changes Made

  1. Added linemap import to access line position functions for detecting single-line replacements
  2. Modified string-replacement-format to calculate trailing text length for single-line replacements and subtract it from the allowed formatting width
  3. Added comprehensive tests including a realistic test case based on Automated Resyntax fixes herbie-fp/herbie#1391
  4. Added #lang resyntax/test test case for regression testing with expectations adjusted to match actual formatter output

How the Fix Works

The fix prevents line length violations by accounting for trailing text when calculating the allowed formatting width. When a replacement is on a single line, the formatter now:

  • Detects the trailing text length (e.g., closing brackets)
  • Reduces the allowed width accordingly
  • Either fits the code within the limit or breaks it across multiple lines

In the test case, the formatter correctly prevents the line from exceeding 102 characters by breaking the replacement across multiple lines when it cannot fit on one line with the trailing ].

Original prompt

This section details on the original issue you should resolve

<issue_title>One-line changes sometimes exceed line length</issue_title>
<issue_description>In herbie-fp/herbie#1391, the quasiquote-to-list rule made this suggestion:

-      [`(,(and (or '+ '- '* '/ 'and 'or) op) ,as ..2 ,b) `(,op ,(loop `(,op ,@as) env) ,(loop b env))]
+      [`(,(and (or '+ '- '* '/ 'and 'or) op) ,as ..2 ,b) (list op (loop `(,op ,@as) env) (loop b env))]

This rewrites a line that's 102 characters long into one that's 103 characters long, exceeding the standard Racket line length.

Resyntax has some logic to avoid this, but it fails in this case because for one-line changes it doesn't consider the trailing text after the replacement. That is, the replacement above gets formatted with fmt by seeing if the new code, (list op (loop `(,op ,@as) env) (loop b env)), fits within 102 - 57 = 45 characters because the replaced expression starts at column 57. This does fit, but it fails to consider the trailing ] added at the end of the expression. Resyntax's formatting logic for single-line changes should consider not only the start position of the replaced expression but also the quantity of same-line trailing characters after the replaced expression's end position.</issue_description>

Comments on the Issue (you are @copilot in this section)

Fixes #605


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI changed the title [WIP] Fix line length issue for one-line changes Fix single-line replacements exceeding line length by accounting for trailing text Oct 22, 2025
Copilot AI requested a review from jackfirth October 22, 2025 20:40
@jackfirth jackfirth marked this pull request as ready for review October 24, 2025 06:14
Copy link
Owner

@jackfirth jackfirth left a comment

Choose a reason for hiding this comment

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

Copy link
Owner

@jackfirth jackfirth left a comment

Choose a reason for hiding this comment

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

@jackfirth jackfirth merged commit e2217fd into master Oct 24, 2025
4 checks passed
@jackfirth jackfirth deleted the copilot/fix-line-length-issue branch October 24, 2025 08:41
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.

One-line changes sometimes exceed line length

2 participants