diff options
| author | henbr <henrik.brandt@protonmail.com> | 2025-01-19 20:20:42 +0100 |
|---|---|---|
| committer | henbr <henrik.brandt@protonmail.com> | 2025-01-19 20:20:42 +0100 |
| commit | 92d17f328fa66b226cd3b995773830e16bae66ed (patch) | |
| tree | db6dcae09a55c32ef1f73dddac818d3141557b38 /tools | |
| parent | 79b83b2bd90c69acaf13110c2cd6ab76b47c9cba (diff) | |
Read config from file if available
Diffstat (limited to 'tools')
| -rw-r--r-- | tools/odinfmt/snapshot/snapshot.odin | 69 |
1 files changed, 32 insertions, 37 deletions
diff --git a/tools/odinfmt/snapshot/snapshot.odin b/tools/odinfmt/snapshot/snapshot.odin index 8252c40..7f60897 100644 --- a/tools/odinfmt/snapshot/snapshot.odin +++ b/tools/odinfmt/snapshot/snapshot.odin @@ -1,38 +1,46 @@ package odinfmt_testing -import "core:testing" +import "core:encoding/json" +import "core:fmt" import "core:os" import "core:path/filepath" import "core:strings" +import "core:testing" import "core:text/scanner" -import "core:fmt" import "src:odin/format" +import "src:odin/printer" -format_file :: proc( - filepath: string, - allocator := context.allocator, -) -> ( - string, - bool, -) { - style := format.default_style - style.character_width = 80 - style.newline_style = .LF //We want to make sure it works on linux and windows. - +format_file :: proc(filepath: string, allocator := context.allocator) -> (string, bool) { if data, ok := os.read_entire_file(filepath, allocator); ok { - return format.format( - filepath, - string(data), - style, - {.Optional_Semicolons}, - allocator, - ) + config := read_config_file_or_default(filepath) + return format.format(filepath, string(data), config, {.Optional_Semicolons}, allocator) } else { return "", false } } +read_config_file_or_default :: proc(fullpath: string, allocator := context.allocator) -> printer.Config { + default_style := format.default_style + default_style.character_width = 80 + default_style.newline_style = .LF //We want to make sure it works on linux and windows. + + dirpath := filepath.dir(fullpath, allocator) + configpath := fmt.tprintf("%v/odinfmt.json", dirpath) + + if (os.exists(configpath)) { + json_config := default_style + if data, ok := os.read_entire_file(configpath, allocator); ok { + if json.unmarshal(data, &json_config) == nil { + return json_config + } + } + } + + return default_style + +} + snapshot_directory :: proc(directory: string) -> bool { matches, err := filepath.glob(fmt.tprintf("%v/*", directory)) @@ -62,11 +70,7 @@ snapshot_file :: proc(path: string) -> bool { snapshot_path := filepath.join( - elems = { - filepath.dir(path, context.temp_allocator), - "/.snapshots", - filepath.base(path), - }, + elems = {filepath.dir(path, context.temp_allocator), "/.snapshots", filepath.base(path)}, allocator = context.temp_allocator, ) @@ -78,10 +82,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, ok := os.read_entire_file(snapshot_path, context.temp_allocator); ok { snapshot_scanner := scanner.Scanner{} scanner.init(&snapshot_scanner, string(snapshot_data)) formatted_scanner := scanner.Scanner{} @@ -105,14 +106,8 @@ snapshot_file :: proc(path: string) -> bool { } if s_ch != f_ch { - fmt.eprintf( - "\nFormatted file was different from snapshot file: %v", - snapshot_path, - ) - os.write_entire_file( - fmt.tprintf("%v_failed", snapshot_path), - transmute([]u8)formatted, - ) + fmt.eprintf("\nFormatted file was different from snapshot file: %v", snapshot_path) + os.write_entire_file(fmt.tprintf("%v_failed", snapshot_path), transmute([]u8)formatted) return false } } |