aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorKirill Bulatov <[email protected]>2019-12-07 22:54:18 +0000
committerKirill Bulatov <[email protected]>2019-12-19 10:27:33 +0000
commit14c167a9f6da07024a5101ffa04bc2f79ce64353 (patch)
treea5fc10aaa8ed0cad0c08a9ff478acb23795aa322 /crates
parent8dd0e0086fc07422c9b1044b1db021cff6563214 (diff)
Omit default parameter types
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_hir/src/lib.rs5
-rw-r--r--crates/ra_hir_ty/src/display.rs21
-rw-r--r--crates/ra_hir_ty/src/lib.rs33
-rw-r--r--crates/ra_ide/src/inlay_hints.rs90
-rw-r--r--crates/ra_ide/src/lib.rs7
-rw-r--r--crates/ra_lsp_server/src/config.rs2
-rw-r--r--crates/ra_lsp_server/src/main_loop.rs1
-rw-r--r--crates/ra_lsp_server/src/main_loop/handlers.rs6
-rw-r--r--crates/ra_lsp_server/src/world.rs1
9 files changed, 137 insertions, 29 deletions
diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs
index 2e52a1f5c..e9ca548ca 100644
--- a/crates/ra_hir/src/lib.rs
+++ b/crates/ra_hir/src/lib.rs
@@ -60,4 +60,7 @@ pub use hir_def::{
60pub use hir_expand::{ 60pub use hir_expand::{
61 name::Name, HirFileId, InFile, MacroCallId, MacroCallLoc, MacroDefId, MacroFile, Origin, 61 name::Name, HirFileId, InFile, MacroCallId, MacroCallLoc, MacroDefId, MacroFile, Origin,
62}; 62};
63pub use hir_ty::{display::HirDisplay, CallableDef}; 63pub use hir_ty::{
64 display::{HirDisplay, TruncateOptions},
65 CallableDef,
66};
diff --git a/crates/ra_hir_ty/src/display.rs b/crates/ra_hir_ty/src/display.rs
index 9bb3ece6c..9176c0629 100644
--- a/crates/ra_hir_ty/src/display.rs
+++ b/crates/ra_hir_ty/src/display.rs
@@ -9,7 +9,7 @@ pub struct HirFormatter<'a, 'b, DB> {
9 fmt: &'a mut fmt::Formatter<'b>, 9 fmt: &'a mut fmt::Formatter<'b>,
10 buf: String, 10 buf: String,
11 curr_size: usize, 11 curr_size: usize,
12 max_size: Option<usize>, 12 truncate_options: Option<&'a TruncateOptions>,
13} 13}
14 14
15pub trait HirDisplay { 15pub trait HirDisplay {
@@ -25,12 +25,12 @@ pub trait HirDisplay {
25 fn display_truncated<'a, DB>( 25 fn display_truncated<'a, DB>(
26 &'a self, 26 &'a self,
27 db: &'a DB, 27 db: &'a DB,
28 max_size: Option<usize>, 28 truncate_options: &'a TruncateOptions,
29 ) -> HirDisplayWrapper<'a, DB, Self> 29 ) -> HirDisplayWrapper<'a, DB, Self>
30 where 30 where
31 Self: Sized, 31 Self: Sized,
32 { 32 {
33 HirDisplayWrapper(db, self, max_size) 33 HirDisplayWrapper(db, self, Some(truncate_options))
34 } 34 }
35} 35}
36 36
@@ -66,15 +66,24 @@ where
66 } 66 }
67 67
68 pub fn should_truncate(&self) -> bool { 68 pub fn should_truncate(&self) -> bool {
69 if let Some(max_size) = self.max_size { 69 if let Some(max_size) = self.truncate_options.and_then(|options| options.max_length) {
70 self.curr_size >= max_size 70 self.curr_size >= max_size
71 } else { 71 } else {
72 false 72 false
73 } 73 }
74 } 74 }
75
76 pub fn should_display_default_types(&self) -> bool {
77 self.truncate_options.map(|options| options.show_default_types).unwrap_or(true)
78 }
79}
80
81pub struct TruncateOptions {
82 pub max_length: Option<usize>,
83 pub show_default_types: bool,
75} 84}
76 85
77pub struct HirDisplayWrapper<'a, DB, T>(&'a DB, &'a T, Option<usize>); 86pub struct HirDisplayWrapper<'a, DB, T>(&'a DB, &'a T, Option<&'a TruncateOptions>);
78 87
79impl<'a, DB, T> fmt::Display for HirDisplayWrapper<'a, DB, T> 88impl<'a, DB, T> fmt::Display for HirDisplayWrapper<'a, DB, T>
80where 89where
@@ -87,7 +96,7 @@ where
87 fmt: f, 96 fmt: f,
88 buf: String::with_capacity(20), 97 buf: String::with_capacity(20),
89 curr_size: 0, 98 curr_size: 0,
90 max_size: self.2, 99 truncate_options: self.2,
91 }) 100 })
92 } 101 }
93} 102}
diff --git a/crates/ra_hir_ty/src/lib.rs b/crates/ra_hir_ty/src/lib.rs
index 3ad913e55..7ca9e6b8a 100644
--- a/crates/ra_hir_ty/src/lib.rs
+++ b/crates/ra_hir_ty/src/lib.rs
@@ -906,7 +906,38 @@ impl HirDisplay for ApplicationTy {
906 write!(f, "{}", name)?; 906 write!(f, "{}", name)?;
907 if self.parameters.len() > 0 { 907 if self.parameters.len() > 0 {
908 write!(f, "<")?; 908 write!(f, "<")?;
909 f.write_joined(&*self.parameters.0, ", ")?; 909
910 let mut non_default_parameters = Vec::with_capacity(self.parameters.len());
911 let parameters_to_write = if f.should_display_default_types() {
912 self.parameters.0.as_ref()
913 } else {
914 match self
915 .ctor
916 .as_generic_def()
917 .map(|generic_def_id| f.db.generic_defaults(generic_def_id))
918 .filter(|defaults| !defaults.is_empty())
919 {
920 Option::None => self.parameters.0.as_ref(),
921 Option::Some(default_parameters) => {
922 for (i, parameter) in self.parameters.into_iter().enumerate() {
923 match (parameter, default_parameters.get(i)) {
924 (&Ty::Unknown, _) | (_, None) => {
925 non_default_parameters.push(parameter.clone())
926 }
927 (_, Some(default_parameter))
928 if parameter != default_parameter =>
929 {
930 non_default_parameters.push(parameter.clone())
931 }
932 _ => (),
933 }
934 }
935 &non_default_parameters
936 }
937 }
938 };
939
940 f.write_joined(parameters_to_write, ", ")?;
910 write!(f, ">")?; 941 write!(f, ">")?;
911 } 942 }
912 } 943 }
diff --git a/crates/ra_ide/src/inlay_hints.rs b/crates/ra_ide/src/inlay_hints.rs
index 3730121af..8674912a6 100644
--- a/crates/ra_ide/src/inlay_hints.rs
+++ b/crates/ra_ide/src/inlay_hints.rs
@@ -1,7 +1,7 @@
1//! FIXME: write short doc here 1//! FIXME: write short doc here
2 2
3use crate::{db::RootDatabase, FileId}; 3use crate::{db::RootDatabase, FileId};
4use hir::{HirDisplay, SourceAnalyzer}; 4use hir::{HirDisplay, SourceAnalyzer, TruncateOptions};
5use ra_syntax::{ 5use ra_syntax::{
6 ast::{self, AstNode, TypeAscriptionOwner}, 6 ast::{self, AstNode, TypeAscriptionOwner},
7 match_ast, SmolStr, SourceFile, SyntaxKind, SyntaxNode, TextRange, 7 match_ast, SmolStr, SourceFile, SyntaxKind, SyntaxNode, TextRange,
@@ -23,11 +23,11 @@ pub(crate) fn inlay_hints(
23 db: &RootDatabase, 23 db: &RootDatabase,
24 file_id: FileId, 24 file_id: FileId,
25 file: &SourceFile, 25 file: &SourceFile,
26 max_inlay_hint_length: Option<usize>, 26 truncate_options: &TruncateOptions,
27) -> Vec<InlayHint> { 27) -> Vec<InlayHint> {
28 file.syntax() 28 file.syntax()
29 .descendants() 29 .descendants()
30 .map(|node| get_inlay_hints(db, file_id, &node, max_inlay_hint_length).unwrap_or_default()) 30 .map(|node| get_inlay_hints(db, file_id, &node, truncate_options).unwrap_or_default())
31 .flatten() 31 .flatten()
32 .collect() 32 .collect()
33} 33}
@@ -36,7 +36,7 @@ fn get_inlay_hints(
36 db: &RootDatabase, 36 db: &RootDatabase,
37 file_id: FileId, 37 file_id: FileId,
38 node: &SyntaxNode, 38 node: &SyntaxNode,
39 max_inlay_hint_length: Option<usize>, 39 truncate_options: &TruncateOptions,
40) -> Option<Vec<InlayHint>> { 40) -> Option<Vec<InlayHint>> {
41 let analyzer = SourceAnalyzer::new(db, hir::InFile::new(file_id.into(), node), None); 41 let analyzer = SourceAnalyzer::new(db, hir::InFile::new(file_id.into(), node), None);
42 match_ast! { 42 match_ast! {
@@ -46,7 +46,7 @@ fn get_inlay_hints(
46 return None; 46 return None;
47 } 47 }
48 let pat = it.pat()?; 48 let pat = it.pat()?;
49 Some(get_pat_type_hints(db, &analyzer, pat, false, max_inlay_hint_length)) 49 Some(get_pat_type_hints(db, &analyzer, pat, false, truncate_options))
50 }, 50 },
51 ast::LambdaExpr(it) => { 51 ast::LambdaExpr(it) => {
52 it.param_list().map(|param_list| { 52 it.param_list().map(|param_list| {
@@ -54,22 +54,22 @@ fn get_inlay_hints(
54 .params() 54 .params()
55 .filter(|closure_param| closure_param.ascribed_type().is_none()) 55 .filter(|closure_param| closure_param.ascribed_type().is_none())
56 .filter_map(|closure_param| closure_param.pat()) 56 .filter_map(|closure_param| closure_param.pat())
57 .map(|root_pat| get_pat_type_hints(db, &analyzer, root_pat, false, max_inlay_hint_length)) 57 .map(|root_pat| get_pat_type_hints(db, &analyzer, root_pat, false, truncate_options))
58 .flatten() 58 .flatten()
59 .collect() 59 .collect()
60 }) 60 })
61 }, 61 },
62 ast::ForExpr(it) => { 62 ast::ForExpr(it) => {
63 let pat = it.pat()?; 63 let pat = it.pat()?;
64 Some(get_pat_type_hints(db, &analyzer, pat, false, max_inlay_hint_length)) 64 Some(get_pat_type_hints(db, &analyzer, pat, false, truncate_options))
65 }, 65 },
66 ast::IfExpr(it) => { 66 ast::IfExpr(it) => {
67 let pat = it.condition()?.pat()?; 67 let pat = it.condition()?.pat()?;
68 Some(get_pat_type_hints(db, &analyzer, pat, true, max_inlay_hint_length)) 68 Some(get_pat_type_hints(db, &analyzer, pat, true, truncate_options))
69 }, 69 },
70 ast::WhileExpr(it) => { 70 ast::WhileExpr(it) => {
71 let pat = it.condition()?.pat()?; 71 let pat = it.condition()?.pat()?;
72 Some(get_pat_type_hints(db, &analyzer, pat, true, max_inlay_hint_length)) 72 Some(get_pat_type_hints(db, &analyzer, pat, true, truncate_options))
73 }, 73 },
74 ast::MatchArmList(it) => { 74 ast::MatchArmList(it) => {
75 Some( 75 Some(
@@ -77,7 +77,7 @@ fn get_inlay_hints(
77 .arms() 77 .arms()
78 .map(|match_arm| match_arm.pats()) 78 .map(|match_arm| match_arm.pats())
79 .flatten() 79 .flatten()
80 .map(|root_pat| get_pat_type_hints(db, &analyzer, root_pat, true, max_inlay_hint_length)) 80 .map(|root_pat| get_pat_type_hints(db, &analyzer, root_pat, true, truncate_options))
81 .flatten() 81 .flatten()
82 .collect(), 82 .collect(),
83 ) 83 )
@@ -92,7 +92,7 @@ fn get_pat_type_hints(
92 analyzer: &SourceAnalyzer, 92 analyzer: &SourceAnalyzer,
93 root_pat: ast::Pat, 93 root_pat: ast::Pat,
94 skip_root_pat_hint: bool, 94 skip_root_pat_hint: bool,
95 max_inlay_hint_length: Option<usize>, 95 truncate_options: &TruncateOptions,
96) -> Vec<InlayHint> { 96) -> Vec<InlayHint> {
97 let original_pat = &root_pat.clone(); 97 let original_pat = &root_pat.clone();
98 98
@@ -109,7 +109,7 @@ fn get_pat_type_hints(
109 .map(|(range, pat_type)| InlayHint { 109 .map(|(range, pat_type)| InlayHint {
110 range, 110 range,
111 kind: InlayKind::TypeHint, 111 kind: InlayKind::TypeHint,
112 label: pat_type.display_truncated(db, max_inlay_hint_length).to_string().into(), 112 label: pat_type.display_truncated(db, truncate_options).to_string().into(),
113 }) 113 })
114 .collect() 114 .collect()
115} 115}
@@ -160,6 +160,58 @@ mod tests {
160 use crate::mock_analysis::single_file; 160 use crate::mock_analysis::single_file;
161 161
162 #[test] 162 #[test]
163 fn default_generic_types_disabled() {
164 let (analysis, file_id) = single_file(
165 r#"
166struct Test<K, T = u8> {
167k: K,
168 t: T,
169}
170
171fn main() {
172 let zz = Test { t: 23, k: 33 };
173}"#,
174 );
175
176 assert_debug_snapshot!(analysis.inlay_hints(file_id, None, false).unwrap(), @r###"
177 [
178 InlayHint {
179 range: [65; 67),
180 kind: TypeHint,
181 label: "Test<i32>",
182 },
183 ]
184 "###
185 );
186 }
187
188 #[test]
189 fn default_generic_types_enabled() {
190 let (analysis, file_id) = single_file(
191 r#"
192struct Test<K, T = u8> {
193 k: K,
194 t: T,
195}
196
197fn main() {
198 let zz = Test { t: 23, k: 33 };
199}"#,
200 );
201
202 assert_debug_snapshot!(analysis.inlay_hints(file_id, None, true).unwrap(), @r###"
203 [
204 InlayHint {
205 range: [69; 71),
206 kind: TypeHint,
207 label: "Test<i32, u8>",
208 },
209 ]
210 "###
211 );
212 }
213
214 #[test]
163 fn let_statement() { 215 fn let_statement() {
164 let (analysis, file_id) = single_file( 216 let (analysis, file_id) = single_file(
165 r#" 217 r#"
@@ -199,7 +251,7 @@ fn main() {
199}"#, 251}"#,
200 ); 252 );
201 253
202 assert_debug_snapshot!(analysis.inlay_hints(file_id, None).unwrap(), @r###" 254 assert_debug_snapshot!(analysis.inlay_hints(file_id, None, true).unwrap(), @r###"
203 [ 255 [
204 InlayHint { 256 InlayHint {
205 range: [193; 197), 257 range: [193; 197),
@@ -273,7 +325,7 @@ fn main() {
273}"#, 325}"#,
274 ); 326 );
275 327
276 assert_debug_snapshot!(analysis.inlay_hints(file_id, None).unwrap(), @r###" 328 assert_debug_snapshot!(analysis.inlay_hints(file_id, None, true).unwrap(), @r###"
277 [ 329 [
278 InlayHint { 330 InlayHint {
279 range: [21; 30), 331 range: [21; 30),
@@ -302,7 +354,7 @@ fn main() {
302}"#, 354}"#,
303 ); 355 );
304 356
305 assert_debug_snapshot!(analysis.inlay_hints(file_id, None).unwrap(), @r###" 357 assert_debug_snapshot!(analysis.inlay_hints(file_id, None, true).unwrap(), @r###"
306 [ 358 [
307 InlayHint { 359 InlayHint {
308 range: [21; 30), 360 range: [21; 30),
@@ -350,7 +402,7 @@ fn main() {
350}"#, 402}"#,
351 ); 403 );
352 404
353 assert_debug_snapshot!(analysis.inlay_hints(file_id, None).unwrap(), @r###" 405 assert_debug_snapshot!(analysis.inlay_hints(file_id, None, true).unwrap(), @r###"
354 [ 406 [
355 InlayHint { 407 InlayHint {
356 range: [166; 170), 408 range: [166; 170),
@@ -413,7 +465,7 @@ fn main() {
413}"#, 465}"#,
414 ); 466 );
415 467
416 assert_debug_snapshot!(analysis.inlay_hints(file_id, None).unwrap(), @r###" 468 assert_debug_snapshot!(analysis.inlay_hints(file_id, None, true).unwrap(), @r###"
417 [ 469 [
418 InlayHint { 470 InlayHint {
419 range: [166; 170), 471 range: [166; 170),
@@ -476,7 +528,7 @@ fn main() {
476}"#, 528}"#,
477 ); 529 );
478 530
479 assert_debug_snapshot!(analysis.inlay_hints(file_id, None).unwrap(), @r###" 531 assert_debug_snapshot!(analysis.inlay_hints(file_id, None, true).unwrap(), @r###"
480 [ 532 [
481 InlayHint { 533 InlayHint {
482 range: [311; 315), 534 range: [311; 315),
@@ -518,7 +570,7 @@ fn main() {
518}"#, 570}"#,
519 ); 571 );
520 572
521 assert_debug_snapshot!(analysis.inlay_hints(file_id, Some(8)).unwrap(), @r###" 573 assert_debug_snapshot!(analysis.inlay_hints(file_id, Some(8), true).unwrap(), @r###"
522 [ 574 [
523 InlayHint { 575 InlayHint {
524 range: [74; 75), 576 range: [74; 75),
diff --git a/crates/ra_ide/src/lib.rs b/crates/ra_ide/src/lib.rs
index 779a81b2c..c3244a8dd 100644
--- a/crates/ra_ide/src/lib.rs
+++ b/crates/ra_ide/src/lib.rs
@@ -348,9 +348,14 @@ impl Analysis {
348 &self, 348 &self,
349 file_id: FileId, 349 file_id: FileId,
350 max_inlay_hint_length: Option<usize>, 350 max_inlay_hint_length: Option<usize>,
351 show_default_types_in_inlay_hints: bool,
351 ) -> Cancelable<Vec<InlayHint>> { 352 ) -> Cancelable<Vec<InlayHint>> {
353 let truncate_options = hir::TruncateOptions {
354 max_length: max_inlay_hint_length,
355 show_default_types: show_default_types_in_inlay_hints,
356 };
352 self.with_db(|db| { 357 self.with_db(|db| {
353 inlay_hints::inlay_hints(db, file_id, &db.parse(file_id).tree(), max_inlay_hint_length) 358 inlay_hints::inlay_hints(db, file_id, &db.parse(file_id).tree(), &truncate_options)
354 }) 359 })
355 } 360 }
356 361
diff --git a/crates/ra_lsp_server/src/config.rs b/crates/ra_lsp_server/src/config.rs
index 67942aa41..a916c5fd6 100644
--- a/crates/ra_lsp_server/src/config.rs
+++ b/crates/ra_lsp_server/src/config.rs
@@ -31,6 +31,7 @@ pub struct ServerConfig {
31 pub lru_capacity: Option<usize>, 31 pub lru_capacity: Option<usize>,
32 32
33 pub max_inlay_hint_length: Option<usize>, 33 pub max_inlay_hint_length: Option<usize>,
34 pub show_default_types_in_inlay_hints: bool,
34 35
35 /// For internal usage to make integrated tests faster. 36 /// For internal usage to make integrated tests faster.
36 #[serde(deserialize_with = "nullable_bool_true")] 37 #[serde(deserialize_with = "nullable_bool_true")]
@@ -51,6 +52,7 @@ impl Default for ServerConfig {
51 use_client_watching: false, 52 use_client_watching: false,
52 lru_capacity: None, 53 lru_capacity: None,
53 max_inlay_hint_length: None, 54 max_inlay_hint_length: None,
55 show_default_types_in_inlay_hints: false,
54 with_sysroot: true, 56 with_sysroot: true,
55 feature_flags: FxHashMap::default(), 57 feature_flags: FxHashMap::default(),
56 cargo_features: Default::default(), 58 cargo_features: Default::default(),
diff --git a/crates/ra_lsp_server/src/main_loop.rs b/crates/ra_lsp_server/src/main_loop.rs
index 965e7c53c..01fde3b2d 100644
--- a/crates/ra_lsp_server/src/main_loop.rs
+++ b/crates/ra_lsp_server/src/main_loop.rs
@@ -125,6 +125,7 @@ pub fn main_loop(
125 .and_then(|it| it.line_folding_only) 125 .and_then(|it| it.line_folding_only)
126 .unwrap_or(false), 126 .unwrap_or(false),
127 max_inlay_hint_length: config.max_inlay_hint_length, 127 max_inlay_hint_length: config.max_inlay_hint_length,
128 show_default_types_in_inlay_hints: config.show_default_types_in_inlay_hints,
128 } 129 }
129 }; 130 };
130 131
diff --git a/crates/ra_lsp_server/src/main_loop/handlers.rs b/crates/ra_lsp_server/src/main_loop/handlers.rs
index 5b64b27cd..9069d1f1d 100644
--- a/crates/ra_lsp_server/src/main_loop/handlers.rs
+++ b/crates/ra_lsp_server/src/main_loop/handlers.rs
@@ -895,7 +895,11 @@ pub fn handle_inlay_hints(
895 let analysis = world.analysis(); 895 let analysis = world.analysis();
896 let line_index = analysis.file_line_index(file_id)?; 896 let line_index = analysis.file_line_index(file_id)?;
897 Ok(analysis 897 Ok(analysis
898 .inlay_hints(file_id, world.options.max_inlay_hint_length)? 898 .inlay_hints(
899 file_id,
900 world.options.max_inlay_hint_length,
901 world.options.show_default_types_in_inlay_hints,
902 )?
899 .into_iter() 903 .into_iter()
900 .map(|api_type| InlayHint { 904 .map(|api_type| InlayHint {
901 label: api_type.label.to_string(), 905 label: api_type.label.to_string(),
diff --git a/crates/ra_lsp_server/src/world.rs b/crates/ra_lsp_server/src/world.rs
index 16cc11e8c..0b6eefcbc 100644
--- a/crates/ra_lsp_server/src/world.rs
+++ b/crates/ra_lsp_server/src/world.rs
@@ -31,6 +31,7 @@ pub struct Options {
31 pub supports_location_link: bool, 31 pub supports_location_link: bool,
32 pub line_folding_only: bool, 32 pub line_folding_only: bool,
33 pub max_inlay_hint_length: Option<usize>, 33 pub max_inlay_hint_length: Option<usize>,
34 pub show_default_types_in_inlay_hints: bool,
34} 35}
35 36
36/// `WorldState` is the primary mutable state of the language server 37/// `WorldState` is the primary mutable state of the language server