v.fmt: keep comments after struct embeds

pull/10508/head
Delyan Angelov 2021-06-18 18:42:05 +03:00
parent de384f1cc8
commit d56ae2d508
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
6 changed files with 142 additions and 151 deletions

View File

@ -33,7 +33,6 @@ const (
'vlib/builtin/int.v' /* TODO byteptr: vfmt converts `pub fn (nn byteptr) str() string {` to `nn &byte` and that conflicts with `nn byte` */, 'vlib/builtin/int.v' /* TODO byteptr: vfmt converts `pub fn (nn byteptr) str() string {` to `nn &byte` and that conflicts with `nn byte` */,
'vlib/builtin/string_charptr_byteptr_helpers.v' /* TODO byteptr: a temporary shim to ease the byteptr=>&byte transition */, 'vlib/builtin/string_charptr_byteptr_helpers.v' /* TODO byteptr: a temporary shim to ease the byteptr=>&byte transition */,
'vlib/v/tests/interop_test.v', /* bad comment formatting */ 'vlib/v/tests/interop_test.v', /* bad comment formatting */
'vlib/v/tests/string_interpolation_test.v' /* TODO byteptr: &byte.str() behaves differently than byteptr.str() */,
'vlib/v/gen/js/tests/js.v', /* local `hello` fn, gets replaced with module `hello` aliased as `hl` */ 'vlib/v/gen/js/tests/js.v', /* local `hello` fn, gets replaced with module `hello` aliased as `hl` */
] ]
vfmt_verify_list = [ vfmt_verify_list = [
@ -43,7 +42,6 @@ const (
'vlib/', 'vlib/',
] ]
vfmt_known_failing_exceptions = arrays.merge(verify_known_failing_exceptions, [ vfmt_known_failing_exceptions = arrays.merge(verify_known_failing_exceptions, [
'vlib/term/ui/input.v' /* comment after a struct embed is removed */,
'vlib/regex/regex_test.v' /* contains meaningfull formatting of the test case data */, 'vlib/regex/regex_test.v' /* contains meaningfull formatting of the test case data */,
'vlib/readline/readline_test.v' /* vfmt eats `{ Readline }` from `import readline { Readline }` */, 'vlib/readline/readline_test.v' /* vfmt eats `{ Readline }` from `import readline { Readline }` */,
'vlib/glm/glm.v' /* `mut res &f32` => `mut res f32`, which then fails to compile */, 'vlib/glm/glm.v' /* `mut res &f32` => `mut res f32`, which then fails to compile */,

View File

@ -6,122 +6,114 @@ module ui
import os import os
pub enum KeyCode { pub enum KeyCode {
null = 0 null = 0
tab = 9 tab = 9
enter = 10 enter = 10
escape = 27 escape = 27
space = 32 space = 32
backspace = 127 backspace = 127
exclamation = 33
exclamation = 33 double_quote = 34
double_quote = 34 hashtag = 35
hashtag = 35 dollar = 36
dollar = 36 percent = 37
percent = 37 ampersand = 38
ampersand = 38 single_quote = 39
single_quote = 39 left_paren = 40
left_paren = 40 right_paren = 41
right_paren = 41 asterisk = 42
asterisk = 42 plus = 43
plus = 43 comma = 44
comma = 44 minus = 45
minus = 45 period = 46
period = 46 slash = 47
slash = 47 _0 = 48
_1 = 49
_0 = 48 _2 = 50
_1 = 49 _3 = 51
_2 = 50 _4 = 52
_3 = 51 _5 = 53
_4 = 52 _6 = 54
_5 = 53 _7 = 55
_6 = 54 _8 = 56
_7 = 55 _9 = 57
_8 = 56 colon = 58
_9 = 57 semicolon = 59
less_than = 60
colon = 58 equal = 61
semicolon = 59 greater_than = 62
less_than = 60 question_mark = 63
equal = 61 at = 64
greater_than = 62 a = 97
question_mark = 63 b = 98
at = 64 c = 99
d = 100
a = 97 e = 101
b = 98 f = 102
c = 99 g = 103
d = 100 h = 104
e = 101 i = 105
f = 102 j = 106
g = 103 k = 107
h = 104 l = 108
i = 105 m = 109
j = 106 n = 110
k = 107 o = 111
l = 108 p = 112
m = 109 q = 113
n = 110 r = 114
o = 111 s = 115
p = 112 t = 116
q = 113 u = 117
r = 114 v = 118
s = 115 w = 119
t = 116 x = 120
u = 117 y = 121
v = 118 z = 122
w = 119 left_square_bracket = 91
x = 120 backslash = 92
y = 121
z = 122
left_square_bracket = 91
backslash = 92
right_square_bracket = 93 right_square_bracket = 93
caret = 94 caret = 94
underscore = 95 underscore = 95
backtick = 96 backtick = 96
left_curly_bracket = 123
left_curly_bracket = 123 vertical_bar = 124
vertical_bar = 124 right_curly_bracket = 125
right_curly_bracket = 125 tilde = 126
tilde = 126 insert = 260
delete = 261
insert = 260 up = 262
delete = 261 down = 263
up = 262 right = 264
down = 263 left = 265
right = 264 page_up = 266
left = 265 page_down = 267
page_up = 266 home = 268
page_down = 267 end = 269
home = 268 f1 = 290
end = 269 f2 = 291
f3 = 292
f1 = 290 f4 = 293
f2 = 291 f5 = 294
f3 = 292 f6 = 295
f4 = 293 f7 = 296
f5 = 294 f8 = 297
f6 = 295 f9 = 298
f7 = 296 f10 = 299
f8 = 297 f11 = 300
f9 = 298 f12 = 301
f10 = 299 f13 = 302
f11 = 300 f14 = 303
f12 = 301 f15 = 304
f13 = 302 f16 = 305
f14 = 303 f17 = 306
f15 = 304 f18 = 307
f16 = 305 f19 = 308
f17 = 306 f20 = 309
f18 = 307 f21 = 310
f19 = 308 f22 = 311
f20 = 309 f23 = 312
f21 = 310 f24 = 313
f22 = 311
f23 = 312
f24 = 313
} }
pub enum Direction { pub enum Direction {
@ -159,34 +151,31 @@ pub enum Modifiers {
pub struct Event { pub struct Event {
pub: pub:
typ EventType typ EventType
// Mouse event info // Mouse event info
x int x int
y int y int
button MouseButton button MouseButton
direction Direction direction Direction
// Keyboard event info // Keyboard event info
code KeyCode code KeyCode
modifiers Modifiers modifiers Modifiers
ascii byte ascii byte
utf8 string utf8 string
// Resized event info // Resized event info
width int width int
height int height int
} }
pub struct Context { pub struct Context {
ExtraContext // contains fields specific to an implementation ExtraContext // contains fields specific to an implementation
pub: pub:
cfg Config // adsasdas cfg Config // adsasdas
mut: mut:
print_buf []byte print_buf []byte
paused bool paused bool
enable_su bool enable_su bool
enable_rgb bool enable_rgb bool
pub mut: pub mut:
frame_count u64 frame_count u64
window_width int window_width int
@ -194,16 +183,16 @@ pub mut:
} }
pub struct Config { pub struct Config {
user_data voidptr user_data voidptr
init_fn fn(voidptr) init_fn fn (voidptr)
frame_fn fn(voidptr) frame_fn fn (voidptr)
cleanup_fn fn(voidptr) cleanup_fn fn (voidptr)
event_fn fn(&Event, voidptr) event_fn fn (&Event, voidptr)
fail_fn fn(string) fail_fn fn (string)
buffer_size int = 256 buffer_size int = 256
frame_rate int = 30 frame_rate int = 30
use_x11 bool use_x11 bool
window_title string window_title string
hide_cursor bool hide_cursor bool
@ -211,7 +200,9 @@ pub struct Config {
use_alternate_buffer bool = true use_alternate_buffer bool = true
skip_init_checks bool skip_init_checks bool
// All kill signals to set up exit listeners on: // All kill signals to set up exit listeners on:
reset []os.Signal = [.hup, .int, .quit, .ill, .abrt, .bus, .fpe, .kill, .segv, .pipe, .alrm, .term, .stop] reset []os.Signal = [.hup, .int, .quit, .ill, .abrt, .bus, .fpe, .kill, .segv, .pipe, .alrm, .term,
.stop,
]
} }
[inline] [inline]

View File

@ -142,7 +142,12 @@ pub fn (mut f Fmt) struct_decl(node ast.StructDecl) {
for embed in node.embeds { for embed in node.embeds {
f.mark_types_import_as_used(embed.typ) f.mark_types_import_as_used(embed.typ)
styp := f.table.type_to_str_using_aliases(embed.typ, f.mod2alias) styp := f.table.type_to_str_using_aliases(embed.typ, f.mod2alias)
f.writeln('\t$styp') if embed.comments.len == 0 {
f.writeln('\t$styp')
} else {
f.write('\t$styp')
f.comments(embed.comments, level: .indent)
}
} }
mut field_align_i := 0 mut field_align_i := 0
mut comment_align_i := 0 mut comment_align_i := 0

View File

@ -5,13 +5,14 @@ struct Foo {
struct Test {} struct Test {}
struct Bar { struct Bar {
Foo Foo // comment for Foo
Test Test // comment for Test
// another comment for Test
y int y int
z string z string
} }
struct Baz { struct Baz {
Foo Foo // Another comment for Foo
Test Test
} }

View File

@ -187,12 +187,7 @@ fn (mut p Parser) struct_decl() ast.StructDecl {
// struct embedding // struct embedding
type_pos = p.tok.position() type_pos = p.tok.position()
typ = p.parse_type() typ = p.parse_type()
for p.tok.kind == .comment { ecomments := p.eat_comments({})
comments << p.comment()
if p.tok.kind == .rcbr {
break
}
}
type_pos = type_pos.extend(p.prev_tok.position()) type_pos = type_pos.extend(p.prev_tok.position())
if !is_on_top { if !is_on_top {
p.error_with_pos('struct embedding must be declared at the beginning of the struct body', p.error_with_pos('struct embedding must be declared at the beginning of the struct body',
@ -214,6 +209,7 @@ fn (mut p Parser) struct_decl() ast.StructDecl {
embeds << ast.Embed{ embeds << ast.Embed{
typ: typ typ: typ
pos: type_pos pos: type_pos
comments: ecomments
} }
} else { } else {
// struct field // struct field

View File

@ -94,11 +94,11 @@ fn test_inttypes_string_interpolation() {
i := -1622999040 // -0x60BD 0000 i := -1622999040 // -0x60BD 0000
ui := u32(3421958087) // 0xCBF6 EFC7 ui := u32(3421958087) // 0xCBF6 EFC7
vp := voidptr(ui) vp := voidptr(ui)
mut bp := byteptr(0) mut bp := &byte(0)
$if x64 { $if x64 {
bp = byteptr(15541149836) // 0x3 9E53 208C bp = &byte(15541149836) // 0x3 9E53 208C
} $else { } $else {
bp = byteptr(3541149836) // 0xD311 A88C bp = &byte(3541149836) // 0xD311 A88C
} }
l := i64(-7694555558525237396) l := i64(-7694555558525237396)
ul := u64(17234006112912956370) ul := u64(17234006112912956370)