aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_ide/src')
-rw-r--r--crates/ra_ide/src/inlay_hints.rs101
-rw-r--r--crates/ra_ide/src/lib.rs5
2 files changed, 87 insertions, 19 deletions
diff --git a/crates/ra_ide/src/inlay_hints.rs b/crates/ra_ide/src/inlay_hints.rs
index cf0cbdbd0..0f1c13c14 100644
--- a/crates/ra_ide/src/inlay_hints.rs
+++ b/crates/ra_ide/src/inlay_hints.rs
@@ -3,6 +3,7 @@
3use hir::{Adt, HirDisplay, Semantics, Type}; 3use hir::{Adt, HirDisplay, Semantics, Type};
4use ra_ide_db::RootDatabase; 4use ra_ide_db::RootDatabase;
5use ra_prof::profile; 5use ra_prof::profile;
6use ra_project_model::{InlayHintDisplayType, InlayHintOptions};
6use ra_syntax::{ 7use ra_syntax::{
7 ast::{self, ArgListOwner, AstNode, TypeAscriptionOwner}, 8 ast::{self, ArgListOwner, AstNode, TypeAscriptionOwner},
8 match_ast, SmolStr, TextRange, 9 match_ast, SmolStr, TextRange,
@@ -26,7 +27,7 @@ pub struct InlayHint {
26pub(crate) fn inlay_hints( 27pub(crate) fn inlay_hints(
27 db: &RootDatabase, 28 db: &RootDatabase,
28 file_id: FileId, 29 file_id: FileId,
29 max_inlay_hint_length: Option<usize>, 30 inlay_hint_opts: &InlayHintOptions,
30) -> Vec<InlayHint> { 31) -> Vec<InlayHint> {
31 let _p = profile("inlay_hints"); 32 let _p = profile("inlay_hints");
32 let sema = Semantics::new(db); 33 let sema = Semantics::new(db);
@@ -36,9 +37,9 @@ pub(crate) fn inlay_hints(
36 for node in file.syntax().descendants() { 37 for node in file.syntax().descendants() {
37 match_ast! { 38 match_ast! {
38 match node { 39 match node {
39 ast::CallExpr(it) => { get_param_name_hints(&mut res, &sema, ast::Expr::from(it)); }, 40 ast::CallExpr(it) => { get_param_name_hints(&mut res, &sema, inlay_hint_opts, ast::Expr::from(it)); },
40 ast::MethodCallExpr(it) => { get_param_name_hints(&mut res, &sema, ast::Expr::from(it)); }, 41 ast::MethodCallExpr(it) => { get_param_name_hints(&mut res, &sema, inlay_hint_opts, ast::Expr::from(it)); },
41 ast::BindPat(it) => { get_bind_pat_hints(&mut res, &sema, max_inlay_hint_length, it); }, 42 ast::BindPat(it) => { get_bind_pat_hints(&mut res, &sema, inlay_hint_opts, it); },
42 _ => (), 43 _ => (),
43 } 44 }
44 } 45 }
@@ -49,8 +50,14 @@ pub(crate) fn inlay_hints(
49fn get_param_name_hints( 50fn get_param_name_hints(
50 acc: &mut Vec<InlayHint>, 51 acc: &mut Vec<InlayHint>,
51 sema: &Semantics<RootDatabase>, 52 sema: &Semantics<RootDatabase>,
53 inlay_hint_opts: &InlayHintOptions,
52 expr: ast::Expr, 54 expr: ast::Expr,
53) -> Option<()> { 55) -> Option<()> {
56 match inlay_hint_opts.display_type {
57 InlayHintDisplayType::Off | InlayHintDisplayType::TypeHints => return None,
58 _ => {}
59 }
60
54 let args = match &expr { 61 let args = match &expr {
55 ast::Expr::CallExpr(expr) => expr.arg_list()?.args(), 62 ast::Expr::CallExpr(expr) => expr.arg_list()?.args(),
56 ast::Expr::MethodCallExpr(expr) => expr.arg_list()?.args(), 63 ast::Expr::MethodCallExpr(expr) => expr.arg_list()?.args(),
@@ -84,9 +91,14 @@ fn get_param_name_hints(
84fn get_bind_pat_hints( 91fn get_bind_pat_hints(
85 acc: &mut Vec<InlayHint>, 92 acc: &mut Vec<InlayHint>,
86 sema: &Semantics<RootDatabase>, 93 sema: &Semantics<RootDatabase>,
87 max_inlay_hint_length: Option<usize>, 94 inlay_hint_opts: &InlayHintOptions,
88 pat: ast::BindPat, 95 pat: ast::BindPat,
89) -> Option<()> { 96) -> Option<()> {
97 match inlay_hint_opts.display_type {
98 InlayHintDisplayType::Off | InlayHintDisplayType::ParameterHints => return None,
99 _ => {}
100 }
101
90 let ty = sema.type_of_pat(&pat.clone().into())?; 102 let ty = sema.type_of_pat(&pat.clone().into())?;
91 103
92 if should_not_display_type_hint(sema.db, &pat, &ty) { 104 if should_not_display_type_hint(sema.db, &pat, &ty) {
@@ -96,7 +108,7 @@ fn get_bind_pat_hints(
96 acc.push(InlayHint { 108 acc.push(InlayHint {
97 range: pat.syntax().text_range(), 109 range: pat.syntax().text_range(),
98 kind: InlayKind::TypeHint, 110 kind: InlayKind::TypeHint,
99 label: ty.display_truncated(sema.db, max_inlay_hint_length).to_string().into(), 111 label: ty.display_truncated(sema.db, inlay_hint_opts.max_length).to_string().into(),
100 }); 112 });
101 Some(()) 113 Some(())
102} 114}
@@ -205,8 +217,63 @@ mod tests {
205 use insta::assert_debug_snapshot; 217 use insta::assert_debug_snapshot;
206 218
207 use crate::mock_analysis::single_file; 219 use crate::mock_analysis::single_file;
220 use ra_project_model::{InlayHintDisplayType, InlayHintOptions};
208 221
209 #[test] 222 #[test]
223 fn param_hints_only() {
224 let (analysis, file_id) = single_file(
225 r#"
226 fn foo(a: i32, b: i32) -> i32 { a + b }
227 fn main() {
228 let _x = foo(4, 4);
229 }"#,
230 );
231 assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayHintOptions{ display_type: InlayHintDisplayType::ParameterHints, max_length: None}).unwrap(), @r###"
232 [
233 InlayHint {
234 range: [106; 107),
235 kind: ParameterHint,
236 label: "a",
237 },
238 InlayHint {
239 range: [109; 110),
240 kind: ParameterHint,
241 label: "b",
242 },
243 ]"###);
244 }
245
246 #[test]
247 fn hints_disabled() {
248 let (analysis, file_id) = single_file(
249 r#"
250 fn foo(a: i32, b: i32) -> i32 { a + b }
251 fn main() {
252 let _x = foo(4, 4);
253 }"#,
254 );
255 assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayHintOptions{ display_type: InlayHintDisplayType::Off, max_length: None}).unwrap(), @r###"[]"###);
256 }
257
258 #[test]
259 fn type_hints_only() {
260 let (analysis, file_id) = single_file(
261 r#"
262 fn foo(a: i32, b: i32) -> i32 { a + b }
263 fn main() {
264 let _x = foo(4, 4);
265 }"#,
266 );
267 assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayHintOptions{ display_type: InlayHintDisplayType::TypeHints, max_length: None}).unwrap(), @r###"
268 [
269 InlayHint {
270 range: [97; 99),
271 kind: TypeHint,
272 label: "i32",
273 },
274 ]"###);
275 }
276 #[test]
210 fn default_generic_types_should_not_be_displayed() { 277 fn default_generic_types_should_not_be_displayed() {
211 let (analysis, file_id) = single_file( 278 let (analysis, file_id) = single_file(
212 r#" 279 r#"
@@ -221,7 +288,7 @@ fn main() {
221}"#, 288}"#,
222 ); 289 );
223 290
224 assert_debug_snapshot!(analysis.inlay_hints(file_id, None).unwrap(), @r###" 291 assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayHintOptions::new(None)).unwrap(), @r###"
225 [ 292 [
226 InlayHint { 293 InlayHint {
227 range: [69; 71), 294 range: [69; 71),
@@ -278,7 +345,7 @@ fn main() {
278}"#, 345}"#,
279 ); 346 );
280 347
281 assert_debug_snapshot!(analysis.inlay_hints(file_id, None).unwrap(), @r###" 348 assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayHintOptions::new(None)).unwrap(), @r###"
282 [ 349 [
283 InlayHint { 350 InlayHint {
284 range: [193; 197), 351 range: [193; 197),
@@ -358,7 +425,7 @@ fn main() {
358}"#, 425}"#,
359 ); 426 );
360 427
361 assert_debug_snapshot!(analysis.inlay_hints(file_id, None).unwrap(), @r###" 428 assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayHintOptions::new(None)).unwrap(), @r###"
362 [ 429 [
363 InlayHint { 430 InlayHint {
364 range: [21; 30), 431 range: [21; 30),
@@ -422,7 +489,7 @@ fn main() {
422}"#, 489}"#,
423 ); 490 );
424 491
425 assert_debug_snapshot!(analysis.inlay_hints(file_id, None).unwrap(), @r###" 492 assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayHintOptions::new(None)).unwrap(), @r###"
426 [ 493 [
427 InlayHint { 494 InlayHint {
428 range: [21; 30), 495 range: [21; 30),
@@ -472,7 +539,7 @@ fn main() {
472}"#, 539}"#,
473 ); 540 );
474 541
475 assert_debug_snapshot!(analysis.inlay_hints(file_id, None).unwrap(), @r###" 542 assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayHintOptions::new(None)).unwrap(), @r###"
476 [ 543 [
477 InlayHint { 544 InlayHint {
478 range: [188; 192), 545 range: [188; 192),
@@ -567,7 +634,7 @@ fn main() {
567}"#, 634}"#,
568 ); 635 );
569 636
570 assert_debug_snapshot!(analysis.inlay_hints(file_id, None).unwrap(), @r###" 637 assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayHintOptions::new(None)).unwrap(), @r###"
571 [ 638 [
572 InlayHint { 639 InlayHint {
573 range: [188; 192), 640 range: [188; 192),
@@ -662,7 +729,7 @@ fn main() {
662}"#, 729}"#,
663 ); 730 );
664 731
665 assert_debug_snapshot!(analysis.inlay_hints(file_id, None).unwrap(), @r###" 732 assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayHintOptions::new(None)).unwrap(), @r###"
666 [ 733 [
667 InlayHint { 734 InlayHint {
668 range: [252; 256), 735 range: [252; 256),
@@ -734,7 +801,7 @@ fn main() {
734}"#, 801}"#,
735 ); 802 );
736 803
737 assert_debug_snapshot!(analysis.inlay_hints(file_id, Some(8)).unwrap(), @r###" 804 assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayHintOptions::new(Some(8))).unwrap(), @r###"
738 [ 805 [
739 InlayHint { 806 InlayHint {
740 range: [74; 75), 807 range: [74; 75),
@@ -822,7 +889,7 @@ fn main() {
822}"#, 889}"#,
823 ); 890 );
824 891
825 assert_debug_snapshot!(analysis.inlay_hints(file_id, None).unwrap(), @r###" 892 assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayHintOptions::new(None)).unwrap(), @r###"
826 [ 893 [
827 InlayHint { 894 InlayHint {
828 range: [798; 809), 895 range: [798; 809),
@@ -944,7 +1011,7 @@ fn main() {
944}"#, 1011}"#,
945 ); 1012 );
946 1013
947 assert_debug_snapshot!(analysis.inlay_hints(file_id, Some(8)).unwrap(), @r###" 1014 assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayHintOptions::new(Some(8))).unwrap(), @r###"
948 [] 1015 []
949 "### 1016 "###
950 ); 1017 );
@@ -970,7 +1037,7 @@ fn main() {
970}"#, 1037}"#,
971 ); 1038 );
972 1039
973 assert_debug_snapshot!(analysis.inlay_hints(file_id, Some(8)).unwrap(), @r###" 1040 assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayHintOptions::new(Some(8))).unwrap(), @r###"
974 [] 1041 []
975 "### 1042 "###
976 ); 1043 );
diff --git a/crates/ra_ide/src/lib.rs b/crates/ra_ide/src/lib.rs
index 9f45003d3..8b1292a41 100644
--- a/crates/ra_ide/src/lib.rs
+++ b/crates/ra_ide/src/lib.rs
@@ -44,6 +44,7 @@ mod marks;
44#[cfg(test)] 44#[cfg(test)]
45mod test_utils; 45mod test_utils;
46 46
47use ra_project_model::InlayHintOptions;
47use std::sync::Arc; 48use std::sync::Arc;
48 49
49use ra_cfg::CfgOptions; 50use ra_cfg::CfgOptions;
@@ -318,9 +319,9 @@ impl Analysis {
318 pub fn inlay_hints( 319 pub fn inlay_hints(
319 &self, 320 &self,
320 file_id: FileId, 321 file_id: FileId,
321 max_inlay_hint_length: Option<usize>, 322 inlay_hint_opts: &InlayHintOptions,
322 ) -> Cancelable<Vec<InlayHint>> { 323 ) -> Cancelable<Vec<InlayHint>> {
323 self.with_db(|db| inlay_hints::inlay_hints(db, file_id, max_inlay_hint_length)) 324 self.with_db(|db| inlay_hints::inlay_hints(db, file_id, inlay_hint_opts))
324 } 325 }
325 326
326 /// Returns the set of folding ranges. 327 /// Returns the set of folding ranges.