format.odin 905 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package odin_format
  2. import "core:odin/printer"
  3. import "core:odin/parser"
  4. import "core:odin/ast"
  5. default_style := printer.default_style
  6. simplify :: proc(file: ^ast.File) {
  7. }
  8. format :: proc(filepath: string, source: string, config: printer.Config, parser_flags := parser.Flags{}, allocator := context.allocator) -> (string, bool) {
  9. config := config
  10. pkg := ast.Package {
  11. kind = .Normal,
  12. }
  13. file := ast.File {
  14. pkg = &pkg,
  15. src = source,
  16. fullpath = filepath,
  17. }
  18. config.newline_limit = clamp(config.newline_limit, 0, 16)
  19. config.spaces = clamp(config.spaces, 1, 16)
  20. config.align_length_break = clamp(config.align_length_break, 0, 64)
  21. p := parser.default_parser(parser_flags)
  22. ok := parser.parse_file(&p, &file)
  23. if !ok || file.syntax_error_count > 0 {
  24. return {}, false
  25. }
  26. prnt := printer.make_printer(config, allocator)
  27. return printer.print(&prnt, &file), true
  28. }