mirror of
https://github.com/vlang/v.git
synced 2025-08-04 02:07:28 -04:00
fmt: cleanup fields comments alignment and add ignore_newline config (#22027)
This commit is contained in:
parent
ac3045b472
commit
c69dfefedb
@ -76,15 +76,15 @@ fn main() {
|
|||||||
|
|
||||||
// setting values of app
|
// setting values of app
|
||||||
app.gg = gg.new_context(
|
app.gg = gg.new_context(
|
||||||
bg_color: gx.black // background color
|
bg_color: gx.black // background color
|
||||||
width: window_width // window width
|
width: window_width // window width
|
||||||
height: window_height // window height
|
height: window_height // window height
|
||||||
create_window: true // this will create a different window
|
create_window: true // this will create a different window
|
||||||
window_title: 'A* Path finding algorithm visusalizer' // title of the window
|
window_title: 'A* Path finding algorithm visusalizer' // title of the window
|
||||||
frame_fn: frame // this is frame function update the frame
|
frame_fn: frame // this is frame function update the frame
|
||||||
event_fn: on_event // it calls on every event
|
event_fn: on_event // it calls on every event
|
||||||
init_fn: init_images // run at start of application
|
init_fn: init_images // run at start of application
|
||||||
user_data: app // store user data
|
user_data: app // store user data
|
||||||
)
|
)
|
||||||
mut grid := initialise_grid() // initialize the grid variable and populate the matrix with each cell as empty
|
mut grid := initialise_grid() // initialize the grid variable and populate the matrix with each cell as empty
|
||||||
app.grid = grid // set grid to app attribute so you can access it by just passing app variable or with method of app
|
app.grid = grid // set grid to app attribute so you can access it by just passing app variable or with method of app
|
||||||
|
@ -190,12 +190,12 @@ fn (data &StrIntpData) process_str_intp_data(mut sb strings.Builder) {
|
|||||||
sign_set := sign == 1
|
sign_set := sign == 1
|
||||||
|
|
||||||
mut bf := strconv.BF_param{
|
mut bf := strconv.BF_param{
|
||||||
pad_ch: pad_ch // padding char
|
pad_ch: pad_ch // padding char
|
||||||
len0: len0_set // default len for whole the number or string
|
len0: len0_set // default len for whole the number or string
|
||||||
len1: len1_set // number of decimal digits, if needed
|
len1: len1_set // number of decimal digits, if needed
|
||||||
positive: true // mandatory: the sign of the number passed
|
positive: true // mandatory: the sign of the number passed
|
||||||
sign_flag: sign_set // flag for print sign as prefix in padding
|
sign_flag: sign_set // flag for print sign as prefix in padding
|
||||||
align: .left // alignment of the string
|
align: .left // alignment of the string
|
||||||
rm_tail_zero: tail_zeros // false // remove the tail zeros from floats
|
rm_tail_zero: tail_zeros // false // remove the tail zeros from floats
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -259,8 +259,8 @@ import encoding.csv
|
|||||||
fn main() {
|
fn main() {
|
||||||
file_path := 'big2.csv'
|
file_path := 'big2.csv'
|
||||||
mut csvr := csv.csv_reader(
|
mut csvr := csv.csv_reader(
|
||||||
file_path: file_path // path to the file CSV
|
file_path: file_path // path to the file CSV
|
||||||
mem_buf_size: 1024 * 1024 * 64 // we set 64MByte of buffer for this file
|
mem_buf_size: 1024 * 1024 * 64 // we set 64MByte of buffer for this file
|
||||||
end_line_len: csv.endline_crlf_len // we are using a windows text file
|
end_line_len: csv.endline_crlf_len // we are using a windows text file
|
||||||
)!
|
)!
|
||||||
// The data will be saved in this array
|
// The data will be saved in this array
|
||||||
@ -330,9 +330,9 @@ fn main() {
|
|||||||
mut csvr := csv.csv_reader(
|
mut csvr := csv.csv_reader(
|
||||||
scr_buf: txt.str // string pointer
|
scr_buf: txt.str // string pointer
|
||||||
scr_buf_len: txt.len // string length
|
scr_buf_len: txt.len // string length
|
||||||
comment: `#` // line starting with # will be ignored
|
comment: `#` // line starting with # will be ignored
|
||||||
quote: `'` // char used for quotes
|
quote: `'` // char used for quotes
|
||||||
quote_remove: true // remove quotes from the cells
|
quote_remove: true // remove quotes from the cells
|
||||||
)!
|
)!
|
||||||
|
|
||||||
// scan all rows, csvr.csv_map.len contain the valid
|
// scan all rows, csvr.csv_map.len contain the valid
|
||||||
|
@ -455,10 +455,11 @@ pub mut:
|
|||||||
// `field1: val1`
|
// `field1: val1`
|
||||||
pub struct StructInitField {
|
pub struct StructInitField {
|
||||||
pub:
|
pub:
|
||||||
pos token.Pos
|
pos token.Pos
|
||||||
name_pos token.Pos
|
name_pos token.Pos
|
||||||
comments []Comment
|
comments []Comment
|
||||||
next_comments []Comment
|
next_comments []Comment
|
||||||
|
has_prev_newline bool
|
||||||
pub mut:
|
pub mut:
|
||||||
expr Expr // `val1`
|
expr Expr // `val1`
|
||||||
name string // 'field1'
|
name string // 'field1'
|
||||||
|
@ -10,18 +10,26 @@ mut:
|
|||||||
}
|
}
|
||||||
|
|
||||||
@[params]
|
@[params]
|
||||||
struct AddInfoConfig {
|
struct AlignConfig {
|
||||||
pub:
|
pub:
|
||||||
use_threshold bool
|
ignore_newline bool // ignore newline or comment
|
||||||
threshold int = 25
|
use_threshold bool
|
||||||
|
threshold int = 25
|
||||||
}
|
}
|
||||||
|
|
||||||
struct FieldAlign {
|
struct FieldAlign {
|
||||||
|
cfg AlignConfig
|
||||||
mut:
|
mut:
|
||||||
infos []AlignInfo
|
infos []AlignInfo
|
||||||
cur_idx int
|
cur_idx int
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn new_field_align(cfg AlignConfig) FieldAlign {
|
||||||
|
return FieldAlign{
|
||||||
|
cfg: cfg
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn (mut fa FieldAlign) add_new_info(len int, line int) {
|
fn (mut fa FieldAlign) add_new_info(len int, line int) {
|
||||||
fa.infos << AlignInfo{
|
fa.infos << AlignInfo{
|
||||||
line_nr: line
|
line_nr: line
|
||||||
@ -30,24 +38,24 @@ fn (mut fa FieldAlign) add_new_info(len int, line int) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@[direct_array_access]
|
@[direct_array_access]
|
||||||
fn (mut fa FieldAlign) add_info(len int, line int, cfg AddInfoConfig) {
|
fn (mut fa FieldAlign) add_info(len int, line int) {
|
||||||
if fa.infos.len == 0 {
|
if fa.infos.len == 0 {
|
||||||
fa.add_new_info(len, line)
|
fa.add_new_info(len, line)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
i := fa.infos.len - 1
|
i := fa.infos.len - 1
|
||||||
if line - fa.infos[i].line_nr > 1 {
|
if !fa.cfg.ignore_newline && line - fa.infos[i].line_nr > 1 {
|
||||||
fa.add_new_info(len, line)
|
fa.add_new_info(len, line)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if cfg.use_threshold {
|
if fa.cfg.use_threshold {
|
||||||
len_diff := if fa.infos[i].max_len >= len {
|
len_diff := if fa.infos[i].max_len >= len {
|
||||||
fa.infos[i].max_len - len
|
fa.infos[i].max_len - len
|
||||||
} else {
|
} else {
|
||||||
len - fa.infos[i].max_len
|
len - fa.infos[i].max_len
|
||||||
}
|
}
|
||||||
|
|
||||||
if len_diff >= cfg.threshold {
|
if len_diff >= fa.cfg.threshold {
|
||||||
fa.add_new_info(len, line)
|
fa.add_new_info(len, line)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -64,7 +72,6 @@ fn (mut fa FieldAlign) max_len(line_nr int) int {
|
|||||||
}
|
}
|
||||||
if fa.cur_idx < fa.infos.len {
|
if fa.cur_idx < fa.infos.len {
|
||||||
return fa.infos[fa.cur_idx].max_len
|
return fa.infos[fa.cur_idx].max_len
|
||||||
} else {
|
|
||||||
return 0
|
|
||||||
}
|
}
|
||||||
|
return 0
|
||||||
}
|
}
|
||||||
|
@ -1039,28 +1039,28 @@ pub fn (mut f Fmt) enum_decl(node ast.EnumDecl) {
|
|||||||
f.writeln('enum ${name} {')
|
f.writeln('enum ${name} {')
|
||||||
f.comments(node.comments, same_line: true, level: .indent)
|
f.comments(node.comments, same_line: true, level: .indent)
|
||||||
|
|
||||||
mut value_aligns := FieldAlign{}
|
mut value_align := new_field_align()
|
||||||
mut attr_aligns := FieldAlign{}
|
mut attr_align := new_field_align()
|
||||||
mut comment_aligns := FieldAlign{}
|
mut comment_align := new_field_align()
|
||||||
for field in node.fields {
|
for field in node.fields {
|
||||||
if field.has_expr {
|
if field.has_expr {
|
||||||
value_aligns.add_info(field.name.len, field.pos.line_nr)
|
value_align.add_info(field.name.len, field.pos.line_nr)
|
||||||
}
|
}
|
||||||
attrs_len := inline_attrs_len(field.attrs)
|
attrs_len := inline_attrs_len(field.attrs)
|
||||||
if field.attrs.len > 0 {
|
if field.attrs.len > 0 {
|
||||||
if field.has_expr {
|
if field.has_expr {
|
||||||
attr_aligns.add_info(field.expr.str().len + 2, field.pos.line_nr)
|
attr_align.add_info(field.expr.str().len + 2, field.pos.line_nr)
|
||||||
} else {
|
} else {
|
||||||
attr_aligns.add_info(field.name.len, field.pos.line_nr)
|
attr_align.add_info(field.name.len, field.pos.line_nr)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if field.comments.len > 0 {
|
if field.comments.len > 0 {
|
||||||
if field.attrs.len > 0 {
|
if field.attrs.len > 0 {
|
||||||
comment_aligns.add_info(attrs_len, field.pos.line_nr)
|
comment_align.add_info(attrs_len, field.pos.line_nr)
|
||||||
} else if field.has_expr {
|
} else if field.has_expr {
|
||||||
comment_aligns.add_info(field.expr.str().len + 2, field.pos.line_nr)
|
comment_align.add_info(field.expr.str().len + 2, field.pos.line_nr)
|
||||||
} else {
|
} else {
|
||||||
comment_aligns.add_info(field.name.len, field.pos.line_nr)
|
comment_align.add_info(field.name.len, field.pos.line_nr)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1071,16 +1071,16 @@ pub fn (mut f Fmt) enum_decl(node ast.EnumDecl) {
|
|||||||
}
|
}
|
||||||
f.write('\t${field.name}')
|
f.write('\t${field.name}')
|
||||||
if field.has_expr {
|
if field.has_expr {
|
||||||
f.write(strings.repeat(` `, value_aligns.max_len(field.pos.line_nr) - field.name.len))
|
f.write(strings.repeat(` `, value_align.max_len(field.pos.line_nr) - field.name.len))
|
||||||
f.write(' = ')
|
f.write(' = ')
|
||||||
f.expr(field.expr)
|
f.expr(field.expr)
|
||||||
}
|
}
|
||||||
attrs_len := inline_attrs_len(field.attrs)
|
attrs_len := inline_attrs_len(field.attrs)
|
||||||
if field.attrs.len > 0 {
|
if field.attrs.len > 0 {
|
||||||
if field.has_expr {
|
if field.has_expr {
|
||||||
f.write(strings.repeat(` `, attr_aligns.max_len(field.pos.line_nr) - field.expr.str().len - 2))
|
f.write(strings.repeat(` `, attr_align.max_len(field.pos.line_nr) - field.expr.str().len - 2))
|
||||||
} else {
|
} else {
|
||||||
f.write(strings.repeat(` `, attr_aligns.max_len(field.pos.line_nr) - field.name.len))
|
f.write(strings.repeat(` `, attr_align.max_len(field.pos.line_nr) - field.name.len))
|
||||||
}
|
}
|
||||||
f.write(' ')
|
f.write(' ')
|
||||||
f.single_line_attrs(field.attrs, same_line: true)
|
f.single_line_attrs(field.attrs, same_line: true)
|
||||||
@ -1088,11 +1088,11 @@ pub fn (mut f Fmt) enum_decl(node ast.EnumDecl) {
|
|||||||
// f.comments(field.comments, same_line: true, has_nl: false, level: .indent)
|
// f.comments(field.comments, same_line: true, has_nl: false, level: .indent)
|
||||||
if field.comments.len > 0 {
|
if field.comments.len > 0 {
|
||||||
if field.attrs.len > 0 {
|
if field.attrs.len > 0 {
|
||||||
f.write(strings.repeat(` `, comment_aligns.max_len(field.pos.line_nr) - attrs_len))
|
f.write(strings.repeat(` `, comment_align.max_len(field.pos.line_nr) - attrs_len))
|
||||||
} else if field.has_expr {
|
} else if field.has_expr {
|
||||||
f.write(strings.repeat(` `, comment_aligns.max_len(field.pos.line_nr) - field.expr.str().len - 2))
|
f.write(strings.repeat(` `, comment_align.max_len(field.pos.line_nr) - field.expr.str().len - 2))
|
||||||
} else {
|
} else {
|
||||||
f.write(strings.repeat(` `, comment_aligns.max_len(field.pos.line_nr) - field.name.len))
|
f.write(strings.repeat(` `, comment_align.max_len(field.pos.line_nr) - field.name.len))
|
||||||
}
|
}
|
||||||
f.write(' ')
|
f.write(' ')
|
||||||
f.comments(field.comments, same_line: true, has_nl: false)
|
f.comments(field.comments, same_line: true, has_nl: false)
|
||||||
@ -1396,19 +1396,19 @@ pub fn (mut f Fmt) interface_decl(node ast.InterfaceDecl) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
mut type_aligns := FieldAlign{}
|
mut type_align := new_field_align()
|
||||||
mut comment_aligns := FieldAlign{}
|
mut comment_align := new_field_align()
|
||||||
mut default_expr_aligns := FieldAlign{}
|
mut default_expr_align := new_field_align()
|
||||||
mut attr_aligns := FieldAlign{}
|
mut attr_align := new_field_align()
|
||||||
mut field_types := []string{cap: node.fields.len}
|
mut field_types := []string{cap: node.fields.len}
|
||||||
|
|
||||||
// Calculate the alignments first
|
// Calculate the alignments first
|
||||||
f.calculate_alignment(node.fields, mut type_aligns, mut comment_aligns, mut default_expr_aligns, mut
|
f.calculate_alignment(node.fields, mut type_align, mut comment_align, mut default_expr_align, mut
|
||||||
attr_aligns, mut field_types)
|
attr_align, mut field_types)
|
||||||
|
|
||||||
// TODO: alignment, comments, etc.
|
// TODO: alignment, comments, etc.
|
||||||
for field in immut_fields {
|
for field in immut_fields {
|
||||||
f.interface_field(field, type_aligns.max_len(field.pos.line_nr))
|
f.interface_field(field, type_align.max_len(field.pos.line_nr))
|
||||||
}
|
}
|
||||||
for method in immut_methods {
|
for method in immut_methods {
|
||||||
f.interface_method(method)
|
f.interface_method(method)
|
||||||
@ -1416,7 +1416,7 @@ pub fn (mut f Fmt) interface_decl(node ast.InterfaceDecl) {
|
|||||||
if mut_fields.len + mut_methods.len > 0 {
|
if mut_fields.len + mut_methods.len > 0 {
|
||||||
f.writeln('mut:')
|
f.writeln('mut:')
|
||||||
for field in mut_fields {
|
for field in mut_fields {
|
||||||
f.interface_field(field, type_aligns.max_len(field.pos.line_nr))
|
f.interface_field(field, type_align.max_len(field.pos.line_nr))
|
||||||
}
|
}
|
||||||
for method in mut_methods {
|
for method in mut_methods {
|
||||||
f.interface_method(method)
|
f.interface_method(method)
|
||||||
@ -1432,8 +1432,8 @@ enum AlignState {
|
|||||||
has_everything
|
has_everything
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (mut f Fmt) calculate_alignment(fields []ast.StructField, mut type_aligns FieldAlign, mut comment_aligns FieldAlign,
|
pub fn (mut f Fmt) calculate_alignment(fields []ast.StructField, mut type_align FieldAlign, mut comment_align FieldAlign,
|
||||||
mut default_expr_aligns FieldAlign, mut attr_aligns FieldAlign, mut field_types []string) {
|
mut default_expr_align FieldAlign, mut attr_align FieldAlign, mut field_types []string) {
|
||||||
// Calculate the alignments first
|
// Calculate the alignments first
|
||||||
mut prev_state := AlignState.plain
|
mut prev_state := AlignState.plain
|
||||||
for field in fields {
|
for field in fields {
|
||||||
@ -1442,45 +1442,36 @@ pub fn (mut f Fmt) calculate_alignment(fields []ast.StructField, mut type_aligns
|
|||||||
field_types << ft
|
field_types << ft
|
||||||
attrs_len := inline_attrs_len(field.attrs)
|
attrs_len := inline_attrs_len(field.attrs)
|
||||||
end_pos := field.pos.pos + field.pos.len
|
end_pos := field.pos.pos + field.pos.len
|
||||||
type_aligns.add_info(field.name.len, field.pos.line_nr)
|
type_align.add_info(field.name.len, field.pos.line_nr)
|
||||||
if field.has_default_expr {
|
if field.has_default_expr {
|
||||||
default_expr_aligns.add_info(ft.len, field.pos.line_nr,
|
default_expr_align.add_info(ft.len, field.pos.line_nr)
|
||||||
use_threshold: true
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
if field.attrs.len > 0 {
|
if field.attrs.len > 0 {
|
||||||
attr_aligns.add_info(ft.len, field.pos.line_nr, use_threshold: true)
|
attr_align.add_info(ft.len, field.pos.line_nr)
|
||||||
}
|
}
|
||||||
for comment in field.comments {
|
for comment in field.comments {
|
||||||
if comment.pos.pos >= end_pos {
|
if comment.pos.pos >= end_pos {
|
||||||
if comment.pos.line_nr == field.pos.line_nr {
|
if comment.pos.line_nr == field.pos.line_nr {
|
||||||
if field.attrs.len > 0 {
|
if field.attrs.len > 0 {
|
||||||
if prev_state != AlignState.has_attributes {
|
if prev_state != AlignState.has_attributes {
|
||||||
comment_aligns.add_new_info(attrs_len, comment.pos.line_nr)
|
comment_align.add_new_info(attrs_len, comment.pos.line_nr)
|
||||||
} else {
|
} else {
|
||||||
comment_aligns.add_info(attrs_len, comment.pos.line_nr,
|
comment_align.add_info(attrs_len, comment.pos.line_nr)
|
||||||
use_threshold: true
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
prev_state = AlignState.has_attributes
|
prev_state = AlignState.has_attributes
|
||||||
} else if field.has_default_expr {
|
} else if field.has_default_expr {
|
||||||
if prev_state != AlignState.has_default_expression {
|
if prev_state != AlignState.has_default_expression {
|
||||||
comment_aligns.add_new_info(field.default_expr.str().len + 2,
|
comment_align.add_new_info(field.default_expr.str().len + 2,
|
||||||
comment.pos.line_nr)
|
comment.pos.line_nr)
|
||||||
} else {
|
} else {
|
||||||
comment_aligns.add_info(field.default_expr.str().len + 2,
|
comment_align.add_info(field.default_expr.str().len + 2, comment.pos.line_nr)
|
||||||
comment.pos.line_nr,
|
|
||||||
use_threshold: true
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
prev_state = AlignState.has_default_expression
|
prev_state = AlignState.has_default_expression
|
||||||
} else {
|
} else {
|
||||||
if prev_state != AlignState.has_everything {
|
if prev_state != AlignState.has_everything {
|
||||||
comment_aligns.add_new_info(ft.len, comment.pos.line_nr)
|
comment_align.add_new_info(ft.len, comment.pos.line_nr)
|
||||||
} else {
|
} else {
|
||||||
comment_aligns.add_info(ft.len, comment.pos.line_nr,
|
comment_align.add_info(ft.len, comment.pos.line_nr)
|
||||||
use_threshold: true
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
prev_state = AlignState.has_everything
|
prev_state = AlignState.has_everything
|
||||||
}
|
}
|
||||||
|
@ -27,14 +27,14 @@ pub fn (mut f Fmt) struct_decl(node ast.StructDecl, is_anon bool) {
|
|||||||
f.writeln(' {}')
|
f.writeln(' {}')
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
mut type_aligns := FieldAlign{}
|
mut type_align := new_field_align()
|
||||||
mut default_expr_aligns := FieldAlign{}
|
mut default_expr_align := new_field_align(use_threshold: true)
|
||||||
mut attr_aligns := FieldAlign{}
|
mut attr_align := new_field_align(use_threshold: true)
|
||||||
mut comment_aligns := FieldAlign{}
|
mut comment_align := new_field_align(use_threshold: true)
|
||||||
mut field_types := []string{cap: node.fields.len}
|
mut field_types := []string{cap: node.fields.len}
|
||||||
// Calculate the alignments first
|
// Calculate the alignments first
|
||||||
f.calculate_alignment(node.fields, mut type_aligns, mut comment_aligns, mut default_expr_aligns, mut
|
f.calculate_alignment(node.fields, mut type_align, mut comment_align, mut default_expr_align, mut
|
||||||
attr_aligns, mut field_types)
|
attr_align, mut field_types)
|
||||||
f.writeln(' {')
|
f.writeln(' {')
|
||||||
if node.pre_comments.len > 0 {
|
if node.pre_comments.len > 0 {
|
||||||
f.comments_before_field(node.pre_comments)
|
f.comments_before_field(node.pre_comments)
|
||||||
@ -90,7 +90,7 @@ pub fn (mut f Fmt) struct_decl(node ast.StructDecl, is_anon bool) {
|
|||||||
f.comments_before_field(pre_cmts)
|
f.comments_before_field(pre_cmts)
|
||||||
volatile_prefix := if field.is_volatile { 'volatile ' } else { '' }
|
volatile_prefix := if field.is_volatile { 'volatile ' } else { '' }
|
||||||
f.write('\t${volatile_prefix}${field.name} ')
|
f.write('\t${volatile_prefix}${field.name} ')
|
||||||
f.write(strings.repeat(` `, type_aligns.max_len(field.pos.line_nr) - field.name.len))
|
f.write(strings.repeat(` `, type_align.max_len(field.pos.line_nr) - field.name.len))
|
||||||
// Handle anon structs recursively
|
// Handle anon structs recursively
|
||||||
if !f.write_anon_struct_field_decl(field.typ, field.anon_struct_decl) {
|
if !f.write_anon_struct_field_decl(field.typ, field.anon_struct_decl) {
|
||||||
f.write(field_types[i])
|
f.write(field_types[i])
|
||||||
@ -98,7 +98,7 @@ pub fn (mut f Fmt) struct_decl(node ast.StructDecl, is_anon bool) {
|
|||||||
f.mark_types_import_as_used(field.typ)
|
f.mark_types_import_as_used(field.typ)
|
||||||
attrs_len := inline_attrs_len(field.attrs)
|
attrs_len := inline_attrs_len(field.attrs)
|
||||||
if field.has_default_expr {
|
if field.has_default_expr {
|
||||||
f.write(strings.repeat(` `, default_expr_aligns.max_len(field.pos.line_nr) - field_types[i].len))
|
f.write(strings.repeat(` `, default_expr_align.max_len(field.pos.line_nr) - field_types[i].len))
|
||||||
f.write(' = ')
|
f.write(' = ')
|
||||||
if !expr_is_single_line(field.default_expr) {
|
if !expr_is_single_line(field.default_expr) {
|
||||||
f.indent++
|
f.indent++
|
||||||
@ -111,17 +111,17 @@ pub fn (mut f Fmt) struct_decl(node ast.StructDecl, is_anon bool) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if field.attrs.len > 0 {
|
if field.attrs.len > 0 {
|
||||||
f.write(strings.repeat(` `, attr_aligns.max_len(field.pos.line_nr) - field_types[i].len))
|
f.write(strings.repeat(` `, attr_align.max_len(field.pos.line_nr) - field_types[i].len))
|
||||||
f.single_line_attrs(field.attrs, same_line: true)
|
f.single_line_attrs(field.attrs, same_line: true)
|
||||||
}
|
}
|
||||||
// Handle comments at the end of the line
|
// Handle comments at the end of the line
|
||||||
if end_cmts.len > 0 {
|
if end_cmts.len > 0 {
|
||||||
if field.has_default_expr {
|
if field.has_default_expr {
|
||||||
f.write(strings.repeat(` `, comment_aligns.max_len(field.pos.line_nr) - field.default_expr.str().len - 2))
|
f.write(strings.repeat(` `, comment_align.max_len(field.pos.line_nr) - field.default_expr.str().len - 2))
|
||||||
} else if field.attrs.len > 0 {
|
} else if field.attrs.len > 0 {
|
||||||
f.write(strings.repeat(` `, comment_aligns.max_len(field.pos.line_nr) - attrs_len))
|
f.write(strings.repeat(` `, comment_align.max_len(field.pos.line_nr) - attrs_len))
|
||||||
} else {
|
} else {
|
||||||
f.write(strings.repeat(` `, comment_aligns.max_len(field.pos.line_nr) - field_types[i].len))
|
f.write(strings.repeat(` `, comment_align.max_len(field.pos.line_nr) - field_types[i].len))
|
||||||
}
|
}
|
||||||
f.write(' ')
|
f.write(' ')
|
||||||
f.comments(end_cmts, level: .indent)
|
f.comments(end_cmts, level: .indent)
|
||||||
@ -275,23 +275,27 @@ pub fn (mut f Fmt) struct_init(node ast.StructInit) {
|
|||||||
}
|
}
|
||||||
f.comments(node.update_expr_comments, same_line: true, has_nl: true, level: .keep)
|
f.comments(node.update_expr_comments, same_line: true, has_nl: true, level: .keep)
|
||||||
}
|
}
|
||||||
mut value_aligns := FieldAlign{}
|
mut value_align := new_field_align()
|
||||||
mut comment_aligns := FieldAlign{}
|
mut comment_align := new_field_align(use_threshold: true)
|
||||||
for init_field in node.init_fields {
|
for init_field in node.init_fields {
|
||||||
value_aligns.add_info(init_field.name.len, init_field.pos.line_nr)
|
value_align.add_info(init_field.name.len, init_field.pos.line_nr)
|
||||||
if init_field.comments.len > 0 {
|
if init_field.comments.len > 0 {
|
||||||
comment_aligns.add_info(init_field.expr.str().len + 1, init_field.pos.line_nr)
|
comment_align.add_info(init_field.expr.str().len, init_field.pos.line_nr)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for i, init_field in node.init_fields {
|
for i, init_field in node.init_fields {
|
||||||
|
if i > 0 && init_field.has_prev_newline {
|
||||||
|
f.writeln('')
|
||||||
|
}
|
||||||
f.write('${init_field.name}: ')
|
f.write('${init_field.name}: ')
|
||||||
if !single_line_fields {
|
if !single_line_fields {
|
||||||
f.write(strings.repeat(` `, value_aligns.max_len(init_field.pos.line_nr) - init_field.name.len))
|
f.write(strings.repeat(` `, value_align.max_len(init_field.pos.line_nr) - init_field.name.len))
|
||||||
}
|
}
|
||||||
f.expr(init_field.expr)
|
f.expr(init_field.expr)
|
||||||
if init_field.comments.len > 0 {
|
if init_field.comments.len > 0 {
|
||||||
f.write(strings.repeat(` `, comment_aligns.max_len(init_field.pos.line_nr) - init_field.expr.str().len))
|
f.write(strings.repeat(` `, comment_align.max_len(init_field.pos.line_nr) - init_field.expr.str().len))
|
||||||
f.comments(init_field.comments, same_line: true, has_nl: false, level: .indent)
|
f.write(' ')
|
||||||
|
f.comments(init_field.comments, has_nl: false, level: .indent)
|
||||||
}
|
}
|
||||||
if single_line_fields {
|
if single_line_fields {
|
||||||
if i < node.init_fields.len - 1 {
|
if i < node.init_fields.len - 1 {
|
||||||
|
@ -13,8 +13,8 @@ pub fn new_builder(initial_size int) Builder {
|
|||||||
return Builder{
|
return Builder{
|
||||||
// buf: make(0, initial_size)
|
// buf: make(0, initial_size)
|
||||||
buf: []u8{cap: initial_size}
|
buf: []u8{cap: initial_size}
|
||||||
str_calls: 0 // after str_calls
|
str_calls: 0 // after str_calls
|
||||||
len: 0 // after len
|
len: 0 // after len
|
||||||
initial_size: initial_size // final
|
initial_size: initial_size // final
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,22 @@
|
|||||||
|
module abcde
|
||||||
|
|
||||||
|
pub struct Builder {
|
||||||
|
pub mut:
|
||||||
|
// inline before field
|
||||||
|
buf []u8
|
||||||
|
str_calls int
|
||||||
|
len int
|
||||||
|
initial_size int = 1
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn new_builder(initial_size int) Builder {
|
||||||
|
return Builder{
|
||||||
|
// buf: make(0, initial_size)
|
||||||
|
buf: []u8{cap: initial_size}
|
||||||
|
|
||||||
|
str_calls: 0 // after str_calls
|
||||||
|
len: 0 // after len
|
||||||
|
|
||||||
|
initial_size: initial_size // final
|
||||||
|
}
|
||||||
|
}
|
@ -423,6 +423,7 @@ fn (mut p Parser) struct_init(typ_str string, kind ast.StructInitKind, is_option
|
|||||||
mut update_expr_comments := []ast.Comment{}
|
mut update_expr_comments := []ast.Comment{}
|
||||||
mut has_update_expr := false
|
mut has_update_expr := false
|
||||||
mut update_expr_pos := token.Pos{}
|
mut update_expr_pos := token.Pos{}
|
||||||
|
mut has_prev_newline := false
|
||||||
for p.tok.kind !in [.rcbr, .rpar, .eof] {
|
for p.tok.kind !in [.rcbr, .rpar, .eof] {
|
||||||
mut field_name := ''
|
mut field_name := ''
|
||||||
mut expr := ast.empty_expr
|
mut expr := ast.empty_expr
|
||||||
@ -446,6 +447,7 @@ fn (mut p Parser) struct_init(typ_str string, kind ast.StructInitKind, is_option
|
|||||||
has_update_expr = true
|
has_update_expr = true
|
||||||
} else {
|
} else {
|
||||||
first_field_pos = p.tok.pos()
|
first_field_pos = p.tok.pos()
|
||||||
|
has_prev_newline = p.tok.line_nr - p.prev_tok.line_nr - p.prev_tok.lit.count('\n') > 1
|
||||||
field_name = p.check_name()
|
field_name = p.check_name()
|
||||||
p.check(.colon)
|
p.check(.colon)
|
||||||
expr = p.expr(0)
|
expr = p.expr(0)
|
||||||
@ -471,13 +473,14 @@ fn (mut p Parser) struct_init(typ_str string, kind ast.StructInitKind, is_option
|
|||||||
nline_comments << p.eat_comments()
|
nline_comments << p.eat_comments()
|
||||||
if !is_update_expr {
|
if !is_update_expr {
|
||||||
init_fields << ast.StructInitField{
|
init_fields << ast.StructInitField{
|
||||||
name: field_name
|
name: field_name
|
||||||
expr: expr
|
expr: expr
|
||||||
pos: field_pos
|
pos: field_pos
|
||||||
name_pos: first_field_pos
|
name_pos: first_field_pos
|
||||||
comments: comments
|
comments: comments
|
||||||
next_comments: nline_comments
|
next_comments: nline_comments
|
||||||
parent_type: typ
|
parent_type: typ
|
||||||
|
has_prev_newline: has_prev_newline
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -11,7 +11,7 @@ pub struct ABC {
|
|||||||
fn test_main() {
|
fn test_main() {
|
||||||
abc := ABC{
|
abc := ABC{
|
||||||
test: &Test{} // non option init
|
test: &Test{} // non option init
|
||||||
test2: Test{} // non option init
|
test2: Test{} // non option init
|
||||||
}
|
}
|
||||||
|
|
||||||
if ttt := abc.test {
|
if ttt := abc.test {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user