feat: add start column and end column to position struct

This commit is contained in:
Laytan Laats
2023-03-26 01:54:00 +01:00
parent 92019441d0
commit 7c12f73974
19 changed files with 316 additions and 109 deletions

View File

@@ -10,7 +10,7 @@ import (
)
func TestConstructor(t *testing.T) {
pos := position.NewPosition(1, 2, 3, 4)
pos := position.NewPosition(1, 2, 3, 4, 1, 2)
actual := errors.NewError("message", pos)
@@ -23,7 +23,7 @@ func TestConstructor(t *testing.T) {
}
func TestPrint(t *testing.T) {
pos := position.NewPosition(1, 2, 3, 4)
pos := position.NewPosition(1, 2, 3, 4, 1, 2)
Error := errors.NewError("message", pos)

View File

@@ -1,19 +1,64 @@
package position
// Tests that were written before the addition of StartCol and EndCol can set this to false,
// Which will not check if StartCol or EndCol are equal in the Equal function.
var CheckColEquality = true
// Position represents node position
type Position struct {
StartLine int
EndLine int
StartCol int
EndCol int
StartPos int
EndPos int
}
// NewPosition Position constructor
func NewPosition(StartLine int, EndLine int, StartPos int, EndPos int) *Position {
func NewPosition(
StartLine int,
EndLine int,
StartPos int,
EndPos int,
StartCol int,
EndCol int,
) *Position {
return &Position{
StartLine: StartLine,
EndLine: EndLine,
StartPos: StartPos,
EndPos: EndPos,
StartCol: StartCol,
EndCol: EndCol,
}
}
func (p Position) Equal(other Position) bool {
if p.StartLine != other.StartLine {
return false
}
if p.StartPos != other.StartPos {
return false
}
if p.EndLine != other.EndLine {
return false
}
if p.EndPos != other.EndPos {
return false
}
if CheckColEquality {
if p.StartCol != other.StartCol {
return false
}
if p.EndCol != other.EndCol {
return false
}
}
return true
}

View File

@@ -148,6 +148,8 @@ func (v *Dumper) dumpPosition(pos *position.Position) {
v.print(v.indent, "StartLine: "+strconv.Itoa(pos.StartLine)+",\n")
v.print(v.indent, "EndLine: "+strconv.Itoa(pos.EndLine)+",\n")
v.print(v.indent, "StartCol: "+strconv.Itoa(pos.StartCol)+",\n")
v.print(v.indent, "EndCol: "+strconv.Itoa(pos.EndCol)+",\n")
v.print(v.indent, "StartPos: "+strconv.Itoa(pos.StartPos)+",\n")
v.print(v.indent, "EndPos: "+strconv.Itoa(pos.EndPos)+",\n")

View File

@@ -2,10 +2,11 @@ package dumper_test
import (
"bytes"
"testing"
"github.com/VKCOM/php-parser/pkg/position"
"github.com/VKCOM/php-parser/pkg/token"
"github.com/VKCOM/php-parser/pkg/visitor/dumper"
"testing"
"github.com/VKCOM/php-parser/pkg/ast"
)
@@ -45,6 +46,8 @@ func TestDumper_root(t *testing.T) {
Position: &position.Position{
StartLine: 1,
EndLine: 2,
StartCol: 0,
EndCol: 0,
StartPos: 3,
EndPos: 4,
},
@@ -60,6 +63,8 @@ func TestDumper_root(t *testing.T) {
Position: &position.Position{
StartLine: 1,
EndLine: 2,
StartCol: 0,
EndCol: 0,
StartPos: 3,
EndPos: 4,
},