diff options
Diffstat (limited to 'tools')
| -rw-r--r-- | tools/odinfmt/main.odin | 80 | ||||
| -rw-r--r-- | tools/odinfmt/snapshot/snapshot.odin | 18 | ||||
| -rw-r--r-- | tools/odinfmt/tests.odin | 2 |
3 files changed, 51 insertions, 49 deletions
diff --git a/tools/odinfmt/main.odin b/tools/odinfmt/main.odin index b44af5c..54eab88 100644 --- a/tools/odinfmt/main.odin +++ b/tools/odinfmt/main.odin @@ -1,12 +1,9 @@ package odinfmt -import "core:encoding/json" import "core:flags" import "core:fmt" -import "core:io" import "core:mem" import vmem "core:mem/virtual" -import "core:odin/tokenizer" import "core:os" import "core:path/filepath" import "core:strings" @@ -15,36 +12,27 @@ import "src:odin/format" import "src:odin/printer" Args :: struct { - write: bool `args:"name=w" usage:"write the new format to file"`, - stdin: bool `usage:"formats code from standard input"`, - path: string `args:"pos=0" usage:"set the file or directory to format"`, - config: string `usage:"path to a config file"` + write: bool `args:"name=w" usage:"write the new format to file"`, + stdin: bool `usage:"formats code from standard input"`, + path: string `args:"pos=0" usage:"set the file or directory to format"`, + config: string `usage:"path to a config file"`, } -format_file :: proc(filepath: string, config: printer.Config, allocator := context.allocator) -> (string, bool) { - if data, ok := os.read_entire_file(filepath, allocator); ok { +format_file :: proc( + filepath: string, + config: printer.Config, + allocator := context.allocator, +) -> ( + string, + bool, +) { + if data, err := os.read_entire_file(filepath, allocator); err == nil { return format.format(filepath, string(data), config, {.Optional_Semicolons}, allocator) } else { return "", false } } -files: [dynamic]string - -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 - } - - if filepath.ext(info.name) != ".odin" { - return nil, false - } - - append(&files, strings.clone(info.fullpath)) - - return nil, false -} - main :: proc() { arena: vmem.Arena arena_err := vmem.arena_init_growing(&arena) @@ -63,7 +51,7 @@ main :: proc() { args.path = "." } else { fmt.fprint(os.stderr, "Missing path to format\n") - flags.write_usage(os.stream_from_handle(os.stderr), Args, os.args[0]) + flags.write_usage(os.to_stream(os.stderr), Args, os.args[0]) os.exit(1) } } @@ -72,14 +60,14 @@ main :: proc() { write_failure := false - watermark : uint = 0 + watermark: uint = 0 - config: printer.Config - if args.config == "" { - config = format.find_config_file_or_default(args.path) - } else { - config = format.read_config_file_from_path_or_default(args.config) - } + config: printer.Config + if args.config == "" { + config = format.find_config_file_or_default(args.path) + } else { + config = format.read_config_file_from_path_or_default(args.config) + } if args.stdin { data := make([dynamic]byte, arena_allocator) @@ -93,7 +81,13 @@ 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) @@ -108,7 +102,7 @@ main :: proc() { if data, ok := format_file(args.path, config, arena_allocator); ok { os.rename(args.path, backup_path) - if os.write_entire_file(args.path, transmute([]byte)data) { + if err := os.write_entire_file(args.path, transmute([]byte)data); err == nil { os.remove(backup_path) } } else { @@ -121,7 +115,19 @@ main :: proc() { } } } else if os.is_dir(args.path) { - filepath.walk(args.path, walk_files, nil) + files: [dynamic]string + w := os.walker_create(args.path) + for info in os.walker_walk(&w) { + if info.type == .Directory { + continue + } + + if filepath.ext(info.name) != ".odin" { + continue + } + + append(&files, strings.clone(info.fullpath)) + } for file in files { fmt.println(file) @@ -133,7 +139,7 @@ main :: proc() { if args.write { os.rename(file, backup_path) - if os.write_entire_file(file, transmute([]byte)data) { + if err := os.write_entire_file(file, transmute([]byte)data); err == nil { os.remove(backup_path) } } else { diff --git a/tools/odinfmt/snapshot/snapshot.odin b/tools/odinfmt/snapshot/snapshot.odin index 3220845..c51c216 100644 --- a/tools/odinfmt/snapshot/snapshot.odin +++ b/tools/odinfmt/snapshot/snapshot.odin @@ -5,14 +5,13 @@ import "core:fmt" import "core:os" import "core:path/filepath" import "core:strings" -import "core:testing" import "core:text/scanner" import "src:odin/format" import "src:odin/printer" format_file :: proc(filepath: string, allocator := context.allocator) -> (string, bool) { - if data, ok := os.read_entire_file(filepath, allocator); ok { + if data, err := os.read_entire_file(filepath, allocator); err == nil { config := read_config_file_or_default(filepath) return format.format(filepath, string(data), config, {.Optional_Semicolons}, allocator) } else { @@ -30,7 +29,7 @@ read_config_file_or_default :: proc(fullpath: string, allocator := context.alloc if (os.exists(configpath)) { json_config := default_style - if data, ok := os.read_entire_file(configpath, allocator); ok { + if data, err := os.read_entire_file(configpath, allocator); err == nil { if json.unmarshal(data, &json_config) == nil { return json_config } @@ -44,7 +43,7 @@ read_config_file_or_default :: proc(fullpath: string, allocator := context.alloc snapshot_directory :: proc(directory: string) -> bool { matches, err := filepath.glob(fmt.tprintf("%v/*", directory)) - if err != .None { + if err != nil { fmt.eprintf("Error in globbing directory: %v", directory) } @@ -69,7 +68,7 @@ snapshot_file :: proc(path: string) -> bool { fmt.printf("Testing snapshot %v", path) - snapshot_path := filepath.join( + snapshot_path, _ := filepath.join( elems = {filepath.dir(path, context.temp_allocator), "/.snapshots", filepath.base(path)}, allocator = context.temp_allocator, ) @@ -82,7 +81,7 @@ snapshot_file :: proc(path: string) -> bool { } if os.exists(snapshot_path) { - if snapshot_data, ok := os.read_entire_file(snapshot_path, context.temp_allocator); ok { + if snapshot_data, err := os.read_entire_file(snapshot_path, context.temp_allocator); err == nil { snapshot_scanner := scanner.Scanner{} scanner.init(&snapshot_scanner, string(snapshot_data)) formatted_scanner := scanner.Scanner{} @@ -107,7 +106,7 @@ snapshot_file :: proc(path: string) -> bool { if s_ch != f_ch { fmt.eprintf("\nFormatted file was different from snapshot file: %v\n", snapshot_path) - os.write_entire_file(fmt.tprintf("%v_failed", snapshot_path), transmute([]u8)formatted) + _ = os.write_entire_file(fmt.tprintf("%v_failed", snapshot_path), transmute([]u8)formatted) return false } } @@ -118,9 +117,8 @@ snapshot_file :: proc(path: string) -> bool { } } else { os.make_directory(filepath.dir(snapshot_path, context.temp_allocator)) - ok = os.write_entire_file(snapshot_path, transmute([]byte)formatted) - if !ok { - fmt.eprintf("Failed to write snapshot file %v", snapshot_path) + if err := os.write_entire_file(snapshot_path, transmute([]byte)formatted); err != nil { + fmt.eprintf("Failed to write snapshot file %v: %v", snapshot_path, err) return false } } diff --git a/tools/odinfmt/tests.odin b/tools/odinfmt/tests.odin index 313e33c..0917838 100644 --- a/tools/odinfmt/tests.odin +++ b/tools/odinfmt/tests.odin @@ -1,8 +1,6 @@ package odinfmt_tests -import "core:testing" import "core:os" -import "core:fmt" import "core:mem" import "snapshot" |