mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-29 13:25:09 +00:00
Move type inlay hint truncation to language server
This commit implements a general truncation framework for HirFormatter that keeps track of how much has been output so far. This information can then be used to perform truncation inside the language server, instead of relying on the client. Initial support is implemented for truncating types hints using the maxInlayHintLength server config option. The existing solution in the VSCode extension has been removed in favor of letting the server truncate type hints.
This commit is contained in:
parent
c24ee09904
commit
dadad36bb9
10 changed files with 97 additions and 35 deletions
|
@ -800,6 +800,10 @@ impl HirDisplay for &Ty {
|
|||
|
||||
impl HirDisplay for ApplicationTy {
|
||||
fn hir_fmt(&self, f: &mut HirFormatter<impl HirDatabase>) -> fmt::Result {
|
||||
if f.should_truncate() {
|
||||
return write!(f, "…");
|
||||
}
|
||||
|
||||
match self.ctor {
|
||||
TypeCtor::Bool => write!(f, "bool")?,
|
||||
TypeCtor::Char => write!(f, "char")?,
|
||||
|
@ -901,6 +905,10 @@ impl HirDisplay for ApplicationTy {
|
|||
|
||||
impl HirDisplay for ProjectionTy {
|
||||
fn hir_fmt(&self, f: &mut HirFormatter<impl HirDatabase>) -> fmt::Result {
|
||||
if f.should_truncate() {
|
||||
return write!(f, "…");
|
||||
}
|
||||
|
||||
let trait_name = self
|
||||
.associated_ty
|
||||
.parent_trait(f.db)
|
||||
|
@ -919,6 +927,10 @@ impl HirDisplay for ProjectionTy {
|
|||
|
||||
impl HirDisplay for Ty {
|
||||
fn hir_fmt(&self, f: &mut HirFormatter<impl HirDatabase>) -> fmt::Result {
|
||||
if f.should_truncate() {
|
||||
return write!(f, "…");
|
||||
}
|
||||
|
||||
match self {
|
||||
Ty::Apply(a_ty) => a_ty.hir_fmt(f)?,
|
||||
Ty::Projection(p_ty) => p_ty.hir_fmt(f)?,
|
||||
|
@ -1001,6 +1013,10 @@ impl HirDisplay for Ty {
|
|||
|
||||
impl TraitRef {
|
||||
fn hir_fmt_ext(&self, f: &mut HirFormatter<impl HirDatabase>, use_as: bool) -> fmt::Result {
|
||||
if f.should_truncate() {
|
||||
return write!(f, "…");
|
||||
}
|
||||
|
||||
self.substs[0].hir_fmt(f)?;
|
||||
if use_as {
|
||||
write!(f, " as ")?;
|
||||
|
@ -1031,6 +1047,10 @@ impl HirDisplay for &GenericPredicate {
|
|||
|
||||
impl HirDisplay for GenericPredicate {
|
||||
fn hir_fmt(&self, f: &mut HirFormatter<impl HirDatabase>) -> fmt::Result {
|
||||
if f.should_truncate() {
|
||||
return write!(f, "…");
|
||||
}
|
||||
|
||||
match self {
|
||||
GenericPredicate::Implemented(trait_ref) => trait_ref.hir_fmt(f)?,
|
||||
GenericPredicate::Projection(projection_pred) => {
|
||||
|
|
|
@ -7,15 +7,30 @@ use crate::db::HirDatabase;
|
|||
pub struct HirFormatter<'a, 'b, DB> {
|
||||
pub db: &'a DB,
|
||||
fmt: &'a mut fmt::Formatter<'b>,
|
||||
buf: String,
|
||||
curr_size: usize,
|
||||
max_size: Option<usize>,
|
||||
}
|
||||
|
||||
pub trait HirDisplay {
|
||||
fn hir_fmt(&self, f: &mut HirFormatter<impl HirDatabase>) -> fmt::Result;
|
||||
|
||||
fn display<'a, DB>(&'a self, db: &'a DB) -> HirDisplayWrapper<'a, DB, Self>
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
HirDisplayWrapper(db, self)
|
||||
HirDisplayWrapper(db, self, None)
|
||||
}
|
||||
|
||||
fn display_truncated<'a, DB>(
|
||||
&'a self,
|
||||
db: &'a DB,
|
||||
max_size: Option<usize>,
|
||||
) -> HirDisplayWrapper<'a, DB, Self>
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
HirDisplayWrapper(db, self, max_size)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -41,11 +56,25 @@ where
|
|||
|
||||
/// This allows using the `write!` macro directly with a `HirFormatter`.
|
||||
pub fn write_fmt(&mut self, args: fmt::Arguments) -> fmt::Result {
|
||||
fmt::write(self.fmt, args)
|
||||
// We write to a buffer first to track output size
|
||||
self.buf.clear();
|
||||
fmt::write(&mut self.buf, args)?;
|
||||
self.curr_size += self.buf.len();
|
||||
|
||||
// Then we write to the internal formatter from the buffer
|
||||
self.fmt.write_str(&self.buf)
|
||||
}
|
||||
|
||||
pub fn should_truncate(&self) -> bool {
|
||||
if let Some(max_size) = self.max_size {
|
||||
self.curr_size >= max_size
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct HirDisplayWrapper<'a, DB, T>(&'a DB, &'a T);
|
||||
pub struct HirDisplayWrapper<'a, DB, T>(&'a DB, &'a T, Option<usize>);
|
||||
|
||||
impl<'a, DB, T> fmt::Display for HirDisplayWrapper<'a, DB, T>
|
||||
where
|
||||
|
@ -53,6 +82,12 @@ where
|
|||
T: HirDisplay,
|
||||
{
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
self.1.hir_fmt(&mut HirFormatter { db: self.0, fmt: f })
|
||||
self.1.hir_fmt(&mut HirFormatter {
|
||||
db: self.0,
|
||||
fmt: f,
|
||||
buf: String::with_capacity(20),
|
||||
curr_size: 0,
|
||||
max_size: self.2,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue