From 460e06b9fff6cc29f0e0e638e9d8dfebd35bea90 Mon Sep 17 00:00:00 2001 From: Lukas Neubert Date: Wed, 3 Mar 2021 09:14:05 +0100 Subject: [PATCH] fmt: do not insert a newline before returned $vweb.html() (#9063) --- vlib/v/ast/ast.v | 14 +++++++------- vlib/v/fmt/tests/comptime_keep.vv | 9 +++++++-- vlib/v/parser/comptime.v | 13 +++++++++++++ 3 files changed, 27 insertions(+), 9 deletions(-) diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index 5165a5e214..a3bb7e03ad 100644 --- a/vlib/v/ast/ast.v +++ b/vlib/v/ast/ast.v @@ -1142,6 +1142,7 @@ pub: has_parens bool // if $() is used, for vfmt left Expr field_expr Expr + pos token.Position pub mut: left_type table.Type typ table.Type @@ -1149,6 +1150,7 @@ pub mut: pub struct ComptimeCall { pub: + pos token.Position has_parens bool // if $() is used, for vfmt method_name string method_pos token.Position @@ -1235,18 +1237,16 @@ pub fn (expr Expr) position() token.Position { return expr.decl.pos } ArrayDecompose, ArrayInit, AsCast, Assoc, AtExpr, BoolLiteral, CallExpr, CastExpr, ChanInit, - CharLiteral, ConcatExpr, Comment, EnumVal, FloatLiteral, GoExpr, Ident, IfExpr, IndexExpr, - IntegerLiteral, Likely, LockExpr, MapInit, MatchExpr, None, OffsetOf, OrExpr, ParExpr, - PostfixExpr, PrefixExpr, RangeExpr, SelectExpr, SelectorExpr, SizeOf, SqlExpr, StringInterLiteral, - StringLiteral, StructInit, Type, TypeOf, UnsafeExpr { + CharLiteral, ConcatExpr, Comment, ComptimeCall, ComptimeSelector, EnumVal, FloatLiteral, + GoExpr, Ident, IfExpr, IndexExpr, IntegerLiteral, Likely, LockExpr, MapInit, MatchExpr, + None, OffsetOf, OrExpr, ParExpr, PostfixExpr, PrefixExpr, RangeExpr, SelectExpr, SelectorExpr, + SizeOf, SqlExpr, StringInterLiteral, StringLiteral, StructInit, Type, TypeOf, UnsafeExpr + { return expr.pos } IfGuardExpr { return expr.expr.position() } - ComptimeCall, ComptimeSelector { - return expr.left.position() - } InfixExpr { left_pos := expr.left.position() right_pos := expr.right.position() diff --git a/vlib/v/fmt/tests/comptime_keep.vv b/vlib/v/fmt/tests/comptime_keep.vv index ae14a6e6ee..59f8b0c438 100644 --- a/vlib/v/fmt/tests/comptime_keep.vv +++ b/vlib/v/fmt/tests/comptime_keep.vv @@ -1,3 +1,5 @@ +import vweb + struct App { a string b string @@ -57,8 +59,7 @@ fn comptime_for_fields() { } } -struct Result { -} +struct Result {} fn (mut a App) my_method(p string) Result { println('>>>> ${@FN} | p: $p') @@ -81,6 +82,10 @@ fn comptime_call_dollar_method() { handle_conn(mut app) } +fn (mut app App) create() vweb.Result { + return $vweb.html() +} + fn main() { comptime_for() comptime_for_with_if() diff --git a/vlib/v/parser/comptime.v b/vlib/v/parser/comptime.v index 78c508f469..26b7ca9c0d 100644 --- a/vlib/v/parser/comptime.v +++ b/vlib/v/parser/comptime.v @@ -47,6 +47,7 @@ fn (mut p Parser) comp_call() ast.ComptimeCall { scope: 0 } p.check(.dollar) + start_pos := p.prev_tok.position() error_msg := 'only `\$tmpl()`, `\$env()`, `\$embed_file()` and `\$vweb.html()` comptime functions are supported right now' if p.peek_tok.kind == .dot { n := p.check_name() // skip `vweb.html()` TODO @@ -146,6 +147,14 @@ fn (mut p Parser) comp_call() ast.ComptimeCall { path += '.html' } if !os.exists(path) { + if p.pref.is_fmt { + return ast.ComptimeCall{ + scope: 0 + is_vweb: true + method_name: n + pos: start_pos.extend(p.prev_tok.position()) + } + } if is_html { p.error('vweb HTML template "$path" not found') } else { @@ -211,6 +220,7 @@ fn (mut p Parser) comp_call() ast.ComptimeCall { vweb_tmpl: file method_name: n args_var: literal_string_param + pos: start_pos.extend(p.prev_tok.position()) } } @@ -288,6 +298,7 @@ fn (mut p Parser) at() ast.AtExpr { fn (mut p Parser) comptime_selector(left ast.Expr) ast.Expr { p.check(.dollar) + start_pos := p.prev_tok.position() if p.peek_tok.kind == .lpar { method_pos := p.tok.position() method_name := p.check_name() @@ -311,6 +322,7 @@ fn (mut p Parser) comptime_selector(left ast.Expr) ast.Expr { method_pos: method_pos scope: p.scope args_var: args_var + pos: start_pos.extend(p.prev_tok.position()) } } mut has_parens := false @@ -328,5 +340,6 @@ fn (mut p Parser) comptime_selector(left ast.Expr) ast.Expr { has_parens: has_parens left: left field_expr: expr + pos: start_pos.extend(p.prev_tok.position()) } }