Omit default parameter types

This commit is contained in:
Kirill Bulatov 2019-12-08 00:54:18 +02:00
parent 8dd0e0086f
commit 14c167a9f6
12 changed files with 150 additions and 29 deletions

View file

@ -60,4 +60,7 @@ 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::{display::HirDisplay, CallableDef}; pub use hir_ty::{
display::{HirDisplay, TruncateOptions},
CallableDef,
};

View file

@ -9,7 +9,7 @@ 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,
max_size: Option<usize>, truncate_options: Option<&'a TruncateOptions>,
} }
pub trait HirDisplay { pub trait HirDisplay {
@ -25,12 +25,12 @@ pub trait HirDisplay {
fn display_truncated<'a, DB>( fn display_truncated<'a, DB>(
&'a self, &'a self,
db: &'a DB, db: &'a DB,
max_size: Option<usize>, truncate_options: &'a TruncateOptions,
) -> HirDisplayWrapper<'a, DB, Self> ) -> HirDisplayWrapper<'a, DB, Self>
where where
Self: Sized, Self: Sized,
{ {
HirDisplayWrapper(db, self, max_size) HirDisplayWrapper(db, self, Some(truncate_options))
} }
} }
@ -66,15 +66,24 @@ where
} }
pub fn should_truncate(&self) -> bool { pub fn should_truncate(&self) -> bool {
if let Some(max_size) = self.max_size { if let Some(max_size) = self.truncate_options.and_then(|options| options.max_length) {
self.curr_size >= max_size self.curr_size >= max_size
} else { } else {
false false
} }
} }
pub fn should_display_default_types(&self) -> bool {
self.truncate_options.map(|options| options.show_default_types).unwrap_or(true)
}
} }
pub struct HirDisplayWrapper<'a, DB, T>(&'a DB, &'a T, Option<usize>); pub struct TruncateOptions {
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
@ -87,7 +96,7 @@ where
fmt: f, fmt: f,
buf: String::with_capacity(20), buf: String::with_capacity(20),
curr_size: 0, curr_size: 0,
max_size: self.2, truncate_options: self.2,
}) })
} }
} }

View file

@ -906,7 +906,38 @@ impl HirDisplay for ApplicationTy {
write!(f, "{}", name)?; write!(f, "{}", name)?;
if self.parameters.len() > 0 { if self.parameters.len() > 0 {
write!(f, "<")?; write!(f, "<")?;
f.write_joined(&*self.parameters.0, ", ")?;
let mut non_default_parameters = Vec::with_capacity(self.parameters.len());
let parameters_to_write = if f.should_display_default_types() {
self.parameters.0.as_ref()
} else {
match self
.ctor
.as_generic_def()
.map(|generic_def_id| f.db.generic_defaults(generic_def_id))
.filter(|defaults| !defaults.is_empty())
{
Option::None => self.parameters.0.as_ref(),
Option::Some(default_parameters) => {
for (i, parameter) in self.parameters.into_iter().enumerate() {
match (parameter, default_parameters.get(i)) {
(&Ty::Unknown, _) | (_, None) => {
non_default_parameters.push(parameter.clone())
}
(_, Some(default_parameter))
if parameter != default_parameter =>
{
non_default_parameters.push(parameter.clone())
}
_ => (),
}
}
&non_default_parameters
}
}
};
f.write_joined(parameters_to_write, ", ")?;
write!(f, ">")?; write!(f, ">")?;
} }
} }

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}; use hir::{HirDisplay, SourceAnalyzer, TruncateOptions};
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,
max_inlay_hint_length: Option<usize>, truncate_options: &TruncateOptions,
) -> Vec<InlayHint> { ) -> Vec<InlayHint> {
file.syntax() file.syntax()
.descendants() .descendants()
.map(|node| get_inlay_hints(db, file_id, &node, max_inlay_hint_length).unwrap_or_default()) .map(|node| get_inlay_hints(db, file_id, &node, truncate_options).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,
max_inlay_hint_length: Option<usize>, truncate_options: &TruncateOptions,
) -> 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, max_inlay_hint_length)) Some(get_pat_type_hints(db, &analyzer, pat, false, truncate_options))
}, },
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, max_inlay_hint_length)) .map(|root_pat| get_pat_type_hints(db, &analyzer, root_pat, false, truncate_options))
.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, max_inlay_hint_length)) Some(get_pat_type_hints(db, &analyzer, pat, false, truncate_options))
}, },
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, max_inlay_hint_length)) Some(get_pat_type_hints(db, &analyzer, pat, true, truncate_options))
}, },
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, max_inlay_hint_length)) Some(get_pat_type_hints(db, &analyzer, pat, true, truncate_options))
}, },
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, max_inlay_hint_length)) .map(|root_pat| get_pat_type_hints(db, &analyzer, root_pat, true, truncate_options))
.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,
max_inlay_hint_length: Option<usize>, truncate_options: &TruncateOptions,
) -> 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, max_inlay_hint_length).to_string().into(), label: pat_type.display_truncated(db, truncate_options).to_string().into(),
}) })
.collect() .collect()
} }
@ -159,6 +159,58 @@ mod tests {
use crate::mock_analysis::single_file; use crate::mock_analysis::single_file;
#[test]
fn default_generic_types_disabled() {
let (analysis, file_id) = single_file(
r#"
struct Test<K, T = u8> {
k: K,
t: T,
}
fn main() {
let zz = Test { t: 23, k: 33 };
}"#,
);
assert_debug_snapshot!(analysis.inlay_hints(file_id, None, false).unwrap(), @r###"
[
InlayHint {
range: [65; 67),
kind: TypeHint,
label: "Test<i32>",
},
]
"###
);
}
#[test]
fn default_generic_types_enabled() {
let (analysis, file_id) = single_file(
r#"
struct Test<K, T = u8> {
k: K,
t: T,
}
fn main() {
let zz = Test { t: 23, k: 33 };
}"#,
);
assert_debug_snapshot!(analysis.inlay_hints(file_id, None, true).unwrap(), @r###"
[
InlayHint {
range: [69; 71),
kind: TypeHint,
label: "Test<i32, u8>",
},
]
"###
);
}
#[test] #[test]
fn let_statement() { fn let_statement() {
let (analysis, file_id) = single_file( let (analysis, file_id) = single_file(
@ -199,7 +251,7 @@ fn main() {
}"#, }"#,
); );
assert_debug_snapshot!(analysis.inlay_hints(file_id, None).unwrap(), @r###" assert_debug_snapshot!(analysis.inlay_hints(file_id, None, true).unwrap(), @r###"
[ [
InlayHint { InlayHint {
range: [193; 197), range: [193; 197),
@ -273,7 +325,7 @@ fn main() {
}"#, }"#,
); );
assert_debug_snapshot!(analysis.inlay_hints(file_id, None).unwrap(), @r###" assert_debug_snapshot!(analysis.inlay_hints(file_id, None, true).unwrap(), @r###"
[ [
InlayHint { InlayHint {
range: [21; 30), range: [21; 30),
@ -302,7 +354,7 @@ fn main() {
}"#, }"#,
); );
assert_debug_snapshot!(analysis.inlay_hints(file_id, None).unwrap(), @r###" assert_debug_snapshot!(analysis.inlay_hints(file_id, None, true).unwrap(), @r###"
[ [
InlayHint { InlayHint {
range: [21; 30), range: [21; 30),
@ -350,7 +402,7 @@ fn main() {
}"#, }"#,
); );
assert_debug_snapshot!(analysis.inlay_hints(file_id, None).unwrap(), @r###" assert_debug_snapshot!(analysis.inlay_hints(file_id, None, true).unwrap(), @r###"
[ [
InlayHint { InlayHint {
range: [166; 170), range: [166; 170),
@ -413,7 +465,7 @@ fn main() {
}"#, }"#,
); );
assert_debug_snapshot!(analysis.inlay_hints(file_id, None).unwrap(), @r###" assert_debug_snapshot!(analysis.inlay_hints(file_id, None, true).unwrap(), @r###"
[ [
InlayHint { InlayHint {
range: [166; 170), range: [166; 170),
@ -476,7 +528,7 @@ fn main() {
}"#, }"#,
); );
assert_debug_snapshot!(analysis.inlay_hints(file_id, None).unwrap(), @r###" assert_debug_snapshot!(analysis.inlay_hints(file_id, None, true).unwrap(), @r###"
[ [
InlayHint { InlayHint {
range: [311; 315), range: [311; 315),
@ -518,7 +570,7 @@ fn main() {
}"#, }"#,
); );
assert_debug_snapshot!(analysis.inlay_hints(file_id, Some(8)).unwrap(), @r###" assert_debug_snapshot!(analysis.inlay_hints(file_id, Some(8), true).unwrap(), @r###"
[ [
InlayHint { InlayHint {
range: [74; 75), range: [74; 75),

View file

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

View file

@ -31,6 +31,7 @@ pub struct ServerConfig {
pub lru_capacity: Option<usize>, pub lru_capacity: Option<usize>,
pub max_inlay_hint_length: Option<usize>, pub max_inlay_hint_length: Option<usize>,
pub show_default_types_in_inlay_hints: bool,
/// For internal usage to make integrated tests faster. /// For internal usage to make integrated tests faster.
#[serde(deserialize_with = "nullable_bool_true")] #[serde(deserialize_with = "nullable_bool_true")]
@ -51,6 +52,7 @@ impl Default for ServerConfig {
use_client_watching: false, use_client_watching: false,
lru_capacity: None, lru_capacity: None,
max_inlay_hint_length: None, max_inlay_hint_length: None,
show_default_types_in_inlay_hints: false,
with_sysroot: true, with_sysroot: true,
feature_flags: FxHashMap::default(), feature_flags: FxHashMap::default(),
cargo_features: Default::default(), cargo_features: Default::default(),

View file

@ -125,6 +125,7 @@ pub fn main_loop(
.and_then(|it| it.line_folding_only) .and_then(|it| it.line_folding_only)
.unwrap_or(false), .unwrap_or(false),
max_inlay_hint_length: config.max_inlay_hint_length, max_inlay_hint_length: config.max_inlay_hint_length,
show_default_types_in_inlay_hints: config.show_default_types_in_inlay_hints,
} }
}; };

View file

@ -895,7 +895,11 @@ pub fn handle_inlay_hints(
let analysis = world.analysis(); let analysis = world.analysis();
let line_index = analysis.file_line_index(file_id)?; let line_index = analysis.file_line_index(file_id)?;
Ok(analysis Ok(analysis
.inlay_hints(file_id, world.options.max_inlay_hint_length)? .inlay_hints(
file_id,
world.options.max_inlay_hint_length,
world.options.show_default_types_in_inlay_hints,
)?
.into_iter() .into_iter()
.map(|api_type| InlayHint { .map(|api_type| InlayHint {
label: api_type.label.to_string(), label: api_type.label.to_string(),

View file

@ -31,6 +31,7 @@ pub struct Options {
pub supports_location_link: bool, pub supports_location_link: bool,
pub line_folding_only: bool, pub line_folding_only: bool,
pub max_inlay_hint_length: Option<usize>, pub max_inlay_hint_length: Option<usize>,
pub show_default_types_in_inlay_hints: bool,
} }
/// `WorldState` is the primary mutable state of the language server /// `WorldState` is the primary mutable state of the language server

View file

@ -285,6 +285,11 @@
"default": 20, "default": 20,
"description": "Maximum length for inlay hints" "description": "Maximum length for inlay hints"
}, },
"rust-analyzer.showDefaultTypesInInlayHints": {
"type": "boolean",
"default": false,
"description": "Display default types in inlay hints"
},
"rust-analyzer.cargoFeatures.noDefaultFeatures": { "rust-analyzer.cargoFeatures.noDefaultFeatures": {
"type": "boolean", "type": "boolean",
"default": false, "default": false,

View file

@ -30,6 +30,7 @@ export class Config {
public lruCapacity: null | number = null; public lruCapacity: null | number = null;
public displayInlayHints = true; public displayInlayHints = true;
public maxInlayHintLength: null | number = null; public maxInlayHintLength: null | number = null;
public showDefaultTypesInInlayHints = false;
public excludeGlobs = []; public excludeGlobs = [];
public useClientWatching = true; public useClientWatching = true;
public featureFlags = {}; public featureFlags = {};
@ -153,6 +154,11 @@ export class Config {
'maxInlayHintLength', 'maxInlayHintLength',
) as number; ) as number;
} }
if (config.has('showDefaultTypesInInlayHints')) {
this.showDefaultTypesInInlayHints = config.get(
'showDefaultTypesInInlayHints',
) as boolean;
}
if (config.has('excludeGlobs')) { if (config.has('excludeGlobs')) {
this.excludeGlobs = config.get('excludeGlobs') || []; this.excludeGlobs = config.get('excludeGlobs') || [];
} }

View file

@ -55,6 +55,8 @@ export class Server {
publishDecorations: true, publishDecorations: true,
lruCapacity: Server.config.lruCapacity, lruCapacity: Server.config.lruCapacity,
maxInlayHintLength: Server.config.maxInlayHintLength, maxInlayHintLength: Server.config.maxInlayHintLength,
showDefaultTypesInInlayHints:
Server.config.showDefaultTypesInInlayHints,
excludeGlobs: Server.config.excludeGlobs, excludeGlobs: Server.config.excludeGlobs,
useClientWatching: Server.config.useClientWatching, useClientWatching: Server.config.useClientWatching,
featureFlags: Server.config.featureFlags, featureFlags: Server.config.featureFlags,