-
Notifications
You must be signed in to change notification settings - Fork 731
Fix incorrect formatting for comments inside multi-line argument lists and method chains #1929
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix incorrect formatting for comments inside multi-line argument lists and method chains #1929
Conversation
…guments Co-authored-by: jakebailey <[email protected]>
…thod chains Co-authored-by: jakebailey <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR fixes a formatting bug where comments inside multi-line argument lists and chained method calls were incorrectly losing their indentation. The fix aligns the Go port with the TypeScript reference implementation by checking and using the token-specific indentation value when available.
Key changes:
- Added logic to use
tokenIndentationwhen it's not-1(unknown) ingetIndentationForComment - Added two comprehensive test cases verifying comment indentation is preserved in multi-line argument lists and method chains
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| internal/format/span.go | Added check to use token-specific indentation for comments when available |
| internal/format/comment_test.go | Added tests for comment formatting in multi-line argument lists and chained method calls |
…call) Co-authored-by: jakebailey <[email protected]>
Problem
The formatter was incorrectly removing indentation from comments inside multi-line argument lists and chained method calls. This caused comments to be flush with the left margin instead of maintaining their proper indentation level, and in some cases caused a panic with "strings: negative Repeat count".
Example 1: Multi-line argument list
Example 2: Chained method calls
Root Cause
The
getIndentationForCommentfunction ininternal/format/span.gowas not using thetokenIndentationparameter passed to it. This parameter contains the calculated indentation for the token that follows the comment. When available (not-1), it should be used instead of the base indentation level.The TypeScript reference implementation at
_submodules/TypeScript/src/services/formatting/formatting.ts:704includes this check:But the Go port was missing this logic, always returning the base indentation regardless of the token-specific indentation. When
tokenIndentationwas-1, it was passed directly to the indentation string generation, causing a panic instrings.Repeat.Solution
Added a check to use
tokenIndentationwhen it's not-1(the "unknown" sentinel value):This ensures that comments inherit the indentation of the token they precede, maintaining consistent formatting within multi-line structures.
Testing
Fixes #1927
Fixes #1928
Fixes #1925
Original prompt
Fixes #1927
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.