Skip to content

Commit

Permalink
fix(grainfmt): Indent lines when wrapping infix operators (#1465)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcusroberts authored Nov 10, 2022
1 parent b980212 commit d705849
Show file tree
Hide file tree
Showing 29 changed files with 807 additions and 440 deletions.
510 changes: 403 additions & 107 deletions compiler/src/formatting/format.re

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions compiler/test/formatter_inputs/application_indenting.gr
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,5 @@ let grisuRound = (buffer, len, delta, rest, ten_kappa, wp_w) => {
}
Module32.store8(lastp, digit, 0n)
}

assert Hash.hash("grain > ore > wool > lumber > brick") == Hash.hash("grain > ore > wool > lumber > brick")
13 changes: 13 additions & 0 deletions compiler/test/formatter_inputs/binops.gr
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
fofdfdfdfdfdfdfdfdfdfdfdo &&
bafdfdfdfdfdfdddefdfdfdfdfdr &&
badfdfdfdfdfdfdffdffdfdfdz


let x= fofdfdfdfdfdfdfdfdfdfdfdo &&
bafdfdfdfdfdfdddefdfdfdfdfdr &&
badfdfdfdfdfdfdffdffdfdfdz


if (fofdfdfdfdfdfdfdfdfdfdfdo &&
bafdfdfdfdfdfdddefdfdfdfdfdr &&
badfdfdfdfdfdfdffdffdfdfdz) true
7 changes: 6 additions & 1 deletion compiler/test/formatter_inputs/lets.gr
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,9 @@ let rotate = (count, list) => {
append(end, beginning)
}


let qsize = (if (WasmI32.eqz(WasmI32.and(m + 1n, 1n))) {
m + 1n
} else {
m + 2n
}) >>
1n
5 changes: 4 additions & 1 deletion compiler/test/formatter_outputs/application_indenting.gr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ let flagsToWasmVal = (flag, i) => {
let riiiiiiightsInheriting = Module64.load(structPtr, 16n)
(riiiiiiightsInheriting &
1N << Module64.extendI32U(Module32.fromGrain(i) >> 1n)) >
0N
0N
}

let grisuRound = (buffer, len, delta, rest, ten_kappa, wp_w) => {
Expand All @@ -23,3 +23,6 @@ let grisuRound = (buffer, len, delta, rest, ten_kappa, wp_w) => {
}
Module32.store8(lastp, digit, 0n)
}

assert Hash.hash("grain > ore > wool > lumber > brick") ==
Hash.hash("grain > ore > wool > lumber > brick")
13 changes: 13 additions & 0 deletions compiler/test/formatter_outputs/binops.gr
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
fofdfdfdfdfdfdfdfdfdfdfdo &&
bafdfdfdfdfdfdddefdfdfdfdfdr &&
badfdfdfdfdfdfdffdffdfdfdz

let x = fofdfdfdfdfdfdfdfdfdfdfdo &&
bafdfdfdfdfdfdddefdfdfdfdfdr &&
badfdfdfdfdfdfdffdffdfdfdz

if (
fofdfdfdfdfdfdfdfdfdfdfdo &&
bafdfdfdfdfdfdddefdfdfdfdfdr &&
badfdfdfdfdfdfdffdffdfdfdz
) true
11 changes: 9 additions & 2 deletions compiler/test/formatter_outputs/lets.gr
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,14 @@ let myFunction4 = () => {
}

let rotate = (count, list) => {
let (beginning, end) = if (count >= 0) part(count, list)
else part(length(list) + count, list)
let (beginning, end) =
if (count >= 0) part(count, list) else part(length(list) + count, list)
append(end, beginning)
}

let qsize = (if (WasmI32.eqz(WasmI32.and(m + 1n, 1n))) {
m + 1n
} else {
m + 2n
}) >>
1n
2 changes: 1 addition & 1 deletion compiler/test/stdlib/array.test.gr
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ let arrC = [> 1]
let arrD = [> 2]

assert Array.product(arrA, arrB) ==
[> (1, 2), (1, 3), (1, 4), (2, 2), (2, 3), (2, 4)]
[> (1, 2), (1, 3), (1, 4), (2, 2), (2, 3), (2, 4)]
assert Array.product(arrC, arrB) == [> (1, 2), (1, 3), (1, 4)]
assert Array.product(arrC, arrD) == [> (1, 2)]
assert Array.product([>], [>]) == [>]
Expand Down
20 changes: 10 additions & 10 deletions compiler/test/stdlib/bigint.test.gr
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ assert toString(-15t) == "-15"
assert toString(
120892581961462917470617512089258196146291747061751208925819614629174706175t
) ==
"120892581961462917470617512089258196146291747061751208925819614629174706175"
"120892581961462917470617512089258196146291747061751208925819614629174706175"
assert toString(
-120892581961462917470617512089258196146291747061751208925819614629174706175t
) ==
"-120892581961462917470617512089258196146291747061751208925819614629174706175"
"-120892581961462917470617512089258196146291747061751208925819614629174706175"

assert toString(15t) == "15"
assert toString(-15t) == "-15"
Expand Down Expand Up @@ -136,16 +136,16 @@ assert ctz(5t) == 0L
assert ctz(-8t) == 3L
assert ctz(8t) == 3L
assert 203L ==
ctz(
0x54416a78947fb6c84aab2a0e4673a8cacdc0229b747283f43865f20994eca718f5b3dc0a4bc4558a6e97a428d90b863ac2659c5307fd61c2da582383d58a313d178cc8956855b34b3ad978b1cfa5d8e2c293fbfdfc4a8c9e4007bbfba1ee54077c6e88748cce1f514d3135d1d1729f6a80c40499d773b9e9f00ae73013d8fa664921ca6d029cd2886054d12412ab70b0bafed88045742094f684eebe67800000000000000000000000000000000000000000000000000t
)
ctz(
0x54416a78947fb6c84aab2a0e4673a8cacdc0229b747283f43865f20994eca718f5b3dc0a4bc4558a6e97a428d90b863ac2659c5307fd61c2da582383d58a313d178cc8956855b34b3ad978b1cfa5d8e2c293fbfdfc4a8c9e4007bbfba1ee54077c6e88748cce1f514d3135d1d1729f6a80c40499d773b9e9f00ae73013d8fa664921ca6d029cd2886054d12412ab70b0bafed88045742094f684eebe67800000000000000000000000000000000000000000000000000t
)

assert popcnt(8t) == Some(1L)
assert popcnt(35242t) == Some(7L)
assert popcnt(
105283664383537698179048568639738324582407620177142255774517468458859582470144t
) ==
Some(88L)
Some(88L)
assert popcnt(-1t) == None

let equals = (==)
Expand All @@ -157,16 +157,16 @@ assert 0t + 0xFFFFFFFFFFFFFFFFt == 0xFFFFFFFFFFFFFFFFt
assert 0xFFFFFFFFFFFFFFFFt + 0xFFFFFFFFFFFFFFFFt == 0x1fffffffffffffffet
// mul
assert 0xFFFFFFFFFFFFFFFFt * 0xFFFFFFFFFFFFFFFFt ==
0xfffffffffffffffe0000000000000001t
0xfffffffffffffffe0000000000000001t
assert 0xFFFFFFFFFFFFFFFFt * 0xFFFFFFFFFFFFFFFFt * 0xAAAt ==
0xaa9ffffffffffffeaac0000000000000aaat
0xaa9ffffffffffffeaac0000000000000aaat

// bitwise
assert shl(24t, 1l) == 48t
assert shl(24t, 2l) == 96t
assert shl(0xf0f0f0f0f0f0f0f0t, 14l) == 0x3c3c3c3c3c3c3c3c0000t
assert shl(0xf0f0f0f0f0f0f0f0t, 127l) ==
0x787878787878787800000000000000000000000000000000t
0x787878787878787800000000000000000000000000000000t

assert eq(shr(24t, 0l), 24t)
assert eq(shr(24t, 1l), 12t)
Expand Down Expand Up @@ -194,7 +194,7 @@ assert (-341237t ^ -60597t) == 383040t

assert (0xe8c4713afbb863c16a214fb9fbee69d0610d131f2c55328b641d61eff9037848t &
0x16A214FB9FBEE69D061E8C4713AFBB863C16At) ==
9059764183839950850606742203634691510321224t
9059764183839950850606742203634691510321224t

assert eq(lnot(5t), -6t)
assert eq(
Expand Down
6 changes: 3 additions & 3 deletions compiler/test/stdlib/hash.test.gr
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ assert uniq(List.map(n => Hash.hash(n), strings))

assert Hash.hash("") == Hash.hash("")
assert Hash.hash("grain > ore > wool > lumber > brick") ==
Hash.hash("grain > ore > wool > lumber > brick")
Hash.hash("grain > ore > wool > lumber > brick")

let chars = String.explode(
"!@#$%^&*()1234567890-qwertyuiop🌾💯🔥😈😤💪🏼"
Expand Down Expand Up @@ -162,9 +162,9 @@ let recs = [
assert uniq(List.map(n => Hash.hash(n), recs))

assert Hash.hash({ num: 12, var: E("wasm"), str: "gr" }) ==
Hash.hash({ num: 12, var: E("wasm"), str: "gr" })
Hash.hash({ num: 12, var: E("wasm"), str: "gr" })
assert Hash.hash({ num: 0, var: A, str: "" }) ==
Hash.hash({ num: 0, var: A, str: "" })
Hash.hash({ num: 0, var: A, str: "" })

assert Hash.hash(Bytes.fromString("foo")) == Hash.hash(Bytes.fromString("foo"))
assert Hash.hash(Bytes.fromString("foo")) != Hash.hash(Bytes.fromString("bar"))
6 changes: 3 additions & 3 deletions compiler/test/stdlib/immutablepriorityqueue.test.gr
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,11 @@ assert ImmutablePriorityQueue.drain(maxPqWithAll) == List.reverse(sortedList)
assert ImmutablePriorityQueue.drain(
ImmutablePriorityQueue.fromList(Array.toList(lotsOfVals), (a, b) => a - b)
) ==
sortedList
sortedList
assert ImmutablePriorityQueue.drain(
ImmutablePriorityQueue.fromList(Array.toList(lotsOfVals), (a, b) => b - a)
) ==
List.reverse(sortedList)
List.reverse(sortedList)

assert ImmutablePriorityQueue.fromList(Array.toList(lotsOfVals), compare) ==
ImmutablePriorityQueue.fromArray(lotsOfVals, compare)
ImmutablePriorityQueue.fromArray(lotsOfVals, compare)
10 changes: 5 additions & 5 deletions compiler/test/stdlib/number.test.gr
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ assert Number.max(1/2, 1/4) == 1/2
assert Number.max(0.5, 0.25) == 0.5
assert Number.max(BI.toNumber(1234t), BI.toNumber(12t)) == BI.toNumber(1234t)
assert Number.max(355894508425808343204914141312, 6) ==
355894508425808343204914141312
355894508425808343204914141312
assert Number.max(Number.infinity, 10) == Number.infinity
assert Number.max(Number.nan, 10) == 10
assert Number.max(Number.nan, Number.infinity) == Number.infinity
Expand Down Expand Up @@ -253,15 +253,15 @@ assert Number.parseInt("-42", 10) == Ok(-42)
assert Number.parseInt("-042", 10) == Ok(-42)
assert Number.parseInt("-_0___42___", 10) == Ok(-42)
assert Number.parseInt("1073741823", 10) ==
Ok(1073741823) // grain simple number max
Ok(1073741823) // grain simple number max
assert Number.parseInt("-1073741824", 10) ==
Ok(-1073741824) // grain simple number min
Ok(-1073741824) // grain simple number min
assert Number.parseInt("2147483647", 10) == Ok(2147483647) // i32 max
assert Number.parseInt("-2147483648", 10) == Ok(-2147483648) // i32 min
assert Number.parseInt("9223372036854775807", 10) ==
Ok(9223372036854775807) // i64 max
Ok(9223372036854775807) // i64 max
assert Number.parseInt("-9223372036854775808", 10) ==
Ok(-9223372036854775808) // i64 min
Ok(-9223372036854775808) // i64 min
assert Number.parseInt("0xabcdef", 10) == Ok(0xabcdef)
assert Number.parseInt("0Xabcdef", 10) == Ok(0xabcdef)
assert Number.parseInt("abcdef", 16) == Ok(0xabcdef)
Expand Down
22 changes: 11 additions & 11 deletions compiler/test/stdlib/option.test.gr
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ assert Option.unwrapWithDefault("🐑", None) == "🐑"

assert Option.map(x => x * 2, Some(2)) == Some(4)
assert Option.map(x => String.concat("hello ", x), Some("🌾")) ==
Some("hello 🌾")
Some("hello 🌾")
assert Option.map(x => fail "Shouldn't be called", None) == None

// Option.mapWithDefault
Expand All @@ -56,39 +56,39 @@ assert Option.mapWithDefault(x => String.concat("hello ", x),
"🐑",
Some("🌾")
) ==
"hello 🌾"
"hello 🌾"
assert Option.mapWithDefault(x => x * 2, 1, None) == 1
assert Option.mapWithDefault(x => String.concat("hello ", x), "🐑", None) ==
"🐑"
"🐑"

// Option.mapWithDefaultFn

assert Option.mapWithDefaultFn(x => x * 2,
() => fail "Shouldn't be called",
Some(2)
) ==
4
4
assert Option.mapWithDefaultFn(x => String.concat("hello ", x),
() => fail "Shouldn't be called",
Some("🌾")
) ==
"hello 🌾"
"hello 🌾"
assert Option.mapWithDefaultFn(x => fail "Shouldn't be called",
() => 1,
None
) ==
1
1
assert Option.mapWithDefaultFn(x => fail "Shouldn't be called",
() => "🐑",
None
) ==
"🐑"
"🐑"

// Option.flatMap

assert Option.flatMap(x => Some(x * 2), Some(2)) == Some(4)
assert Option.flatMap(x => Some(String.concat("hello ", x)), Some("🌾")) ==
Some("hello 🌾")
Some("hello 🌾")
assert Option.flatMap(x => None, Some("🌾")) == None
assert Option.flatMap(x => fail "Shouldn't be called", None) == None

Expand All @@ -109,14 +109,14 @@ assert Option.zip(None, Some("🌾")) == None

assert Option.zipWith((a, b) => a + b, Some(1), Some(2)) == Some(3)
assert Option.zipWith((a, b) => (a, b), Some(1), Some("🌾")) ==
Some((1, "🌾"))
Some((1, "🌾"))
assert Option.zipWith((a, b) => fail "Shouldn't be called", Some(1), None) ==
None
None
assert Option.zipWith((a, b) => fail "Shouldn't be called",
None,
Some("🌾")
) ==
None
None

// Option.flatten

Expand Down
6 changes: 3 additions & 3 deletions compiler/test/stdlib/priorityqueue.test.gr
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,11 @@ let sortedList = Array.toList(sortedVals)
assert PriorityQueue.drain(
PriorityQueue.fromList(Array.toList(lotsOfVals), (a, b) => a - b)
) ==
sortedList
sortedList
assert PriorityQueue.drain(
PriorityQueue.fromList(Array.toList(lotsOfVals), (a, b) => b - a)
) ==
List.reverse(sortedList)
List.reverse(sortedList)

assert PriorityQueue.fromList(Array.toList(lotsOfVals), compare) ==
PriorityQueue.fromArray(lotsOfVals, compare)
PriorityQueue.fromArray(lotsOfVals, compare)
2 changes: 1 addition & 1 deletion compiler/test/stdlib/range.test.gr
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ Range.forEach(idx => {

assert Range.map(toString, inclusiveAscendingRange) == ["1", "2", "3", "4", "5"]
assert Range.map(toString, inclusiveDescendingRange) ==
["5", "4", "3", "2", "1"]
["5", "4", "3", "2", "1"]
assert Range.map(toString, inclusiveSameRange) == ["1"]
assert Range.map(toString, exclusiveAscendingRange) == ["1", "2", "3", "4"]
assert Range.map(toString, exclusiveDescendingRange) == ["5", "4", "3", "2"]
Expand Down
Loading

0 comments on commit d705849

Please sign in to comment.