Skip to content

Commit

Permalink
linenumbers work with new position
Browse files Browse the repository at this point in the history
  • Loading branch information
Ernest Micklei committed Nov 25, 2017
1 parent d6789b6 commit 67fc05e
Show file tree
Hide file tree
Showing 27 changed files with 172 additions and 163 deletions.
12 changes: 6 additions & 6 deletions comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (

// Comment holds a message.
type Comment struct {
Pos Position
Position Position
Lines []string
Cstyle bool // refers to /* ... */, C++ style is using //
ExtraSlash bool
Expand All @@ -53,7 +53,7 @@ func newComment(pos Position, lit string) *Comment {
nonEmpty = append(nonEmpty, lit)
}
}
return &Comment{Pos: pos, Lines: nonEmpty, Cstyle: len(lines) > 1, ExtraSlash: extraSlash}
return &Comment{Position: pos, Lines: nonEmpty, Cstyle: len(lines) > 1, ExtraSlash: extraSlash}
}

// columns is part of columnsPrintable
Expand Down Expand Up @@ -119,7 +119,7 @@ func maybeScanInlineComment(p *Parser, c elementContainer) {
line, tok, lit := p.scanIgnoreWhitespace()
esize := len(c.elements())
// seen comment and on same line and elements have been added
if tCOMMENT == tok && p.s.line <= currentLine+1 && esize > 0 {
if tCOMMENT == tok && p.s.pos.Line <= currentPos.Line+1 && esize > 0 {
// if the last added element can have an inline comment then set it
last := c.elements()[esize-1]
if inliner, ok := last.(commentInliner); ok {
Expand All @@ -143,13 +143,13 @@ func takeLastComment(list []Visitee) (*Comment, []Visitee) {
}

// mergeOrReturnComment creates a new comment and tries to merge it with the last element (if is a comment and is on the next line).
func mergeOrReturnComment(elements []Visitee, lit string, lineNumber int) *Comment {
com := newComment(lineNumber, lit)
func mergeOrReturnComment(elements []Visitee, lit string, pos Position) *Comment {
com := newComment(pos, lit)
// last element must be a comment to merge +
// do not merge c-style comments +
// last comment line was on previous line
if esize := len(elements); esize > 0 {
if last, ok := elements[esize-1].(*Comment); ok && !last.Cstyle && lineNumber <= last.LineNumber+len(last.Lines) { // less than because last line of file could be inline comment
if last, ok := elements[esize-1].(*Comment); ok && !last.Cstyle && pos.Line <= last.Position.Line+len(last.Lines) { // less than because last line of file could be inline comment
last.Merge(com)
// mark as merged
com = nil
Expand Down
12 changes: 6 additions & 6 deletions comment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ import (
)

func TestCreateComment(t *testing.T) {
c0 := newComment(0, "")
c0 := newComment(startPosition, "")
if got, want := len(c0.Lines), 1; got != want {
t.Errorf("got [%v] want [%v]", got, want)
}
c1 := newComment(0, `hello
c1 := newComment(startPosition, `hello
world`)
if got, want := len(c1.Lines), 2; got != want {
t.Errorf("got [%v] want [%v]", got, want)
Expand All @@ -49,8 +49,8 @@ world`)
}

func TestTakeLastComment(t *testing.T) {
c0 := newComment(0, "hi")
c1 := newComment(0, "there")
c0 := newComment(startPosition, "hi")
c1 := newComment(startPosition, "there")
_, l := takeLastComment([]Visitee{c0, c1})
if got, want := len(l), 1; got != want {
t.Fatalf("got [%v] want [%v]", got, want)
Expand Down Expand Up @@ -83,7 +83,7 @@ func TestParseCommentWithEmptyLinesAndTripleSlash(t *testing.T) {
if got, want := def.Elements[0].(*Comment).Lines[4], " comment 4"; got != want {
t.Fatalf("got [%v] want [%v]", got, want)
}
if got, want := def.Elements[0].(*Comment).LineNumber, 2; got != want {
if got, want := def.Elements[0].(*Comment).Position.Line, 2; got != want {
t.Fatalf("got [%d] want [%d]", got, want)
}
}
Expand All @@ -107,7 +107,7 @@ func TestParseCommentWithTripleSlash(t *testing.T) {
if got, want := def.Elements[0].(*Comment).Lines[0], " comment 1"; got != want {
t.Fatalf("got [%v] want [%v]", got, want)
}
if got, want := def.Elements[0].(*Comment).LineNumber, 2; got != want {
if got, want := def.Elements[0].(*Comment).Position.Line, 2; got != want {
t.Fatalf("got [%d] want [%d]", got, want)
}
}
30 changes: 15 additions & 15 deletions enum.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ import "strconv"

// Enum definition consists of a name and an enum body.
type Enum struct {
LineNumber int
Comment *Comment
Name string
Elements []Visitee
Position Position
Comment *Comment
Name string
Elements []Visitee
}

// Accept dispatches the call to the visitor.
Expand Down Expand Up @@ -61,7 +61,7 @@ func (e *Enum) takeLastComment() (last *Comment) {
}

func (e *Enum) parse(p *Parser) error {
line, tok, lit := p.scanIgnoreWhitespace()
pos, tok, lit := p.scanIgnoreWhitespace()
if tok != tIDENT {
if !isKeyword(tok) {
return p.unexpected(lit, "enum identifier", e)
Expand All @@ -73,15 +73,15 @@ func (e *Enum) parse(p *Parser) error {
return p.unexpected(lit, "enum opening {", e)
}
for {
line, tok, lit = p.scanIgnoreWhitespace()
pos, tok, lit = p.scanIgnoreWhitespace()
switch tok {
case tCOMMENT:
if com := mergeOrReturnComment(e.elements(), lit, line); com != nil { // not merged?
if com := mergeOrReturnComment(e.elements(), lit, pos); com != nil { // not merged?
e.Elements = append(e.Elements, com)
}
case tOPTION:
v := new(Option)
v.LineNumber = line
v.Position = pos
v.Comment = e.takeLastComment()
err := v.parse(p)
if err != nil {
Expand All @@ -95,7 +95,7 @@ func (e *Enum) parse(p *Parser) error {
default:
p.unscan()
f := new(EnumField)
f.LineNumber = line
f.Position = pos
f.Comment = e.takeLastComment()
err := f.parse(p)
if err != nil {
Expand All @@ -113,7 +113,7 @@ done:

// EnumField is part of the body of an Enum.
type EnumField struct {
LineNumber int
Position Position
Comment *Comment
Name string
Integer int
Expand Down Expand Up @@ -150,14 +150,14 @@ func (f EnumField) columns() (cols []aligned) {
}

func (f *EnumField) parse(p *Parser) error {
line, tok, lit := p.scanIgnoreWhitespace()
pos, tok, lit := p.scanIgnoreWhitespace()
if tok != tIDENT {
if !isKeyword(tok) {
return p.unexpected(lit, "enum field identifier", f)
}
}
f.Name = lit
line, tok, lit = p.scanIgnoreWhitespace()
pos, tok, lit = p.scanIgnoreWhitespace()
if tok != tEQUALS {
return p.unexpected(lit, "enum field =", f)
}
Expand All @@ -166,17 +166,17 @@ func (f *EnumField) parse(p *Parser) error {
return p.unexpected(lit, "enum field integer", f)
}
f.Integer = i
line, tok, lit = p.scanIgnoreWhitespace()
pos, tok, lit = p.scanIgnoreWhitespace()
if tok == tLEFTSQUARE {
o := new(Option)
o.LineNumber = line
o.Position = pos
o.IsEmbedded = true
err := o.parse(p)
if err != nil {
return err
}
f.ValueOption = o
line, tok, lit = p.scanIgnoreWhitespace()
pos, tok, lit = p.scanIgnoreWhitespace()
if tok != tRIGHTSQUARE {
return p.unexpected(lit, "option closing ]", f)
}
Expand Down
6 changes: 3 additions & 3 deletions enum_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@ enum EnumAllowingAlias {
if got, want := enums[0].Comment.Message(), " enum"; got != want {
t.Errorf("got [%v] want [%v]", enums[0].Comment, want)
}
if got, want := enums[0].LineNumber, 3; got != want {
if got, want := enums[0].Position.Line, 3; got != want {
t.Errorf("got [%d] want [%d]", got, want)
}
ef1 := enums[0].Elements[1].(*EnumField)
if got, want := ef1.Integer, 0; got != want {
t.Errorf("got [%v] want [%v]", got, want)
}
if got, want := ef1.LineNumber, 5; got != want {
if got, want := ef1.Position.Line, 5; got != want {
t.Errorf("got [%d] want [%d]", got, want)
}
ef3 := enums[0].Elements[3].(*EnumField)
Expand All @@ -72,7 +72,7 @@ enum EnumAllowingAlias {
if got, want := ef3.ValueOption.Constant.Source, "hello world"; got != want {
t.Errorf("got [%v] want [%v]", got, want)
}
if got, want := ef3.LineNumber, 7; got != want {
if got, want := ef3.Position.Line, 7; got != want {
t.Errorf("got [%d] want [%d]", got, want)
}
}
2 changes: 1 addition & 1 deletion extensions.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ package proto
// Extensions declare that a range of field numbers in a message are available for third-party extensions.
// proto2 only
type Extensions struct {
LineNumber int
Position Position
Comment *Comment
Ranges []Range
InlineComment *Comment
Expand Down
2 changes: 1 addition & 1 deletion extensions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func TestExtensions(t *testing.T) {
if got, want := len(f.Ranges), 2; got != want {
t.Fatalf("got [%d] want [%d]", got, want)
}
if got, want := f.LineNumber, 3; got != want {
if got, want := f.Position.Line, 3; got != want {
t.Fatalf("got [%d] want [%d]", got, want)
}
if got, want := f.Ranges[1].String(), "20 to max"; got != want {
Expand Down
16 changes: 8 additions & 8 deletions field.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import "strconv"

// Field is an abstract message field.
type Field struct {
LineNumber int
Position Position
Comment *Comment
Name string
Type string
Expand Down Expand Up @@ -95,8 +95,8 @@ func (f *NormalField) columns() (cols []aligned) {
// [ "repeated" | "optional" ] type fieldName "=" fieldNumber [ "[" fieldOptions "]" ] ";"
func (f *NormalField) parse(p *Parser) error {
for {
line, tok, lit := p.scanIgnoreWhitespace()
f.LineNumber = line
pos, tok, lit := p.scanIgnoreWhitespace()
f.Position = pos
switch tok {
case tREPEATED:
f.Repeated = true
Expand All @@ -118,14 +118,14 @@ done:
// parseFieldAfterType expects:
// fieldName "=" fieldNumber [ "[" fieldOptions "]" ] ";
func parseFieldAfterType(f *Field, p *Parser) error {
line, tok, lit := p.scanIgnoreWhitespace()
pos, tok, lit := p.scanIgnoreWhitespace()
if tok != tIDENT {
if !isKeyword(tok) {
return p.unexpected(lit, "field identifier", f)
}
}
f.Name = lit
line, tok, lit = p.scanIgnoreWhitespace()
pos, tok, lit = p.scanIgnoreWhitespace()
if tok != tEQUALS {
return p.unexpected(lit, "field =", f)
}
Expand All @@ -135,23 +135,23 @@ func parseFieldAfterType(f *Field, p *Parser) error {
}
f.Sequence = i
// see if there are options
line, tok, lit = p.scanIgnoreWhitespace()
pos, tok, lit = p.scanIgnoreWhitespace()
if tLEFTSQUARE != tok {
p.unscan()
return nil
}
// consume options
for {
o := new(Option)
o.LineNumber = line
o.Position = pos
o.IsEmbedded = true
err := o.parse(p)
if err != nil {
return err
}
f.Options = append(f.Options, o)

line, tok, lit = p.scanIgnoreWhitespace()
pos, tok, lit = p.scanIgnoreWhitespace()
if tRIGHTSQUARE == tok {
break
}
Expand Down
2 changes: 1 addition & 1 deletion field_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func TestField(t *testing.T) {
if got, want := f.Options[2].Constant.Source, "happy"; got != want {
t.Errorf("got [%v] want [%v]", got, want)
}
if got, want := f.LineNumber, 1; got != want {
if got, want := f.Position.Line, 1; got != want {
t.Errorf("got [%v] want [%v]", got, want)
}
}
Expand Down
12 changes: 6 additions & 6 deletions group.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ package proto
// Group represents a (proto2 only) group.
// https://developers.google.com/protocol-buffers/docs/reference/proto2-spec#group_field
type Group struct {
LineNumber int
Comment *Comment
Name string
Optional bool
Sequence int
Elements []Visitee
Position Position
Comment *Comment
Name string
Optional bool
Sequence int
Elements []Visitee
}

// Accept dispatches the call to the visitor.
Expand Down
2 changes: 1 addition & 1 deletion group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func TestGroup(t *testing.T) {
if got, want := len(g.Elements), 1; got != want {
t.Fatalf("got [%v] want [%v]", got, want)
}
if got, want := g.LineNumber, 3; got != want {
if got, want := g.Position.Line, 3; got != want {
t.Fatalf("got [%v] want [%v]", got, want)
}
if got, want := g.Comment != nil, true; got != want {
Expand Down
2 changes: 1 addition & 1 deletion import.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import "fmt"

// Import holds a filename to another .proto definition.
type Import struct {
LineNumber int
Position Position
Comment *Comment
Filename string
Kind string // weak, public, <empty>
Expand Down
Loading

0 comments on commit 67fc05e

Please sign in to comment.