formatter aggressively reformats externs into a mess
background: https://forum.rescript-lang.org/t/my-main-gripes-with-rescript-constructive-criticism/6981/5
right now, the formatter is ruthless in formatting. this is usually appreciated, but for externs, it becomes messy sometimes.
imagine a file such as
@module("moduleX") external f1: someType1 => someType2 = "someFunc"
@module("moduleX") external f2: anotherType1 => thirdType = "someOtherFunc"
@module("moduleX") external f3: interestingType1 => interestingType2 = "someFuncWithALongerName"
@module("moduleX") external f4: t1 => t2 = "anotherFunc"
the formatter reformats it to:
@module("moduleX") external f1: someType1 => someType2 = "someFunc"
@module("moduleX") external f2: anotherType1 => thirdType = "someOtherFunc"
@module("moduleX")
external f3: interestingType1 => interestingType2 = "someFuncWithALongerName"
@module("moduleX") external f4: t1 => t2 = "anotherFunc"
because of the different lengths. this makes the externs really difficult to parse easily by looking at them because there are often repetitions and variants in the functions, so keeping them all on the same format is easier despite some lines being a little longer than what the formatter prefers.
let's discuss potential solutions for this.
@shulhi do you think we can relax this a bit? I am not keen of adding a @@format("off") attribute.
@shulhi do you think we can relax this a bit? I am not keen of adding a
@@format("off")attribute.
I'll take a look.
@tx46 can I see your real world example of this?
@tx46 can I see your real world example of this?
i don't have any at hand but the example is real, because of the different line lengths they're wrapped differently which just looks like a complete mess. even though keeping the externs on one line each would make them too long, it would be way more readable.
I edited the initial example. Here's also playground link:
https://rescript-lang.org/try?version=v12.0.0-rc.2&module=esmodule&code=AIWw9gJgrgNgpgCgETmvAGkglAAjgDwBc4AnAOwEMYcAzARgC4cBnMEOAFQE8AHOOnAF4AfCzadecAExCcSVuwBiUMgGMkAKA2hIsRCl0ZseIqUrUaUphTJhCAC1Lc+AkTgcBLEhGdxZ88QB5B1JlNU0NAHpInFUYD1UAa1owEhAKQhwbCFjHJJwwKEyQnHiyPwAjOBgwAHdtVD1kRqNcAmJyKloAZiYPMg64ZkJ+gHNfV1F+weGx3xlBOQU4MNUAdQ8HAEEAGTAyUdIAOQp2CJ00fRa4TDbTTosAFiZCSfcFuRs7RxJVzSA
Another idea is to force print decorators on the line above the external keyword in general.
@decorator1 @decorator2 @decorator3
external f1: someType1 => someType2 = "someFunc"
I still sometimes think though this should be superseded by some real import syntax.
Another idea is to force print decorators on the line above the external keyword in general.
@decorator1 @decorator2 @decorator3 external f1: someType1 => someType2 = "someFunc"
I still sometimes think though this should be superseded by some real import syntax.
i wouldn't like that. the formatter is too aggressive. if i have 10 short externs, that would go from 10 lines to 30 (blank lines inbetween each extern if they are multi-line formatter, otherwise it's gonna be a mess). pointless. it sort of annoys me that the formatter thinks it knows better than i do for choices like these.
I edited the initial example. Here's also playground link:
https://rescript-lang.org/try?version=v12.0.0-rc.2&module=esmodule&code=AIWw9gJgrgNgpgCgETmvAGkglAAjgDwBc4AnAOwEMYcAzARgC4cBnMEOAFQE8AHOOnAF4AfCzadecAExCcSVuwBiUMgGMkAKA2hIsRCl0ZseIqUrUaUphTJhCAC1Lc+AkTgcBLEhGdxZ88QB5B1JlNU0NAHpInFUYD1UAa1owEhAKQhwbCFjHJJwwKEyQnHiyPwAjOBgwAHdtVD1kRqNcAmJyKloAZiYPMg64ZkJ+gHNfV1F+weGx3xlBOQU4MNUAdQ8HAEEAGTAyUdIAOQp2CJ00fRa4TDbTTosAFiZCSfcFuRs7RxJVzSA
This example looks fine to me when I reformat it. I like to see an example where it reformat this aggresively like mentioned by @tx46
I edited the initial example. Here's also playground link: https://rescript-lang.org/try?version=v12.0.0-rc.2&module=esmodule&code=AIWw9gJgrgNgpgCgETmvAGkglAAjgDwBc4AnAOwEMYcAzARgC4cBnMEOAFQE8AHOOnAF4AfCzadecAExCcSVuwBiUMgGMkAKA2hIsRCl0ZseIqUrUaUphTJhCAC1Lc+AkTgcBLEhGdxZ88QB5B1JlNU0NAHpInFUYD1UAa1owEhAKQhwbCFjHJJwwKEyQnHiyPwAjOBgwAHdtVD1kRqNcAmJyKloAZiYPMg64ZkJ+gHNfV1F+weGx3xlBOQU4MNUAdQ8HAEEAGTAyUdIAOQp2CJ00fRa4TDbTTosAFiZCSfcFuRs7RxJVzSA
This example looks fine to me when I reformat it. I like to see an example where it reformat this aggresively like mentioned by @tx46
it's not about the formatting per extern being bad or incorrect, it's that when you have many externs of different lengths, they will all be broken up differently
https://rescript-lang.org/try?version=v12.0.0-rc.2&module=esmodule&code=AIWw9gJgrgNgpgCgETmvAGkglAAjgDwBc4AnAOwEMYcAzARgC4cBnMEOAFQE8AHOOnAF4AfCzadecAExCcSVuwBiUMgGMkAKA2hIsRCl0ZseIqUrUaUphTJhCAC1Lc+AkTgQBLMoQA0OL77+3n4BId64gnKsANbMEDQAVszR0ewA8g6kymqa2qh6yPlGuATE5FS0AMxMAaRwzIReAOZxCTSxzM78QqK1JPWNZE1dMpHy4tmqAOoeyRAwbRDJMO3MMA4AggAyYEOkAHIU7Lk6aPpFcJglpuUWACxMhK6ihKNyNnaOJJOaQA
it's not about the formatting per extern being bad or incorrect, it's that when you have many externs of different lengths, they will all be broken up differently
https://rescript-lang.org/try?version=v12.0.0-rc.2&module=esmodule&code=AIWw9gJgrgNgpgCgETmvAGkglAAjgDwBc4AnAOwEMYcAzARgC4cBnMEOAFQE8AHOOnAF4AfCzadecAExCcSVuwBiUMgGMkAKA2hIsRCl0ZseIqUrUaUphTJhCAC1Lc+AkTgQBLMoQA0OL77+3n4BId64gnKsANbMEDQAVszR0ewA8g6kymqa2qh6yPlGuATE5FS0AMxMAaRwzIReAOZxCTSxzM78QqK1JPWNZE1dMpHy4tmqAOoeyRAwbRDJMO3MMA4AggAyYEOkAHIU7Lk6aPpFcJglpuUWACxMhK6ihKNyNnaOJJOaQA
I get what you mean now. It seems there's no effective way to address this within the formatter since it doesn't track the context of different lines.
In other formatters like Fourmolu or Ormolu (Haskell), there’s an option to disable the formatter either for an entire file or specific code regions. Perhaps we could consider adding something similar in the future. @fhammerschmidt
Even if you were tracking the context, you'd end up with one external taking up multiple lines more often than not because of the hard 100 character limit per line. Or you disable (or extend) the limit altogether for externals.
A @@formatter(disable) and @@formatter(enable) could help but I find it very ugly as well. However it's probably the only way to circumvent the "no formatter options" policy.
BTw: Technically you could today have a formatting script in your package.json that omits certain files for formatting, so on a per-file level, that feature kinda exists already.
Even if you were tracking the context, you'd end up with one external taking up multiple lines more often than not because of the hard 100 character limit per line. Or you disable (or extend) the limit altogether for externals.
A
@@formatter(disable)and@@formatter(enable)could help but I find it very ugly as well. However it's probably the only way to circumvent the "no formatter options" policy.BTw: Technically you could today have a formatting script in your package.json that omits certain files for formatting, so on a per-file level, that feature kinda exists already.
i would love to have something like // format: off and then // format: on again to be able to format sections as i like