gleam icon indicating copy to clipboard operation
gleam copied to clipboard

Formatter: Merge duplicate imports

Open lpil opened this issue 5 years ago • 5 comments

import a
import a.{one}
import a.{two}

Is formatted as

import a.{one, two}

Note imports with an as clause should not be merged.

lpil avatar Jul 15 '20 11:07 lpil

Hi, I'm interested in this issue.

If import statements are the following,

import a
import b
import a.{one}
import a.{two}

I guess we finally expect such as:

import a.{one, two}
import b

This requires a sort of import statements before the merge. Formatter doesn't sort them currently, so we should implement sort of import statements at first.

What do you think?

thara avatar Dec 17 '20 14:12 thara

Hey @thara! Thanks for helping with Gleam.

Since writing this issue the matter of formatting imports has got slightly more complicated because of a use case for grouping imports so that they can be documented. There's some discussion here https://github.com/gleam-lang/gleam/pull/860

I think to implement this we would need to implement this grouping logic first, which makes it a bigger job. Let me know if you would still be interested in this.

lpil avatar Dec 17 '20 16:12 lpil

Thanks @lpil, I read #860.

I understood we need to modify AST for grouping imports, i.e. update this language semantics and fix its parser. Sure, this may be a large job for me in my first contribution.

I think we should want to work on this issue after the language designer(you!) update this semantics and parser.

thara avatar Dec 18 '20 07:12 thara

I don't think there's anything to change in the parser or the AST as the information required to detect comments and line breaks is already preserve by the parser. This feature can be implemented purely within the formatter.

Good places to look at are how the formatter writes comments and additional empty lines in blocks.

lpil avatar Dec 18 '20 09:12 lpil

the AST as the information required to detect comments and line breaks

the formatter writes comments and additional empty lines in blocks.

Oh, I missed it. It seems like I can do it, so I will see it. Thanks!

thara avatar Dec 18 '20 10:12 thara

Just curious, was this closed intentionally or by accident? @lpil

inoas avatar Jul 01 '23 10:07 inoas

Intentionally, duplicates are no longer valid

lpil avatar Jul 01 '23 10:07 lpil

Oh that's great. Good!

inoas avatar Jul 02 '23 08:07 inoas