prettier: No line breaks for very long arrays?
Just wanted to kick off a discussion about how Prettier prints long arrays. đ I think in some cases it might make sense to have Prettier format a little differently.
Letâs take the following as an example:
const userIds = [
941,
722,
312,
933,
841,
166,
843,
978,
436,
687,
269,
573,
913,
873,
748,
354,
153,
181,
947,
563,
324,
952,
885,
911,
698,
248,
764,
431,
811,
344,
855,
430,
396,
981,
594,
131,
489,
552,
116,
833,
521,
252,
503,
540,
893,
736,
969,
145,
925,
417,
344,
108,
161,
777,
971,
159,
158,
908,
511,
876,
768,
284,
640,
309,
651,
292,
898,
284,
201,
506,
654,
840,
334,
246,
376,
398,
714,
134,
435,
181,
980,
594,
396,
100,
743,
812,
583,
622,
800,
310,
111,
537,
751,
920,
872,
700,
702,
655,
515,
298
];
Lately, I find myself inserting // prettier-ignore
above arrays like this and adjusting the formatting manually to something like this:
// prettier-ignore
const userIds = [
941, 722, 312, 933, 841, 166, 843, 978, 436, 687, 269, 573, 913, 873, 748,
354, 153, 181, 947, 563, 324, 952, 885, 911, 698, 248, 764, 431, 811, 344,
855, 430, 396, 981, 594, 131, 489, 552, 116, 833, 521, 252, 503, 540, 893,
736, 969, 145, 925, 417, 344, 108, 161, 777, 971, 159, 158, 908, 511, 876,
768, 284, 640, 309, 651, 292, 898, 284, 201, 506, 654, 840, 334, 246, 376,
398, 714, 134, 435, 181, 980, 594, 396, 100, 743, 812, 583, 622, 800, 310,
111, 537, 751, 920, 872, 700, 702, 655, 515, 298
];
I think that in cases like this, it doesnât really help to have each item on a new line. It just makes the code harder to scroll through and harder to read. Line breaks definitely make sense for shorter arrays, but there could be some sort of a thresh-hold after which we could format differently.
What do you think?
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 54
- Comments: 41 (16 by maintainers)
@j-f1
I am into Competitive programming. I have solved 316 questions by now, check here - https://leetcode.com/pgmreddy/ Almost every question (more than 290+ questions) has large data input as test case. This is MUST simply because - Algorithms crack when we give large inputs, they segfault due to not enough memory, perform too slow in speed â These algo issues can only be seen with large inputs â Large inputs vary from 100 elements (combinatoric problems like factorial are even less like 15), but most of the most are a minimum of a 50,000 elements.
prettier-ignore
. Withprettier-ignore
I canât scroll the input to the end of the Editor, Editors hang / go very very slow, you can check the Notepad++, some editors only show ââŠâ which is useless. Only the 3rd solution which is format by line-width helps here, a few pages scroll will give what are some end values at test case, overall summary what is the input is.I strive hard that my solution passes on 1st attempt, I have not got a single error in
last 54 problems
submitted. If we want to be the best, we need to use the best tools, prettier is best tool, but with time data grows, prettier needs to catch up as we grow with time. Otherwise prettier wonât be in the good looks of best so we must improve the prettier to continue to be the best.I am algorithm guy, so I can write in bits and pieces of code for this part of change. Though I canât go thru a lengthy process, I can write an algo quickly, send a code review on a branch, etc. However, the owners need to guide and take ownership of review, improvements.
If we have doubt, let it be an experimental feature. A feature that can be enabled via settings. We can have "array width" only for line, this is even better. Someone can always match "line width" to "array width"
I am in, always. I love the tool.prettier-ignore
wont make sense, editors hang, crash, put a ââŠâ, scrolling issues, otherSeparate data file
wont help, there will betoo many files, multiple per problem
to say the least,tests will be scattered
, it is similar to another DLL hell, and others.Letâs work together, have âarray widthâ like âline widthâ, it a real problem, everyday. I am in to contribute, see above.
Given that these occur very rarely in any given codebase, I wonder if the implementation cost for this is worth the savings of one line before the array.
If you have many large arrays in your codebase, why not extract them to a config file?
What about:
Just idea.
Proposal: Arrays containing only numbers (< X characters) or only strings (length
<=
3) will wrap as shown in your second exampleThanks. I hope you are saying about below command of yours, from all the chat msgs above. ( If so, all good, let below command be last msg of this thread for easy retrieval, thank you for your contribution @thorn0 )
Well, these are common in my unittest code. In fact, these cases also reproduce on short arrays:
These arrays are short, but because printWidth is exceeded, the arrays are printed splitting every item per line, which looks ugly.
I proposal that if an array itself does not exceed printWidth, it can be printed on the next line like this:
@manoharreddyporeddy Nope, no ETA. As I wrote, if you urgently need it, Prettier supports direct installation of forks from GitHub.
@thorn0 that never even crossed my mind.
Brilliant! Thanks
@breck7 Itâs unlikely weâll do that for strings⊠but canât you use numbers instead of strings in that code (
"#130d34"
vs0x130d34
)? đ@breck7 See #10106. BTW you can install it directly from Git:
npm install thorn0/prettier#num-arrays
Among all the proposals from this discussion, https://github.com/prettier/prettier/issues/4210#issuecomment-376500821 seems to be the best tradeoff. A PR implementing it is very likely to be accepted.
I love the â// prettier-mode matrixâ suggestion from @evilebottnawi
We hit this issue in dozens of files across a pretty clean codebase. Would be great to have an option like that.
A test case example (canât put each test case into another config)
What is wrong?
How it looks with prettier-ignore
What I right?
Thatâs cool. But it doesnât explain why we canât configure it for arrays, or specifically for JSON as mentioned.
@j-f1 we should maybe check the length of the items and compare it to the preferred line length set by the user. I can imagine that sometimes printing each item on a new line is the only option:
lineLength: 10
lineLength: 15
Also not sure about what types of elements to support but I guess starting off with numbers and strings is pretty safe.
Thank you for movement on this. Any ETA on how soon this will be ready?
Most importantly and urgently, this kind of option would allow me to replace thousands of lines of code with a semantically identical couple hundred lines.
That would work wonders for my code golf scores.
Updated Playground link as well as code diff in this comment, I want config to setup the way it is visible in the left in playground / Input below.
Playground link
Input:
Output
Input:
Output
Input:
Output
Input:
Output
Thatâs not really arrays, but point taken. You might also be interested in #5995
Searching for this issue everywhere. I am facing the same issue, not just for arrays but for objects too like below
Expected:
Actual:
Looking for an option like:
breakByMatrix: true
so that when the array limit exceedsprintWidth
it shall make a break there and continue in the second line till reachingprintWidth
and so on. So that for large arrays/parameters, every line consists of utmost params ofprintWidth
The 80-character print width is not just about screen width â itâs also about how prose is often formatted in columns 60-70 characters wide (like the New York Times and these comment fields). While 80 chars has historical precedent, it can also be justified by adding space for several characters of indentation while still leaving a readable width for the actual code.
I was hunting for this issue đ
I think itâs super important to have an
arrayWidth
option operate independently of theprintWidth
value. As suggested elsewhere in this thread, itâs not always feasible to place anignore
declaration (aka, JSON files).This new option would allow
printWidth
to still control the total container width, but arrays can be broken up into newlines everyarrayWidth
characters.Here are two common examples that I run into that make Prettier (including 2.0) hard on the eyes:
Thanks, that was it. Sorry, I combed the docs before posting but I must have missed thisâŠeven though itâs the first listed option đ„