fmt: combine simple attributes into a single line (#9120)

pull/9128/head
Lukas Neubert 2021-03-05 06:17:57 +01:00 committed by GitHub
parent 208cabc994
commit 80356edd8c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 47 additions and 16 deletions

View File

@ -211,8 +211,7 @@ pub fn (mut a array) trim(index int) {
}
// we manually inline this for single operations for performance without -prod
[inline]
[unsafe]
[inline; unsafe]
fn (a array) get_unsafe(i int) voidptr {
unsafe {
return byteptr(a.data) + i * a.element_size
@ -398,8 +397,7 @@ fn (a &array) slice_clone(start int, _end int) array {
}
// we manually inline this for single operations for performance without -prod
[inline]
[unsafe]
[inline; unsafe]
fn (mut a array) set_unsafe(i int, val voidptr) {
unsafe { C.memcpy(byteptr(a.data) + a.element_size * i, val, a.element_size) }
}

View File

@ -761,7 +761,7 @@ pub fn (mut f Fmt) struct_decl(node ast.StructDecl) {
has_attrs := field.attrs.len > 0
if has_attrs {
f.write(strings.repeat(` `, field_align.max_type_len - field_types[i].len))
f.inline_attrs(field.attrs)
f.single_line_attrs(field.attrs, inline: true)
}
if field.has_default_expr {
mut align := default_expr_aligns[default_expr_align_i]
@ -1098,24 +1098,43 @@ pub fn (mut f Fmt) or_expr(node ast.OrExpr) {
}
}
fn (mut f Fmt) attrs(attrs []table.Attr) {
for attr in attrs {
pub fn (mut f Fmt) attrs(attrs []table.Attr) {
mut sorted_attrs := attrs.clone()
// Sort the attributes. The ones with arguments come first.
sorted_attrs.sort(a.arg.len > b.arg.len)
for i, attr in sorted_attrs {
if attr.arg.len == 0 {
f.single_line_attrs(sorted_attrs[i..], {})
break
}
f.writeln('[$attr]')
}
}
fn (mut f Fmt) inline_attrs(attrs []table.Attr) {
pub struct AttrsOptions {
inline bool
}
pub fn (mut f Fmt) single_line_attrs(attrs []table.Attr, options AttrsOptions) {
if attrs.len == 0 {
return
}
f.write(' [')
for i, attr in attrs {
mut sorted_attrs := attrs.clone()
sorted_attrs.sort(a.name < b.name)
if options.inline {
f.write(' ')
}
f.write('[')
for i, attr in sorted_attrs {
if i > 0 {
f.write('; ')
}
f.write('$attr')
}
f.write(']')
if !options.inline {
f.writeln('')
}
}
fn inline_attrs_len(attrs []table.Attr) int {

View File

@ -0,0 +1,4 @@
[export: 'JNICALL Java_io_vlang_V_callStaticMethods']
[tom: 'jerry']
[direct_array_access; inline; unsafe]
fn heavily_tagged() {}

View File

@ -0,0 +1,6 @@
[inline]
[export: 'JNICALL Java_io_vlang_V_callStaticMethods']
[direct_array_access]
[unsafe]
[tom: 'jerry']
fn heavily_tagged() {}

View File

@ -1,18 +1,23 @@
[inline]
[if debug]
[foo: bar]
[deprecated: 'use bar() instead']
[foo: bar]
[if debug; inline]
fn keep_attributes() {
println('hi !')
}
[bar: 'foo']
fn attr_with_arg() {}
['a_string_name']
fn name_only_attr() {}
struct User {
age int
nums []int
last_name string [json: lastName]
is_registered bool [json: IsRegistered]
typ int [json: 'type']
pets string [raw; json: 'pet_animals']
pets string [json: 'pet_animals'; raw]
}
[_allow_multiple_values]

View File

@ -78,8 +78,7 @@ pub fn (mut app App) user_repo_settings(username string, repository string) vweb
return app.html('username: $username | repository: $repository')
}
[post]
['/json_echo']
['/json_echo'; post]
pub fn (mut app App) json_echo() vweb.Result {
// eprintln('>>>>> received http request at /json_echo is: $app.req')
app.set_content_type(app.req.headers['Content-Type'])