Skip to content

Commit

Permalink
Make block comment parsing more robust to different formatting.
Browse files Browse the repository at this point in the history
Signed-off-by: Charles Korn <me@charleskorn.com>
  • Loading branch information
charleskorn committed Mar 2, 2022
1 parent 66fd3f5 commit de8110b
Show file tree
Hide file tree
Showing 13 changed files with 89 additions and 25 deletions.
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"
)
54 changes: 29 additions & 25 deletions pkg/gci/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,33 +23,37 @@ func parseToImportDefinitions(unformattedLines []string) ([]importPkg.ImportDef,
continue
}

// FIXME: this doesn't correctly handle block comments that start part-way through a line or end part-way through a line, for example:
// /* some comment */ "golang.org/x/tools"
// Or:
// /* some comment
// */ "golang.org/x/tools"
//
// It only supports block comments that start and end on their own line, for example:
// /* some comment */
// "golang.org/x/tools"
// Or:
// /*
// some comment
// */
// "golang.org/x/tools"
if inBlockComment {
if strings.HasSuffix(line, constants.BlockCommentEndFlag) {
inBlockComment = false
} else {
line = "\t" + line
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)
}

newImport.PrefixComment = append(newImport.PrefixComment, line)
continue
} else if strings.HasPrefix(line, constants.BlockCommentStartFlag) {
inBlockComment = true
newImport.PrefixComment = append(newImport.PrefixComment, line)
continue
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
Expand Down

0 comments on commit de8110b

Please sign in to comment.