Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/DanielGavin/ols
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielGavin committed Jan 26, 2025
2 parents 3b4aa2f + c1615ee commit a71caaa
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 37 deletions.
8 changes: 8 additions & 0 deletions editors/vscode/syntaxes/odin.tmLanguage.json
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,14 @@
"name": "constant.character.escape.odin",
"match": "\\\\(\\\\|[abefnrutv''\"]|x\\h{2}|u\\h{4}|U\\h{8}|[0-7]{3})"
},
{
"name": "constant.character.escape.placeholders.odin",
"match": "%(v|w|T|%|t|b|c|r|o|d|i|z|x|X|U|e|E|f|F|g|G|h|H|m|M|s|q|x|X|p|p|s)"
},
{
"name": "constant.character.escape.placeholders-floats.odin",
"match": "%(\\d*\\.?\\d*f)"
},
{
"name": "invalid.illegal.unknown-escape.odin",
"match": "\\\\."
Expand Down
2 changes: 2 additions & 0 deletions src/odin/printer/visit.odin
Original file line number Diff line number Diff line change
Expand Up @@ -1042,7 +1042,9 @@ visit_stmt :: proc(
}

document = cons_with_opl(document, visit_expr(p, v.cond))
set_source_position(p, v.body.pos)
document = cons_with_nopl(document, visit_stmt(p, v.body, .Switch_Stmt))
set_source_position(p, v.body.end)
case ^Case_Clause:
document = cons(document, text("case"))

Expand Down
69 changes: 32 additions & 37 deletions tools/odinfmt/snapshot/snapshot.odin
Original file line number Diff line number Diff line change
@@ -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))

Expand Down Expand Up @@ -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,
)

Expand All @@ -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{}
Expand All @@ -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
}
}
Expand Down
20 changes: 20 additions & 0 deletions tools/odinfmt/tests/allman/.snapshots/switch.odin
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package allman

main :: proc()
{
num := 1

switch num
{
case 0:
case 1:
}

switch num
{
case 0:
case 1:
}


}
3 changes: 3 additions & 0 deletions tools/odinfmt/tests/allman/odinfmt.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"brace_style": "Allman"
}
18 changes: 18 additions & 0 deletions tools/odinfmt/tests/allman/switch.odin
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package allman

main :: proc() {
num := 1

switch num
{
case 0:
case 1:
}

switch num {
case 0:
case 1:
}


}

0 comments on commit a71caaa

Please sign in to comment.