more switch alignment work

This commit is contained in:
Daniel Gavin
2021-04-14 16:03:36 +02:00
parent 1f563f2810
commit 7e90ece84a
+28 -3
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}; 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];
@@ -17,7 +17,7 @@ Line :: struct {
finalized: bool, finalized: bool,
used: bool, used: bool,
depth: int, depth: int,
types: Line_Type, types: Line_Type, //for performance, so you don't have to verify what types are in it by going through the tokens - might give problems when adding linebreaking
} }
Format_Token :: struct { Format_Token :: struct {
@@ -55,8 +55,10 @@ Config :: struct {
convert_do: bool, //Convert all do statements to brace blocks convert_do: bool, //Convert all do statements to brace blocks
semicolons: bool, //Enable semicolons semicolons: bool, //Enable semicolons
split_multiple_stmts: bool, split_multiple_stmts: bool,
align_switch: bool,
brace_style: Brace_Style, brace_style: Brace_Style,
align_assignments: bool, align_assignments: bool,
align_structs: bool,
align_style: Alignment_Style, align_style: Alignment_Style,
indent_cases: bool, indent_cases: bool,
newline_style: Newline_Style, newline_style: Newline_Style,
@@ -99,6 +101,8 @@ default_style := Config {
align_assignments = true, align_assignments = true,
align_style = .Align_On_Type_And_Equals, align_style = .Align_On_Type_And_Equals,
indent_cases = false, indent_cases = false,
align_switch = true,
align_structs = true,
}; };
make_printer :: proc(config: Config, allocator := context.allocator) -> Printer { make_printer :: proc(config: Config, allocator := context.allocator) -> Printer {
@@ -221,6 +225,7 @@ align_switch_smt :: proc(p: ^Printer, index: int) {
} }
largest := 0; largest := 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:] {
@@ -243,14 +248,22 @@ align_switch_smt :: proc(p: ^Printer, index: int) {
if format_token.kind == .Case { if format_token.kind == .Case {
case_found = true; case_found = true;
case_count += 1;
} else if format_token.kind == .Colon { } else if format_token.kind == .Colon {
colon_found = true; colon_found = true;
} }
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 {
break;
}
} }
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;
@@ -271,16 +284,26 @@ align_switch_smt :: proc(p: ^Printer, index: int) {
if format_token.kind == .Case { if format_token.kind == .Case {
case_found = true; case_found = true;
case_count += 1;
} else if format_token.kind == .Colon { } else if format_token.kind == .Colon {
colon_found = true; colon_found = true;
} }
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 {
break;
}
} }
} }
} }
align_struct :: proc(p: ^Printer, index: int) {
}
align_blocks :: proc(p: ^Printer) { align_blocks :: proc(p: ^Printer) {
for line, line_index in p.lines { for line, line_index in p.lines {
@@ -289,8 +312,10 @@ align_blocks :: proc(p: ^Printer) {
continue; continue;
} }
if .Switch_Stmt in line.types { if .Switch_Stmt in line.types && p.config.align_switch {
align_switch_smt(p, line_index); align_switch_smt(p, line_index);
} else if .Struct in line.types && p.config.align_structs {
align_struct(p, line_index);
} }
} }