Skip to content
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

group all standard library imports #8

Closed
sonatard opened this issue Apr 11, 2019 · 6 comments
Closed

group all standard library imports #8

sonatard opened this issue Apr 11, 2019 · 6 comments

Comments

@sonatard
Copy link

import (
	"github.com/b/xxxx"
	"context"

	"time"

	"github.com/a/xxx"
)

I hope following code after gofumports.

import (
	"context"
	"time"

	"github.com/a/xxx"
	"github.com/b/xxxx"
)
@mvdan
Copy link
Owner

mvdan commented Apr 11, 2019

Looks like this is being implemented in golang/go#20818. Since it's planned there, and since implementing this would be non-trivial, let's wait and see how that issue progresses.

@mvdan mvdan changed the title feature request : sort import gofumports: group all standard library imports Apr 11, 2019
@mvdan
Copy link
Owner

mvdan commented Apr 11, 2019

Hmm, if I do this only for standard library imports, that might be simple enough to implement on our side.

@mvdan mvdan changed the title gofumports: group all standard library imports group all standard library imports Apr 13, 2019
@mvdan
Copy link
Owner

mvdan commented Apr 13, 2019

I took a shot at this yesterday, but it's a nightmare to implement properly while keeping good position information. That is, without messing up empty lines or comments.

@sonatard
Copy link
Author

sonatard commented Apr 14, 2019

I think gofumports only delete empty lines in imports. exec goimports after gofumports.

  1. gofumpotrs delete empty lines.
import (
	"github.com/b/xxxx"
	"context"

	"time"

	"github.com/a/xxx"
)
import (
	"github.com/b/xxxx"
	"context"
	"time"
	"github.com/a/xxx"
)
  1. Exec goimports
import (
	"context"
	"time"

	"github.com/a/xxx"
	"github.com/b/xxxx"
)

@mvdan
Copy link
Owner

mvdan commented Apr 16, 2019

While that's an interesting idea, it wouldn't work; we'd also be clumping together non-std imports. For example, a common standard is to group together third-party imports by project or by purpose.

@mvdan mvdan closed this as completed in b7afc71 Apr 27, 2019
mvdan added a commit that referenced this issue Mar 18, 2020
If an empty line existed between any std imports, we would leave it
there. Instead, remove it.

To keep the rule from being too aggressive, treat imports that have
explicit names or comments as non-std.

Improves #8.
Updates #37.
@mvdan
Copy link
Owner

mvdan commented Mar 18, 2020

The heuristic here was a bit lax; I've made it a bit more aggressive. See the commit pushed to master just above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants