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

Lines changed: 5 additions & 3 deletions
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

Lines changed: 1 addition & 1 deletion
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
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
no-prefixComments: true
Lines changed: 6 additions & 0 deletions
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+
)
Lines changed: 9 additions & 0 deletions
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+
)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
no-prefixComments: true
Lines changed: 6 additions & 0 deletions
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+
)
Lines changed: 8 additions & 0 deletions
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+
)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
no-prefixComments: true
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package main
2+
3+
import (
4+
/* #include "types.h" */ "C"
5+
)

0 commit comments

Comments
 (0)