fix proc group, struct align with internal structs

This commit is contained in:
Daniel Gavin
2021-04-23 16:33:11 +02:00
parent f10f7ebbf1
commit 3f9ad6ba09
2 changed files with 32 additions and 33 deletions
+25 -26
View File
@@ -483,7 +483,10 @@ align_var_decls :: proc(p: ^Printer) {
not_mutable = true;
}
if tokenizer.Token_Kind.B_Keyword_Begin <= line.format_tokens[i].kind && line.format_tokens[i].kind <= tokenizer.Token_Kind.B_Keyword_End {
if line.format_tokens[i].kind == tokenizer.Token_Kind.Proc ||
line.format_tokens[i].kind == tokenizer.Token_Kind.Union ||
line.format_tokens[i].kind == tokenizer.Token_Kind.Enum ||
line.format_tokens[i].kind == tokenizer.Token_Kind.Struct {
continue_flag = true;
}
}
@@ -496,6 +499,7 @@ align_var_decls :: proc(p: ^Printer) {
if p.config.align_style == .Align_On_Colon_And_Equals || !current_typed || current_not_mutable {
for colon_token in colon_tokens {
fmt.println(colon_token);
colon_token.format_token.spaces_before = largest_lhs - colon_token.length + 1;
}
} else if p.config.align_style == .Align_On_Type_And_Equals {
@@ -703,7 +707,7 @@ align_enum :: proc(p: ^Printer, index: int) {
return;
}
largest := 0;
largest := 0;
comma_count := 0;
for line, line_index in p.lines[brace_line + 1:] {
@@ -781,30 +785,14 @@ align_struct :: proc(p: ^Printer, index: int) {
largest := 0;
colon_count := 0;
seen_brace := false;
for line, line_index in p.lines[brace_line + 1:] {
length := 0;
TokenAndLength :: struct {
format_token: ^Format_Token,
length: int,
};
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;
format_tokens := make([] TokenAndLength, brace_token.parameter_count, context.temp_allocator);
for line, line_index in p.lines[brace_line + 1:] {
length := 0;
@@ -812,12 +800,18 @@ align_struct :: proc(p: ^Printer, index: int) {
for format_token, i in line.format_tokens {
if format_token.kind == .Comment {
continue;
} else if format_token.kind == .Open_Brace {
seen_brace = true;
} else if format_token.kind == .Close_Brace {
seen_brace = false;
} else if seen_brace {
continue;
}
if format_token.kind == .Colon {
format_tokens[colon_count] = { format_token = &line.format_tokens[i + 1], length = length };
colon_count += 1;
line.format_tokens[i + 1].spaces_before = largest - length + 1;
break;
largest = max(length, largest);
}
length += len(format_token.text) + format_token.spaces_before;
@@ -827,6 +821,11 @@ align_struct :: proc(p: ^Printer, index: int) {
break;
}
}
for token in format_tokens {
token.format_token.spaces_before = largest - token.length + 1;
}
}
align_comments :: proc(p: ^Printer) {
+7 -7
View File
@@ -31,7 +31,7 @@ sort_attribute :: proc(s: ^[dynamic]^ast.Attribute) -> sort.Interface {
@(private)
comment_before_position :: proc(p: ^Printer, pos: tokenizer.Pos) -> bool {
if len(p.comments) <= p.latest_comment_index {
return false;
}
@@ -42,7 +42,7 @@ comment_before_position :: proc(p: ^Printer, pos: tokenizer.Pos) -> bool {
}
@(private)
next_comment_group :: proc(p: ^Printer) {
next_comment_group :: proc(p: ^Printer) {
p.latest_comment_index += 1;
}
@@ -79,7 +79,7 @@ push_comment :: proc(p: ^Printer, comment: tokenizer.Token) -> int {
builder := strings.make_builder(context.temp_allocator);
c_len := len(comment.text);
c_len := len(comment.text);
trim_space := true;
multilines: [dynamic]string;
@@ -157,7 +157,7 @@ push_comment :: proc(p: ^Printer, comment: tokenizer.Token) -> int {
@(private)
push_comments :: proc(p: ^Printer, pos: tokenizer.Pos) {
prev_comment: ^tokenizer.Token;
prev_comment: ^tokenizer.Token;
prev_comment_lines: int;
for comment_before_position(p, pos) {
@@ -1141,7 +1141,7 @@ visit_expr :: proc(p: ^Printer, expr: ^ast.Expr) {
push_generic_token(p, .Close_Bracket, 0);
case Proc_Group:
push_generic_token(p, v.tok.kind, 0);
push_generic_token(p, v.tok.kind, 1);
if len(v.args) != 0 && v.pos.line != v.args[len(v.args) - 1].pos.line {
visit_begin_brace(p, v.pos, .Generic);
@@ -1354,7 +1354,7 @@ visit_proc_type :: proc(p: ^Printer, proc_type: ast.Proc_Type) {
push_generic_token(p, .Gt, 0);
use_parens := false;
use_named := false;
use_named := false;
if len(proc_type.results.list) > 1 {
use_parens = true;
@@ -1497,4 +1497,4 @@ visit_signature_list :: proc(p: ^Printer, list: ^ast.Field_List, remove_blank :=
push_generic_token(p, .Comma, 0);
}
}
}
}