diff options
| author | Taras Madan <tarasmadan@google.com> | 2023-02-22 22:16:50 +0100 |
|---|---|---|
| committer | Taras Madan <tarasmadan@google.com> | 2023-02-24 12:47:23 +0100 |
| commit | 4165372ec8fd142475a4e35fd0cf4f8042132208 (patch) | |
| tree | 21cd62211b4dd80bee469054c5b65db77342333c /vendor/github.com/daixiang0 | |
| parent | 2b3ed821a493b8936c8bacfa6f8b4f1c90a00855 (diff) | |
dependencies: update
set go min requirements to 1.19
update dependencies
update vendor
Diffstat (limited to 'vendor/github.com/daixiang0')
5 files changed, 119 insertions, 40 deletions
diff --git a/vendor/github.com/daixiang0/gci/pkg/gci/gci.go b/vendor/github.com/daixiang0/gci/pkg/gci/gci.go index a8a64a278..7418db209 100644 --- a/vendor/github.com/daixiang0/gci/pkg/gci/gci.go +++ b/vendor/github.com/daixiang0/gci/pkg/gci/gci.go @@ -4,6 +4,7 @@ import ( "bytes" "errors" "fmt" + goFormat "go/format" "os" "sync" @@ -120,7 +121,7 @@ func LoadFormatGoFile(file io.FileObj, cfg config.Config) (src, dist []byte, err return src, src, nil } - imports, headEnd, tailStart, err := parse.ParseFile(src, file.Path()) + imports, headEnd, tailStart, cStart, cEnd, err := parse.ParseFile(src, file.Path()) if err != nil { if errors.Is(err, parse.NoImportError{}) { return src, src, nil @@ -129,7 +130,7 @@ func LoadFormatGoFile(file io.FileObj, cfg config.Config) (src, dist []byte, err } // do not do format if only one import - if len(imports) == 1 { + if len(imports) <= 1 { return src, src, nil } @@ -138,9 +139,6 @@ func LoadFormatGoFile(file io.FileObj, cfg config.Config) (src, dist []byte, err return nil, nil, err } - head := src[:headEnd] - tail := src[tailStart:] - firstWithIndex := true var body []byte @@ -158,13 +156,30 @@ func LoadFormatGoFile(file io.FileObj, cfg config.Config) (src, dist []byte, err } } - // remove breakline in the end - for body[len(body)-1] == utils.Linebreak { - body = body[:len(body)-1] + head := make([]byte, headEnd) + copy(head, src[:headEnd]) + tail := make([]byte, len(src)-tailStart) + copy(tail, src[tailStart:]) + + head = append(head, utils.Linebreak) + // ensure C + if cStart != 0 { + head = append(head, src[cStart:cEnd]...) + head = append(head, utils.Linebreak) } - if tail[0] != utils.Linebreak { - body = append(body, utils.Linebreak) + // add beginning of import block + head = append(head, `import (`...) + head = append(head, utils.Linebreak) + // add end of import block + body = append(body, []byte{utils.RightParenthesis, utils.Linebreak}...) + + log.L().Debug(fmt.Sprintf("head:\n%s", head)) + log.L().Debug(fmt.Sprintf("body:\n%s", body)) + if len(tail) > 20 { + log.L().Debug(fmt.Sprintf("tail:\n%s", tail[:20])) + } else { + log.L().Debug(fmt.Sprintf("tail:\n%s", tail)) } var totalLen int @@ -177,6 +192,11 @@ func LoadFormatGoFile(file io.FileObj, cfg config.Config) (src, dist []byte, err for _, s := range slices { i += copy(dist[i:], s) } + log.L().Debug(fmt.Sprintf("raw:\n%s", dist)) + dist, err = goFormat.Source(dist) + if err != nil { + return nil, nil, err + } return src, dist, nil } diff --git a/vendor/github.com/daixiang0/gci/pkg/parse/parse.go b/vendor/github.com/daixiang0/gci/pkg/parse/parse.go index d84ed1335..33d6e1705 100644 --- a/vendor/github.com/daixiang0/gci/pkg/parse/parse.go +++ b/vendor/github.com/daixiang0/gci/pkg/parse/parse.go @@ -8,6 +8,8 @@ import ( "strings" ) +const C = "\"C\"" + type GciImports struct { // original index of import group, include doc, name, path and comment Start, End int @@ -68,41 +70,96 @@ func getImports(imp *ast.ImportSpec) (start, end int, name string) { return } -func ParseFile(src []byte, filename string) (ImportList, int, int, error) { +func ParseFile(src []byte, filename string) (ImportList, int, int, int, int, error) { fileSet := token.NewFileSet() f, err := parser.ParseFile(fileSet, filename, src, parser.ParseComments) if err != nil { - return nil, 0, 0, err + return nil, 0, 0, 0, 0, err } if len(f.Imports) == 0 { - return nil, 0, 0, NoImportError{} + return nil, 0, 0, 0, 0, NoImportError{} } - var headEnd int - var tailStart int - - var data ImportList - for i, imp := range f.Imports { - start, end, name := getImports(imp) + var ( + // headEnd means the start of import block + headEnd int + // tailStart means the end + 1 of import block + tailStart int + // cStart means the start of C import block + cStart int + // cEnd means the end of C import block + cEnd int + data ImportList + ) - if i == 0 { - headEnd = start + for index, decl := range f.Decls { + switch decl.(type) { + // skip BadDecl and FuncDecl + case *ast.GenDecl: + genDecl := decl.(*ast.GenDecl) + + if genDecl.Tok == token.IMPORT { + // there are two cases, both end with linebreak: + // 1. + // import ( + // "xxxx" + // ) + // 2. + // import "xxx" + if headEnd == 0 { + headEnd = int(decl.Pos()) - 1 + } + tailStart = int(decl.End()) + + for _, spec := range genDecl.Specs { + imp := spec.(*ast.ImportSpec) + // there are only one C import block + // ensure C import block is the first import block + if imp.Path.Value == C { + /* + common case: + + // #include <png.h> + import "C" + + notice that decl.Pos() == genDecl.Pos() > genDecl.Doc.Pos() + */ + if genDecl.Doc != nil { + cStart = int(genDecl.Doc.Pos()) - 1 + // if C import block is the first, update headEnd + if index == 0 { + headEnd = cStart + } + } else { + /* + special case: + + import "C" + */ + cStart = int(decl.Pos()) - 1 + } + + cEnd = int(decl.End()) + + continue + } + + start, end, name := getImports(imp) + + data = append(data, &GciImports{ + Start: start, + End: end, + Name: name, + Path: strings.Trim(imp.Path.Value, `"`), + }) + } + } } - if i == len(f.Imports)-1 { - tailStart = end - } - - data = append(data, &GciImports{ - Start: start, - End: end, - Name: name, - Path: strings.Trim(imp.Path.Value, `"`), - }) } sort.Sort(data) - return data, headEnd, tailStart, nil + return data, headEnd, tailStart, cStart, cEnd, nil } // IsGeneratedFileByComment reports whether the source file is generated code. @@ -113,10 +170,11 @@ func IsGeneratedFileByComment(in string) bool { const ( genCodeGenerated = "code generated" genDoNotEdit = "do not edit" - genAutoFile = "autogenerated file" // easyjson + genAutoFile = "autogenerated file" // easyjson + genAutoGenerated = "automatically generated" // genny ) - markers := []string{genCodeGenerated, genDoNotEdit, genAutoFile} + markers := []string{genCodeGenerated, genDoNotEdit, genAutoFile, genAutoGenerated} in = strings.ToLower(in) for _, marker := range markers { if strings.Contains(in, marker) { diff --git a/vendor/github.com/daixiang0/gci/pkg/section/errors.go b/vendor/github.com/daixiang0/gci/pkg/section/errors.go index 1aa42ecdf..0a1209135 100644 --- a/vendor/github.com/daixiang0/gci/pkg/section/errors.go +++ b/vendor/github.com/daixiang0/gci/pkg/section/errors.go @@ -25,9 +25,9 @@ func (s SectionParsingError) Is(err error) bool { return ok } -var MissingParameterClosingBracketsError = fmt.Errorf("section parameter is missing closing %q", utils.ParameterClosingBrackets) +var MissingParameterClosingBracketsError = fmt.Errorf("section parameter is missing closing %q", utils.RightParenthesis) -var MoreThanOneOpeningQuotesError = fmt.Errorf("found more than one %q parameter start sequences", utils.ParameterClosingBrackets) +var MoreThanOneOpeningQuotesError = fmt.Errorf("found more than one %q parameter start sequences", utils.RightParenthesis) var SectionTypeDoesNotAcceptParametersError = errors.New("section type does not accept a parameter") diff --git a/vendor/github.com/daixiang0/gci/pkg/section/standard_list.go b/vendor/github.com/daixiang0/gci/pkg/section/standard_list.go index b5d884d37..62decfe1c 100644 --- a/vendor/github.com/daixiang0/gci/pkg/section/standard_list.go +++ b/vendor/github.com/daixiang0/gci/pkg/section/standard_list.go @@ -1,6 +1,6 @@ package section -// Code generated based on go1.18.4. DO NOT EDIT. +// Code generated based on go1.19.2. DO NOT EDIT. var standardPackages = map[string]struct{}{ "archive/tar": {}, @@ -67,6 +67,7 @@ var standardPackages = map[string]struct{}{ "go/build/constraint": {}, "go/constant": {}, "go/doc": {}, + "go/doc/comment": {}, "go/format": {}, "go/importer": {}, "go/parser": {}, diff --git a/vendor/github.com/daixiang0/gci/pkg/utils/constants.go b/vendor/github.com/daixiang0/gci/pkg/utils/constants.go index b1de2ea25..0e7cce757 100644 --- a/vendor/github.com/daixiang0/gci/pkg/utils/constants.go +++ b/vendor/github.com/daixiang0/gci/pkg/utils/constants.go @@ -4,8 +4,8 @@ const ( Indent = '\t' Linebreak = '\n' - SectionSeparator = ":" + Colon = ":" - ParameterOpeningBrackets = "(" - ParameterClosingBrackets = ")" + LeftParenthesis = '(' + RightParenthesis = ')' ) |
