Skip to content

Commit

Permalink
refactor: add full range of script, css, html and text templates, sup…
Browse files Browse the repository at this point in the history
…ports #498
  • Loading branch information
a-h committed Jun 16, 2024
1 parent 4471343 commit 9de401c
Show file tree
Hide file tree
Showing 9 changed files with 132 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.2.719
0.2.723
4 changes: 4 additions & 0 deletions parser/v2/cssparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (

// CSS Parser.
var cssParser = parse.Func(func(pi *parse.Input) (r CSSTemplate, ok bool, err error) {
from := pi.Position()

r = CSSTemplate{
Properties: []CSSProperty{},
}
Expand Down Expand Up @@ -56,6 +58,8 @@ var cssParser = parse.Func(func(pi *parse.Input) (r CSSTemplate, ok bool, err er
return
}

r.Range = NewRange(from, pi.Position())

return r, true, nil
}
})
Expand Down
20 changes: 20 additions & 0 deletions parser/v2/cssparser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ func TestCSSParser(t *testing.T) {
}`,
expected: CSSTemplate{
Name: "Name",
Range: Range{
From: Position{Index: 0, Line: 0, Col: 0},
To: Position{Index: 14, Line: 1, Col: 1},
},
Expression: Expression{
Value: "Name()",
Range: Range{
Expand All @@ -157,6 +161,10 @@ func TestCSSParser(t *testing.T) {
}`,
expected: CSSTemplate{
Name: "Name",
Range: Range{
From: Position{Index: 0, Line: 0, Col: 0},
To: Position{Index: 14, Line: 1, Col: 1},
},
Expression: Expression{
Value: "Name()",
Range: Range{
Expand All @@ -182,6 +190,10 @@ background-color: #ffffff;
}`,
expected: CSSTemplate{
Name: "Name",
Range: Range{
From: Position{Index: 0, Line: 0, Col: 0},
To: Position{Index: 41, Line: 2, Col: 1},
},
Expression: Expression{
Value: "Name()",
Range: Range{
Expand Down Expand Up @@ -212,6 +224,10 @@ background-color: { constants.BackgroundColor };
}`,
expected: CSSTemplate{
Name: "Name",
Range: Range{
From: Position{Index: 0, Line: 0, Col: 0},
To: Position{Index: 63, Line: 2, Col: 1},
},
Expression: Expression{
Value: "Name()",
Range: Range{
Expand Down Expand Up @@ -258,6 +274,10 @@ background-color: { prop };
}`,
expected: CSSTemplate{
Name: "Name",
Range: Range{
From: Position{Index: 0, Line: 0, Col: 0},
To: Position{Index: 53, Line: 2, Col: 1},
},
Expression: Expression{
Value: "Name(prop string)",
Range: Range{
Expand Down
4 changes: 4 additions & 0 deletions parser/v2/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ var expressionFuncEnd = parse.All(parse.Rune(')'), openBraceWithOptionalPadding)
// Template

var template = parse.Func(func(pi *parse.Input) (r HTMLTemplate, ok bool, err error) {
start := pi.Position()

// templ FuncName(p Person, other Other) {
var te templateExpression
if te, ok, err = templateExpressionParser.Parse(pi); err != nil || !ok {
Expand Down Expand Up @@ -42,5 +44,7 @@ var template = parse.Func(func(pi *parse.Input) (r HTMLTemplate, ok bool, err er
return
}

r.Range = NewRange(start, pi.Position())

return r, true, nil
})
8 changes: 5 additions & 3 deletions parser/v2/scripttemplateparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import (
)

var scriptTemplateParser = parse.Func(func(pi *parse.Input) (r ScriptTemplate, ok bool, err error) {
start := pi.Index()
start := pi.Position()

// Parse the name.
var se scriptExpression
if se, ok, err = scriptExpressionParser.Parse(pi); err != nil || !ok {
pi.Seek(start)
pi.Seek(start.Index)
return
}
r.Name = se.Name
Expand All @@ -19,7 +19,7 @@ var scriptTemplateParser = parse.Func(func(pi *parse.Input) (r ScriptTemplate, o
// Read code expression.
var e Expression
if e, ok, err = exp.Parse(pi); err != nil || !ok {
pi.Seek(start)
pi.Seek(start.Index)
return
}
r.Value = e.Value
Expand All @@ -30,6 +30,8 @@ var scriptTemplateParser = parse.Func(func(pi *parse.Input) (r ScriptTemplate, o
return
}

r.Range = NewRange(start, pi.Position())

return r, true, nil
})

Expand Down
24 changes: 24 additions & 0 deletions parser/v2/scripttemplateparser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ func TestScriptTemplateParser(t *testing.T) {
input: `script Name() {
}`,
expected: ScriptTemplate{
Range: Range{
From: Position{Index: 0, Line: 0, Col: 0},
To: Position{Index: 17, Line: 1, Col: 1},
},
Name: Expression{
Value: "Name",
Range: Range{
Expand Down Expand Up @@ -56,6 +60,10 @@ func TestScriptTemplateParser(t *testing.T) {
input: `script Name(){
}`,
expected: ScriptTemplate{
Range: Range{
From: Position{Index: 0, Line: 0, Col: 0},
To: Position{Index: 16, Line: 1, Col: 1},
},
Name: Expression{
Value: "Name",
Range: Range{
Expand Down Expand Up @@ -94,6 +102,10 @@ func TestScriptTemplateParser(t *testing.T) {
var x = "x";
}`,
expected: ScriptTemplate{
Range: Range{
From: Position{Index: 0, Line: 0, Col: 0},
To: Position{Index: 30, Line: 2, Col: 1},
},
Name: Expression{
Value: "Name",
Range: Range{
Expand Down Expand Up @@ -133,6 +145,10 @@ var x = "x";
console.log(value);
}`,
expected: ScriptTemplate{
Range: Range{
From: Position{Index: 0, Line: 0, Col: 0},
To: Position{Index: 49, Line: 2, Col: 1},
},
Name: Expression{
Value: "Name",
Range: Range{
Expand Down Expand Up @@ -172,6 +188,10 @@ console.log(value);
//'
}`,
expected: ScriptTemplate{
Range: Range{
From: Position{Index: 0, Line: 0, Col: 0},
To: Position{Index: 22, Line: 2, Col: 1},
},
Name: Expression{
Value: "Name",
Range: Range{
Expand Down Expand Up @@ -211,6 +231,10 @@ console.log(value);
let x = '';
}`,
expected: ScriptTemplate{
Range: Range{
From: Position{Index: 0, Line: 0, Col: 0},
To: Position{Index: 31, Line: 2, Col: 1},
},
Name: Expression{
Value: "Name",
Range: Range{
Expand Down
2 changes: 1 addition & 1 deletion parser/v2/templateparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ type templateNodeParser[TUntil any] struct {
untilName string
}

var rawElements = parse.Any[Node](styleElement, scriptElement)
var rawElements = parse.Any(styleElement, scriptElement)

var templateNodeParsers = []parse.Parser[Node]{
docType, // <!DOCTYPE html>
Expand Down
68 changes: 68 additions & 0 deletions parser/v2/templateparser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ func TestTemplateParser(t *testing.T) {
input: `templ Name() {
}`,
expected: HTMLTemplate{
Range: Range{
From: Position{Index: 0, Line: 0, Col: 0},
To: Position{Index: 16, Line: 1, Col: 1},
},
Expression: Expression{
Value: "Name()",
Range: Range{
Expand All @@ -41,6 +45,10 @@ func TestTemplateParser(t *testing.T) {
input: `templ (data Data) Name() {
}`,
expected: HTMLTemplate{
Range: Range{
From: Position{Index: 0, Line: 0, Col: 0},
To: Position{Index: 28, Line: 1, Col: 1},
},
Expression: Expression{
Value: "(data Data) Name()",
Range: Range{
Expand All @@ -63,6 +71,10 @@ func TestTemplateParser(t *testing.T) {
input: `templ Name(){
}`,
expected: HTMLTemplate{
Range: Range{
From: Position{Index: 0, Line: 0, Col: 0},
To: Position{Index: 15, Line: 1, Col: 1},
},
Expression: Expression{
Value: "Name()",
Range: Range{
Expand All @@ -85,6 +97,10 @@ func TestTemplateParser(t *testing.T) {
input: `templ Name(p Parameter) {
}`,
expected: HTMLTemplate{
Range: Range{
From: Position{Index: 0, Line: 0, Col: 0},
To: Position{Index: 27, Line: 1, Col: 1},
},
Expression: Expression{
Value: "Name(p Parameter)",
Range: Range{
Expand All @@ -109,6 +125,10 @@ func TestTemplateParser(t *testing.T) {
) {
}`,
expected: HTMLTemplate{
Range: Range{
From: Position{Index: 0, Line: 0, Col: 0},
To: Position{Index: 39, Line: 3, Col: 1},
},
Expression: Expression{
Value: "Multiline(\n\tparams expense,\n)",
Range: Range{
Expand All @@ -132,6 +152,10 @@ func TestTemplateParser(t *testing.T) {
<span>{ "span content" }</span>
}`,
expected: HTMLTemplate{
Range: Range{
From: Position{Index: 0, Line: 0, Col: 0},
To: Position{Index: 59, Line: 2, Col: 1},
},
Expression: Expression{
Value: "Name(p Parameter)",
Range: Range{
Expand Down Expand Up @@ -182,6 +206,10 @@ func TestTemplateParser(t *testing.T) {
name: "template: containing element - no spacing",
input: `templ Name(p Parameter) { <span>{ "span content" }</span> }`,
expected: HTMLTemplate{
Range: Range{
From: Position{Index: 0, Line: 0, Col: 0},
To: Position{Index: 59, Line: 0, Col: 59},
},
Expression: Expression{
Value: "Name(p Parameter)",
Range: Range{
Expand Down Expand Up @@ -239,6 +267,10 @@ func TestTemplateParser(t *testing.T) {
</div>
}`,
expected: HTMLTemplate{
Range: Range{
From: Position{Index: 0, Line: 0, Col: 0},
To: Position{Index: 99, Line: 7, Col: 1},
},
Expression: Expression{
Value: "Name(p Parameter)",
Range: Range{
Expand Down Expand Up @@ -328,6 +360,10 @@ func TestTemplateParser(t *testing.T) {
}
}`,
expected: HTMLTemplate{
Range: Range{
From: Position{Index: 0, Line: 0, Col: 0},
To: Position{Index: 84, Line: 6, Col: 1},
},
Expression: Expression{
Value: "Name(p Parameter)",
Range: Range{
Expand Down Expand Up @@ -408,6 +444,10 @@ func TestTemplateParser(t *testing.T) {
<input type="text" value="b" />
}`,
expected: HTMLTemplate{
Range: Range{
From: Position{Index: 0, Line: 0, Col: 0},
To: Position{Index: 93, Line: 3, Col: 1},
},
Expression: Expression{
Value: "Name(p Parameter)",
Range: Range{
Expand Down Expand Up @@ -486,6 +526,10 @@ func TestTemplateParser(t *testing.T) {
<!DOCTYPE html>
}`,
expected: HTMLTemplate{
Range: Range{
From: Position{Index: 0, Line: 0, Col: 0},
To: Position{Index: 32, Line: 2, Col: 1},
},
Expression: Expression{
Value: "Name()",
Range: Range{
Expand Down Expand Up @@ -525,6 +569,10 @@ func TestTemplateParser(t *testing.T) {
<a href="/"> @Icon("home", Inline) Home</a>
}`,
expected: HTMLTemplate{
Range: Range{
From: Position{Index: 0, Line: 0, Col: 0},
To: Position{Index: 58, Line: 2, Col: 1},
},
Expression: Expression{
Value: "x()",
Range: Range{
Expand Down Expand Up @@ -593,6 +641,10 @@ func TestTemplateParser(t *testing.T) {
// Comment
}`,
expected: HTMLTemplate{
Range: Range{
From: Position{Index: 0, Line: 0, Col: 0},
To: Position{Index: 25, Line: 2, Col: 1},
},
Expression: Expression{
Value: "x()",
Range: Range{
Expand All @@ -612,6 +664,10 @@ func TestTemplateParser(t *testing.T) {
/* Comment */
}`,
expected: HTMLTemplate{
Range: Range{
From: Position{Index: 0, Line: 0, Col: 0},
To: Position{Index: 28, Line: 2, Col: 1},
},
Expression: Expression{
Value: "x()",
Range: Range{
Expand All @@ -634,6 +690,10 @@ func TestTemplateParser(t *testing.T) {
*/
}`,
expected: HTMLTemplate{
Range: Range{
From: Position{Index: 0, Line: 0, Col: 0},
To: Position{Index: 38, Line: 4, Col: 1},
},
Expression: Expression{
Value: "x()",
Range: Range{
Expand All @@ -657,6 +717,10 @@ func TestTemplateParser(t *testing.T) {
-->
}`,
expected: HTMLTemplate{
Range: Range{
From: Position{Index: 0, Line: 0, Col: 0},
To: Position{Index: 59, Line: 5, Col: 1},
},
Expression: Expression{
Value: "x()",
Range: Range{
Expand All @@ -681,6 +745,10 @@ func TestTemplateParser(t *testing.T) {
</span>
}`,
expected: HTMLTemplate{
Range: Range{
From: Position{Index: 0, Line: 0, Col: 0},
To: Position{Index: 95, Line: 4, Col: 1},
},
Expression: Expression{
Value: "Name(children templ.Attributes)",
Range: Range{
Expand Down
Loading

0 comments on commit 9de401c

Please sign in to comment.