Skip to content

Commit

Permalink
refactor: add range to text, see #498
Browse files Browse the repository at this point in the history
  • Loading branch information
a-h committed Jun 16, 2024
1 parent 9de401c commit 8d27ad1
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.2.723
0.2.724
8 changes: 4 additions & 4 deletions parser/v2/elementparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type elementOpenTag struct {
}

var elementOpenTagParser = parse.Func(func(pi *parse.Input) (e elementOpenTag, ok bool, err error) {
start := pi.Index()
start := pi.Position()

// <
if _, ok, err = lt.Parse(pi); err != nil || !ok {
Expand All @@ -31,13 +31,13 @@ var elementOpenTagParser = parse.Func(func(pi *parse.Input) (e elementOpenTag, o
// Element name.
l := pi.Position().Line
if e.Name, ok, err = elementNameParser.Parse(pi); err != nil || !ok {
pi.Seek(start)
pi.Seek(start.Index)
return
}
e.NameRange = NewRange(pi.PositionAt(pi.Index()-len(e.Name)), pi.Position())

if e.Attributes, ok, err = (attributesParser{}).Parse(pi); err != nil || !ok {
pi.Seek(start)
pi.Seek(start.Index)
return
}

Expand All @@ -48,7 +48,7 @@ var elementOpenTagParser = parse.Func(func(pi *parse.Input) (e elementOpenTag, o

// Optional whitespace.
if _, _, err = parse.OptionalWhitespace.Parse(pi); err != nil {
pi.Seek(start)
pi.Seek(start.Index)
return
}

Expand Down
36 changes: 33 additions & 3 deletions parser/v2/elementparser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,15 @@ func TestElementParser(t *testing.T) {
From: Position{Index: 1, Line: 0, Col: 1},
To: Position{Index: 9, Line: 0, Col: 9},
},
Children: []Node{Text{Value: "Content"}},
Children: []Node{
Text{
Value: "Content",
Range: Range{
From: Position{Index: 10, Line: 0, Col: 10},
To: Position{Index: 17, Line: 0, Col: 17},
},
},
},
},
},
{
Expand Down Expand Up @@ -654,7 +662,15 @@ func TestElementParser(t *testing.T) {
From: Position{Index: 1, Line: 0, Col: 1},
To: Position{Index: 6, Line: 0, Col: 6},
},
Children: []Node{Text{Value: "Text"}},
Children: []Node{
Text{
Value: "Text",
Range: Range{
From: Position{Index: 7, Line: 0, Col: 7},
To: Position{Index: 11, Line: 0, Col: 11},
},
},
},
},
},
{
Expand Down Expand Up @@ -1008,6 +1024,10 @@ func TestElementParser(t *testing.T) {
Children: []Node{
Text{
Value: "Test",
Range: Range{
From: Position{Index: 70, Line: 4, Col: 1},
To: Position{Index: 74, Line: 4, Col: 5},
},
},
},
TrailingSpace: SpaceVertical,
Expand Down Expand Up @@ -1215,7 +1235,13 @@ func TestElementParser(t *testing.T) {
},
IndentAttrs: true,
Children: []Node{
Text{Value: "Test"},
Text{
Value: "Test",
Range: Range{
From: Position{Index: 66, Line: 4, Col: 1},
To: Position{Index: 70, Line: 4, Col: 5},
},
},
},
},
},
Expand All @@ -1242,6 +1268,10 @@ func TestElementParser(t *testing.T) {
Children: []Node{
Text{
Value: "The text",
Range: Range{
From: Position{Index: 3, Line: 0, Col: 3},
To: Position{Index: 11, Line: 0, Col: 11},
},
},
},
},
Expand Down
9 changes: 8 additions & 1 deletion parser/v2/ifexpressionparser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,14 @@ func TestIfExpression(t *testing.T) {
},
Then: []Node{
Whitespace{Value: " "},
Text{Value: "text", TrailingSpace: SpaceVertical},
Text{
Value: "text",
Range: Range{
From: Position{Index: 15, Line: 1, Col: 2},
To: Position{Index: 19, Line: 1, Col: 6},
},
TrailingSpace: SpaceVertical,
},
},
},
},
Expand Down
8 changes: 7 additions & 1 deletion parser/v2/templateparser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -628,7 +628,13 @@ func TestTemplateParser(t *testing.T) {
},
},
Whitespace{Value: " "},
Text{Value: "Home"},
Text{
Value: "Home",
Range: Range{
From: Position{Index: 48, Line: 1, Col: 36},
To: Position{Index: 52, Line: 1, Col: 40},
},
},
},
TrailingSpace: SpaceVertical,
},
Expand Down
15 changes: 13 additions & 2 deletions parser/v2/templelementparser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,12 @@ func TestTemplElementExpressionParser(t *testing.T) {
},
Children: []Node{
Whitespace{Value: "\n\t"},
Text{Value: "some words",
Text{
Value: "some words",
Range: Range{
From: Position{Index: 18, Line: 1, Col: 1},
To: Position{Index: 28, Line: 1, Col: 11},
},
TrailingSpace: SpaceVertical,
},
},
Expand Down Expand Up @@ -394,7 +399,13 @@ func TestTemplElementExpressionParser(t *testing.T) {
To: Position{Index: 42, Line: 1, Col: 6},
},
Children: []Node{
Text{Value: "hello"},
Text{
Value: "hello",
Range: Range{
From: Position{Index: 43, Line: 1, Col: 7},
To: Position{Index: 48, Line: 1, Col: 12},
},
},
},
TrailingSpace: SpaceVertical,
},
Expand Down
1 change: 1 addition & 0 deletions parser/v2/textparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ var textParser = parse.Func(func(pi *parse.Input) (n Node, ok bool, err error) {
err = parse.Error("textParser: unterminated text, expected tag open, templ expression open, or newline", from)
return
}
t.Range = NewRange(from, pi.Position())

// Parse trailing whitespace.
ws, _, err := parse.Whitespace.Parse(pi)
Expand Down
46 changes: 39 additions & 7 deletions parser/v2/textparser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,56 +18,88 @@ func TestTextParser(t *testing.T) {
input: `abcdef<a href="https://example.com">More</a>`,
expected: Text{
Value: "abcdef",
Range: Range{
From: Position{Index: 0, Line: 0, Col: 0},
To: Position{Index: 6, Line: 0, Col: 6},
},
},
},
{
name: "Text ends at a templ expression start",
input: `abcdef{%= "test" %}`,
input: `abcdef{ "test" }`,
expected: Text{
Value: "abcdef",
Range: Range{
From: Position{Index: 0, Line: 0, Col: 0},
To: Position{Index: 6, Line: 0, Col: 6},
},
},
},
{
name: "Text may contain spaces",
input: `abcdef ghijk{%= "test" %}`,
input: `abcdef ghijk{ "test" }`,
expected: Text{
Value: "abcdef ghijk",
Range: Range{
From: Position{Index: 0, Line: 0, Col: 0},
To: Position{Index: 12, Line: 0, Col: 12},
},
},
},
{
name: "Text may contain named references",
input: `abcdef&nbsp;ghijk{%= "test" %}`,
input: `abcdef&nbsp;ghijk{ "test" }`,
expected: Text{
Value: "abcdef&nbsp;ghijk",
Range: Range{
From: Position{Index: 0, Line: 0, Col: 0},
To: Position{Index: 17, Line: 0, Col: 17},
},
},
},
{
name: "Text may contain base 10 numeric references",
input: `abcdef&#32;ghijk{%= "test" %}`,
input: `abcdef&#32;ghijk{ "test" }`,
expected: Text{
Value: "abcdef&#32;ghijk",
Range: Range{
From: Position{Index: 0, Line: 0, Col: 0},
To: Position{Index: 16, Line: 0, Col: 16},
},
},
},
{
name: "Text may contain hexadecimal numeric references",
input: `abcdef&#x20;ghijk{%= "test" %}`,
input: `abcdef&#x20;ghijk{ "test" }`,
expected: Text{
Value: "abcdef&#x20;ghijk",
Range: Range{
From: Position{Index: 0, Line: 0, Col: 0},
To: Position{Index: 17, Line: 0, Col: 17},
},
},
},
{
name: "Multiline text is colected line by line",
input: "Line 1\nLine 2",
expected: Text{
Value: "Line 1",
Value: "Line 1",
Range: Range{
From: Position{Index: 0, Line: 0, Col: 0},
To: Position{Index: 6, Line: 0, Col: 6},
},
TrailingSpace: "\n",
},
},
{
name: "Multiline text is colected line by line (Windows)",
input: "Line 1\r\nLine 2",
expected: Text{
Value: "Line 1",
Value: "Line 1",
Range: Range{
From: Position{Index: 0, Line: 0, Col: 0},
To: Position{Index: 6, Line: 0, Col: 6},
},
TrailingSpace: "\n",
},
},
Expand Down

0 comments on commit 8d27ad1

Please sign in to comment.