Skip to content

Commit

Permalink
feat: Add a trailing comman on records that take multiple lines
Browse files Browse the repository at this point in the history
  • Loading branch information
Marwes committed Oct 13, 2017
1 parent 8dd9322 commit 8c3aa95
Show file tree
Hide file tree
Showing 22 changed files with 163 additions and 144 deletions.
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ gluon_base = { path = "base", version = "0.6.1" } # GLUON
gluon_check = { path = "check", version = "0.6.1" } # GLUON
gluon_parser = { path = "parser", version = "0.6.1" } # GLUON
gluon_vm = { path = "vm", version = "0.6.1" } # GLUON
gluon_format = { path = "format", version = "0.6.0" } #GLUON

log = "0.3.6"
quick-error = "1.0.0"
Expand Down Expand Up @@ -68,7 +67,7 @@ gluon_completion = { path = "completion", version = "0.6.1" } # GLUON
default = ["regex"]
serialization = ["serde", "serde_state", "serde_derive_state", "gluon_vm/serialization"]

test = ["serialization", "gluon_vm/test", "gluon_check/test", "gluon_parser/test", "gluon_format/test"]
test = ["serialization", "gluon_vm/test", "gluon_check/test", "gluon_parser/test"]
nightly = ["compiletest_rs"]


Expand Down
4 changes: 2 additions & 2 deletions format/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ repository = "https://github.com/gluon-lang/gluon"
documentation = "https://docs.rs/gluon"

[dependencies]
env_logger = { version = "0.3.4", optional = true }
log = "0.3.6"
pretty = "0.3.2"
itertools = "0.5.6"
Expand All @@ -20,8 +19,9 @@ gluon_base = { path = "../base", version = "0.6.1" } # GLUON
gluon_parser = { path = "../parser", version = "0.6.1" } # GLUON

[dev-dependencies]
env_logger = "0.3.4"
difference = "1.0"
pretty_assertions = "0.3.0"

[features]
test = ["env_logger"]
test = []
26 changes: 20 additions & 6 deletions format/src/pretty_print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -416,13 +416,27 @@ impl<'a: 'e, 'e> Printer<'a, 'e> {
}),
|spanned| spanned.value,
))
.append(
if (!exprs.is_empty() || !types.is_empty()) && line.1 == arena.newline().1 {
arena.text(",")
} else {
arena.nil()
},
)
.append(match *base {
Some(ref base) => chain![arena;
self.space_after(last_field_end),
"..",
self.space_before(base.span.start),
self.pretty_expr_(base.span.start, base)
],
Some(ref base) => {
let comments = self.comments_after(last_field_end);
chain![arena;
if comments.1 == arena.nil().1 {
line.clone()
} else {
comments
},
"..",
self.space_before(base.span.start),
self.pretty_expr_(base.span.start, base)
]
}
None => arena.nil(),
})
.nest(INDENT)
Expand Down
14 changes: 12 additions & 2 deletions format/tests/pretty_print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@ fn test_format(name: &str) {
let out_str = format_expr(&contents).unwrap();

if contents != out_str {
let out_path = Path::new(&env::var("OUT_DIR").unwrap()).join(name.file_name().unwrap());
let args: Vec<_> = env::args().collect();
let out_path = Path::new(&args[0][..])
.parent()
.and_then(|p| p.parent())
.expect("folder")
.join(name.file_name().unwrap());
File::create(out_path)
.unwrap()
.write_all(out_str.as_bytes())
Expand Down Expand Up @@ -102,6 +107,11 @@ fn writer() {
test_format("std/writer.glu");
}

#[test]
fn parser() {
test_format("std/parser.glu");
}

#[test]
fn repl() {
test_format("repl/src/repl.glu");
Expand Down Expand Up @@ -152,7 +162,7 @@ fn preserve_whitespace_in_record() {
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaax = 1,
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbby = 2
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbby = 2,
}
"#;
assert_diff!(&format_expr(expr).unwrap(), expr, " ", 0);
Expand Down
1 change: 1 addition & 0 deletions repl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ log = "0.3.6"
env_logger = { version = "0.3.4", optional = true }
lazy_static = "0.2.0"
rustyline = "1.0.0"
walkdir = "1"

[dev-dependencies]
pretty_assertions = "0.3.4"
Expand Down
10 changes: 5 additions & 5 deletions repl/src/repl.glu
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,19 @@ let commands : Map String Cmd =
let cmds =
singleton "q" { info = "Quit the REPL", action = \_ -> wrap False } <> singleton "t" {
info = "Prints the type with an expression",
action = \arg -> repl_prim.type_of_expr arg >>= print_result *> wrap True
action = \arg -> repl_prim.type_of_expr arg >>= print_result *> wrap True,
}
<> singleton "i" {
info = "Prints information about the given name",
action = \arg -> repl_prim.find_info arg >>= print_result *> wrap True
action = \arg -> repl_prim.find_info arg >>= print_result *> wrap True,
}
<> singleton "k" {
info = "Prints the kind with the given type",
action = \arg -> repl_prim.find_kind arg >>= print_result *> wrap True
action = \arg -> repl_prim.find_kind arg >>= print_result *> wrap True,
}
<> singleton "l" {
info = "Loads the file at \'folder/module.ext\' and stores it at \'module\'",
action = \arg -> load_file arg >>= io.println *> wrap True
action = \arg -> load_file arg >>= io.println *> wrap True,
}
<> singleton "h" {
info = "Print this help",
Expand All @@ -65,7 +65,7 @@ let commands : Map String Cmd =
io.println (" :" ++ cmd.key ++ " " ++ cmd.value.info)

print_header *> for (load commands) print_cmd
*> wrap True
*> wrap True,
}
commands <- cmds
load commands
Expand Down
1 change: 1 addition & 0 deletions repl/src/repl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use futures::Future;

use self::rustyline::error::ReadlineError;


use base::ast::{Expr, Pattern, SpannedPattern, Typed};
use base::error::InFile;
use base::kind::Kind;
Expand Down
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ extern crate serde_state as serde;

pub extern crate gluon_base as base;
pub extern crate gluon_check as check;
pub extern crate gluon_format as format;
pub extern crate gluon_parser as parser;
#[macro_use]
pub extern crate gluon_vm as vm;
Expand Down
10 changes: 5 additions & 5 deletions std/bool.glu
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ let conjunctive =

let monoid : Monoid Bool = {
semigroup = semigroup,
empty = True
empty = True,
}

{ semigroup, monoid }
Expand All @@ -28,7 +28,7 @@ let disjunctive =

let monoid : Monoid Bool = {
semigroup = semigroup,
empty = False
empty = False,
}

{ semigroup, monoid }
Expand All @@ -38,12 +38,12 @@ let exclusive =

let monoid : Monoid Bool = {
semigroup = semigroup,
empty = False
empty = False,
}

let group : Group Bool = {
monoid = monoid,
inverse = id
inverse = id,
}

{ semigroup, monoid, group }
Expand All @@ -69,5 +69,5 @@ let show : Show Bool = {
exclusive,
eq,
ord,
show
show,
}
2 changes: 1 addition & 1 deletion std/char.glu
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ let eq : Eq Char = { (==) = \l r -> l #Char== r }

let ord : Ord Char = {
eq = eq,
compare = \l r -> if l #Char< r then LT else if l #Char== r then EQ else GT
compare = \l r -> if l #Char< r then LT else if l #Char== r then EQ else GT,
}

let show : Show Char = { show = prim.show_char }
Expand Down
12 changes: 6 additions & 6 deletions std/float.glu
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ let additive =

let monoid : Monoid Float = {
semigroup = semigroup,
empty = 0.0
empty = 0.0,
}

let group : Group Float = {
monoid = monoid,
inverse = \x -> 0.0 #Float- x
inverse = \x -> 0.0 #Float- x,
}

{ semigroup, monoid, group }
Expand All @@ -22,12 +22,12 @@ let multiplicative =

let monoid : Monoid Float = {
semigroup = semigroup,
empty = 1.0
empty = 1.0,
}

let group : Group Float = {
monoid = monoid,
inverse = \x -> 1.0 #Float/ x
inverse = \x -> 1.0 #Float/ x,
}

{ semigroup, monoid, group }
Expand All @@ -38,7 +38,7 @@ let eq : Eq Float = {

let ord : Ord Float = {
eq = eq,
compare = \l r -> if l #Float< r then LT else if l #Float== r then EQ else GT
compare = \l r -> if l #Float< r then LT else if l #Float== r then EQ else GT,
}

let num : Num Float = {
Expand All @@ -47,7 +47,7 @@ let num : Num Float = {
(-) = \l r -> l #Float- r,
(*) = multiplicative.semigroup.append,
(/) = \l r -> l #Float/ r,
negate = additive.group.inverse
negate = additive.group.inverse,
}

let show : Show Float = {
Expand Down
10 changes: 5 additions & 5 deletions std/int.glu
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ let additive =

let monoid : Monoid Int = {
semigroup = semigroup,
empty = 0
empty = 0,
}

let group : Group Int = {
monoid = monoid,
inverse = \x -> 0 #Int- x
inverse = \x -> 0 #Int- x,
}

{ semigroup, monoid, group }
Expand All @@ -26,7 +26,7 @@ let multiplicative =

let monoid : Monoid Int = {
semigroup = semigroup,
empty = 1
empty = 1,
}

{ semigroup, monoid }
Expand All @@ -37,7 +37,7 @@ let eq = {

let ord = {
eq = eq,
compare = \l r -> if l #Int< r then LT else if l #Int== r then EQ else GT
compare = \l r -> if l #Int< r then LT else if l #Int== r then EQ else GT,
}

let num = {
Expand All @@ -46,7 +46,7 @@ let num = {
(-) = \l r -> l #Int- r,
(*) = multiplicative.semigroup.append,
(/) = \l r -> l #Int/ r,
negate = additive.group.inverse
negate = additive.group.inverse,
}

let show : Show Int = {
Expand Down
2 changes: 1 addition & 1 deletion std/io.glu
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ let applicative : Applicative IO =

let monad : Monad IO = {
applicative = applicative,
flat_map = io_flat_map
flat_map = io_flat_map,
}

{
Expand Down
10 changes: 5 additions & 5 deletions std/list.glu
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ let semigroup : Semigroup (List a) =

let monoid : Monoid (List a) = {
semigroup = semigroup,
empty = Nil
empty = Nil,
}

let eq a : Eq a -> Eq (List a) =
Expand Down Expand Up @@ -81,7 +81,7 @@ let some alternative x : Alternative f -> f a -> f (List a) =
let alternative : Alternative List = {
applicative = applicative,
or = semigroup.append,
empty = Nil
empty = Nil,
}

let monad : Monad List =
Expand All @@ -107,7 +107,7 @@ let show : Show a -> Show (List a) = \d ->
| Nil -> d.show y
| Nil -> ""

"[" ++ show_elems xs ++ "]"
"[" ++ show_elems xs ++ "]",
}

let foldable : Foldable List =
Expand All @@ -129,7 +129,7 @@ let traversable : Traversable List = {
traverse = \app f ->
foldable.foldr
(\a b -> app.apply (app.functor.map Cons (f a)) b)
(app.wrap Nil)
(app.wrap Nil),
}

{
Expand All @@ -146,5 +146,5 @@ let traversable : Traversable List = {
monad,
show,
foldable,
traversable
traversable,
}
2 changes: 1 addition & 1 deletion std/map.glu
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ and make ord : Ord k -> _ =
traverse_with_key,
to_list,
keys,
values
values,
}

{ Map, make }
Loading

0 comments on commit 8c3aa95

Please sign in to comment.