From e8815b614e4a02f9270245f2d5c557999b58239d Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 9 Jan 2019 19:14:21 +0300 Subject: [PATCH] nicer trailing comma handling in types --- Cargo.lock | 9 ++++---- crates/ra_hir/Cargo.toml | 1 + crates/ra_hir/src/ty.rs | 22 +++++++++++-------- crates/ra_hir/src/ty/tests/data/backwards.txt | 2 +- crates/ra_hir/src/ty/tests/data/binary_op.txt | 2 +- 5 files changed, 21 insertions(+), 15 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 43458f60e5..b3172dc283 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -409,7 +409,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "join_to_string" -version = "0.1.1" +version = "0.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] @@ -646,7 +646,7 @@ version = "0.1.0" dependencies = [ "clap 2.32.0 (registry+https://github.com/rust-lang/crates.io-index)", "failure 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", - "join_to_string 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", + "join_to_string 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", "ra_ide_api_light 0.1.0", "ra_syntax 0.1.0", "tools 0.1.0", @@ -672,6 +672,7 @@ dependencies = [ "arrayvec 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)", "ena 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", "flexi_logger 0.10.4 (registry+https://github.com/rust-lang/crates.io-index)", + "join_to_string 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "parking_lot 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)", "ra_arena 0.1.0", @@ -709,7 +710,7 @@ name = "ra_ide_api_light" version = "0.1.0" dependencies = [ "itertools 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", - "join_to_string 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", + "join_to_string 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", "proptest 0.8.7 (registry+https://github.com/rust-lang/crates.io-index)", "ra_syntax 0.1.0", "ra_text_edit 0.1.0", @@ -1531,7 +1532,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum indexmap 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7e81a7c05f79578dbc15793d8b619db9ba32b4577003ef3af1a91c416798c58d" "checksum itertools 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5b8467d9c1cebe26feb08c640139247fac215782d35371ade9a2136ed6085358" "checksum itoa 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "1306f3464951f30e30d12373d31c79fbd52d236e5e896fd92f96ec7babbbe60b" -"checksum join_to_string 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7bddc885f3fd69dd4b5d747c2efe6dd2c36d795ea9938281ed50910e32c95e31" +"checksum join_to_string 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "4dc7a5290e8c2606ce2be49f456d50f69173cb96d1541e4f66e34ac8b331a98f" "checksum languageserver-types 0.53.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7c01845f71b8b3b3557a8179af4434a4b2570829da12371f05272d28183a06ce" "checksum lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a374c89b9db55895453a74c1e38861d9deec0b01b405a82516e9d5de4820dea1" "checksum libc 0.2.46 (registry+https://github.com/rust-lang/crates.io-index)" = "023a4cd09b2ff695f9734c1934145a315594b7986398496841c7031a5a1bbdbd" diff --git a/crates/ra_hir/Cargo.toml b/crates/ra_hir/Cargo.toml index 5a8fdbfc68..0135e0e166 100644 --- a/crates/ra_hir/Cargo.toml +++ b/crates/ra_hir/Cargo.toml @@ -12,6 +12,7 @@ salsa = "0.9.1" rustc-hash = "1.0" parking_lot = "0.7.0" ena = "0.11" +join_to_string = "0.1.3" ra_syntax = { path = "../ra_syntax" } ra_arena = { path = "../ra_arena" } ra_db = { path = "../ra_db" } diff --git a/crates/ra_hir/src/ty.rs b/crates/ra_hir/src/ty.rs index 3b2bfd67ac..eb7764f652 100644 --- a/crates/ra_hir/src/ty.rs +++ b/crates/ra_hir/src/ty.rs @@ -25,6 +25,7 @@ use std::{fmt, mem}; use log; use ena::unify::{InPlaceUnificationTable, UnifyKey, UnifyValue, NoError}; use ra_arena::map::ArenaMap; +use join_to_string::join; use ra_db::Cancelable; @@ -396,18 +397,21 @@ impl fmt::Display for Ty { Ty::Ref(t, m) => write!(f, "&{}{}", m.as_keyword_for_ref(), t), Ty::Never => write!(f, "!"), Ty::Tuple(ts) => { - write!(f, "(")?; - for t in ts.iter() { - write!(f, "{},", t)?; + if ts.len() == 1 { + write!(f, "({},)", ts[0]) + } else { + join(ts.iter()) + .surround_with("(", ")") + .separator(", ") + .to_fmt(f) } - write!(f, ")") } Ty::FnPtr(sig) => { - write!(f, "fn(")?; - for t in &sig.input { - write!(f, "{},", t)?; - } - write!(f, ") -> {}", sig.output) + join(sig.input.iter()) + .surround_with("fn(", ")") + .separator(", ") + .to_fmt(f)?; + write!(f, " -> {}", sig.output) } Ty::Adt { name, .. } => write!(f, "{}", name), Ty::Unknown => write!(f, "[unknown]"), diff --git a/crates/ra_hir/src/ty/tests/data/backwards.txt b/crates/ra_hir/src/ty/tests/data/backwards.txt index b6807fb2a9..b8593cfd16 100644 --- a/crates/ra_hir/src/ty/tests/data/backwards.txt +++ b/crates/ra_hir/src/ty/tests/data/backwards.txt @@ -4,7 +4,7 @@ [88; 89) 'a': u32 [92; 108) 'unknow...nction': [unknown] [92; 110) 'unknow...tion()': u32 -[116; 125) 'takes_u32': fn(u32,) -> () +[116; 125) 'takes_u32': fn(u32) -> () [116; 128) 'takes_u32(a)': () [126; 127) 'a': u32 [138; 139) 'b': i32 diff --git a/crates/ra_hir/src/ty/tests/data/binary_op.txt b/crates/ra_hir/src/ty/tests/data/binary_op.txt index 0fb9dc0976..8a515ac5e4 100644 --- a/crates/ra_hir/src/ty/tests/data/binary_op.txt +++ b/crates/ra_hir/src/ty/tests/data/binary_op.txt @@ -22,7 +22,7 @@ [166; 188) 'minus_...ONST_2': bool [181; 188) 'CONST_2': isize [198; 199) 'c': i32 -[202; 203) 'f': fn(bool,) -> i32 +[202; 203) 'f': fn(bool) -> i32 [202; 211) 'f(z || y)': i32 [202; 215) 'f(z || y) + 5': i32 [204; 205) 'z': bool