diff options
| author | DanielGavin <danielgavin5@hotmail.com> | 2024-10-05 20:58:23 +0200 |
|---|---|---|
| committer | DanielGavin <danielgavin5@hotmail.com> | 2024-10-05 20:58:23 +0200 |
| commit | 90c90ae5dd0619f246c2379f48a4f0a7b5f5e0b7 (patch) | |
| tree | c989ed5cc3672827c848b277c27e0776245f3824 | |
| parent | 8508eb6004ec4dd3fa032d08cf7036daf94d97e5 (diff) | |
Add the ability to error out of the formatter.
| -rw-r--r-- | src/main.odin | 6 | ||||
| -rw-r--r-- | src/odin/format/format.odin | 9 | ||||
| -rw-r--r-- | src/odin/printer/printer.odin | 5 | ||||
| -rw-r--r-- | src/odin/printer/visit.odin | 13 | ||||
| -rw-r--r-- | src/server/format.odin | 4 | ||||
| -rw-r--r-- | tools/odinfmt/main.odin | 34 |
6 files changed, 28 insertions, 43 deletions
diff --git a/src/main.odin b/src/main.odin index 00ed8b0..6e91583 100644 --- a/src/main.odin +++ b/src/main.odin @@ -63,10 +63,7 @@ run :: proc(reader: ^server.Reader, writer: ^server.Writer) { server.requests = make([dynamic]server.Request, context.allocator) server.deletings = make([dynamic]server.Request, context.allocator) - request_thread = thread.create_and_start_with_data( - cast(rawptr)&request_thread_data, - server.thread_request_main, - ) + request_thread = thread.create_and_start_with_data(cast(rawptr)&request_thread_data, server.thread_request_main) for common.config.running { if common.config.verbose { @@ -119,6 +116,5 @@ main :: proc() { init_global_temporary_allocator(mem.Megabyte * 100) - run(&reader, &writer) } diff --git a/src/odin/format/format.odin b/src/odin/format/format.odin index 468f6de..e732d51 100644 --- a/src/odin/format/format.odin +++ b/src/odin/format/format.odin @@ -34,10 +34,7 @@ find_config_file_or_default :: proc(path: string) -> printer.Config { } } } else { - new_path := filepath.join( - elems = {path, ".."}, - allocator = context.temp_allocator, - ) + new_path := filepath.join(elems = {path, ".."}, allocator = context.temp_allocator) //Currently the filepath implementation seems to stop at the root level, this might not be the best solution. if new_path == path { return default_style @@ -87,5 +84,7 @@ format :: proc( prnt := printer.make_printer(config, allocator) - return printer.print(&prnt, &file), true + src := printer.print(&prnt, &file) + + return src, !prnt.errored_out } diff --git a/src/odin/printer/printer.odin b/src/odin/printer/printer.odin index 2258466..a02557e 100644 --- a/src/odin/printer/printer.odin +++ b/src/odin/printer/printer.odin @@ -30,6 +30,7 @@ Printer :: struct { group_modes: map[string]Document_Group_Mode, force_statement_fit: bool, src: string, + errored_out: bool, } Disabled_Info :: struct { @@ -256,6 +257,10 @@ print_file :: proc(p: ^Printer, file: ^ast.File) -> string { } } + if p.errored_out { + return "" + } + // If the file ends with imports. if import_group_start != nil { print_sorted_imports(p, file.decls[import_group_start.?:]) diff --git a/src/odin/printer/visit.odin b/src/odin/printer/visit.odin index 4aa5082..fbc836c 100644 --- a/src/odin/printer/visit.odin +++ b/src/odin/printer/visit.odin @@ -1,6 +1,7 @@ package odin_printer import "core:fmt" +import "core:log" import "core:odin/ast" import "core:odin/parser" import "core:odin/tokenizer" @@ -341,7 +342,9 @@ visit_decl :: proc(p: ^Printer, decl: ^ast.Decl, called_in_stmt := false) -> ^Do return cons(document, group(lhs)) } case: - panic(fmt.aprint(decl.derived)) + log.error(decl.derived) + p.errored_out = true + return nil } return empty() @@ -1252,7 +1255,9 @@ visit_stmt :: proc( document = cons_with_nopl(document, visit_expr(p, v.label)) } case: - panic(fmt.aprint(stmt.derived)) + log.error(stmt.derived) + p.errored_out = true + return nil } set_source_position(p, stmt.end) @@ -1865,7 +1870,9 @@ visit_expr :: proc( document = cons_with_opl(document, visit_expr(p, v.column_index)) document = cons(document, text("]")) case: - panic(fmt.aprint(expr.derived)) + log.error(expr.derived) + p.errored_out = true + return nil } return cons(comments, document) diff --git a/src/server/format.odin b/src/server/format.odin index c518532..d981b9c 100644 --- a/src/server/format.odin +++ b/src/server/format.odin @@ -38,6 +38,10 @@ get_complete_format :: proc(document: ^Document, config: ^common.Config) -> ([]T src := printer.print(&prnt, &document.ast) + if prnt.errored_out { + return {}, true + } + edit := TextEdit { newText = src, range = common.get_document_range(document.text[0:document.used_text]), diff --git a/tools/odinfmt/main.odin b/tools/odinfmt/main.odin index 83bccb7..a912001 100644 --- a/tools/odinfmt/main.odin +++ b/tools/odinfmt/main.odin @@ -43,22 +43,9 @@ print_arg_error :: proc(args: []string, error: flag.Flag_Error) { } -format_file :: proc( - filepath: string, - config: printer.Config, - allocator := context.allocator, -) -> ( - string, - bool, -) { +format_file :: proc(filepath: string, config: printer.Config, allocator := context.allocator) -> (string, bool) { if data, ok := os.read_entire_file(filepath, allocator); ok { - return format.format( - filepath, - string(data), - config, - {.Optional_Semicolons}, - allocator, - ) + return format.format(filepath, string(data), config, {.Optional_Semicolons}, allocator) } else { return "", false } @@ -66,14 +53,7 @@ format_file :: proc( files: [dynamic]string -walk_files :: proc( - info: os.File_Info, - in_err: os.Errno, - user_data: rawptr, -) -> ( - err: os.Error, - skip_dir: bool, -) { +walk_files :: proc(info: os.File_Info, in_err: os.Errno, user_data: rawptr) -> (err: os.Error, skip_dir: bool) { if info.is_dir { return nil, false } @@ -138,13 +118,7 @@ main :: proc() { append(&data, ..tmp[:r]) } - source, ok := format.format( - "<stdin>", - string(data[:]), - config, - {.Optional_Semicolons}, - arena_allocator, - ) + source, ok := format.format("<stdin>", string(data[:]), config, {.Optional_Semicolons}, arena_allocator) if ok { fmt.println(source) |