Skip to content
This repository has been archived by the owner on Jan 5, 2019. It is now read-only.

Commit

Permalink
fix issue emicklei#54 and updates docs on Comment and its fields (emi…
Browse files Browse the repository at this point in the history
…cklei#56)

fix issue emicklei#54 and updates docs on Comment and its fields
  • Loading branch information
emicklei authored Jan 12, 2018
1 parent e122c20 commit 5a91db7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 24 deletions.
30 changes: 12 additions & 18 deletions comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,33 +28,27 @@ import (
"text/scanner"
)

// Comment holds a message.
// Comment one or more comment text lines, either in c- or c++ style.
type Comment struct {
Position scanner.Position
Position scanner.Position
// Lines are comment text lines without prefixes //, ///, /* or suffix */
Lines []string
Cstyle bool // refers to /* ... */, C++ style is using //
ExtraSlash bool
ExtraSlash bool // is true if the comment starts with 3 slashes
}

// newComment returns a comment.
func newComment(pos scanner.Position, lit string) *Comment {
nonEmpty := []string{}
extraSlash := false
lines := strings.Split(lit, "\n")
if len(lines) > 1 {
for _, each := range lines {
// do not modify if a line has an extra slash prefix
nonEmpty = append(nonEmpty, each)
}
extraSlash := strings.HasPrefix(lit, "///")
isCstyle := strings.HasPrefix(lit, "/*") && strings.HasSuffix(lit, "*/")
var lines []string
if isCstyle {
withoutMarkers := strings.TrimRight(strings.TrimLeft(lit, "/*"), "*/")
lines = strings.Split(withoutMarkers, "\n")
} else {
if strings.HasPrefix(lit, "/") {
extraSlash = strings.HasPrefix(lit, "///")
nonEmpty = append(nonEmpty, strings.TrimLeft(lit, "/"))
} else {
nonEmpty = append(nonEmpty, lit)
}
lines = strings.Split(strings.TrimLeft(lit, "/"), "\n")
}
return &Comment{Position: pos, Lines: nonEmpty, Cstyle: len(lines) > 1, ExtraSlash: extraSlash}
return &Comment{Position: pos, Lines: lines, Cstyle: isCstyle, ExtraSlash: extraSlash}
}

type inlineComment struct {
Expand Down
13 changes: 7 additions & 6 deletions comment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ world`)
if got, want := c1.Lines[1], "world"; got != want {
t.Errorf("got [%v] want [%v]", got, want)
}
if got, want := c1.Cstyle, true; got != want {
t.Errorf("got [%v] want [%v]", c1, want)
if got, want := c1.Cstyle, false; got != want {
t.Errorf("got [%v] want [%v]", got, want)
}
}

Expand Down Expand Up @@ -158,7 +158,6 @@ func TestParseCStyleCommentWithIndent(t *testing.T) {
}

func TestParseCStyleOneLineComment(t *testing.T) {
t.Skip("See https://github.com/emicklei/proto/issues/54")
proto := `/* comment 1 */`
p := newParserOn(proto)
def, err := p.Parse()
Expand All @@ -172,7 +171,7 @@ func TestParseCStyleOneLineComment(t *testing.T) {
if got, want := len(def.Elements[0].(*Comment).Lines), 1; got != want {
t.Fatalf("got [%v] want [%v]", got, want)
}
if got, want := def.Elements[0].(*Comment).Lines[0], "/* comment 1 */"; got != want {
if got, want := def.Elements[0].(*Comment).Lines[0], " comment 1 "; got != want {
t.Errorf("got [%v] want [%v]", got, want)
}
if got, want := def.Elements[0].(*Comment).Cstyle, true; got != want {
Expand Down Expand Up @@ -204,7 +203,10 @@ func TestParseCStyleInlineComment(t *testing.T) {
if got, want := len(comment.Lines), 3; got != want {
t.Fatalf("got [%v] want [%v]", got, want)
}
if got, want := comment.Lines[0], "/*"; got != want {
if got, want := comment.Lines[0], ""; got != want {
t.Errorf("got [%v] want [%v]", got, want)
}
if got, want := comment.Lines[1], " comment 1"; got != want {
t.Errorf("got [%v] want [%v]", got, want)
}
if got, want := comment.Cstyle, true; got != want {
Expand All @@ -221,7 +223,6 @@ func TestParseCommentWithTripleSlash(t *testing.T) {
if err != nil {
t.Fatal(err)
}
//spew.Dump(def)
if got, want := len(def.Elements), 1; got != want {
t.Fatalf("got [%v] want [%v]", got, want)
}
Expand Down

0 comments on commit 5a91db7

Please sign in to comment.