mago icon indicating copy to clipboard operation
mago copied to clipboard

Add a way to disable `print_width`

Open lyrixx opened this issue 8 months ago • 7 comments

🚀 Describe the Feature

Mago "optimize" line length via print_width parameter. There is something similar with JS/TS and I absolutely hate it. It keeps editing my code, and pollute my diff because I add or removed a parameter. I want to controller if I need a \n or not.

✅ Use Case / Example

-    #[Route('/user/password-forgot',                                                                                                                                                       
-        name: 'user_password_forgot',                                                                                                                                                      
-        methods: ['GET', 'POST'],                                                                                                                                                          
-    )]                                                                                                                                                                                     
+    #[Route('/user/password-forgot', name: 'user_password_forgot', methods: ['GET', 'POST'])]   

💡 Proposed Solution

Allow false

📌 Priority

Must have

📝 Additional Context

No response

lyrixx avatar Jun 16 '25 09:06 lyrixx

This is currently not possible, and i don't think we will add more format configuration options, as we are aiming for less.

I think what you are looking for is the preserve_* options, you can tell mago to preserve some things to the way they are, meaning even if they fit into a single line, keep them in multiple lines.

See: https://docs.rs/mago-formatter/latest/mago_formatter/settings/struct.FormatSettings.html#structfield.preserve_breaking_argument_list

azjezz avatar Jun 16 '25 10:06 azjezz

Note: a lot of behaviors that php-cs-fixer and phpcs have can't be replicated in mago because mago formatter doesn't "fix" code, it parses the file, and rewrites the ast from scratch using the settings, without taking how the code looked like previously into consideration, the only exception for this is the "preserve" settings, in which mago will look at the code previously and check if it contains line breaks at specific positions.

azjezz avatar Jun 16 '25 10:06 azjezz

@azjezz It would be awesome to reconsider this, because it has been the source of many of my own issues.

I get how great it is to just think about the formatter as a process that cleanly rewrite an AST, but as a developer, it brings a lot of friction when new lines get added unnecessarily...

The preserve_* options have improved the experience in this regard, but I still regularly get cases where this happens (eg. https://github.com/carthage-software/mago/issues/205, https://github.com/carthage-software/mago/issues/207, https://github.com/carthage-software/mago/issues/197) 😔

innocenzi avatar Jun 16 '25 11:06 innocenzi

I simply just set the print_width to a large number such as

print_width = 2147483647

trymeouteh avatar Jul 10 '25 19:07 trymeouteh

@trymeouteh this works generally correctly but sometimes Mago would just have really long lines then, even though it should ideally split them

innocenzi avatar Jul 10 '25 20:07 innocenzi

My use-case for varied print-width requirements is when it comes to auto-formatting arrays as tables (which is an amazing feature btw), but can result in tables not being well formatted when they exceed the print width. In my case I'd like to somehow ignore or re-specify the print-width for these instances.

rodnaph avatar Sep 19 '25 10:09 rodnaph

@rodnaph you can probably open issues with your specific cases so @azjezz can find heuristics to format them properly in those situations

innocenzi avatar Sep 19 '25 11:09 innocenzi

This is going to be closed. as we do not plan on ever removing this setting.

azjezz avatar Dec 17 '25 07:12 azjezz