-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
feat: golines formatter #5432
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
Merged
Merged
feat: golines formatter #5432
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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,41 @@ | ||
| package golines | ||
|
|
||
| import ( | ||
| "github.com/golangci/golines" | ||
|
|
||
| "github.com/golangci/golangci-lint/pkg/config" | ||
| ) | ||
|
|
||
| const Name = "golines" | ||
|
|
||
| type Formatter struct { | ||
| shortener *golines.Shortener | ||
| } | ||
|
|
||
| func New(settings *config.GoLinesSettings) *Formatter { | ||
| options := golines.ShortenerConfig{} | ||
|
|
||
| if settings != nil { | ||
| options = golines.ShortenerConfig{ | ||
| MaxLen: settings.MaxLen, | ||
| TabLen: settings.TabLen, | ||
| KeepAnnotations: false, // debug | ||
| ShortenComments: settings.ShortenComments, | ||
| ReformatTags: settings.ReformatTags, | ||
| IgnoreGenerated: false, // handle globally | ||
| DotFile: "", // debug | ||
| ChainSplitDots: settings.ChainSplitDots, | ||
| BaseFormatterCmd: "fmt", // fake cmd | ||
| } | ||
| } | ||
|
|
||
| return &Formatter{shortener: golines.NewShortener(options)} | ||
| } | ||
|
|
||
| func (*Formatter) Name() string { | ||
| return Name | ||
| } | ||
|
|
||
| func (f *Formatter) Format(_ string, src []byte) ([]byte, error) { | ||
| return f.shortener.Shorten(src) | ||
| } | ||
This file contains hidden or 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 hidden or 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,28 @@ | ||
| package golines | ||
|
|
||
| import ( | ||
| "golang.org/x/tools/go/analysis" | ||
|
|
||
| "github.com/golangci/golangci-lint/pkg/config" | ||
| "github.com/golangci/golangci-lint/pkg/goanalysis" | ||
| "github.com/golangci/golangci-lint/pkg/goformatters" | ||
| golinesbase "github.com/golangci/golangci-lint/pkg/goformatters/golines" | ||
| "github.com/golangci/golangci-lint/pkg/golinters/internal" | ||
| ) | ||
|
|
||
| const linterName = "golines" | ||
|
|
||
| func New(settings *config.GoLinesSettings) *goanalysis.Linter { | ||
| a := goformatters.NewAnalyzer( | ||
| internal.LinterLogger.Child(linterName), | ||
| "Checks if code is formatted, and fixes long lines", | ||
| golinesbase.New(settings), | ||
| ) | ||
|
|
||
| return goanalysis.NewLinter( | ||
| a.Name, | ||
| a.Doc, | ||
| []*analysis.Analyzer{a}, | ||
| nil, | ||
| ).WithLoadMode(goanalysis.LoadModeSyntax) | ||
| } |
This file contains hidden or 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,19 @@ | ||
| package golines | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/golangci/golangci-lint/test/testshared/integration" | ||
| ) | ||
|
|
||
| func TestFromTestdata(t *testing.T) { | ||
| integration.RunTestdata(t) | ||
| } | ||
|
|
||
| func TestFix(t *testing.T) { | ||
| integration.RunFix(t) | ||
| } | ||
|
|
||
| func TestFixPathPrefix(t *testing.T) { | ||
| integration.RunFixPathPrefix(t) | ||
| } |
This file contains hidden or 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,83 @@ | ||
| //golangcitest:args -Egolines | ||
| //golangcitest:expected_exitcode 0 | ||
| package testdata | ||
|
|
||
| import "fmt" | ||
|
|
||
| var ( | ||
| abc = []string{"a really long string", "another really long string", "a third really long string", "a fourth really long string", fmt.Sprintf("%s %s %s %s >>>>> %s %s", "first argument", "second argument", "third argument", "fourth argument", "fifth argument", "sixth argument")} | ||
| ) | ||
|
|
||
| type MyStruct struct { | ||
| aLongProperty int `help:"This is a really long string for this property"` | ||
| anotherLongProperty int `help:"This is a really long string for this property, part 2"` | ||
| athirdLongProperty int `help:"This is a really long string for this property, part 3...."` | ||
| } | ||
|
|
||
| type MyInterface interface { | ||
| aReallyLongFunctionName(argument1 string, argument2 string, argument3 string, argument4 string, argument5 string, argument6 string) (string, error) | ||
| } | ||
|
|
||
| // Something here | ||
|
|
||
| // Another comment | ||
| // A third comment | ||
| // This is a really really long comment that needs to be split up into multiple lines. I don't know how easy it will be to do, but I think we can do it! | ||
| func longLine(aReallyLongName string, anotherLongName string, aThirdLongName string) (string, error) { | ||
| argument1 := "argument1" | ||
| argument2 := "argument2" | ||
| argument3 := "argument3" | ||
| argument4 := "argument4" | ||
|
|
||
| fmt.Printf("This is a really long string with a bunch of arguments: %s %s %s %s >>>>>>>>>>>>>>>>>>>>>>", argument1, argument2, argument3, argument4) | ||
| fmt.Printf("This is a short statement: %d %d %d", 1, 2, 3) | ||
|
|
||
| z := argument1 + argument2 + fmt.Sprintf("This is a really long statement that should be broken up %s %s %s", argument1, argument2, argument3) | ||
|
|
||
| fmt.Printf("This is a really long line that can be broken up twice %s %s", fmt.Sprintf("This is a really long sub-line that should be broken up more because %s %s", argument1, argument2), fmt.Sprintf("A short one %d", 3)) | ||
|
|
||
| fmt.Print("This is a function with a really long single argument. We want to see if it's properly split") | ||
|
|
||
| fmt.Println(z) | ||
|
|
||
| // This is a really long comment on an indented line. Do you think we can split it up or should we just leave it as is? | ||
| if argument4 == "5" { | ||
| return "", fmt.Errorf("a very long query with ID %d failed. Check Query History in AWS UI", 12341251) | ||
| } | ||
|
|
||
| go func() { | ||
| fmt.Printf("This is a really long line inside of a go routine call. It should be split if at all possible.") | ||
| }() | ||
|
|
||
| if "hello this is a big string" == "this is a small string" && "this is another big string" == "this is an even bigger string >>>" { | ||
| fmt.Print("inside if statement") | ||
| } | ||
|
|
||
| fmt.Println(map[string]string{"key1": "a very long value", "key2": "a very long value", "key3": "another very long value"}) | ||
|
|
||
| return "", nil | ||
| } | ||
|
|
||
| func shortFunc(a int, b int) error { | ||
| c := make(chan int) | ||
|
|
||
| for { | ||
| select { | ||
| case <-c: | ||
| switch a { | ||
| case 1: | ||
| return fmt.Errorf("This is a really long line that can be broken up twice %s %s", fmt.Sprintf("This is a really long sub-line that should be broken up more because %s %s", "xxxx", "yyyy"), fmt.Sprintf("A short one %d", 3)) | ||
| case 2: | ||
| } | ||
| } | ||
|
|
||
| break | ||
| } | ||
|
|
||
| if a > 5 { | ||
| panic(fmt.Sprintf(">>>>>>>>>>>>>>>>>>> %s %s %s %s", "really long argument", "another really long argument", "a third really long arguement", abc[1:2])) | ||
| } | ||
|
|
||
| return nil | ||
| // This is an end decoration | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.