diff --git a/pkg/constants/sequences.go b/pkg/constants/sequences.go index 512d6d1..faf90d8 100644 --- a/pkg/constants/sequences.go +++ b/pkg/constants/sequences.go @@ -1,9 +1,11 @@ package constants const ( - CommentFlag = "//" - ImportStartFlag = "\nimport (\n" - ImportEndFlag = "\n)" + LineCommentFlag = "//" + BlockCommentStartFlag = "/*" + BlockCommentEndFlag = "*/" + ImportStartFlag = "\nimport (\n" + ImportEndFlag = "\n)" Blank = " " Indent = "\t" diff --git a/pkg/gci/imports/import.go b/pkg/gci/imports/import.go index f9229cb..2e87ca0 100644 --- a/pkg/gci/imports/import.go +++ b/pkg/gci/imports/import.go @@ -56,7 +56,7 @@ func (i ImportDef) String() string { func (i ImportDef) Format(cfg configuration.FormatterConfiguration) string { linePrefix := constants.Indent var output string - if cfg.NoPrefixComments == false { + if cfg.NoPrefixComments == false || i.QuotedPath == `"C"` { for _, prefixComment := range i.PrefixComment { output += linePrefix + prefixComment + constants.Linebreak } diff --git a/pkg/gci/internal/testdata/cgo-block-mixed-with-content.cfg.yaml b/pkg/gci/internal/testdata/cgo-block-mixed-with-content.cfg.yaml new file mode 100644 index 0000000..68c1f75 --- /dev/null +++ b/pkg/gci/internal/testdata/cgo-block-mixed-with-content.cfg.yaml @@ -0,0 +1 @@ +no-prefixComments: true diff --git a/pkg/gci/internal/testdata/cgo-block-mixed-with-content.in.go b/pkg/gci/internal/testdata/cgo-block-mixed-with-content.in.go new file mode 100644 index 0000000..525aed5 --- /dev/null +++ b/pkg/gci/internal/testdata/cgo-block-mixed-with-content.in.go @@ -0,0 +1,6 @@ +package main + +import ( + /* #include "types.h" + #include "other.h" */"C" +) diff --git a/pkg/gci/internal/testdata/cgo-block-mixed-with-content.out.go b/pkg/gci/internal/testdata/cgo-block-mixed-with-content.out.go new file mode 100644 index 0000000..24840a6 --- /dev/null +++ b/pkg/gci/internal/testdata/cgo-block-mixed-with-content.out.go @@ -0,0 +1,9 @@ +package main + +import ( + /* + #include "types.h" + #include "other.h" + */ + "C" +) diff --git a/pkg/gci/internal/testdata/cgo-block-mixed.cfg.yaml b/pkg/gci/internal/testdata/cgo-block-mixed.cfg.yaml new file mode 100644 index 0000000..68c1f75 --- /dev/null +++ b/pkg/gci/internal/testdata/cgo-block-mixed.cfg.yaml @@ -0,0 +1 @@ +no-prefixComments: true diff --git a/pkg/gci/internal/testdata/cgo-block-mixed.in.go b/pkg/gci/internal/testdata/cgo-block-mixed.in.go new file mode 100644 index 0000000..16448b7 --- /dev/null +++ b/pkg/gci/internal/testdata/cgo-block-mixed.in.go @@ -0,0 +1,6 @@ +package main + +import ( + /* #include "types.h" + */"C" +) diff --git a/pkg/gci/internal/testdata/cgo-block-mixed.out.go b/pkg/gci/internal/testdata/cgo-block-mixed.out.go new file mode 100644 index 0000000..864b6e3 --- /dev/null +++ b/pkg/gci/internal/testdata/cgo-block-mixed.out.go @@ -0,0 +1,8 @@ +package main + +import ( + /* + #include "types.h" + */ + "C" +) diff --git a/pkg/gci/internal/testdata/cgo-block-prefix.cfg.yaml b/pkg/gci/internal/testdata/cgo-block-prefix.cfg.yaml new file mode 100644 index 0000000..68c1f75 --- /dev/null +++ b/pkg/gci/internal/testdata/cgo-block-prefix.cfg.yaml @@ -0,0 +1 @@ +no-prefixComments: true diff --git a/pkg/gci/internal/testdata/cgo-block-prefix.in.go b/pkg/gci/internal/testdata/cgo-block-prefix.in.go new file mode 100644 index 0000000..fa6bac4 --- /dev/null +++ b/pkg/gci/internal/testdata/cgo-block-prefix.in.go @@ -0,0 +1,5 @@ +package main + +import ( + /* #include "types.h" */ "C" +) diff --git a/pkg/gci/internal/testdata/cgo-block-prefix.out.go b/pkg/gci/internal/testdata/cgo-block-prefix.out.go new file mode 100644 index 0000000..864b6e3 --- /dev/null +++ b/pkg/gci/internal/testdata/cgo-block-prefix.out.go @@ -0,0 +1,8 @@ +package main + +import ( + /* + #include "types.h" + */ + "C" +) diff --git a/pkg/gci/internal/testdata/cgo-block-single-line.cfg.yaml b/pkg/gci/internal/testdata/cgo-block-single-line.cfg.yaml new file mode 100644 index 0000000..68c1f75 --- /dev/null +++ b/pkg/gci/internal/testdata/cgo-block-single-line.cfg.yaml @@ -0,0 +1 @@ +no-prefixComments: true diff --git a/pkg/gci/internal/testdata/cgo-block-single-line.in.go b/pkg/gci/internal/testdata/cgo-block-single-line.in.go new file mode 100644 index 0000000..e26d6a6 --- /dev/null +++ b/pkg/gci/internal/testdata/cgo-block-single-line.in.go @@ -0,0 +1,6 @@ +package main + +import ( + /* #include "types.h" */ + "C" +) diff --git a/pkg/gci/internal/testdata/cgo-block-single-line.out.go b/pkg/gci/internal/testdata/cgo-block-single-line.out.go new file mode 100644 index 0000000..864b6e3 --- /dev/null +++ b/pkg/gci/internal/testdata/cgo-block-single-line.out.go @@ -0,0 +1,8 @@ +package main + +import ( + /* + #include "types.h" + */ + "C" +) diff --git a/pkg/gci/internal/testdata/cgo-block.cfg.yaml b/pkg/gci/internal/testdata/cgo-block.cfg.yaml new file mode 100644 index 0000000..68c1f75 --- /dev/null +++ b/pkg/gci/internal/testdata/cgo-block.cfg.yaml @@ -0,0 +1 @@ +no-prefixComments: true diff --git a/pkg/gci/internal/testdata/cgo-block.in.go b/pkg/gci/internal/testdata/cgo-block.in.go new file mode 100644 index 0000000..864b6e3 --- /dev/null +++ b/pkg/gci/internal/testdata/cgo-block.in.go @@ -0,0 +1,8 @@ +package main + +import ( + /* + #include "types.h" + */ + "C" +) diff --git a/pkg/gci/internal/testdata/cgo-block.out.go b/pkg/gci/internal/testdata/cgo-block.out.go new file mode 100644 index 0000000..864b6e3 --- /dev/null +++ b/pkg/gci/internal/testdata/cgo-block.out.go @@ -0,0 +1,8 @@ +package main + +import ( + /* + #include "types.h" + */ + "C" +) diff --git a/pkg/gci/internal/testdata/cgo-line.cfg.yaml b/pkg/gci/internal/testdata/cgo-line.cfg.yaml new file mode 100644 index 0000000..68c1f75 --- /dev/null +++ b/pkg/gci/internal/testdata/cgo-line.cfg.yaml @@ -0,0 +1 @@ +no-prefixComments: true diff --git a/pkg/gci/internal/testdata/cgo-line.in.go b/pkg/gci/internal/testdata/cgo-line.in.go new file mode 100644 index 0000000..a5e367a --- /dev/null +++ b/pkg/gci/internal/testdata/cgo-line.in.go @@ -0,0 +1,6 @@ +package main + +import ( + // #include "types.h" + "C" +) diff --git a/pkg/gci/internal/testdata/cgo-line.out.go b/pkg/gci/internal/testdata/cgo-line.out.go new file mode 100644 index 0000000..a5e367a --- /dev/null +++ b/pkg/gci/internal/testdata/cgo-line.out.go @@ -0,0 +1,6 @@ +package main + +import ( + // #include "types.h" + "C" +) diff --git a/pkg/gci/internal/testdata/cgo-multiline.cfg.yaml b/pkg/gci/internal/testdata/cgo-multiline.cfg.yaml new file mode 100644 index 0000000..68c1f75 --- /dev/null +++ b/pkg/gci/internal/testdata/cgo-multiline.cfg.yaml @@ -0,0 +1 @@ +no-prefixComments: true diff --git a/pkg/gci/internal/testdata/cgo-multiline.in.go b/pkg/gci/internal/testdata/cgo-multiline.in.go new file mode 100644 index 0000000..171bee5 --- /dev/null +++ b/pkg/gci/internal/testdata/cgo-multiline.in.go @@ -0,0 +1,7 @@ +package main + +import ( + // #include "types.h" + // #include "other.h" + "C" +) diff --git a/pkg/gci/internal/testdata/cgo-multiline.out.go b/pkg/gci/internal/testdata/cgo-multiline.out.go new file mode 100644 index 0000000..171bee5 --- /dev/null +++ b/pkg/gci/internal/testdata/cgo-multiline.out.go @@ -0,0 +1,7 @@ +package main + +import ( + // #include "types.h" + // #include "other.h" + "C" +) diff --git a/pkg/gci/parse.go b/pkg/gci/parse.go index 2ab183d..df31005 100644 --- a/pkg/gci/parse.go +++ b/pkg/gci/parse.go @@ -10,25 +10,60 @@ import ( // Recursively parses import lines into a list of ImportDefs func parseToImportDefinitions(unformattedLines []string) ([]importPkg.ImportDef, error) { newImport := importPkg.ImportDef{} + inBlockComment := false for index, unformattedLine := range unformattedLines { line := strings.TrimSpace(unformattedLine) if line == "" { //empty line --> starts a new import return parseToImportDefinitions(unformattedLines[index+1:]) } - if strings.HasPrefix(line, constants.CommentFlag) { + if strings.HasPrefix(line, constants.LineCommentFlag) { // comment line newImport.PrefixComment = append(newImport.PrefixComment, line) continue } + + if blockCommentStartsOnThisLine := strings.HasPrefix(line, constants.BlockCommentStartFlag); inBlockComment || blockCommentStartsOnThisLine { + blockCommentEndIndex := strings.Index(line, constants.BlockCommentEndFlag) + blockCommentEndsOnThisLine := blockCommentEndIndex != -1 + contentStartsAtIndex := 0 + contentEndsAtIndex := len(line) + + if blockCommentStartsOnThisLine { + newImport.PrefixComment = append(newImport.PrefixComment, constants.BlockCommentStartFlag) + contentStartsAtIndex = len(constants.BlockCommentStartFlag) + } + + if blockCommentEndsOnThisLine { + contentEndsAtIndex = blockCommentEndIndex + } + + if content := strings.TrimSpace(line[contentStartsAtIndex:contentEndsAtIndex]); content != "" { + newImport.PrefixComment = append(newImport.PrefixComment, "\t"+content) + } + + inBlockComment = !blockCommentEndsOnThisLine + + if !blockCommentEndsOnThisLine { + continue + } + + newImport.PrefixComment = append(newImport.PrefixComment, constants.BlockCommentEndFlag) + line = line[blockCommentEndIndex+len(constants.BlockCommentEndFlag):] + + if line == "" { + continue + } + } + // split inline comment from import - importSegments := strings.SplitN(line, constants.CommentFlag, 2) + importSegments := strings.SplitN(line, constants.LineCommentFlag, 2) switch len(importSegments) { case 1: // no inline comment case 2: // inline comment present - newImport.InlineComment = constants.CommentFlag + importSegments[1] + newImport.InlineComment = constants.LineCommentFlag + importSegments[1] default: return nil, InvalidImportSplitError{importSegments} }