fmt: cleanup fields comments alignment and add ignore_newline config (#22027)

This commit is contained in:
yuyi 2024-08-12 15:56:32 +08:00 committed by GitHub
parent ac3045b472
commit c69dfefedb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
11 changed files with 130 additions and 102 deletions

View File

@ -76,15 +76,15 @@ fn main() {
// setting values of app
app.gg = gg.new_context(
bg_color: gx.black // background color
width: window_width // window width
bg_color: gx.black // background color
width: window_width // window width
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
frame_fn: frame // this is frame function update the frame
event_fn: on_event // it calls on every event
frame_fn: frame // this is frame function update the frame
event_fn: on_event // it calls on every event
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
app.grid = grid // set grid to app attribute so you can access it by just passing app variable or with method of app

View File

@ -190,12 +190,12 @@ fn (data &StrIntpData) process_str_intp_data(mut sb strings.Builder) {
sign_set := sign == 1
mut bf := strconv.BF_param{
pad_ch: pad_ch // padding char
len0: len0_set // default len for whole the number or string
len1: len1_set // number of decimal digits, if needed
positive: true // mandatory: the sign of the number passed
sign_flag: sign_set // flag for print sign as prefix in padding
align: .left // alignment of the string
pad_ch: pad_ch // padding char
len0: len0_set // default len for whole the number or string
len1: len1_set // number of decimal digits, if needed
positive: true // mandatory: the sign of the number passed
sign_flag: sign_set // flag for print sign as prefix in padding
align: .left // alignment of the string
rm_tail_zero: tail_zeros // false // remove the tail zeros from floats
}

View File

@ -259,8 +259,8 @@ import encoding.csv
fn main() {
file_path := 'big2.csv'
mut csvr := csv.csv_reader(
file_path: file_path // path to the file CSV
mem_buf_size: 1024 * 1024 * 64 // we set 64MByte of buffer for this file
file_path: file_path // path to the file CSV
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
)!
// The data will be saved in this array
@ -330,9 +330,9 @@ fn main() {
mut csvr := csv.csv_reader(
scr_buf: txt.str // string pointer
scr_buf_len: txt.len // string length
comment: `#` // line starting with # will be ignored
quote: `'` // char used for quotes
quote_remove: true // remove quotes from the cells
comment: `#` // line starting with # will be ignored
quote: `'` // char used for quotes
quote_remove: true // remove quotes from the cells
)!
// scan all rows, csvr.csv_map.len contain the valid

View File

@ -455,10 +455,11 @@ pub mut:
// `field1: val1`
pub struct StructInitField {
pub:
pos token.Pos
name_pos token.Pos
comments []Comment
next_comments []Comment
pos token.Pos
name_pos token.Pos
comments []Comment
next_comments []Comment
has_prev_newline bool
pub mut:
expr Expr // `val1`
name string // 'field1'

View File

@ -10,18 +10,26 @@ mut:
}
@[params]
struct AddInfoConfig {
struct AlignConfig {
pub:
use_threshold bool
threshold int = 25
ignore_newline bool // ignore newline or comment
use_threshold bool
threshold int = 25
}
struct FieldAlign {
cfg AlignConfig
mut:
infos []AlignInfo
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) {
fa.infos << AlignInfo{
line_nr: line
@ -30,24 +38,24 @@ fn (mut fa FieldAlign) add_new_info(len int, line int) {
}
@[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 {
fa.add_new_info(len, line)
return
}
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)
return
}
if cfg.use_threshold {
if fa.cfg.use_threshold {
len_diff := if fa.infos[i].max_len >= len {
fa.infos[i].max_len - len
} else {
len - fa.infos[i].max_len
}
if len_diff >= cfg.threshold {
if len_diff >= fa.cfg.threshold {
fa.add_new_info(len, line)
return
}
@ -64,7 +72,6 @@ fn (mut fa FieldAlign) max_len(line_nr int) int {
}
if fa.cur_idx < fa.infos.len {
return fa.infos[fa.cur_idx].max_len
} else {
return 0
}
return 0
}

View File

@ -1039,28 +1039,28 @@ pub fn (mut f Fmt) enum_decl(node ast.EnumDecl) {
f.writeln('enum ${name} {')
f.comments(node.comments, same_line: true, level: .indent)
mut value_aligns := FieldAlign{}
mut attr_aligns := FieldAlign{}
mut comment_aligns := FieldAlign{}
mut value_align := new_field_align()
mut attr_align := new_field_align()
mut comment_align := new_field_align()
for field in node.fields {
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)
if field.attrs.len > 0 {
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 {
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.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 {
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 {
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}')
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.expr(field.expr)
}
attrs_len := inline_attrs_len(field.attrs)
if field.attrs.len > 0 {
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 {
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.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)
if field.comments.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 {
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 {
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.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 comment_aligns := FieldAlign{}
mut default_expr_aligns := FieldAlign{}
mut attr_aligns := FieldAlign{}
mut type_align := new_field_align()
mut comment_align := new_field_align()
mut default_expr_align := new_field_align()
mut attr_align := new_field_align()
mut field_types := []string{cap: node.fields.len}
// Calculate the alignments first
f.calculate_alignment(node.fields, mut type_aligns, mut comment_aligns, mut default_expr_aligns, mut
attr_aligns, mut field_types)
f.calculate_alignment(node.fields, mut type_align, mut comment_align, mut default_expr_align, mut
attr_align, mut field_types)
// TODO: alignment, comments, etc.
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 {
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 {
f.writeln('mut:')
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 {
f.interface_method(method)
@ -1432,8 +1432,8 @@ enum AlignState {
has_everything
}
pub fn (mut f Fmt) calculate_alignment(fields []ast.StructField, mut type_aligns FieldAlign, mut comment_aligns FieldAlign,
mut default_expr_aligns FieldAlign, mut attr_aligns FieldAlign, mut field_types []string) {
pub fn (mut f Fmt) calculate_alignment(fields []ast.StructField, mut type_align FieldAlign, mut comment_align FieldAlign,
mut default_expr_align FieldAlign, mut attr_align FieldAlign, mut field_types []string) {
// Calculate the alignments first
mut prev_state := AlignState.plain
for field in fields {
@ -1442,45 +1442,36 @@ pub fn (mut f Fmt) calculate_alignment(fields []ast.StructField, mut type_aligns
field_types << ft
attrs_len := inline_attrs_len(field.attrs)
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 {
default_expr_aligns.add_info(ft.len, field.pos.line_nr,
use_threshold: true
)
default_expr_align.add_info(ft.len, field.pos.line_nr)
}
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 {
if comment.pos.pos >= end_pos {
if comment.pos.line_nr == field.pos.line_nr {
if field.attrs.len > 0 {
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 {
comment_aligns.add_info(attrs_len, comment.pos.line_nr,
use_threshold: true
)
comment_align.add_info(attrs_len, comment.pos.line_nr)
}
prev_state = AlignState.has_attributes
} else if field.has_default_expr {
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)
} else {
comment_aligns.add_info(field.default_expr.str().len + 2,
comment.pos.line_nr,
use_threshold: true
)
comment_align.add_info(field.default_expr.str().len + 2, comment.pos.line_nr)
}
prev_state = AlignState.has_default_expression
} else {
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 {
comment_aligns.add_info(ft.len, comment.pos.line_nr,
use_threshold: true
)
comment_align.add_info(ft.len, comment.pos.line_nr)
}
prev_state = AlignState.has_everything
}

View File

@ -27,14 +27,14 @@ pub fn (mut f Fmt) struct_decl(node ast.StructDecl, is_anon bool) {
f.writeln(' {}')
return
}
mut type_aligns := FieldAlign{}
mut default_expr_aligns := FieldAlign{}
mut attr_aligns := FieldAlign{}
mut comment_aligns := FieldAlign{}
mut type_align := new_field_align()
mut default_expr_align := new_field_align(use_threshold: true)
mut attr_align := new_field_align(use_threshold: true)
mut comment_align := new_field_align(use_threshold: true)
mut field_types := []string{cap: node.fields.len}
// Calculate the alignments first
f.calculate_alignment(node.fields, mut type_aligns, mut comment_aligns, mut default_expr_aligns, mut
attr_aligns, mut field_types)
f.calculate_alignment(node.fields, mut type_align, mut comment_align, mut default_expr_align, mut
attr_align, mut field_types)
f.writeln(' {')
if node.pre_comments.len > 0 {
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)
volatile_prefix := if field.is_volatile { 'volatile ' } else { '' }
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
if !f.write_anon_struct_field_decl(field.typ, field.anon_struct_decl) {
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)
attrs_len := inline_attrs_len(field.attrs)
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(' = ')
if !expr_is_single_line(field.default_expr) {
f.indent++
@ -111,17 +111,17 @@ pub fn (mut f Fmt) struct_decl(node ast.StructDecl, is_anon bool) {
}
}
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)
}
// Handle comments at the end of the line
if end_cmts.len > 0 {
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 {
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 {
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.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)
}
mut value_aligns := FieldAlign{}
mut comment_aligns := FieldAlign{}
mut value_align := new_field_align()
mut comment_align := new_field_align(use_threshold: true)
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 {
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 {
if i > 0 && init_field.has_prev_newline {
f.writeln('')
}
f.write('${init_field.name}: ')
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)
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.comments(init_field.comments, same_line: true, has_nl: false, level: .indent)
f.write(strings.repeat(` `, comment_align.max_len(init_field.pos.line_nr) - init_field.expr.str().len))
f.write(' ')
f.comments(init_field.comments, has_nl: false, level: .indent)
}
if single_line_fields {
if i < node.init_fields.len - 1 {

View File

@ -13,8 +13,8 @@ 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
str_calls: 0 // after str_calls
len: 0 // after len
initial_size: initial_size // final
}
}

View File

@ -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
}
}

View File

@ -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 has_update_expr := false
mut update_expr_pos := token.Pos{}
mut has_prev_newline := false
for p.tok.kind !in [.rcbr, .rpar, .eof] {
mut field_name := ''
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
} else {
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()
p.check(.colon)
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()
if !is_update_expr {
init_fields << ast.StructInitField{
name: field_name
expr: expr
pos: field_pos
name_pos: first_field_pos
comments: comments
next_comments: nline_comments
parent_type: typ
name: field_name
expr: expr
pos: field_pos
name_pos: first_field_pos
comments: comments
next_comments: nline_comments
parent_type: typ
has_prev_newline: has_prev_newline
}
}
}

View File

@ -11,7 +11,7 @@ pub struct ABC {
fn test_main() {
abc := ABC{
test: &Test{} // non option init
test2: Test{} // non option init
test2: Test{} // non option init
}
if ttt := abc.test {