Skip to content

Commit fc307ff

Browse files
committed
Format exitential type
1 parent 6eb0bf2 commit fc307ff

File tree

2 files changed

+107
-34
lines changed

2 files changed

+107
-34
lines changed

src/items.rs

Lines changed: 84 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1400,36 +1400,27 @@ fn format_tuple_struct(
14001400
Some(result)
14011401
}
14021402

1403-
pub fn rewrite_type_alias(
1403+
fn rewrite_type_prefix(
14041404
context: &RewriteContext,
14051405
indent: Indent,
1406+
prefix: &str,
14061407
ident: ast::Ident,
1407-
ty: &ast::Ty,
14081408
generics: &ast::Generics,
1409-
vis: &ast::Visibility,
1410-
span: Span,
14111409
) -> Option<String> {
14121410
let mut result = String::with_capacity(128);
1413-
1414-
result.push_str(&format_visibility(context, vis));
1415-
result.push_str("type ");
1411+
result.push_str(prefix);
1412+
let ident_str = rewrite_ident(context, ident);
14161413

14171414
// 2 = `= `
1418-
let g_shape = Shape::indented(indent, context.config)
1419-
.offset_left(result.len())?
1420-
.sub_width(2)?;
1421-
let g_span = mk_sp(
1422-
context.snippet_provider.span_after(span, "type"),
1423-
ty.span.lo(),
1424-
);
1425-
let generics_str = rewrite_generics(
1426-
context,
1427-
rewrite_ident(context, ident),
1428-
generics,
1429-
g_shape,
1430-
g_span,
1431-
)?;
1432-
result.push_str(&generics_str);
1415+
if generics.params.is_empty() {
1416+
result.push_str(ident_str)
1417+
} else {
1418+
let g_shape = Shape::indented(indent, context.config)
1419+
.offset_left(result.len())?
1420+
.sub_width(2)?;
1421+
let generics_str = rewrite_generics(context, ident_str, generics, g_shape, generics.span)?;
1422+
result.push_str(&generics_str);
1423+
}
14331424

14341425
let where_budget = context.budget(last_line_width(&result));
14351426
let option = WhereClauseOption::snuggled(&result);
@@ -1440,24 +1431,76 @@ pub fn rewrite_type_alias(
14401431
Shape::legacy(where_budget, indent),
14411432
Density::Vertical,
14421433
"=",
1443-
Some(span.hi()),
1434+
None,
14441435
generics.span.hi(),
14451436
option,
14461437
false,
14471438
)?;
14481439
result.push_str(&where_clause_str);
1449-
if where_clause_str.is_empty() {
1450-
result.push_str(" =");
1440+
1441+
Some(result)
1442+
}
1443+
1444+
fn rewrite_type_item<R: Rewrite>(
1445+
context: &RewriteContext,
1446+
indent: Indent,
1447+
prefix: &str,
1448+
suffix: &str,
1449+
ident: ast::Ident,
1450+
rhs: &R,
1451+
generics: &ast::Generics,
1452+
vis: &ast::Visibility,
1453+
) -> Option<String> {
1454+
let mut result = String::with_capacity(128);
1455+
result.push_str(&rewrite_type_prefix(
1456+
context,
1457+
indent,
1458+
&format!("{}{} ", format_visibility(context, vis), prefix),
1459+
ident,
1460+
generics,
1461+
)?);
1462+
1463+
if generics.where_clause.predicates.is_empty() {
1464+
result.push_str(suffix);
14511465
} else {
1452-
result.push_str(&format!(
1453-
"{}=",
1454-
indent.to_string_with_newline(context.config)
1455-
));
1466+
result.push_str(&indent.to_string_with_newline(context.config));
1467+
result.push_str(suffix.trim_left());
14561468
}
14571469

14581470
// 1 = ";"
1459-
let ty_shape = Shape::indented(indent, context.config).sub_width(1)?;
1460-
rewrite_assign_rhs(context, result, ty, ty_shape).map(|s| s + ";")
1471+
let rhs_shape = Shape::indented(indent, context.config).sub_width(1)?;
1472+
rewrite_assign_rhs(context, result, rhs, rhs_shape).map(|s| s + ";")
1473+
}
1474+
1475+
pub fn rewrite_type_alias(
1476+
context: &RewriteContext,
1477+
indent: Indent,
1478+
ident: ast::Ident,
1479+
ty: &ast::Ty,
1480+
generics: &ast::Generics,
1481+
vis: &ast::Visibility,
1482+
) -> Option<String> {
1483+
rewrite_type_item(context, indent, "type", " =", ident, ty, generics, vis)
1484+
}
1485+
1486+
pub fn rewrite_existential_type(
1487+
context: &RewriteContext,
1488+
indent: Indent,
1489+
ident: ast::Ident,
1490+
generic_bounds: &ast::GenericBounds,
1491+
generics: &ast::Generics,
1492+
vis: &ast::Visibility,
1493+
) -> Option<String> {
1494+
rewrite_type_item(
1495+
context,
1496+
indent,
1497+
"existential type",
1498+
":",
1499+
ident,
1500+
generic_bounds,
1501+
generics,
1502+
vis,
1503+
)
14611504
}
14621505

14631506
fn type_annotation_spacing(config: &Config) -> (&str, &str) {
@@ -1706,6 +1749,16 @@ pub fn rewrite_associated_type(
17061749
}
17071750
}
17081751

1752+
pub fn rewrite_existential_impl_type(
1753+
context: &RewriteContext,
1754+
ident: ast::Ident,
1755+
generic_bounds: &ast::GenericBounds,
1756+
indent: Indent,
1757+
) -> Option<String> {
1758+
rewrite_associated_type(ident, None, Some(generic_bounds), context, indent)
1759+
.map(|s| format!("existential {}", s))
1760+
}
1761+
17091762
pub fn rewrite_associated_impl_type(
17101763
ident: ast::Ident,
17111764
defaultness: ast::Defaultness,

src/visitor.rs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ use comment::{CodeCharKind, CommentCodeSlices, FindUncommented};
1919
use config::{BraceStyle, Config};
2020
use items::{
2121
format_impl, format_trait, format_trait_alias, is_mod_decl, is_use_item,
22-
rewrite_associated_impl_type, rewrite_associated_type, rewrite_extern_crate,
23-
rewrite_type_alias, FnSig, StaticParts, StructParts,
22+
rewrite_associated_impl_type, rewrite_associated_type, rewrite_existential_impl_type,
23+
rewrite_existential_type, rewrite_extern_crate, rewrite_type_alias, FnSig, StaticParts,
24+
StructParts,
2425
};
2526
use macros::{rewrite_macro, rewrite_macro_def, MacroPosition};
2627
use rewrite::{Rewrite, RewriteContext};
@@ -412,7 +413,17 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
412413
ty,
413414
generics,
414415
&item.vis,
415-
item.span,
416+
);
417+
self.push_rewrite(item.span, rewrite);
418+
}
419+
ast::ItemKind::Existential(ref generic_bounds, ref generics) => {
420+
let rewrite = rewrite_existential_type(
421+
&self.get_context(),
422+
self.block_indent,
423+
item.ident,
424+
generic_bounds,
425+
generics,
426+
&item.vis,
416427
);
417428
self.push_rewrite(item.span, rewrite);
418429
}
@@ -510,6 +521,15 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
510521
);
511522
self.push_rewrite(ii.span, rewrite);
512523
}
524+
ast::ImplItemKind::Existential(ref generic_bounds) => {
525+
let rewrite = rewrite_existential_impl_type(
526+
&self.get_context(),
527+
ii.ident,
528+
generic_bounds,
529+
self.block_indent,
530+
);
531+
self.push_rewrite(ii.span, rewrite);
532+
}
513533
ast::ImplItemKind::Macro(ref mac) => {
514534
self.visit_mac(mac, Some(ii.ident), MacroPosition::Item);
515535
}

0 commit comments

Comments
 (0)