mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-01 22:31:43 +00:00
Render where clauses and more generic params
This commit is contained in:
parent
380293d6c2
commit
15ff7faf3d
2 changed files with 155 additions and 34 deletions
|
@ -3,7 +3,10 @@
|
||||||
use std::fmt::{self, Write};
|
use std::fmt::{self, Write};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
attr::RawAttrs, generics::TypeParamProvenance, path::GenericArg, visibility::RawVisibility,
|
attr::RawAttrs,
|
||||||
|
generics::{WherePredicate, WherePredicateTypeTarget},
|
||||||
|
path::GenericArg,
|
||||||
|
visibility::RawVisibility,
|
||||||
};
|
};
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
|
@ -72,6 +75,13 @@ impl<'a> Printer<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn whitespace(&mut self) {
|
||||||
|
match self.buf.chars().next_back() {
|
||||||
|
None | Some('\n') | Some(' ') => {}
|
||||||
|
_ => self.buf.push(' '),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn print_attrs(&mut self, attrs: &RawAttrs, inner: bool) {
|
fn print_attrs(&mut self, attrs: &RawAttrs, inner: bool) {
|
||||||
let inner = if inner { "!" } else { "" };
|
let inner = if inner { "!" } else { "" };
|
||||||
for attr in &**attrs {
|
for attr in &**attrs {
|
||||||
|
@ -102,7 +112,8 @@ impl<'a> Printer<'a> {
|
||||||
fn print_fields(&mut self, fields: &Fields) {
|
fn print_fields(&mut self, fields: &Fields) {
|
||||||
match fields {
|
match fields {
|
||||||
Fields::Record(fields) => {
|
Fields::Record(fields) => {
|
||||||
w!(self, " {{");
|
self.whitespace();
|
||||||
|
w!(self, "{{");
|
||||||
self.indented(|this| {
|
self.indented(|this| {
|
||||||
for field in fields.clone() {
|
for field in fields.clone() {
|
||||||
let Field { visibility, name, type_ref } = &this.tree[field];
|
let Field { visibility, name, type_ref } = &this.tree[field];
|
||||||
|
@ -133,6 +144,25 @@ impl<'a> Printer<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn print_fields_and_where_clause(&mut self, fields: &Fields, params: &GenericParams) {
|
||||||
|
match fields {
|
||||||
|
Fields::Record(_) => {
|
||||||
|
if self.print_where_clause(params) {
|
||||||
|
wln!(self);
|
||||||
|
}
|
||||||
|
self.print_fields(fields);
|
||||||
|
}
|
||||||
|
Fields::Unit => {
|
||||||
|
self.print_where_clause(params);
|
||||||
|
self.print_fields(fields);
|
||||||
|
}
|
||||||
|
Fields::Tuple(_) => {
|
||||||
|
self.print_fields(fields);
|
||||||
|
self.print_where_clause(params);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn print_mod_item(&mut self, item: ModItem) {
|
fn print_mod_item(&mut self, item: ModItem) {
|
||||||
self.print_attrs_of(item);
|
self.print_attrs_of(item);
|
||||||
|
|
||||||
|
@ -212,6 +242,7 @@ impl<'a> Printer<'a> {
|
||||||
}
|
}
|
||||||
w!(self, ") -> ");
|
w!(self, ") -> ");
|
||||||
self.print_type_ref(ret_type);
|
self.print_type_ref(ret_type);
|
||||||
|
self.print_where_clause(generic_params);
|
||||||
wln!(self, ";");
|
wln!(self, ";");
|
||||||
}
|
}
|
||||||
ModItem::Struct(it) => {
|
ModItem::Struct(it) => {
|
||||||
|
@ -219,7 +250,7 @@ impl<'a> Printer<'a> {
|
||||||
self.print_visibility(*visibility);
|
self.print_visibility(*visibility);
|
||||||
w!(self, "struct {}", name);
|
w!(self, "struct {}", name);
|
||||||
self.print_generic_params(generic_params);
|
self.print_generic_params(generic_params);
|
||||||
self.print_fields(fields);
|
self.print_fields_and_where_clause(fields, generic_params);
|
||||||
if matches!(fields, Fields::Record(_)) {
|
if matches!(fields, Fields::Record(_)) {
|
||||||
wln!(self);
|
wln!(self);
|
||||||
} else {
|
} else {
|
||||||
|
@ -231,7 +262,7 @@ impl<'a> Printer<'a> {
|
||||||
self.print_visibility(*visibility);
|
self.print_visibility(*visibility);
|
||||||
w!(self, "union {}", name);
|
w!(self, "union {}", name);
|
||||||
self.print_generic_params(generic_params);
|
self.print_generic_params(generic_params);
|
||||||
self.print_fields(fields);
|
self.print_fields_and_where_clause(fields, generic_params);
|
||||||
if matches!(fields, Fields::Record(_)) {
|
if matches!(fields, Fields::Record(_)) {
|
||||||
wln!(self);
|
wln!(self);
|
||||||
} else {
|
} else {
|
||||||
|
@ -243,7 +274,7 @@ impl<'a> Printer<'a> {
|
||||||
self.print_visibility(*visibility);
|
self.print_visibility(*visibility);
|
||||||
w!(self, "enum {}", name);
|
w!(self, "enum {}", name);
|
||||||
self.print_generic_params(generic_params);
|
self.print_generic_params(generic_params);
|
||||||
w!(self, " {{");
|
self.print_where_clause_and_opening_brace(generic_params);
|
||||||
self.indented(|this| {
|
self.indented(|this| {
|
||||||
for variant in variants.clone() {
|
for variant in variants.clone() {
|
||||||
let Variant { name, fields } = &this.tree[variant];
|
let Variant { name, fields } = &this.tree[variant];
|
||||||
|
@ -301,14 +332,13 @@ impl<'a> Printer<'a> {
|
||||||
if *is_auto {
|
if *is_auto {
|
||||||
w!(self, "auto ");
|
w!(self, "auto ");
|
||||||
}
|
}
|
||||||
w!(self, "trait");
|
w!(self, "trait {}", name);
|
||||||
self.print_generic_params(generic_params);
|
self.print_generic_params(generic_params);
|
||||||
w!(self, " {}", name);
|
|
||||||
if !bounds.is_empty() {
|
if !bounds.is_empty() {
|
||||||
w!(self, ": ");
|
w!(self, ": ");
|
||||||
self.print_type_bounds(bounds);
|
self.print_type_bounds(bounds);
|
||||||
}
|
}
|
||||||
w!(self, " {{");
|
self.print_where_clause_and_opening_brace(generic_params);
|
||||||
self.indented(|this| {
|
self.indented(|this| {
|
||||||
for item in &**items {
|
for item in &**items {
|
||||||
this.print_mod_item((*item).into());
|
this.print_mod_item((*item).into());
|
||||||
|
@ -330,7 +360,7 @@ impl<'a> Printer<'a> {
|
||||||
w!(self, " for ");
|
w!(self, " for ");
|
||||||
}
|
}
|
||||||
self.print_type_ref(self_ty);
|
self.print_type_ref(self_ty);
|
||||||
w!(self, " {{");
|
self.print_where_clause_and_opening_brace(generic_params);
|
||||||
self.indented(|this| {
|
self.indented(|this| {
|
||||||
for item in &**items {
|
for item in &**items {
|
||||||
this.print_mod_item((*item).into());
|
this.print_mod_item((*item).into());
|
||||||
|
@ -359,6 +389,7 @@ impl<'a> Printer<'a> {
|
||||||
w!(self, " = ");
|
w!(self, " = ");
|
||||||
self.print_type_ref(ty);
|
self.print_type_ref(ty);
|
||||||
}
|
}
|
||||||
|
self.print_where_clause(generic_params);
|
||||||
w!(self, ";");
|
w!(self, ";");
|
||||||
if *is_extern {
|
if *is_extern {
|
||||||
w!(self, " // extern");
|
w!(self, " // extern");
|
||||||
|
@ -572,44 +603,93 @@ impl<'a> Printer<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn print_generic_params(&mut self, params: &GenericParams) {
|
fn print_generic_params(&mut self, params: &GenericParams) {
|
||||||
|
if params.types.is_empty() && params.lifetimes.is_empty() && params.consts.is_empty() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
w!(self, "<");
|
||||||
let mut first = true;
|
let mut first = true;
|
||||||
for (_, lt) in params.lifetimes.iter() {
|
for (_, lt) in params.lifetimes.iter() {
|
||||||
if first {
|
if !first {
|
||||||
w!(self, "<");
|
|
||||||
} else {
|
|
||||||
w!(self, ", ");
|
w!(self, ", ");
|
||||||
}
|
}
|
||||||
first = false;
|
first = false;
|
||||||
w!(self, "{}", lt.name);
|
w!(self, "{}", lt.name);
|
||||||
}
|
}
|
||||||
for (_, ty) in params.types.iter() {
|
for (idx, ty) in params.types.iter() {
|
||||||
if ty.provenance != TypeParamProvenance::TypeParamList {
|
if !first {
|
||||||
continue;
|
w!(self, ", ");
|
||||||
}
|
}
|
||||||
if let Some(name) = &ty.name {
|
first = false;
|
||||||
if first {
|
match &ty.name {
|
||||||
w!(self, "<");
|
Some(name) => w!(self, "{}", name),
|
||||||
} else {
|
None => w!(self, "_anon_{}", idx.into_raw()),
|
||||||
w!(self, ", ");
|
|
||||||
}
|
|
||||||
first = false;
|
|
||||||
w!(self, "{}", name);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (_, konst) in params.consts.iter() {
|
for (_, konst) in params.consts.iter() {
|
||||||
if first {
|
if !first {
|
||||||
w!(self, "<");
|
|
||||||
} else {
|
|
||||||
w!(self, ", ");
|
w!(self, ", ");
|
||||||
}
|
}
|
||||||
first = false;
|
first = false;
|
||||||
w!(self, "const {}: ", konst.name);
|
w!(self, "const {}: ", konst.name);
|
||||||
self.print_type_ref(&konst.ty);
|
self.print_type_ref(&konst.ty);
|
||||||
}
|
}
|
||||||
if !first {
|
w!(self, ">");
|
||||||
w!(self, ">");
|
}
|
||||||
|
|
||||||
|
fn print_where_clause_and_opening_brace(&mut self, params: &GenericParams) {
|
||||||
|
if self.print_where_clause(params) {
|
||||||
|
w!(self, "\n{{");
|
||||||
|
} else {
|
||||||
|
self.whitespace();
|
||||||
|
w!(self, "{{");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn print_where_clause(&mut self, params: &GenericParams) -> bool {
|
||||||
|
if params.where_predicates.is_empty() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
w!(self, "\nwhere");
|
||||||
|
self.indented(|this| {
|
||||||
|
for (i, pred) in params.where_predicates.iter().enumerate() {
|
||||||
|
if i != 0 {
|
||||||
|
wln!(this, ",");
|
||||||
|
}
|
||||||
|
|
||||||
|
let (target, bound) = match pred {
|
||||||
|
WherePredicate::TypeBound { target, bound } => (target, bound),
|
||||||
|
WherePredicate::Lifetime { target, bound } => {
|
||||||
|
wln!(this, "{}: {},", target.name, bound.name);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
WherePredicate::ForLifetime { lifetimes, target, bound } => {
|
||||||
|
w!(this, "for<");
|
||||||
|
for (i, lt) in lifetimes.iter().enumerate() {
|
||||||
|
if i != 0 {
|
||||||
|
w!(this, ", ");
|
||||||
|
}
|
||||||
|
w!(this, "{}", lt);
|
||||||
|
}
|
||||||
|
w!(this, "> ");
|
||||||
|
(target, bound)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
match target {
|
||||||
|
WherePredicateTypeTarget::TypeRef(ty) => this.print_type_ref(ty),
|
||||||
|
WherePredicateTypeTarget::TypeParam(id) => match ¶ms.types[*id].name {
|
||||||
|
Some(name) => w!(this, "{}", name),
|
||||||
|
None => w!(this, "_anon_{}", id.into_raw()),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
w!(this, ": ");
|
||||||
|
this.print_type_bounds(std::slice::from_ref(bound));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Write for Printer<'a> {
|
impl<'a> Write for Printer<'a> {
|
||||||
|
|
|
@ -183,7 +183,11 @@ trait Tr: SuperTrait + 'lifetime {
|
||||||
_: (),
|
_: (),
|
||||||
) -> ();
|
) -> ();
|
||||||
|
|
||||||
pub(self) trait Tr: SuperTrait + 'lifetime {
|
pub(self) trait Tr<Self>: SuperTrait + 'lifetime
|
||||||
|
where
|
||||||
|
Self: SuperTrait,
|
||||||
|
Self: 'lifetime
|
||||||
|
{
|
||||||
pub(self) type Assoc: AssocBound = Default;
|
pub(self) type Assoc: AssocBound = Default;
|
||||||
|
|
||||||
// flags = 0x1
|
// flags = 0x1
|
||||||
|
@ -207,6 +211,8 @@ mod inline {
|
||||||
|
|
||||||
fn fn_in_module() {}
|
fn fn_in_module() {}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
mod outline;
|
||||||
"#,
|
"#,
|
||||||
expect![[r##"
|
expect![[r##"
|
||||||
#[doc = " outer"] // AttrId { is_doc_comment: true, ast_index: 0 }
|
#[doc = " outer"] // AttrId { is_doc_comment: true, ast_index: 0 }
|
||||||
|
@ -217,6 +223,8 @@ mod inline {
|
||||||
// flags = 0x2
|
// flags = 0x2
|
||||||
pub(self) fn fn_in_module() -> ();
|
pub(self) fn fn_in_module() -> ();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(self) mod outline;
|
||||||
"##]],
|
"##]],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -293,7 +301,11 @@ struct S {
|
||||||
fn generics() {
|
fn generics() {
|
||||||
check(
|
check(
|
||||||
r#"
|
r#"
|
||||||
struct S<'a, 'b: 'a, T: Copy + 'a + 'b, const K: u8 = 0> {}
|
struct S<'a, 'b: 'a, T: Copy + 'a + 'b, const K: u8 = 0> {
|
||||||
|
field: &'a &'b T,
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Tuple<T: Copy>(T);
|
||||||
|
|
||||||
impl<'a, 'b: 'a, T: Copy + 'a + 'b, const K: u8 = 0> S<'a, 'b, T, K> {
|
impl<'a, 'b: 'a, T: Copy + 'a + 'b, const K: u8 = 0> S<'a, 'b, T, K> {
|
||||||
fn f<G: 'a>(arg: impl Copy) -> impl Copy {}
|
fn f<G: 'a>(arg: impl Copy) -> impl Copy {}
|
||||||
|
@ -301,16 +313,38 @@ impl<'a, 'b: 'a, T: Copy + 'a + 'b, const K: u8 = 0> S<'a, 'b, T, K> {
|
||||||
|
|
||||||
enum Enum<'a, T, const U: u8> {}
|
enum Enum<'a, T, const U: u8> {}
|
||||||
union Union<'a, T, const U: u8> {}
|
union Union<'a, T, const U: u8> {}
|
||||||
|
|
||||||
|
trait Tr<'a, T: 'a>: Super {}
|
||||||
"#,
|
"#,
|
||||||
expect![[r#"
|
expect![[r#"
|
||||||
pub(self) struct S<'a, 'b, T, const K: u8> {
|
pub(self) struct S<'a, 'b, T, const K: u8>
|
||||||
|
where
|
||||||
|
T: Copy,
|
||||||
|
T: 'a,
|
||||||
|
T: 'b
|
||||||
|
{
|
||||||
|
pub(self) field: &'a &'b T,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, 'b, T, const K: u8> S<'a, 'b, T, K> {
|
pub(self) struct Tuple<T>(
|
||||||
|
pub(self) 0: T,
|
||||||
|
)
|
||||||
|
where
|
||||||
|
T: Copy;
|
||||||
|
|
||||||
|
impl<'a, 'b, T, const K: u8> S<'a, 'b, T, K>
|
||||||
|
where
|
||||||
|
T: Copy,
|
||||||
|
T: 'a,
|
||||||
|
T: 'b
|
||||||
|
{
|
||||||
// flags = 0x2
|
// flags = 0x2
|
||||||
pub(self) fn f<G>(
|
pub(self) fn f<G, _anon_1>(
|
||||||
_: impl Copy,
|
_: impl Copy,
|
||||||
) -> impl Copy;
|
) -> impl Copy
|
||||||
|
where
|
||||||
|
G: 'a,
|
||||||
|
_anon_1: Copy;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(self) enum Enum<'a, T, const U: u8> {
|
pub(self) enum Enum<'a, T, const U: u8> {
|
||||||
|
@ -318,6 +352,13 @@ union Union<'a, T, const U: u8> {}
|
||||||
|
|
||||||
pub(self) union Union<'a, T, const U: u8> {
|
pub(self) union Union<'a, T, const U: u8> {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(self) trait Tr<'a, Self, T>: Super
|
||||||
|
where
|
||||||
|
Self: Super,
|
||||||
|
T: 'a
|
||||||
|
{
|
||||||
|
}
|
||||||
"#]],
|
"#]],
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue