Skip to content

Commit

Permalink
Use trait object writer?
Browse files Browse the repository at this point in the history
  • Loading branch information
TimoFreiberg committed Apr 28, 2020
1 parent 9b11823 commit 1a8d3dd
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 45 deletions.
5 changes: 2 additions & 3 deletions crates/ra_hir/src/code_model.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//! FIXME: write short doc here
use std::{fmt, sync::Arc};
use std::sync::Arc;

use arrayvec::ArrayVec;
use either::Either;
Expand Down Expand Up @@ -1310,9 +1310,8 @@ impl Type {
}

impl HirDisplay for Type {
fn hir_fmt<F, R>(&self, f: &mut HirFormatter<F, R>) -> Result<(), R::Error>
fn hir_fmt<R>(&self, f: &mut HirFormatter<R>) -> Result<(), R::Error>
where
F: fmt::Write,
R: RenderTarget,
{
self.ty.value.hir_fmt(f)
Expand Down
60 changes: 18 additions & 42 deletions crates/ra_hir_ty/src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ use hir_def::{
};
use hir_expand::name::Name;

pub struct HirFormatter<'a, F, R> {
pub struct HirFormatter<'a, R> {
pub db: &'a dyn HirDatabase,
fmt: &'a mut F,
fmt: &'a mut dyn fmt::Write,
buf: String,
curr_size: usize,
pub(crate) max_size: Option<usize>,
Expand All @@ -23,9 +23,8 @@ pub struct HirFormatter<'a, F, R> {
}

pub trait HirDisplay {
fn hir_fmt<F, R>(&self, f: &mut HirFormatter<F, R>) -> Result<(), R::Error>
fn hir_fmt<R>(&self, f: &mut HirFormatter<R>) -> Result<(), R::Error>
where
F: fmt::Write,
R: RenderTarget;

/// Returns a `Display`able type that is human-readable.
Expand Down Expand Up @@ -90,9 +89,8 @@ pub trait HirDisplay {
}
}

impl<'a, F, R> HirFormatter<'a, F, R>
impl<'a, R> HirFormatter<'a, R>
where
F: fmt::Write,
R: RenderTarget,
{
pub fn write_joined<T: HirDisplay>(
Expand Down Expand Up @@ -137,11 +135,7 @@ where

pub trait RenderTarget: Sized + Copy {
type Error: From<fmt::Error>;
fn render_adt<W: fmt::Write>(
&self,
f: &mut HirFormatter<W, Self>,
def_id: AdtId,
) -> Result<(), Self::Error>;
fn render_adt(&self, f: &mut HirFormatter<Self>, def_id: AdtId) -> Result<(), Self::Error>;
}

/// Render types for inlays, doc popups, autocompletion, etc...
Expand All @@ -153,11 +147,7 @@ pub struct RenderDiagnostics;
impl RenderTarget for RenderDiagnostics {
type Error = fmt::Error;

fn render_adt<W: fmt::Write>(
&self,
f: &mut HirFormatter<W, Self>,
def_id: AdtId,
) -> Result<(), Self::Error> {
fn render_adt(&self, f: &mut HirFormatter<Self>, def_id: AdtId) -> Result<(), Self::Error> {
let name = match def_id {
AdtId::StructId(it) => f.db.struct_data(it).name.clone(),
AdtId::UnionId(it) => f.db.union_data(it).name.clone(),
Expand Down Expand Up @@ -199,11 +189,7 @@ impl RenderTarget for RenderSourceCode {
/// If the type was not displayable or a path could not be qualified,
/// we just return an empty error.
type Error = FormatSourceCodeError;
fn render_adt<W: fmt::Write>(
&self,
f: &mut HirFormatter<W, Self>,
def_id: AdtId,
) -> Result<(), Self::Error> {
fn render_adt(&self, f: &mut HirFormatter<Self>, def_id: AdtId) -> Result<(), Self::Error> {
Ok(
if let Some(path) =
find_path::find_path(f.db.upcast(), ItemInNs::Types(def_id.into()), self.in_module)
Expand Down Expand Up @@ -246,19 +232,17 @@ where
const TYPE_HINT_TRUNCATION: &str = "…";

impl HirDisplay for &Ty {
fn hir_fmt<F, R>(&self, f: &mut HirFormatter<F, R>) -> Result<(), R::Error>
fn hir_fmt<R>(&self, f: &mut HirFormatter<R>) -> Result<(), R::Error>
where
F: fmt::Write,
R: RenderTarget,
{
HirDisplay::hir_fmt(*self, f)
}
}

impl HirDisplay for ApplicationTy {
fn hir_fmt<F, R>(&self, mut f: &mut HirFormatter<F, R>) -> Result<(), R::Error>
fn hir_fmt<R>(&self, mut f: &mut HirFormatter<R>) -> Result<(), R::Error>
where
F: fmt::Write,
R: RenderTarget,
{
if f.should_truncate() {
Expand Down Expand Up @@ -418,9 +402,8 @@ impl HirDisplay for ApplicationTy {
}

impl HirDisplay for ProjectionTy {
fn hir_fmt<F, R>(&self, f: &mut HirFormatter<F, R>) -> Result<(), R::Error>
fn hir_fmt<R>(&self, f: &mut HirFormatter<R>) -> Result<(), R::Error>
where
F: fmt::Write,
R: RenderTarget,
{
if f.should_truncate() {
Expand All @@ -440,9 +423,8 @@ impl HirDisplay for ProjectionTy {
}

impl HirDisplay for Ty {
fn hir_fmt<F, R>(&self, f: &mut HirFormatter<F, R>) -> Result<(), R::Error>
fn hir_fmt<R>(&self, f: &mut HirFormatter<R>) -> Result<(), R::Error>
where
F: fmt::Write,
R: RenderTarget,
{
if f.should_truncate() {
Expand Down Expand Up @@ -486,12 +468,11 @@ impl HirDisplay for Ty {
}
}

fn write_bounds_like_dyn_trait<F, R>(
fn write_bounds_like_dyn_trait<R>(
predicates: &[GenericPredicate],
f: &mut HirFormatter<F, R>,
f: &mut HirFormatter<R>,
) -> Result<(), R::Error>
where
F: fmt::Write,
R: RenderTarget,
{
// Note: This code is written to produce nice results (i.e.
Expand Down Expand Up @@ -555,9 +536,8 @@ where
}

impl TraitRef {
fn hir_fmt_ext<F, R>(&self, f: &mut HirFormatter<F, R>, use_as: bool) -> Result<(), R::Error>
fn hir_fmt_ext<R>(&self, f: &mut HirFormatter<R>, use_as: bool) -> Result<(), R::Error>
where
F: fmt::Write,
R: RenderTarget,
{
if f.should_truncate() {
Expand All @@ -581,29 +561,26 @@ impl TraitRef {
}

impl HirDisplay for TraitRef {
fn hir_fmt<F, R>(&self, f: &mut HirFormatter<F, R>) -> Result<(), R::Error>
fn hir_fmt<R>(&self, f: &mut HirFormatter<R>) -> Result<(), R::Error>
where
F: fmt::Write,
R: RenderTarget,
{
self.hir_fmt_ext(f, false)
}
}

impl HirDisplay for &GenericPredicate {
fn hir_fmt<F, R>(&self, f: &mut HirFormatter<F, R>) -> Result<(), R::Error>
fn hir_fmt<R>(&self, f: &mut HirFormatter<R>) -> Result<(), R::Error>
where
F: fmt::Write,
R: RenderTarget,
{
HirDisplay::hir_fmt(*self, f)
}
}

impl HirDisplay for GenericPredicate {
fn hir_fmt<F, R>(&self, f: &mut HirFormatter<F, R>) -> Result<(), R::Error>
fn hir_fmt<R>(&self, f: &mut HirFormatter<R>) -> Result<(), R::Error>
where
F: fmt::Write,
R: RenderTarget,
{
if f.should_truncate() {
Expand All @@ -629,9 +606,8 @@ impl HirDisplay for GenericPredicate {
}

impl HirDisplay for Obligation {
fn hir_fmt<F, R>(&self, f: &mut HirFormatter<F, R>) -> Result<(), R::Error>
fn hir_fmt<R>(&self, f: &mut HirFormatter<R>) -> Result<(), R::Error>
where
F: fmt::Write,
R: RenderTarget,
{
Ok(match self {
Expand Down

0 comments on commit 1a8d3dd

Please sign in to comment.