aligning structs now work

This commit is contained in:
Daniel Gavin
2021-04-14 16:31:31 +02:00
parent 7e90ece84a
commit b0721f1e0c
2 changed files with 96 additions and 9 deletions
+87 -3
View File
@@ -208,7 +208,7 @@ align_switch_smt :: proc(p: ^Printer, index: int) {
if format_token.kind == .Open_Brace && switch_found {
brace_token = format_token;
brace_line = line_index;
brace_line = line_index+index;
break found_switch_brace;
} else if format_token.kind == .Open_Brace {
break;
@@ -294,7 +294,7 @@ align_switch_smt :: proc(p: ^Printer, index: int) {
if case_count > brace_token.parameter_count {
break;
}
}
}
@@ -302,6 +302,88 @@ align_switch_smt :: proc(p: ^Printer, index: int) {
align_struct :: proc(p: ^Printer, index: int) {
struct_found := false;
brace_token: Format_Token;
brace_line: int;
found_struct_brace: for line, line_index in p.lines[index:] {
for format_token in line.format_tokens {
if format_token.kind == .Open_Brace && struct_found {
brace_token = format_token;
brace_line = line_index+index;
break found_struct_brace;
} else if format_token.kind == .Open_Brace {
break;
} else if format_token.kind == .Struct {
struct_found = true;
}
}
}
if !struct_found {
return;
}
largest := 0;
colon_count := 0;
for line, line_index in p.lines[brace_line+1:] {
length := 0;
for format_token in line.format_tokens {
if format_token.kind == .Comment {
continue;
}
if format_token.kind == .Colon {
colon_count += 1;
largest = max(length, largest);
break;
}
length += len(format_token.text) + format_token.spaces_before;
}
if colon_count > brace_token.parameter_count {
break;
}
}
colon_count = 0;
for line, line_index in p.lines[brace_line+1:] {
length := 0;
for format_token, i in line.format_tokens {
if format_token.kind == .Comment {
continue;
}
if format_token.kind == .Colon {
colon_count += 1;
line.format_tokens[i+1].spaces_before += (largest - length) - 1;
break;
}
length += len(format_token.text) + format_token.spaces_before;
}
if colon_count > brace_token.parameter_count {
break;
}
}
}
align_blocks :: proc(p: ^Printer) {
@@ -314,7 +396,9 @@ align_blocks :: proc(p: ^Printer) {
if .Switch_Stmt in line.types && p.config.align_switch {
align_switch_smt(p, line_index);
} else if .Struct in line.types && p.config.align_structs {
}
if .Struct in line.types && p.config.align_structs {
align_struct(p, line_index);
}
+9 -6
View File
@@ -1028,6 +1028,8 @@ visit_expr :: proc(p: ^Printer, expr: ^ast.Expr) {
case Struct_Type:
push_generic_token(p, .Struct, 1);
hint_current_line(p, {.Struct});
if v.is_packed {
push_ident_token(p, "#packed", 1);
}
@@ -1058,11 +1060,10 @@ visit_expr :: proc(p: ^Printer, expr: ^ast.Expr) {
set_source_position(p, v.fields.pos);
visit_field_list(p, v.fields, true);
push_generic_token(p, .Close_Brace, 0);
} else {
visit_begin_brace(p, v.pos, .Generic);
newline_position(p, 1);
} else if v.fields != nil {
visit_begin_brace(p, v.pos, .Generic, len(v.fields.list));
set_source_position(p, v.fields.pos);
visit_field_list(p, v.fields, true, true);
visit_field_list(p, v.fields, true, true, true);
visit_end_brace(p, v.end);
}
@@ -1256,7 +1257,7 @@ visit_block_stmts :: proc(p: ^Printer, stmts: []^ast.Stmt, newline_each := false
}
}
visit_field_list :: proc(p: ^Printer, list: ^ast.Field_List, add_comma := false, trailing := false) {
visit_field_list :: proc(p: ^Printer, list: ^ast.Field_List, add_comma := false, trailing := false, enforce_newline := false) {
if list.list == nil {
return;
@@ -1264,7 +1265,9 @@ visit_field_list :: proc(p: ^Printer, list: ^ast.Field_List, add_comma := false,
for field, i in list.list {
move_line_limit(p, field.pos, 1);
if !move_line_limit(p, field.pos, 1) && enforce_newline {
newline_position(p, 1);
}
if .Using in field.flags {
push_generic_token(p, .Using, 0);