From 8d27ad14edda1cd494729d4cf736022f50fbd9b6 Mon Sep 17 00:00:00 2001 From: Adrian Hesketh Date: Sun, 16 Jun 2024 20:43:44 +0100 Subject: [PATCH] refactor: add range to text, see #498 --- .version | 2 +- parser/v2/elementparser.go | 8 ++--- parser/v2/elementparser_test.go | 36 ++++++++++++++++++++-- parser/v2/ifexpressionparser_test.go | 9 +++++- parser/v2/templateparser_test.go | 8 ++++- parser/v2/templelementparser_test.go | 15 +++++++-- parser/v2/textparser.go | 1 + parser/v2/textparser_test.go | 46 +++++++++++++++++++++++----- 8 files changed, 106 insertions(+), 19 deletions(-) diff --git a/.version b/.version index 59d34e477..91bccc87a 100644 --- a/.version +++ b/.version @@ -1 +1 @@ -0.2.723 \ No newline at end of file +0.2.724 \ No newline at end of file diff --git a/parser/v2/elementparser.go b/parser/v2/elementparser.go index 478d17621..d5c7e66e4 100644 --- a/parser/v2/elementparser.go +++ b/parser/v2/elementparser.go @@ -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 { @@ -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 } @@ -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 } diff --git a/parser/v2/elementparser_test.go b/parser/v2/elementparser_test.go index 2ca10c1d4..294fbf363 100644 --- a/parser/v2/elementparser_test.go +++ b/parser/v2/elementparser_test.go @@ -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}, + }, + }, + }, }, }, { @@ -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}, + }, + }, + }, }, }, { @@ -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, @@ -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}, + }, + }, }, }, }, @@ -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}, + }, }, }, }, diff --git a/parser/v2/ifexpressionparser_test.go b/parser/v2/ifexpressionparser_test.go index de3f3fcd8..d80300e41 100644 --- a/parser/v2/ifexpressionparser_test.go +++ b/parser/v2/ifexpressionparser_test.go @@ -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, + }, }, }, }, diff --git a/parser/v2/templateparser_test.go b/parser/v2/templateparser_test.go index 3c1b5de28..4bd287b67 100644 --- a/parser/v2/templateparser_test.go +++ b/parser/v2/templateparser_test.go @@ -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, }, diff --git a/parser/v2/templelementparser_test.go b/parser/v2/templelementparser_test.go index f3ddee05e..478620dc3 100644 --- a/parser/v2/templelementparser_test.go +++ b/parser/v2/templelementparser_test.go @@ -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, }, }, @@ -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, }, diff --git a/parser/v2/textparser.go b/parser/v2/textparser.go index 5e88033d8..12a9d2b12 100644 --- a/parser/v2/textparser.go +++ b/parser/v2/textparser.go @@ -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) diff --git a/parser/v2/textparser_test.go b/parser/v2/textparser_test.go index cf70a128c..2e4602f33 100644 --- a/parser/v2/textparser_test.go +++ b/parser/v2/textparser_test.go @@ -18,48 +18,76 @@ func TestTextParser(t *testing.T) { input: `abcdefMore`, 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 ghijk{%= "test" %}`, + input: `abcdef ghijk{ "test" }`, expected: Text{ Value: "abcdef 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 ghijk{%= "test" %}`, + input: `abcdef ghijk{ "test" }`, expected: Text{ Value: "abcdef 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 ghijk{%= "test" %}`, + input: `abcdef ghijk{ "test" }`, expected: Text{ Value: "abcdef 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", }, }, @@ -67,7 +95,11 @@ func TestTextParser(t *testing.T) { 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", }, },