1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

fmt: respect user choice of empty lines between type declarations (#9135)

This commit is contained in:
Lukas Neubert 2021-03-06 20:05:55 +01:00 committed by GitHub
parent fdcfe397d4
commit 053d6870d2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 16 additions and 5 deletions

View File

@ -334,6 +334,10 @@ fn (f Fmt) should_insert_newline_before_node(node ast.Node, prev_node ast.Node)
if prev_stmt is ast.StructDecl {
return true
}
// Empty line after an block of type declarations
if prev_stmt is ast.TypeDecl && stmt !is ast.TypeDecl {
return true
}
// Imports are handled special hence they are ignored here
if stmt is ast.Import || prev_stmt is ast.Import {
return false
@ -481,6 +485,7 @@ pub fn (mut f Fmt) type_decl(node ast.TypeDecl) {
ast.FnTypeDecl { f.fn_type_decl(node) }
ast.SumTypeDecl { f.sum_type_decl(node) }
}
f.writeln('')
}
pub fn (mut f Fmt) alias_type_decl(node ast.AliasTypeDecl) {
@ -491,7 +496,6 @@ pub fn (mut f Fmt) alias_type_decl(node ast.AliasTypeDecl) {
f.write('type $node.name = $ptype')
f.comments(node.comments, has_nl: false)
f.writeln('\n')
}
pub fn (mut f Fmt) fn_type_decl(node ast.FnTypeDecl) {
@ -539,7 +543,6 @@ pub fn (mut f Fmt) fn_type_decl(node ast.FnTypeDecl) {
}
f.comments(node.comments, has_nl: false)
f.writeln('\n')
}
pub fn (mut f Fmt) sum_type_decl(node ast.SumTypeDecl) {
@ -563,7 +566,6 @@ pub fn (mut f Fmt) sum_type_decl(node ast.SumTypeDecl) {
}
f.comments(node.comments, has_nl: false)
f.writeln('\n')
}
[inline]

View File

@ -1,3 +1,6 @@
type MyInt = int
type EmptyLineAfterLastType = fn () int
struct EmptyLineAfterStructs {}
fn empty_line_after_functions() {}

View File

@ -1,3 +1,5 @@
type MyInt = int
type EmptyLineAfterLastType = fn() int
struct EmptyLineAfterStructs {}
fn empty_line_after_functions() {}
fn squash_multiple_empty_lines() {

View File

@ -1,3 +1,9 @@
// Keep empty lines between types
type Expr = IfExpr | IntegerLiteral
type Node2 = Expr | string
type MyInt = int
// Keep empty lines in const blocks
const (
_ = SomeStruct{

View File

@ -1,6 +1,5 @@
// Sumtype
type FooBar = Bar | Foo
pub type PublicBar = Bar | Foo | FooBar
type Uint = byte | u16 | u32 | u64 // This should stay on the same line
@ -15,7 +14,6 @@ pub type Abc = f32
// Fn type decl
type EmptyFn = fn ()
type OneArgFn = fn (i int)
type TwoDiffArgs = fn (i int, s string) bool