From aef3643ab892056ca6920a0ccfcb8fd300283d12 Mon Sep 17 00:00:00 2001 From: shove Date: Wed, 13 Dec 2023 15:46:08 +0800 Subject: [PATCH] fmt: add linebreak after const decl (fix #20157) (#20162) --- vlib/v/fmt/fmt.v | 5 +++++ .../tests/consts_after_linebreak_expected.vv | 18 ++++++++++++++++++ .../fmt/tests/consts_after_linebreak_input.vv | 15 +++++++++++++++ vlib/v/fmt/tests/donut_card_expected.vv | 1 + 4 files changed, 39 insertions(+) create mode 100644 vlib/v/fmt/tests/consts_after_linebreak_expected.vv create mode 100644 vlib/v/fmt/tests/consts_after_linebreak_input.vv diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index 6994c3bce..163d234c6 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -422,6 +422,11 @@ fn (f Fmt) should_insert_newline_before_node(node ast.Node, prev_node ast.Node) ast.Import { return false } + ast.ConstDecl { + if node !is ast.ConstDecl && !(node is ast.ExprStmt && node.expr is ast.Comment) { + return true + } + } else {} } match node { diff --git a/vlib/v/fmt/tests/consts_after_linebreak_expected.vv b/vlib/v/fmt/tests/consts_after_linebreak_expected.vv new file mode 100644 index 000000000..b2f358682 --- /dev/null +++ b/vlib/v/fmt/tests/consts_after_linebreak_expected.vv @@ -0,0 +1,18 @@ +import os + +const foo = 'foo' +const bar = 1 +// ... +const a = 1 + +fn baz() { +} + +const b = '' + +fn foobar() { +} + +const c = '' // ... + +struct Struct {} diff --git a/vlib/v/fmt/tests/consts_after_linebreak_input.vv b/vlib/v/fmt/tests/consts_after_linebreak_input.vv new file mode 100644 index 000000000..08a740f8c --- /dev/null +++ b/vlib/v/fmt/tests/consts_after_linebreak_input.vv @@ -0,0 +1,15 @@ +import os + +const foo = 'foo' +const bar = 1 +// ... +const a = 1 +fn baz() { +} + +const b = '' +fn foobar() { +} + +const c = '' // ... +struct Struct {} diff --git a/vlib/v/fmt/tests/donut_card_expected.vv b/vlib/v/fmt/tests/donut_card_expected.vv index 53c92f84a..c184a01e4 100644 --- a/vlib/v/fmt/tests/donut_card_expected.vv +++ b/vlib/v/fmt/tests/donut_card_expected.vv @@ -4,6 +4,7 @@ import strings import time { sleep } const alt = '.,-~:;=!*#$@' + fn rd(cs int, ls int, a f64, b f64) { tt_spacing, p_spacing := 0.07, 0.02 r1, r2, k2 := 1, 2, 5 -- 2.39.5