-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add basic attempt at local module handling
The idea here is to support a new formatting section, `module`, which is the module we're currently running in as a replacement for `prefix(module/we/are/running/in)`. Since this is dependent on the directory structure and where things are run, some tests have been added which run on a real module structure. This approach just focusses on the use-case of running `gci` from somewhere in a module's path. It does not, for example support: * Discovering modules with a nested structure * Discovering modules defined in a `go.work` file Signed-off-by: Matthew Hughes <matthewhughes934@gmail.com>
- Loading branch information
1 parent
19d84f7
commit 8546008
Showing
16 changed files
with
233 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
sections: | ||
- Standard | ||
- Default | ||
- Module |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module example.com/simple | ||
|
||
go 1.20 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package bar |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package foo | ||
|
||
import ( | ||
"example.com/simple/internal/bar" | ||
"log" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package foo | ||
|
||
import ( | ||
"log" | ||
|
||
"example.com/simple/internal/bar" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package internal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package lib | ||
|
||
import ( | ||
"golang.org/x/net" | ||
"example.com/simple/internal" | ||
"example.com/simple/internal/foo" | ||
"example.com/simple/internal/bar" | ||
"log" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package lib | ||
|
||
import ( | ||
"log" | ||
|
||
"golang.org/x/net" | ||
|
||
"example.com/simple/internal" | ||
"example.com/simple/internal/bar" | ||
"example.com/simple/internal/foo" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package section | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"golang.org/x/tools/go/packages" | ||
|
||
"github.com/daixiang0/gci/pkg/parse" | ||
"github.com/daixiang0/gci/pkg/specificity" | ||
) | ||
|
||
const ModuleType = "module" | ||
|
||
type Module struct { | ||
ModulePaths []string | ||
} | ||
|
||
func (m *Module) MatchSpecificity(spec *parse.GciImports) specificity.MatchSpecificity { | ||
for _, modPath := range m.ModulePaths { | ||
if strings.HasPrefix(spec.Path, modPath) { | ||
return specificity.Module{} | ||
} | ||
} | ||
|
||
return specificity.MisMatch{} | ||
} | ||
|
||
func (m *Module) String() string { | ||
return ModuleType | ||
} | ||
|
||
func (m *Module) Type() string { | ||
return ModuleType | ||
} | ||
|
||
// Populate populates m.ModulePaths by finding modules for files | ||
func (m *Module) Populate(files []string) error { | ||
packages, err := packages.Load( | ||
&packages.Config{Mode: packages.NeedModule}, files..., | ||
) | ||
if err != nil { | ||
return fmt.Errorf("failed to load local modules: %v", err) | ||
} | ||
|
||
uniqueModules := make(map[string]struct{}) | ||
|
||
for _, pkg := range packages { | ||
if len(pkg.Errors) != 0 { | ||
return fmt.Errorf("error while listing modules: %v", pkg.Errors[0]) | ||
} | ||
if _, ok := uniqueModules[pkg.Module.Path]; !ok { | ||
uniqueModules[pkg.Module.Path] = struct{}{} | ||
} | ||
} | ||
|
||
modPaths := make([]string, 0, len(uniqueModules)) | ||
for mod := range uniqueModules { | ||
modPaths = append(modPaths, mod) | ||
} | ||
|
||
m.ModulePaths = modPaths | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package specificity | ||
|
||
type Module struct{} | ||
|
||
func (m Module) IsMoreSpecific(than MatchSpecificity) bool { | ||
return isMoreSpecific(m, than) | ||
} | ||
|
||
func (m Module) Equal(to MatchSpecificity) bool { | ||
return equalSpecificity(m, to) | ||
} | ||
|
||
func (Module) class() specificityClass { | ||
return ModuleClass | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters