Skip to content

Commit 16e79bc

Browse files
author
Lukas Neubert
authored
tools: format most examples and tutorials, add them to v test-cleancode (#9826)
1 parent dff5068 commit 16e79bc

File tree

38 files changed

+471
-433
lines changed

38 files changed

+471
-433
lines changed

cmd/tools/vtest-cleancode.v

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,18 @@ const (
1818
'examples/term.ui',
1919
]
2020
verify_known_failing_exceptions = [
21+
// Handcrafted meaningful formatting of code parts (mostly arrays)
22+
'examples/sokol/02_cubes_glsl/cube_glsl.v',
23+
'examples/sokol/03_march_tracing_glsl/rt_glsl.v',
24+
'examples/sokol/04_multi_shader_glsl/rt_glsl.v',
25+
'examples/sokol/05_instancing_glsl/rt_glsl.v',
26+
'vlib/gg/m4/graphic.v',
27+
'vlib/gg/m4/m4_test.v',
28+
'vlib/gg/m4/matrix.v',
2129
'vlib/builtin/int_test.v' /* special number formatting that should be tested */,
30+
// TODOs and unfixed vfmt bugs
2231
'vlib/builtin/int.v' /* TODO byteptr: vfmt converts `pub fn (nn byteptr) str() string {` to `nn &byte` and that conflicts with `nn byte` */,
2332
'vlib/builtin/string_charptr_byteptr_helpers.v' /* TODO byteptr: a temporary shim to ease the byteptr=>&byte transition */,
24-
'vlib/gg/m4/graphic.v' /* has hand crafted meaningful formatting of matrices */,
25-
'vlib/gg/m4/m4_test.v' /* has hand crafted meaningful formatting of matrices */,
26-
'vlib/gg/m4/matrix.v' /* has hand crafted meaningful formatting of matrices */,
2733
'vlib/v/tests/array_append_short_struct_test.v', /* extra empty line */
2834
'vlib/v/tests/fixed_array_const_size_test.v', /* fixed arr type is changed */
2935
'vlib/v/tests/fn_high_test.v', /* param name removed */
@@ -34,9 +40,13 @@ const (
3440
'vlib/v/tests/string_interpolation_test.v' /* TODO byteptr: &byte.str() behaves differently than byteptr.str() */,
3541
'vlib/v/gen/js/tests/js.v', /* local `hello` fn, gets replaced with module `hello` aliased as `hl` */
3642
'vlib/v/gen/c/cheaders.v' /* the preprocessor directives are formated to the V standard, even though they are in a string literal */,
43+
'examples/c_interop_wkhtmltopdf.v', /* &charptr --> &&char */
44+
'examples/path_tracing.v', /* block --> line comments corrupts code */
3745
]
3846
vfmt_verify_list = [
3947
'cmd/',
48+
'examples/',
49+
'tutorials/',
4050
'vlib/arrays/',
4151
'vlib/benchmark/',
4252
'vlib/bitfield/',

examples/asm.v

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ fn main() {
66
mov eax, a
77
add eax, b
88
mov c, eax
9-
; =r (c) // output
10-
; r (a) // input
9+
; =r (c) // output
10+
; r (a) // input
1111
r (b)
1212
}
1313
println('a: $a') // 100

examples/cli.v

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ fn main() {
3232
description: 'Number of times the message gets printed.'
3333
})
3434
greet_cmd.add_flag(Flag{
35-
flag: .string_array
36-
name: 'fun'
37-
description: 'Just a dumby flags to show multiple.'
35+
flag: .string_array
36+
name: 'fun'
37+
description: 'Just a dumby flags to show multiple.'
3838
})
3939
cmd.add_command(greet_cmd)
4040
cmd.setup()

examples/compiletime/compile-time-for.v

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
struct App {}
22

33
fn (mut app App) method_one() {}
4-
fn (mut app App) method_two() int { return 0 }
5-
fn (mut app App) method_three(s string) string { return s }
4+
5+
fn (mut app App) method_two() int {
6+
return 0
7+
}
8+
9+
fn (mut app App) method_three(s string) string {
10+
return s
11+
}
612

713
fn main() {
814
$for method in App.methods {
9-
$if method.typ is fn(string) string {
15+
$if method.typ is fn (string) string {
1016
println('$method.name IS `fn(string) string`')
1117
} $else {
1218
println('$method.name is NOT `fn(string) string`')
@@ -17,12 +23,12 @@ fn main() {
1723
println('$method.name DOES return `int`')
1824
}
1925
$if method.args[0].typ !is string {
20-
println("${method.name}'s first arg is NOT `string`")
26+
println("$method.name's first arg is NOT `string`")
2127
} $else {
22-
println("${method.name}'s first arg IS `string`")
28+
println("$method.name's first arg IS `string`")
2329
}
2430
// TODO: Double inversion, should this even be allowed?
25-
$if method.typ is fn() {
31+
$if method.typ is fn () {
2632
println('$method.name IS a void method')
2733
} $else {
2834
println('$method.name is NOT a void method')

examples/concurrency/concurrency_http.v

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import time
44

55
fn vlang_time(mut wg sync.WaitGroup) ?string {
66
start := time.ticks()
7-
data := http.get('https://vlang.io/utc_now')?
7+
data := http.get('https://vlang.io/utc_now') ?
88
finish := time.ticks()
99
println('Finish getting time ${finish - start} ms')
1010
println(data.text)
@@ -14,7 +14,7 @@ fn vlang_time(mut wg sync.WaitGroup) ?string {
1414

1515
fn remote_ip(mut wg sync.WaitGroup) ?string {
1616
start := time.ticks()
17-
data := http.get('https://api.ipify.org')?
17+
data := http.get('https://api.ipify.org') ?
1818
finish := time.ticks()
1919
println('Finish getting ip ${finish - start} ms')
2020
println(data.text)

examples/database/sqlite.v

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import sqlite
22

33
fn main() {
4-
db := sqlite.connect(':memory:')?
4+
db := sqlite.connect(':memory:') ?
55
db.exec("create table users (id integer primary key, name text default '');")
66

77
db.exec("insert into users (name) values ('Sam')")
@@ -15,7 +15,7 @@ fn main() {
1515
assert name == 'Sam'
1616

1717
users, code := db.exec('select * from users')
18-
println("SQL Result code: $code")
18+
println('SQL Result code: $code')
1919
for row in users {
2020
println(row.vals)
2121
}

examples/eventbus/eventbus.v

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ module main
22

33
import some_module
44

5-
fn main(){
5+
fn main() {
66
mut sub := some_module.get_subscriber()
7-
sub.subscribe("error", on_error)
7+
sub.subscribe('error', on_error)
88
some_module.do_work()
99
}
1010

examples/eventbus/modules/some_module/some_module.v

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,19 @@ pub:
1616
message string
1717
}
1818

19-
pub fn do_work(){
19+
pub fn do_work() {
2020
work := Work{20}
21-
for i in 0..20 {
22-
println("working...")
21+
for i in 0 .. 20 {
22+
println('working...')
2323
if i == 15 {
24-
error := &MyError{"There was an error."}
25-
eb.publish("error", work, error)
26-
eb.publish("error", work, error)
24+
error := &MyError{'There was an error.'}
25+
some_module.eb.publish('error', work, error)
26+
some_module.eb.publish('error', work, error)
2727
return
2828
}
2929
}
3030
}
3131

3232
pub fn get_subscriber() eventbus.Subscriber {
33-
return *eb.subscriber
33+
return *some_module.eb.subscriber
3434
}

examples/fibonacci.v

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33

44
fn main() {
55
// Check for user input
6-
//if os.args.len != 2 {
7-
// println('usage: fibonacci [rank]')
6+
// if os.args.len != 2 {
7+
// println('usage: fibonacci [rank]')
88

9-
// Exit
10-
// return
11-
// }
9+
// Exit
10+
// return
11+
// }
1212

1313
// Parse first argument and cast it to int
14-
// stop := os.args[1].int()
14+
// stop := os.args[1].int()
1515
stop := 23
1616
// Can only calculate correctly until rank 92
1717
if stop > 92 {
@@ -23,7 +23,7 @@ fn main() {
2323
mut a := 0
2424
mut b := 0
2525
mut c := 1
26-
println(a+c+c)
26+
println(a + c + c)
2727
for _ in 0 .. stop {
2828
// Set a and b to the next term
2929
a = b

examples/fireworks/fireworks.v

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,18 @@ fn on_frame(mut app App) {
6161

6262
fn on_event(e &gg.Event, mut app App) {
6363
match e.typ {
64-
.resized, .resumed { app.resize() }
65-
.iconified { app.draw_flag = false }
66-
.restored {
64+
.resized, .resumed {
65+
app.resize()
66+
}
67+
.iconified {
68+
app.draw_flag = false
69+
}
70+
.restored {
6771
app.draw_flag = true
6872
app.resize()
69-
}
73+
}
7074
else {
71-
//println("Type ${e.typ}")
75+
// println("Type ${e.typ}")
7276
}
7377
}
7478
}
@@ -80,16 +84,17 @@ fn (mut app App) resize() {
8084
return
8185
}
8286
mut s := gg.dpi_scale()
83-
87+
8488
if s == 0.0 {
8589
s = 1.0
86-
}
90+
}
8791
app.ui.dpi_scale = s
8892
app.ui.width = size.width
8993
app.ui.height = size.height
9094
}
9195

92-
[console] // is needed for easier diagnostics on windows
96+
// is needed for easier diagnostics on windows
97+
[console]
9398
fn main() {
9499
mut font_path := os.resource_abs_path(os.join_path('../assets/fonts/', 'RobotoMono-Regular.ttf'))
95100
$if android {

0 commit comments

Comments
 (0)