Skip to content

Commit ec40303

Browse files
authored
Fix issue parsing block comments, and always retain prefix comments for 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]>
1 parent 76d765e commit ec40303

24 files changed

+149
-7
lines changed

pkg/constants/sequences.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package constants
22

33
const (
4-
CommentFlag = "//"
5-
ImportStartFlag = "\nimport (\n"
6-
ImportEndFlag = "\n)"
4+
LineCommentFlag = "//"
5+
BlockCommentStartFlag = "/*"
6+
BlockCommentEndFlag = "*/"
7+
ImportStartFlag = "\nimport (\n"
8+
ImportEndFlag = "\n)"
79

810
Blank = " "
911
Indent = "\t"

pkg/gci/imports/import.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func (i ImportDef) String() string {
5656
func (i ImportDef) Format(cfg configuration.FormatterConfiguration) string {
5757
linePrefix := constants.Indent
5858
var output string
59-
if cfg.NoPrefixComments == false {
59+
if cfg.NoPrefixComments == false || i.QuotedPath == `"C"` {
6060
for _, prefixComment := range i.PrefixComment {
6161
output += linePrefix + prefixComment + constants.Linebreak
6262
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
no-prefixComments: true
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package main
2+
3+
import (
4+
/* #include "types.h"
5+
#include "other.h" */"C"
6+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package main
2+
3+
import (
4+
/*
5+
#include "types.h"
6+
#include "other.h"
7+
*/
8+
"C"
9+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
no-prefixComments: true
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package main
2+
3+
import (
4+
/* #include "types.h"
5+
*/"C"
6+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package main
2+
3+
import (
4+
/*
5+
#include "types.h"
6+
*/
7+
"C"
8+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
no-prefixComments: true
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package main
2+
3+
import (
4+
/* #include "types.h" */ "C"
5+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package main
2+
3+
import (
4+
/*
5+
#include "types.h"
6+
*/
7+
"C"
8+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
no-prefixComments: true
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package main
2+
3+
import (
4+
/* #include "types.h" */
5+
"C"
6+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package main
2+
3+
import (
4+
/*
5+
#include "types.h"
6+
*/
7+
"C"
8+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
no-prefixComments: true
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package main
2+
3+
import (
4+
/*
5+
#include "types.h"
6+
*/
7+
"C"
8+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package main
2+
3+
import (
4+
/*
5+
#include "types.h"
6+
*/
7+
"C"
8+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
no-prefixComments: true
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package main
2+
3+
import (
4+
// #include "types.h"
5+
"C"
6+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package main
2+
3+
import (
4+
// #include "types.h"
5+
"C"
6+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
no-prefixComments: true
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package main
2+
3+
import (
4+
// #include "types.h"
5+
// #include "other.h"
6+
"C"
7+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package main
2+
3+
import (
4+
// #include "types.h"
5+
// #include "other.h"
6+
"C"
7+
)

pkg/gci/parse.go

+38-3
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,60 @@ import (
1010
// Recursively parses import lines into a list of ImportDefs
1111
func parseToImportDefinitions(unformattedLines []string) ([]importPkg.ImportDef, error) {
1212
newImport := importPkg.ImportDef{}
13+
inBlockComment := false
1314
for index, unformattedLine := range unformattedLines {
1415
line := strings.TrimSpace(unformattedLine)
1516
if line == "" {
1617
//empty line --> starts a new import
1718
return parseToImportDefinitions(unformattedLines[index+1:])
1819
}
19-
if strings.HasPrefix(line, constants.CommentFlag) {
20+
if strings.HasPrefix(line, constants.LineCommentFlag) {
2021
// comment line
2122
newImport.PrefixComment = append(newImport.PrefixComment, line)
2223
continue
2324
}
25+
26+
if blockCommentStartsOnThisLine := strings.HasPrefix(line, constants.BlockCommentStartFlag); inBlockComment || blockCommentStartsOnThisLine {
27+
blockCommentEndIndex := strings.Index(line, constants.BlockCommentEndFlag)
28+
blockCommentEndsOnThisLine := blockCommentEndIndex != -1
29+
contentStartsAtIndex := 0
30+
contentEndsAtIndex := len(line)
31+
32+
if blockCommentStartsOnThisLine {
33+
newImport.PrefixComment = append(newImport.PrefixComment, constants.BlockCommentStartFlag)
34+
contentStartsAtIndex = len(constants.BlockCommentStartFlag)
35+
}
36+
37+
if blockCommentEndsOnThisLine {
38+
contentEndsAtIndex = blockCommentEndIndex
39+
}
40+
41+
if content := strings.TrimSpace(line[contentStartsAtIndex:contentEndsAtIndex]); content != "" {
42+
newImport.PrefixComment = append(newImport.PrefixComment, "\t"+content)
43+
}
44+
45+
inBlockComment = !blockCommentEndsOnThisLine
46+
47+
if !blockCommentEndsOnThisLine {
48+
continue
49+
}
50+
51+
newImport.PrefixComment = append(newImport.PrefixComment, constants.BlockCommentEndFlag)
52+
line = line[blockCommentEndIndex+len(constants.BlockCommentEndFlag):]
53+
54+
if line == "" {
55+
continue
56+
}
57+
}
58+
2459
// split inline comment from import
25-
importSegments := strings.SplitN(line, constants.CommentFlag, 2)
60+
importSegments := strings.SplitN(line, constants.LineCommentFlag, 2)
2661
switch len(importSegments) {
2762
case 1:
2863
// no inline comment
2964
case 2:
3065
// inline comment present
31-
newImport.InlineComment = constants.CommentFlag + importSegments[1]
66+
newImport.InlineComment = constants.LineCommentFlag + importSegments[1]
3267
default:
3368
return nil, InvalidImportSplitError{importSegments}
3469
}

0 commit comments

Comments
 (0)