From 8841c68bcc33ba9bca30ae3508e23bd49cf06ee5 Mon Sep 17 00:00:00 2001 From: Laytan Laats Date: Sat, 31 Aug 2024 00:45:56 +0200 Subject: fix formatting of `@(require)` imports --- src/odin/printer/printer.odin | 6 ++++++ src/odin/printer/visit.odin | 9 +++++---- 2 files changed, 11 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/odin/printer/printer.odin b/src/odin/printer/printer.odin index 1c5f5b2..73a544e 100644 --- a/src/odin/printer/printer.odin +++ b/src/odin/printer/printer.odin @@ -273,6 +273,12 @@ print_sorted_imports :: proc(p: ^Printer, decls: []^ast.Stmt) { decl.pos.line = start_line + i decl.end.line = start_line + i + imp := decl.derived.(^ast.Import_Decl) + for attr in imp.attributes { + attr.pos.line = start_line + i + attr.end.line = start_line + i + } + p.document = cons(p.document, visit_decl(p, cast(^ast.Decl)decl)) } } diff --git a/src/odin/printer/visit.odin b/src/odin/printer/visit.odin index 02e2867..4c8902b 100644 --- a/src/odin/printer/visit.odin +++ b/src/odin/printer/visit.odin @@ -265,12 +265,13 @@ visit_decl :: proc(p: ^Printer, decl: ^ast.Decl, called_in_stmt := false) -> ^Do return document case ^Import_Decl: - document := move_line(p, decl.pos) - + document := empty() if len(v.attributes) > 0 { document = cons(document, visit_attributes(p, &v.attributes, v.pos)) } + document = cons(document, move_line(p, decl.pos)) + if v.name.text != "" { document = cons( document, @@ -820,8 +821,8 @@ visit_attributes :: proc(p: ^Printer, attributes: ^[dynamic]^ast.Attribute, pos: //Ensure static is not forced newline, but until if the width is full if len(attributes) == 1 && len(attributes[0].elems) == 1 { - if ident, ok := attributes[0].elems[0].derived.(^ast.Ident); ok && ident.name == "static" { - document = cons(document, text("@"), text("("), visit_expr(p, attributes[0].elems[0]), text(")")) + if ident, ok := attributes[0].elems[0].derived.(^ast.Ident); ok && (ident.name == "static" || ident.name == "require") { + document = cons(document, text("@"), text("("), visit_expr(p, attributes[0].elems[0]), text(")"), break_with_no_newline()) set_source_position(p, pos) return document } -- cgit v1.2.3 From 5a17ec35bd8859a954931c22634ef538e44d3735 Mon Sep 17 00:00:00 2001 From: Laytan Laats Date: Sat, 31 Aug 2024 00:46:07 +0200 Subject: fix removing imports at the end of the file --- src/odin/printer/printer.odin | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'src') diff --git a/src/odin/printer/printer.odin b/src/odin/printer/printer.odin index 73a544e..b71eab0 100644 --- a/src/odin/printer/printer.odin +++ b/src/odin/printer/printer.odin @@ -242,6 +242,11 @@ print_file :: proc(p: ^Printer, file: ^ast.File) -> string { } } + // If the file ends with imports. + if import_group_start != nil { + print_sorted_imports(p, file.decls[import_group_start.?:]) + } + if len(p.comments) > 0 { infinite := p.comments[len(p.comments) - 1].end infinite.offset = 9999999 -- cgit v1.2.3