diff options
| author | Daniel Gavin <danielgavin5@hotmail.com> | 2022-11-23 21:00:59 +0100 |
|---|---|---|
| committer | Daniel Gavin <danielgavin5@hotmail.com> | 2022-11-23 21:00:59 +0100 |
| commit | 6b01ef5eb33ccac4de7dcb46182998af5a34bbb0 (patch) | |
| tree | 156caba3abf83a94fc17206551ef5fcacab1faaa | |
| parent | 7c50708ca4e4fe0be1b4010df43190bc02a8ecee (diff) | |
| parent | 6e118a8640612fce3155dcbddee555778d21b04a (diff) | |
Merge branch 'master' of github.com:DanielGavin/ols
| -rw-r--r-- | .gitignore | 3 | ||||
| -rw-r--r-- | README.md | 3 | ||||
| -rw-r--r-- | tools/odinfmt/main.odin | 51 |
3 files changed, 38 insertions, 19 deletions
@@ -11,4 +11,7 @@ *.sqlite
*.suo
*node_modules
+ols
+odinfmt
+
@@ -1,7 +1,7 @@ # ols Language server for Odin. This project is still in early development. -**Status**: Works on all platforms. +**Status**: No longer works after new Odin update(48c9c1682c347adb7e743a6a6f8c70f08420c197 and before works fine) ## Table Of Contents - [Installation](#installation) @@ -166,4 +166,5 @@ indent = { tab-width = 2, unit = " " } language-server = { command = "ols" } injection-regex = "odin" roots = ["ols.json"] +formatter = { command = "odinfmt", args = [ "-stdin", "true" ] } ``` diff --git a/tools/odinfmt/main.odin b/tools/odinfmt/main.odin index de6787a..c13e4cc 100644 --- a/tools/odinfmt/main.odin +++ b/tools/odinfmt/main.odin @@ -14,10 +14,11 @@ import "flag" Args :: struct { write: Maybe(bool) `flag:"w" usage:"write the new format to file"`, + stdin: Maybe(bool) `flag:"stdin" usage:"formats code from standard input"`, } print_help :: proc(args: []string) { - fmt.eprintln("usage: odinfmt -w {filepath}") + fmt.eprintln("usage: odinfmt [-w {filepath}] [-stdin]") } print_arg_error :: proc(args: []string, error: flag.Flag_Error) { @@ -51,13 +52,7 @@ format_file :: proc( 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 } @@ -65,13 +60,7 @@ format_file :: proc( files: [dynamic]string -walk_files :: proc( - info: os.File_Info, - in_err: os.Errno, -) -> ( - err: os.Errno, - skip_dir: bool, -) { +walk_files :: proc(info: os.File_Info, in_err: os.Errno) -> (err: os.Errno, skip_dir: bool) { if info.is_dir { return 0, false } @@ -113,8 +102,34 @@ main :: proc() { watermark := 0 - if os.is_file(path) { - config := format.find_config_file_or_default(path) + config := format.find_config_file_or_default(path) + + if _, ok := args.stdin.(bool); ok { + data := make([dynamic]byte, arena_allocator) + + for { + tmp: [mem.Kilobyte]byte + + r, err := os.read(os.stdin, tmp[:]) + if err != os.ERROR_NONE || r <= 0 do break + + append(&data, ..tmp[:r]) + } + + source, ok := format.format( + "<stdin>", + string(data[:]), + config, + {.Optional_Semicolons}, + arena_allocator, + ) + + if ok { + fmt.println(source) + } + + write_failure = !ok + } else if os.is_file(path) { if _, ok := args.write.(bool); ok { backup_path := strings.concatenate({path, "_bk"}) defer delete(backup_path) @@ -135,7 +150,6 @@ main :: proc() { } } } else if os.is_dir(path) { - config := format.find_config_file_or_default(path) filepath.walk(path, walk_files) for file in files { @@ -177,3 +191,4 @@ main :: proc() { os.exit(1 if write_failure else 0) } + |