ran the odinfmt - looks good, except for multi line binary operations

This commit is contained in:
Daniel Gavin
2021-04-15 00:19:13 +02:00
parent a09300fb0e
commit 22daa50374
2 changed files with 1346 additions and 1370 deletions
+13 -29
View File
@@ -8,7 +8,7 @@ import "core:fmt"
import "core:unicode/utf8" import "core:unicode/utf8"
import "core:mem" import "core:mem"
Line_Type_Enum :: enum{Line_Comment, Value_Decl, Switch_Stmt, Struct}; Line_Type_Enum :: enum {Line_Comment, Value_Decl, Switch_Stmt, Struct}
Line_Type :: bit_set[Line_Type_Enum]; Line_Type :: bit_set[Line_Type_Enum];
@@ -31,7 +31,7 @@ Printer :: struct {
string_builder: strings.Builder, string_builder: strings.Builder,
config: Config, config: Config,
depth: int, //the identation depth depth: int, //the identation depth
comments: [dynamic]^ast.Comment_Group, comments: [dynamic] ^ast.Comment_Group,
latest_comment_index: int, latest_comment_index: int,
allocator: mem.Allocator, allocator: mem.Allocator,
file: ^ast.File, file: ^ast.File,
@@ -103,7 +103,7 @@ default_style := Config {
indent_cases = false, indent_cases = false,
align_switch = true, align_switch = true,
align_structs = true, align_structs = true,
newline_style = .LF, newline_style = .CRLF,
}; };
make_printer :: proc(config: Config, allocator := context.allocator) -> Printer { make_printer :: proc(config: Config, allocator := context.allocator) -> Printer {
@@ -119,7 +119,7 @@ print :: proc(p: ^Printer, file: ^ast.File) -> string {
p.comments = file.comments; p.comments = file.comments;
if len(file.decls) > 0 { if len(file.decls) > 0 {
p.lines = make([dynamic] Line, 0, (file.decls[len(file.decls)-1].end.line - file.decls[0].pos.line) * 2, context.temp_allocator); p.lines = make([dynamic] Line, 0, (file.decls[len(file.decls) - 1].end.line - file.decls[0].pos.line) * 2, context.temp_allocator);
} }
set_line(p, 0); set_line(p, 0);
@@ -132,7 +132,7 @@ print :: proc(p: ^Printer, file: ^ast.File) -> string {
} }
if len(p.comments) > 0 { if len(p.comments) > 0 {
infinite := p.comments[len(p.comments)-1].end; infinite := p.comments[len(p.comments) - 1].end;
infinite.offset = 9999999; infinite.offset = 9999999;
push_comments(p, infinite); push_comments(p, infinite);
} }
@@ -194,7 +194,6 @@ fix_lines :: proc(p: ^Printer) {
} }
align_var_decls :: proc(p: ^Printer) { align_var_decls :: proc(p: ^Printer) {
} }
align_switch_smt :: proc(p: ^Printer, index: int) { align_switch_smt :: proc(p: ^Printer, index: int) {
@@ -209,16 +208,14 @@ align_switch_smt :: proc(p: ^Printer, index: int) {
if format_token.kind == .Open_Brace && switch_found { if format_token.kind == .Open_Brace && switch_found {
brace_token = format_token; brace_token = format_token;
brace_line = line_index+index; brace_line = line_index + index;
break found_switch_brace; break found_switch_brace;
} else if format_token.kind == .Open_Brace { } else if format_token.kind == .Open_Brace {
break; break;
} else if format_token.kind == .Switch { } else if format_token.kind == .Switch {
switch_found = true; switch_found = true;
} }
} }
} }
if !switch_found { if !switch_found {
@@ -229,7 +226,7 @@ align_switch_smt :: proc(p: ^Printer, index: int) {
case_count := 0; case_count := 0;
//find all the switch cases that are one lined //find all the switch cases that are one lined
for line, line_index in p.lines[brace_line+1:] { for line, line_index in p.lines[brace_line + 1:] {
case_found := false; case_found := false;
colon_found := false; colon_found := false;
@@ -260,12 +257,11 @@ align_switch_smt :: proc(p: ^Printer, index: int) {
if case_count >= brace_token.parameter_count { if case_count >= brace_token.parameter_count {
break; break;
} }
} }
case_count = 0; case_count = 0;
for line, line_index in p.lines[brace_line+1:] { for line, line_index in p.lines[brace_line + 1:] {
case_found := false; case_found := false;
colon_found := false; colon_found := false;
@@ -291,14 +287,12 @@ align_switch_smt :: proc(p: ^Printer, index: int) {
} }
length += len(format_token.text) + format_token.spaces_before; length += len(format_token.text) + format_token.spaces_before;
} }
if case_count >= brace_token.parameter_count { if case_count >= brace_token.parameter_count {
break; break;
} }
} }
} }
align_struct :: proc(p: ^Printer, index: int) { align_struct :: proc(p: ^Printer, index: int) {
@@ -313,16 +307,14 @@ align_struct :: proc(p: ^Printer, index: int) {
if format_token.kind == .Open_Brace && struct_found { if format_token.kind == .Open_Brace && struct_found {
brace_token = format_token; brace_token = format_token;
brace_line = line_index+index; brace_line = line_index + index;
break found_struct_brace; break found_struct_brace;
} else if format_token.kind == .Open_Brace { } else if format_token.kind == .Open_Brace {
break; break;
} else if format_token.kind == .Struct { } else if format_token.kind == .Struct {
struct_found = true; struct_found = true;
} }
} }
} }
if !struct_found { if !struct_found {
@@ -332,7 +324,7 @@ align_struct :: proc(p: ^Printer, index: int) {
largest := 0; largest := 0;
colon_count := 0; colon_count := 0;
for line, line_index in p.lines[brace_line+1:] { for line, line_index in p.lines[brace_line + 1:] {
length := 0; length := 0;
@@ -358,7 +350,7 @@ align_struct :: proc(p: ^Printer, index: int) {
colon_count = 0; colon_count = 0;
for line, line_index in p.lines[brace_line+1:] { for line, line_index in p.lines[brace_line + 1:] {
length := 0; length := 0;
@@ -370,7 +362,7 @@ align_struct :: proc(p: ^Printer, index: int) {
if format_token.kind == .Colon { if format_token.kind == .Colon {
colon_count += 1; colon_count += 1;
line.format_tokens[i+1].spaces_before = largest - length + 1; line.format_tokens[i + 1].spaces_before = largest - length + 1;
break; break;
} }
@@ -381,7 +373,6 @@ align_struct :: proc(p: ^Printer, index: int) {
break; break;
} }
} }
} }
align_blocks :: proc(p: ^Printer) { align_blocks :: proc(p: ^Printer) {
@@ -399,9 +390,7 @@ align_blocks :: proc(p: ^Printer) {
if .Struct in line.types && p.config.align_structs { if .Struct in line.types && p.config.align_structs {
align_struct(p, line_index); align_struct(p, line_index);
} }
} }
} }
align_comments :: proc(p: ^Printer) { align_comments :: proc(p: ^Printer) {
@@ -413,7 +402,7 @@ align_comments :: proc(p: ^Printer) {
depth: int, depth: int,
}; };
comment_infos := make([dynamic]Comment_Align_Info, 0, context.temp_allocator); comment_infos := make([dynamic] Comment_Align_Info, 0, context.temp_allocator);
current_info: Comment_Align_Info; current_info: Comment_Align_Info;
@@ -449,9 +438,7 @@ align_comments :: proc(p: ^Printer) {
length += format_token.spaces_before + len(format_token.text); length += format_token.spaces_before + len(format_token.text);
} }
} }
} }
if (current_info.begin != 0 && current_info.end != 0) || current_info.length > 0 { if (current_info.begin != 0 && current_info.end != 0) || current_info.length > 0 {
@@ -482,9 +469,6 @@ align_comments :: proc(p: ^Printer) {
length += format_token.spaces_before + len(format_token.text); length += format_token.spaces_before + len(format_token.text);
} }
} }
} }
} }
+16 -24
View File
@@ -11,19 +11,19 @@ import "core:sort"
//right the attribute order is not linearly parsed(bug?) //right the attribute order is not linearly parsed(bug?)
@(private) @(private)
sort_attribute :: proc(s: ^[dynamic]^ast.Attribute) -> sort.Interface { sort_attribute :: proc(s: ^[dynamic] ^ast.Attribute) -> sort.Interface {
return sort.Interface { return sort.Interface {
collection = rawptr(s), collection = rawptr(s),
len = proc(it: sort.Interface) -> int { len = proc(it: sort.Interface) -> int {
s := (^[dynamic]^ast.Attribute)(it.collection); s := (^[dynamic] ^ast.Attribute)(it.collection);
return len(s^); return len(s^);
}, },
less = proc(it: sort.Interface, i, j: int) -> bool { less = proc(it: sort.Interface, i, j: int) -> bool {
s := (^[dynamic]^ast.Attribute)(it.collection); s := (^[dynamic] ^ast.Attribute)(it.collection);
return s[i].pos.offset < s[j].pos.offset; return s[i].pos.offset < s[j].pos.offset;
}, },
swap = proc(it: sort.Interface, i, j: int) { swap = proc(it: sort.Interface, i, j: int) {
s := (^[dynamic]^ast.Attribute)(it.collection); s := (^[dynamic] ^ast.Attribute)(it.collection);
s[i], s[j] = s[j], s[i]; s[i], s[j] = s[j], s[i];
}, },
}; };
@@ -70,9 +70,9 @@ push_comment :: proc(p: ^Printer, comment: tokenizer.Token) -> int {
} }
append(&p.current_line.format_tokens, format_token); append(&p.current_line.format_tokens, format_token);
p.last_token = &p.current_line.format_tokens[len(p.current_line.format_tokens)-1]; p.last_token = &p.current_line.format_tokens[len(p.current_line.format_tokens) - 1];
hint_current_line(p, {.Line_Comment}); hint_current_line(p,{.Line_Comment});
return 0; return 0;
} else { } else {
@@ -116,7 +116,6 @@ push_comment :: proc(p: ^Printer, comment: tokenizer.Token) -> int {
} else { } else {
strings.write_byte(&builder, c); strings.write_byte(&builder, c);
} }
} }
if strings.builder_len(builder) > 0 { if strings.builder_len(builder) > 0 {
@@ -144,7 +143,7 @@ push_comment :: proc(p: ^Printer, comment: tokenizer.Token) -> int {
} }
append(&p.current_line.format_tokens, format_token); append(&p.current_line.format_tokens, format_token);
p.last_token = &p.current_line.format_tokens[len(p.current_line.format_tokens)-1]; p.last_token = &p.current_line.format_tokens[len(p.current_line.format_tokens) - 1];
if strings.contains(line, "/*") { if strings.contains(line, "/*") {
indent(p); indent(p);
@@ -224,7 +223,7 @@ append_format_token :: proc(p: ^Printer, format_token: Format_Token) -> ^Format_
p.last_line_index = p.current_line_index; p.last_line_index = p.current_line_index;
append(&unwrapped_line.format_tokens, format_token); append(&unwrapped_line.format_tokens, format_token);
return &unwrapped_line.format_tokens[len(unwrapped_line.format_tokens)-1]; return &unwrapped_line.format_tokens[len(unwrapped_line.format_tokens) - 1];
} }
@(private) @(private)
@@ -473,7 +472,7 @@ visit_decl :: proc(p: ^Printer, decl: ^ast.Decl, called_in_stmt := false) {
} }
@(private) @(private)
visit_exprs :: proc(p: ^Printer, list: []^ast.Expr, add_comma := false, trailing := false) { visit_exprs :: proc(p: ^Printer, list: [] ^ast.Expr, add_comma := false, trailing := false) {
if len(list) == 0 { if len(list) == 0 {
return; return;
@@ -493,7 +492,7 @@ visit_exprs :: proc(p: ^Printer, list: []^ast.Expr, add_comma := false, trailing
} }
@(private) @(private)
visit_attributes :: proc(p: ^Printer, attributes: [dynamic]^ast.Attribute) { visit_attributes :: proc(p: ^Printer, attributes: [dynamic] ^ast.Attribute) {
if len(attributes) == 0 { if len(attributes) == 0 {
return; return;
@@ -638,7 +637,7 @@ visit_stmt :: proc(p: ^Printer, stmt: ^ast.Stmt, block_type: Block_Type = .Gener
push_generic_token(p, .Switch, 1); push_generic_token(p, .Switch, 1);
hint_current_line(p, {.Switch_Stmt}); hint_current_line(p,{.Switch_Stmt});
if v.init != nil { if v.init != nil {
p.skip_semicolon = true; p.skip_semicolon = true;
@@ -851,7 +850,6 @@ visit_stmt :: proc(p: ^Printer, stmt: ^ast.Stmt, block_type: Block_Type = .Gener
set_source_position(p, stmt.end); set_source_position(p, stmt.end);
} }
@(private) @(private)
visit_expr :: proc(p: ^Printer, expr: ^ast.Expr) { visit_expr :: proc(p: ^Printer, expr: ^ast.Expr) {
@@ -1014,7 +1012,7 @@ visit_expr :: proc(p: ^Printer, expr: ^ast.Expr) {
case Struct_Type: case Struct_Type:
push_generic_token(p, .Struct, 1); push_generic_token(p, .Struct, 1);
hint_current_line(p, {.Struct}); hint_current_line(p,{.Struct});
if v.is_packed { if v.is_packed {
push_ident_token(p, "#packed", 1); push_ident_token(p, "#packed", 1);
@@ -1198,7 +1196,6 @@ visit_expr :: proc(p: ^Printer, expr: ^ast.Expr) {
} }
} }
visit_begin_brace :: proc(p: ^Printer, begin: tokenizer.Pos, type: Block_Type, count := 0) { visit_begin_brace :: proc(p: ^Printer, begin: tokenizer.Pos, type: Block_Type, count := 0) {
set_source_position(p, begin); set_source_position(p, begin);
@@ -1232,11 +1229,11 @@ visit_end_brace :: proc(p: ^Printer, end: tokenizer.Pos) {
p.current_line.depth = p.depth; p.current_line.depth = p.depth;
} }
visit_block_stmts :: proc(p: ^Printer, stmts: []^ast.Stmt, split := false) { visit_block_stmts :: proc(p: ^Printer, stmts: [] ^ast.Stmt, split := false) {
for stmt, i in stmts { for stmt, i in stmts {
visit_stmt(p, stmt, .Generic, false, true); visit_stmt(p, stmt, .Generic, false, true);
if split && i != len(stmts)-1 && stmt.pos.line == stmts[i+1].pos.line { if split && i != len(stmts) - 1 && stmt.pos.line == stmts[i + 1].pos.line {
newline_position(p, 1); newline_position(p, 1);
} }
} }
@@ -1346,7 +1343,6 @@ visit_proc_type :: proc(p: ^Printer, proc_type: ast.Proc_Type) {
visit_signature_list(p, proc_type.results); visit_signature_list(p, proc_type.results);
} }
} }
} }
visit_binary_expr :: proc(p: ^Printer, binary: ast.Binary_Expr) { visit_binary_expr :: proc(p: ^Printer, binary: ast.Binary_Expr) {
@@ -1372,10 +1368,9 @@ visit_binary_expr :: proc(p: ^Printer, binary: ast.Binary_Expr) {
} else { } else {
visit_expr(p, binary.right); visit_expr(p, binary.right);
} }
} }
visit_call_exprs :: proc(p: ^Printer, list: []^ast.Expr, ellipsis := false) { visit_call_exprs :: proc(p: ^Printer, list: [] ^ast.Expr, ellipsis := false) {
if len(list) == 0 { if len(list) == 0 {
return; return;
@@ -1401,7 +1396,6 @@ visit_call_exprs :: proc(p: ^Printer, list: []^ast.Expr, ellipsis := false) {
//we have to newline the expressions to respect the source //we have to newline the expressions to respect the source
move_line_limit(p, expr.pos, 1); move_line_limit(p, expr.pos, 1);
if i == len(list) - 1 && ellipsis { if i == len(list) - 1 && ellipsis {
push_generic_token(p, .Ellipsis, 0); push_generic_token(p, .Ellipsis, 0);
} }
@@ -1415,7 +1409,6 @@ visit_call_exprs :: proc(p: ^Printer, list: []^ast.Expr, ellipsis := false) {
} }
} }
visit_signature_list :: proc(p: ^Printer, list: ^ast.Field_List, remove_blank := true) { visit_signature_list :: proc(p: ^Printer, list: ^ast.Field_List, remove_blank := true) {
if list.list == nil { if list.list == nil {
@@ -1454,7 +1447,7 @@ visit_signature_list :: proc(p: ^Printer, list: ^ast.Field_List, remove_blank :=
if field.type != nil && field.default_value != nil { if field.type != nil && field.default_value != nil {
visit_expr(p, field.type); visit_expr(p, field.type);
push_generic_token(p, .Eq, 0); push_generic_token(p, .Eq, 1);
visit_expr(p, field.default_value); visit_expr(p, field.default_value);
} else if field.type != nil { } else if field.type != nil {
visit_expr(p, field.type); visit_expr(p, field.type);
@@ -1469,4 +1462,3 @@ visit_signature_list :: proc(p: ^Printer, list: ^ast.Field_List, remove_blank :=
} }
} }
} }