From 0f8f8bd90da6250d67136b6f97820b4a48d7dc5b Mon Sep 17 00:00:00 2001 From: Lukas Neubert Date: Wed, 3 Mar 2021 23:56:40 +0100 Subject: [PATCH] fmt: force empty lines after struct declarations and most functions (#9096) --- vlib/v/fmt/fmt.v | 15 ++++++++------- vlib/v/fmt/tests/empty_lines_expected.vv | 4 ++++ vlib/v/fmt/tests/empty_lines_input.vv | 2 ++ vlib/vweb/route_test.v | 3 +++ 4 files changed, 17 insertions(+), 7 deletions(-) diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index 327f469ad2..3ed4d1ac68 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -324,21 +324,22 @@ fn (f Fmt) should_insert_newline_before_node(node ast.Node, prev_node ast.Node) if prev_stmt is ast.HashStmt && stmt !is ast.HashStmt && stmt !is ast.ExprStmt { return true } - // Force a newline after a block of function declarations + // Force a newline after function declarations + // The only exception is inside an block of no_body functions if prev_stmt is ast.FnDecl { - if prev_stmt.no_body && stmt !is ast.FnDecl { + if stmt !is ast.FnDecl || !prev_stmt.no_body { return true } } - // The stmt shouldn't have a newline before - // if stmt.position().line_nr - prev_line_nr <= 1 { - // return false - // } + // Force a newline after struct declarations + if prev_stmt is ast.StructDecl { + return true + } // Imports are handled special hence they are ignored here if stmt is ast.Import || prev_stmt is ast.Import { return false } - // Attributes are not respected in the stmts position, so this requires a manual check + // Attributes are not respected in the stmts position, so this requires manual checking if stmt is ast.StructDecl { if stmt.attrs.len > 0 && stmt.attrs[0].pos.line_nr - prev_line_nr <= 1 { return false diff --git a/vlib/v/fmt/tests/empty_lines_expected.vv b/vlib/v/fmt/tests/empty_lines_expected.vv index f9328e0ff1..aa0d82b715 100644 --- a/vlib/v/fmt/tests/empty_lines_expected.vv +++ b/vlib/v/fmt/tests/empty_lines_expected.vv @@ -1,3 +1,7 @@ +struct EmptyLineAfterStructs {} + +fn empty_line_after_functions() {} + fn squash_multiple_empty_lines() { println('a') diff --git a/vlib/v/fmt/tests/empty_lines_input.vv b/vlib/v/fmt/tests/empty_lines_input.vv index acd8b164f5..c9b74b5dac 100644 --- a/vlib/v/fmt/tests/empty_lines_input.vv +++ b/vlib/v/fmt/tests/empty_lines_input.vv @@ -1,3 +1,5 @@ +struct EmptyLineAfterStructs {} +fn empty_line_after_functions() {} fn squash_multiple_empty_lines() { println('a') diff --git a/vlib/vweb/route_test.v b/vlib/vweb/route_test.v index 31a88ba10e..7ca375cf7c 100644 --- a/vlib/vweb/route_test.v +++ b/vlib/vweb/route_test.v @@ -10,13 +10,16 @@ fn (rp RoutePair) test() ?[]string { route := rp.route.split('/').filter(it != '') return route_matches(url, route) } + fn (rp RoutePair) test_match() { rp.test() or { panic('should match: $rp') } } + fn (rp RoutePair) test_no_match() { rp.test() or { return } panic('should not match: $rp') } + fn (rp RoutePair) test_param(expected []string) { res := rp.test() or { panic('should match: $rp') } assert res == expected