Skip to content

Commit

Permalink
format: Remove special case for constants (#1213)
Browse files Browse the repository at this point in the history
* format: Remove special case for constants
  • Loading branch information
V-FEXrt authored Apr 19, 2023
1 parent 69fcfef commit b8569d4
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 72 deletions.
3 changes: 3 additions & 0 deletions tests/wake-format/basic/basic.wake
Original file line number Diff line number Diff line change
Expand Up @@ -1116,6 +1116,9 @@ def tarball Unit =
def x = y
def srcs = allSources # a comment
def x = y
def srcs = allSources # a comment
def x = y

def srcs = allSources # a comment
def x = y
x
Expand Down
110 changes: 65 additions & 45 deletions tests/wake-format/basic/stdout
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
# -------------

# comment
def five = 5
def five =
5

# ordinary comment

Expand Down Expand Up @@ -86,7 +87,8 @@ def name Unit =

another

global export def glob1 = 34
global export def glob1 =
34

# ordinary comment
def name Unit =
Expand Down Expand Up @@ -764,11 +766,14 @@ global export data Let =
def x + y =
add x y

export def (argument: a) | (pipeFn: a => b): b = pipeFn argument
export def (argument: a) | (pipeFn: a => b): b =
pipeFn argument

def tool (dir: String) (addHeaders: String) (addHeaders: String) (addHeaders: String) (addHeaders: String) (addHeaders: String) (addHeaders: String) (addHeaders: String) (addHeaders: String) (addHeaders: String) (addHeaders: String) = ""
def tool (dir: String) (addHeaders: String) (addHeaders: String) (addHeaders: String) (addHeaders: String) (addHeaders: String) (addHeaders: String) (addHeaders: String) (addHeaders: String) (addHeaders: String) (addHeaders: String) =
""

def tool (dir: String) (addHeaders: String) (addHeaders: String) (addHeaders: String) (addHeaders: String) (addHeaders: String) (addHeaders: String) (addHeaders: String) (addHeaders: String) (addHeaders: String) (addHeaders: String) = ""
def tool (dir: String) (addHeaders: String) (addHeaders: String) (addHeaders: String) (addHeaders: String) (addHeaders: String) (addHeaders: String) (addHeaders: String) (addHeaders: String) (addHeaders: String) (addHeaders: String) =
""

def Pair _ _: Pair Integer String =
Pair 44 "as"
Expand Down Expand Up @@ -952,7 +957,8 @@ def name Unit =

# floating 7

def a b c = 5
def a b c =
5

def a =
if false then
Expand Down Expand Up @@ -992,13 +998,17 @@ def x =
def x =
(\a \b \x prim "p") x y

def a = 5
def a =
5

def b = 6
def b =
6

def c = "asdf"
def c =
"asdf"

def d = identifier
def d =
identifier

def longFunc param: Result (List Path) Error =
match _
Expand Down Expand Up @@ -1083,7 +1093,8 @@ def anyArray =
def a =
rec Nil (if indent > 0 then Some "" else None) lhs

def a = Some ""
def a =
Some ""

def a =
def x = if a then b, else !b
Expand All @@ -1092,20 +1103,23 @@ def a =

x

def x = "%
fun day.
happy day.
%"
def x =
"%
fun day.
happy day.
%"

def x = "%
fun day.
happy day.
%"
def x =
"%
fun day.
happy day.
%"

def x = "%
fun day.
happy day.
%"
def x =
"%
fun day.
happy day.
%"

def someFunc a b =
def s =
Expand Down Expand Up @@ -1157,28 +1171,32 @@ def someFunc a b =

s

def x = "%
this is the lowest / prefixed with <space><space>
This line is prefixed with <space><space><tab>
This line is prefixed with <tab><space><space>
This line is prefixed with <tab>
this line is prefixed with <space><space><space><space>
%"

def x = """
fun day.
happy day.
"""

def x = """
fun day.
happy day.
"""

def x = """
fun day.
happy day.
"""
def x =
"%
this is the lowest / prefixed with <space><space>
This line is prefixed with <space><space><tab>
This line is prefixed with <tab><space><space>
This line is prefixed with <tab>
this line is prefixed with <space><space><space><space>
%"

def x =
"""
fun day.
happy day.
"""

def x =
"""
fun day.
happy day.
"""

def x =
"""
fun day.
happy day.
"""

def someFunc a b =
def s =
Expand Down Expand Up @@ -1298,6 +1316,8 @@ def tarball Unit =
def x = y
def srcs = allSources # a comment
def x = y
def srcs = allSources # a comment
def x = y

x

Expand Down
29 changes: 2 additions & 27 deletions tools/wake-format/emitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -418,30 +418,6 @@ static inline bool is_simple_apply(wcl::doc_builder& builder, ctx_t ctx, CSTElem
is_simple_literal(builder, ctx, parts[1], traits);
}

// determines if the rest of the node
// - only has one sibling node
// - and that node is a "simple" thing as defined by is_simple_apply
static inline bool is_single_apply(wcl::doc_builder& builder, ctx_t ctx, CSTElement& node,
const token_traits_map_t& traits) {
CSTElement copy = node;
if (!copy.isNode()) {
copy.nextSiblingNode();
}

size_t node_count = 0;
while (!copy.empty()) {
node_count++;

if (!is_simple_apply(builder, ctx, node, traits)) {
return false;
}

copy.nextSiblingNode();
}

return node_count == 1;
}

Emitter::~Emitter() { MEMO_RESET(); }

auto Emitter::rhs_fmt(bool always_newline) {
Expand All @@ -466,9 +442,8 @@ auto Emitter::rhs_fmt(bool always_newline) {
const token_traits_map_t& traits) {
return count_leading_newlines(token_traits, node) > 0;
}, full_fmt)
// Always newline when requested unless the thing to be formatted is a "single literal".
// Used for top-level defs and top-level "constant" defs
.pred(ConstPredicate(always_newline), fmt().fmt_if_else(is_single_apply, flat_fmt, full_fmt))
// Always newline when requested. Used for top-level defs.
.pred(ConstPredicate(always_newline), full_fmt)

// if our hand hand hasn't yet been forced then decide based on how well RHS fits
.pred(requires_fits_all, fmt().fmt_if_fits_all(flat_fmt, full_fmt))
Expand Down

0 comments on commit b8569d4

Please sign in to comment.