Skip to content

Commit

Permalink
Merge pull request #493 from laytan/fix-import-issues
Browse files Browse the repository at this point in the history
Fix import issues
  • Loading branch information
DanielGavin authored Aug 30, 2024
2 parents bd60025 + 5a17ec3 commit e32e63f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
11 changes: 11 additions & 0 deletions src/odin/printer/printer.odin
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -273,6 +278,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))
}
}
9 changes: 5 additions & 4 deletions src/odin/printer/visit.odin
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit e32e63f

Please sign in to comment.