Skip to content

Commit

Permalink
Fix issue parsing block comments, and always retain prefix comments f…
Browse files Browse the repository at this point in the history
…or cgo imports. (#49)

* Fix "Error: an error occured while trying to parse imports: path is missing starting quotes" when parsing a file with block comments.

Fixes #48.

Note that this only handles simple cases for block comments, and does
not cover more complex cases (see FIXME in parse.go).

Signed-off-by: Charles Korn <[email protected]>

* Make block comment parsing more robust to different formatting.

Signed-off-by: Charles Korn <[email protected]>

* Add test case for multiple comments before a "C" import.

Signed-off-by: Charles Korn <[email protected]>
  • Loading branch information
charleskorn committed Mar 3, 2022
1 parent 76d765e commit ec40303
Show file tree
Hide file tree
Showing 24 changed files with 149 additions and 7 deletions.
8 changes: 5 additions & 3 deletions pkg/constants/sequences.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package constants

const (
CommentFlag = "//"
ImportStartFlag = "\nimport (\n"
ImportEndFlag = "\n)"
LineCommentFlag = "//"
BlockCommentStartFlag = "/*"
BlockCommentEndFlag = "*/"
ImportStartFlag = "\nimport (\n"
ImportEndFlag = "\n)"

Blank = " "
Indent = "\t"
Expand Down
2 changes: 1 addition & 1 deletion pkg/gci/imports/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
no-prefixComments: true
6 changes: 6 additions & 0 deletions pkg/gci/internal/testdata/cgo-block-mixed-with-content.in.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package main

import (
/* #include "types.h"
#include "other.h" */"C"
)
9 changes: 9 additions & 0 deletions pkg/gci/internal/testdata/cgo-block-mixed-with-content.out.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package main

import (
/*
#include "types.h"
#include "other.h"
*/
"C"
)
1 change: 1 addition & 0 deletions pkg/gci/internal/testdata/cgo-block-mixed.cfg.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
no-prefixComments: true
6 changes: 6 additions & 0 deletions pkg/gci/internal/testdata/cgo-block-mixed.in.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package main

import (
/* #include "types.h"
*/"C"
)
8 changes: 8 additions & 0 deletions pkg/gci/internal/testdata/cgo-block-mixed.out.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package main

import (
/*
#include "types.h"
*/
"C"
)
1 change: 1 addition & 0 deletions pkg/gci/internal/testdata/cgo-block-prefix.cfg.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
no-prefixComments: true
5 changes: 5 additions & 0 deletions pkg/gci/internal/testdata/cgo-block-prefix.in.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package main

import (
/* #include "types.h" */ "C"
)
8 changes: 8 additions & 0 deletions pkg/gci/internal/testdata/cgo-block-prefix.out.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package main

import (
/*
#include "types.h"
*/
"C"
)
1 change: 1 addition & 0 deletions pkg/gci/internal/testdata/cgo-block-single-line.cfg.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
no-prefixComments: true
6 changes: 6 additions & 0 deletions pkg/gci/internal/testdata/cgo-block-single-line.in.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package main

import (
/* #include "types.h" */
"C"
)
8 changes: 8 additions & 0 deletions pkg/gci/internal/testdata/cgo-block-single-line.out.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package main

import (
/*
#include "types.h"
*/
"C"
)
1 change: 1 addition & 0 deletions pkg/gci/internal/testdata/cgo-block.cfg.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
no-prefixComments: true
8 changes: 8 additions & 0 deletions pkg/gci/internal/testdata/cgo-block.in.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package main

import (
/*
#include "types.h"
*/
"C"
)
8 changes: 8 additions & 0 deletions pkg/gci/internal/testdata/cgo-block.out.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package main

import (
/*
#include "types.h"
*/
"C"
)
1 change: 1 addition & 0 deletions pkg/gci/internal/testdata/cgo-line.cfg.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
no-prefixComments: true
6 changes: 6 additions & 0 deletions pkg/gci/internal/testdata/cgo-line.in.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package main

import (
// #include "types.h"
"C"
)
6 changes: 6 additions & 0 deletions pkg/gci/internal/testdata/cgo-line.out.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package main

import (
// #include "types.h"
"C"
)
1 change: 1 addition & 0 deletions pkg/gci/internal/testdata/cgo-multiline.cfg.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
no-prefixComments: true
7 changes: 7 additions & 0 deletions pkg/gci/internal/testdata/cgo-multiline.in.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

import (
// #include "types.h"
// #include "other.h"
"C"
)
7 changes: 7 additions & 0 deletions pkg/gci/internal/testdata/cgo-multiline.out.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

import (
// #include "types.h"
// #include "other.h"
"C"
)
41 changes: 38 additions & 3 deletions pkg/gci/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
}
Expand Down

0 comments on commit ec40303

Please sign in to comment.