From e8815b614e4a02f9270245f2d5c557999b58239d Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 9 Jan 2019 19:14:21 +0300 Subject: nicer trailing comma handling in types --- 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 +- 4 files changed, 16 insertions(+), 11 deletions(-) (limited to 'crates') diff --git a/crates/ra_hir/Cargo.toml b/crates/ra_hir/Cargo.toml index 5a8fdbfc6..0135e0e16 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 3b2bfd67a..eb7764f65 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 b6807fb2a..b8593cfd1 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 0fb9dc097..8a515ac5e 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 -- cgit v1.2.3