From 2cf024b913070b56ac2a7efa09f8a1e15fbec72a Mon Sep 17 00:00:00 2001 From: Turiiya <34311583+ttytm@users.noreply.github.com> Date: Sat, 28 Oct 2023 12:07:14 +0200 Subject: [PATCH] fmt: add single-line ternary expr support for constants (#19681) --- vlib/v/fmt/fmt.v | 3 ++- vlib/v/fmt/tests/comptime_if_expr_script_keep.vv | 2 ++ vlib/v/fmt/tests/if_ternary_expected.vv | 16 ++++++++++++++++ vlib/v/fmt/tests/if_ternary_input.vv | 6 ++++++ vlib/v/fmt/tests/if_ternary_keep.vv | 4 ++++ 5 files changed, 30 insertions(+), 1 deletion(-) diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index 140e9f4dc..73e8f1f27 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -2225,7 +2225,8 @@ pub fn (mut f Fmt) if_expr(node ast.IfExpr) { f.inside_comptime_if = node.is_comptime mut is_ternary := node.branches.len == 2 && node.has_else && branch_is_single_line(node.branches[0]) && branch_is_single_line(node.branches[1]) - && (node.is_expr || f.is_assign || f.is_struct_init || f.single_line_fields) + && (node.is_expr || f.is_assign || f.inside_const || f.is_struct_init + || f.single_line_fields) f.single_line_if = is_ternary start_pos := f.out.len start_len := f.line_len diff --git a/vlib/v/fmt/tests/comptime_if_expr_script_keep.vv b/vlib/v/fmt/tests/comptime_if_expr_script_keep.vv index a9e64e9d6..5d73fb2e9 100644 --- a/vlib/v/fmt/tests/comptime_if_expr_script_keep.vv +++ b/vlib/v/fmt/tests/comptime_if_expr_script_keep.vv @@ -1,3 +1,5 @@ +const enable_debug = $if debug { true } $else { false } + mut pre_built_str := '' $if prebuilt ? { the_date := if $env('DATE') != '' { $env('DATE') } else { '##DATE##' } diff --git a/vlib/v/fmt/tests/if_ternary_expected.vv b/vlib/v/fmt/tests/if_ternary_expected.vv index e100a4aad..473233557 100644 --- a/vlib/v/fmt/tests/if_ternary_expected.vv +++ b/vlib/v/fmt/tests/if_ternary_expected.vv @@ -1,3 +1,19 @@ +const ( + spacing_error = if true { true } else { false } + too_long_line = if b.no_cstep { + 'TMP1/${b.nexpected_steps:1d}' + } else { + '${b.cstep:1d}/${b.nexpected_steps:1d}' + } + too_many_branches = if true { + true + } else if true { + true + } else { + false + } +) + fn main() { // This line is too long sprogress := if b.no_cstep { diff --git a/vlib/v/fmt/tests/if_ternary_input.vv b/vlib/v/fmt/tests/if_ternary_input.vv index c76a8ffdc..36a8b5c63 100644 --- a/vlib/v/fmt/tests/if_ternary_input.vv +++ b/vlib/v/fmt/tests/if_ternary_input.vv @@ -1,3 +1,9 @@ +const ( + spacing_error = if true {true}else{ false} + too_long_line = if b.no_cstep { 'TMP1/${b.nexpected_steps:1d}' } else { '${b.cstep:1d}/${b.nexpected_steps:1d}' } + too_many_branches = if true { true } else if true { true } else { false } +) + fn main() { // This line is too long sprogress := if b.no_cstep { 'TMP1/${b.nexpected_steps:1d}' } else { '${b.cstep:1d}/${b.nexpected_steps:1d}' } diff --git a/vlib/v/fmt/tests/if_ternary_keep.vv b/vlib/v/fmt/tests/if_ternary_keep.vv index 8352cc52e..1dae22f43 100644 --- a/vlib/v/fmt/tests/if_ternary_keep.vv +++ b/vlib/v/fmt/tests/if_ternary_keep.vv @@ -1,3 +1,7 @@ +import os + +const exe_extension = if os.user_os() == 'windows' { '.exe' } else { '' } + struct Foo { n int s string -- 2.39.5