Remove TruncateOptions struct

This commit is contained in:
Kirill Bulatov 2019-12-19 16:43:41 +02:00
parent 4fb25ef43b
commit 4ed78f80f4
4 changed files with 24 additions and 32 deletions

View file

@ -60,7 +60,4 @@ pub use hir_def::{
pub use hir_expand::{ pub use hir_expand::{
name::Name, HirFileId, InFile, MacroCallId, MacroCallLoc, MacroDefId, MacroFile, Origin, name::Name, HirFileId, InFile, MacroCallId, MacroCallLoc, MacroDefId, MacroFile, Origin,
}; };
pub use hir_ty::{ pub use hir_ty::{display::HirDisplay, CallableDef};
display::{HirDisplay, TruncateOptions},
CallableDef,
};

View file

@ -9,7 +9,8 @@ pub struct HirFormatter<'a, 'b, DB> {
fmt: &'a mut fmt::Formatter<'b>, fmt: &'a mut fmt::Formatter<'b>,
buf: String, buf: String,
curr_size: usize, curr_size: usize,
truncate_options: Option<&'a TruncateOptions>, max_size: Option<usize>,
should_display_default_types: bool,
} }
pub trait HirDisplay { pub trait HirDisplay {
@ -19,18 +20,18 @@ pub trait HirDisplay {
where where
Self: Sized, Self: Sized,
{ {
HirDisplayWrapper(db, self, None) HirDisplayWrapper(db, self, None, true)
} }
fn display_truncated<'a, DB>( fn display_truncated<'a, DB>(
&'a self, &'a self,
db: &'a DB, db: &'a DB,
truncate_options: &'a TruncateOptions, max_size: Option<usize>,
) -> HirDisplayWrapper<'a, DB, Self> ) -> HirDisplayWrapper<'a, DB, Self>
where where
Self: Sized, Self: Sized,
{ {
HirDisplayWrapper(db, self, Some(truncate_options)) HirDisplayWrapper(db, self, max_size, false)
} }
} }
@ -66,7 +67,7 @@ where
} }
pub fn should_truncate(&self) -> bool { pub fn should_truncate(&self) -> bool {
if let Some(max_size) = self.truncate_options.and_then(|options| options.max_length) { if let Some(max_size) = self.max_size {
self.curr_size >= max_size self.curr_size >= max_size
} else { } else {
false false
@ -74,16 +75,11 @@ where
} }
pub fn should_display_default_types(&self) -> bool { pub fn should_display_default_types(&self) -> bool {
self.truncate_options.map(|options| options.show_default_types).unwrap_or(true) self.should_display_default_types
} }
} }
pub struct TruncateOptions { pub struct HirDisplayWrapper<'a, DB, T>(&'a DB, &'a T, Option<usize>, bool);
pub max_length: Option<usize>,
pub show_default_types: bool,
}
pub struct HirDisplayWrapper<'a, DB, T>(&'a DB, &'a T, Option<&'a TruncateOptions>);
impl<'a, DB, T> fmt::Display for HirDisplayWrapper<'a, DB, T> impl<'a, DB, T> fmt::Display for HirDisplayWrapper<'a, DB, T>
where where
@ -96,7 +92,8 @@ where
fmt: f, fmt: f,
buf: String::with_capacity(20), buf: String::with_capacity(20),
curr_size: 0, curr_size: 0,
truncate_options: self.2, max_size: self.2,
should_display_default_types: self.3,
}) })
} }
} }

View file

@ -1,7 +1,7 @@
//! FIXME: write short doc here //! FIXME: write short doc here
use crate::{db::RootDatabase, FileId}; use crate::{db::RootDatabase, FileId};
use hir::{HirDisplay, SourceAnalyzer, TruncateOptions}; use hir::{HirDisplay, SourceAnalyzer};
use ra_syntax::{ use ra_syntax::{
ast::{self, AstNode, TypeAscriptionOwner}, ast::{self, AstNode, TypeAscriptionOwner},
match_ast, SmolStr, SourceFile, SyntaxKind, SyntaxNode, TextRange, match_ast, SmolStr, SourceFile, SyntaxKind, SyntaxNode, TextRange,
@ -23,11 +23,11 @@ pub(crate) fn inlay_hints(
db: &RootDatabase, db: &RootDatabase,
file_id: FileId, file_id: FileId,
file: &SourceFile, file: &SourceFile,
truncate_options: &TruncateOptions, max_inlay_hint_length: Option<usize>,
) -> Vec<InlayHint> { ) -> Vec<InlayHint> {
file.syntax() file.syntax()
.descendants() .descendants()
.map(|node| get_inlay_hints(db, file_id, &node, truncate_options).unwrap_or_default()) .map(|node| get_inlay_hints(db, file_id, &node, max_inlay_hint_length).unwrap_or_default())
.flatten() .flatten()
.collect() .collect()
} }
@ -36,7 +36,7 @@ fn get_inlay_hints(
db: &RootDatabase, db: &RootDatabase,
file_id: FileId, file_id: FileId,
node: &SyntaxNode, node: &SyntaxNode,
truncate_options: &TruncateOptions, max_inlay_hint_length: Option<usize>,
) -> Option<Vec<InlayHint>> { ) -> Option<Vec<InlayHint>> {
let analyzer = SourceAnalyzer::new(db, hir::InFile::new(file_id.into(), node), None); let analyzer = SourceAnalyzer::new(db, hir::InFile::new(file_id.into(), node), None);
match_ast! { match_ast! {
@ -46,7 +46,7 @@ fn get_inlay_hints(
return None; return None;
} }
let pat = it.pat()?; let pat = it.pat()?;
Some(get_pat_type_hints(db, &analyzer, pat, false, truncate_options)) Some(get_pat_type_hints(db, &analyzer, pat, false, max_inlay_hint_length))
}, },
ast::LambdaExpr(it) => { ast::LambdaExpr(it) => {
it.param_list().map(|param_list| { it.param_list().map(|param_list| {
@ -54,22 +54,22 @@ fn get_inlay_hints(
.params() .params()
.filter(|closure_param| closure_param.ascribed_type().is_none()) .filter(|closure_param| closure_param.ascribed_type().is_none())
.filter_map(|closure_param| closure_param.pat()) .filter_map(|closure_param| closure_param.pat())
.map(|root_pat| get_pat_type_hints(db, &analyzer, root_pat, false, truncate_options)) .map(|root_pat| get_pat_type_hints(db, &analyzer, root_pat, false, max_inlay_hint_length))
.flatten() .flatten()
.collect() .collect()
}) })
}, },
ast::ForExpr(it) => { ast::ForExpr(it) => {
let pat = it.pat()?; let pat = it.pat()?;
Some(get_pat_type_hints(db, &analyzer, pat, false, truncate_options)) Some(get_pat_type_hints(db, &analyzer, pat, false, max_inlay_hint_length))
}, },
ast::IfExpr(it) => { ast::IfExpr(it) => {
let pat = it.condition()?.pat()?; let pat = it.condition()?.pat()?;
Some(get_pat_type_hints(db, &analyzer, pat, true, truncate_options)) Some(get_pat_type_hints(db, &analyzer, pat, true, max_inlay_hint_length))
}, },
ast::WhileExpr(it) => { ast::WhileExpr(it) => {
let pat = it.condition()?.pat()?; let pat = it.condition()?.pat()?;
Some(get_pat_type_hints(db, &analyzer, pat, true, truncate_options)) Some(get_pat_type_hints(db, &analyzer, pat, true, max_inlay_hint_length))
}, },
ast::MatchArmList(it) => { ast::MatchArmList(it) => {
Some( Some(
@ -77,7 +77,7 @@ fn get_inlay_hints(
.arms() .arms()
.map(|match_arm| match_arm.pats()) .map(|match_arm| match_arm.pats())
.flatten() .flatten()
.map(|root_pat| get_pat_type_hints(db, &analyzer, root_pat, true, truncate_options)) .map(|root_pat| get_pat_type_hints(db, &analyzer, root_pat, true, max_inlay_hint_length))
.flatten() .flatten()
.collect(), .collect(),
) )
@ -92,7 +92,7 @@ fn get_pat_type_hints(
analyzer: &SourceAnalyzer, analyzer: &SourceAnalyzer,
root_pat: ast::Pat, root_pat: ast::Pat,
skip_root_pat_hint: bool, skip_root_pat_hint: bool,
truncate_options: &TruncateOptions, max_inlay_hint_length: Option<usize>,
) -> Vec<InlayHint> { ) -> Vec<InlayHint> {
let original_pat = &root_pat.clone(); let original_pat = &root_pat.clone();
@ -109,7 +109,7 @@ fn get_pat_type_hints(
.map(|(range, pat_type)| InlayHint { .map(|(range, pat_type)| InlayHint {
range, range,
kind: InlayKind::TypeHint, kind: InlayKind::TypeHint,
label: pat_type.display_truncated(db, truncate_options).to_string().into(), label: pat_type.display_truncated(db, max_inlay_hint_length).to_string().into(),
}) })
.collect() .collect()
} }

View file

@ -349,10 +349,8 @@ impl Analysis {
file_id: FileId, file_id: FileId,
max_inlay_hint_length: Option<usize>, max_inlay_hint_length: Option<usize>,
) -> Cancelable<Vec<InlayHint>> { ) -> Cancelable<Vec<InlayHint>> {
let truncate_options =
hir::TruncateOptions { max_length: max_inlay_hint_length, show_default_types: false };
self.with_db(|db| { self.with_db(|db| {
inlay_hints::inlay_hints(db, file_id, &db.parse(file_id).tree(), &truncate_options) inlay_hints::inlay_hints(db, file_id, &db.parse(file_id).tree(), max_inlay_hint_length)
}) })
} }