aboutsummaryrefslogtreecommitdiff
path: root/tools/odinfmt/main.odin
diff options
context:
space:
mode:
Diffstat (limited to 'tools/odinfmt/main.odin')
-rw-r--r--tools/odinfmt/main.odin80
1 files changed, 43 insertions, 37 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 {