From b6c662c573014710d4e8d9fd9253793141d8bbe0 Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Fri, 19 Jul 2019 08:53:12 +0300 Subject: If possible, show type lenses for the let bindings --- crates/ra_ide_api/src/display/structure.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'crates/ra_ide_api') diff --git a/crates/ra_ide_api/src/display/structure.rs b/crates/ra_ide_api/src/display/structure.rs index 2ba10b2ef..223941688 100644 --- a/crates/ra_ide_api/src/display/structure.rs +++ b/crates/ra_ide_api/src/display/structure.rs @@ -1,5 +1,6 @@ use crate::TextRange; +use ra_syntax::ast::PatKind; use ra_syntax::{ algo::visit::{visitor, Visitor}, ast::{self, AttrsOwner, NameOwner, TypeAscriptionOwner, TypeParamsOwner}, @@ -155,6 +156,27 @@ fn structure_node(node: &SyntaxNode) -> Option { } decl(mc) }) + .visit(|let_statement: &ast::LetStmt| { + let let_syntax = let_statement.syntax(); + + let mut label = String::new(); + collapse_ws(let_syntax, &mut label); + + let pat = match let_statement.pat()?.kind() { + PatKind::BindPat(bind_pat) => bind_pat, + _ => return None, + }; + + Some(StructureNode { + parent: None, + label, + navigation_range: pat.syntax().range(), + node_range: let_syntax.range(), + kind: let_syntax.kind(), + detail: None, + deprecated: false, + }) + }) .accept(&node)? } -- cgit v1.2.3 From 1ce864ac625d95ed928c02b609b84153f437b9df Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Fri, 19 Jul 2019 15:13:19 +0300 Subject: Fix rebase issue --- crates/ra_ide_api/src/display/structure.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates/ra_ide_api') diff --git a/crates/ra_ide_api/src/display/structure.rs b/crates/ra_ide_api/src/display/structure.rs index 223941688..f891d2c20 100644 --- a/crates/ra_ide_api/src/display/structure.rs +++ b/crates/ra_ide_api/src/display/structure.rs @@ -156,7 +156,7 @@ fn structure_node(node: &SyntaxNode) -> Option { } decl(mc) }) - .visit(|let_statement: &ast::LetStmt| { + .visit(|let_statement: ast::LetStmt| { let let_syntax = let_statement.syntax(); let mut label = String::new(); -- cgit v1.2.3 From dfa8373766d645619d6516342565477134acd668 Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Fri, 19 Jul 2019 15:21:25 +0300 Subject: Do not show a lens when the type is declared explicitly --- crates/ra_ide_api/src/display/structure.rs | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'crates/ra_ide_api') diff --git a/crates/ra_ide_api/src/display/structure.rs b/crates/ra_ide_api/src/display/structure.rs index f891d2c20..ca2321e97 100644 --- a/crates/ra_ide_api/src/display/structure.rs +++ b/crates/ra_ide_api/src/display/structure.rs @@ -162,6 +162,10 @@ fn structure_node(node: &SyntaxNode) -> Option { let mut label = String::new(); collapse_ws(let_syntax, &mut label); + if let_statement.ascribed_type().is_some() { + return None; + } + let pat = match let_statement.pat()?.kind() { PatKind::BindPat(bind_pat) => bind_pat, _ => return None, -- cgit v1.2.3 From 761fc71083b9d810aa006210d6df2b0edf95cf33 Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Fri, 19 Jul 2019 15:44:24 +0300 Subject: Fix tuple type lens resolution --- crates/ra_ide_api/src/display/structure.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'crates/ra_ide_api') diff --git a/crates/ra_ide_api/src/display/structure.rs b/crates/ra_ide_api/src/display/structure.rs index ca2321e97..bd2e908da 100644 --- a/crates/ra_ide_api/src/display/structure.rs +++ b/crates/ra_ide_api/src/display/structure.rs @@ -166,15 +166,16 @@ fn structure_node(node: &SyntaxNode) -> Option { return None; } - let pat = match let_statement.pat()?.kind() { - PatKind::BindPat(bind_pat) => bind_pat, + let pat_range = match let_statement.pat()?.kind() { + PatKind::BindPat(bind_pat) => bind_pat.syntax().range(), + PatKind::TuplePat(tuple_pat) => tuple_pat.syntax().range(), _ => return None, }; Some(StructureNode { parent: None, label, - navigation_range: pat.syntax().range(), + navigation_range: pat_range, node_range: let_syntax.range(), kind: let_syntax.kind(), detail: None, -- cgit v1.2.3 From 201b344f2b0c9e84606115d135cd658d0a955d2c Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Sat, 20 Jul 2019 00:20:09 +0300 Subject: Refactor server api --- crates/ra_ide_api/src/display/structure.rs | 27 ----- crates/ra_ide_api/src/inlay_hints.rs | 110 +++++++++++++++++++++ crates/ra_ide_api/src/lib.rs | 7 ++ .../src/snapshots/tests__inlay_hints.snap | 63 ++++++++++++ 4 files changed, 180 insertions(+), 27 deletions(-) create mode 100644 crates/ra_ide_api/src/inlay_hints.rs create mode 100644 crates/ra_ide_api/src/snapshots/tests__inlay_hints.snap (limited to 'crates/ra_ide_api') diff --git a/crates/ra_ide_api/src/display/structure.rs b/crates/ra_ide_api/src/display/structure.rs index bd2e908da..2ba10b2ef 100644 --- a/crates/ra_ide_api/src/display/structure.rs +++ b/crates/ra_ide_api/src/display/structure.rs @@ -1,6 +1,5 @@ use crate::TextRange; -use ra_syntax::ast::PatKind; use ra_syntax::{ algo::visit::{visitor, Visitor}, ast::{self, AttrsOwner, NameOwner, TypeAscriptionOwner, TypeParamsOwner}, @@ -156,32 +155,6 @@ fn structure_node(node: &SyntaxNode) -> Option { } decl(mc) }) - .visit(|let_statement: ast::LetStmt| { - let let_syntax = let_statement.syntax(); - - let mut label = String::new(); - collapse_ws(let_syntax, &mut label); - - if let_statement.ascribed_type().is_some() { - return None; - } - - let pat_range = match let_statement.pat()?.kind() { - PatKind::BindPat(bind_pat) => bind_pat.syntax().range(), - PatKind::TuplePat(tuple_pat) => tuple_pat.syntax().range(), - _ => return None, - }; - - Some(StructureNode { - parent: None, - label, - navigation_range: pat_range, - node_range: let_syntax.range(), - kind: let_syntax.kind(), - detail: None, - deprecated: false, - }) - }) .accept(&node)? } diff --git a/crates/ra_ide_api/src/inlay_hints.rs b/crates/ra_ide_api/src/inlay_hints.rs new file mode 100644 index 000000000..4d1df65d0 --- /dev/null +++ b/crates/ra_ide_api/src/inlay_hints.rs @@ -0,0 +1,110 @@ +use ra_syntax::{ + algo::visit::{visitor, Visitor}, + ast::{self, PatKind, TypeAscriptionOwner}, + AstNode, SmolStr, SourceFile, SyntaxNode, TextRange, +}; + +#[derive(Debug, PartialEq, Eq)] +pub enum InlayKind { + LetBinding, + ClosureParameter, +} + +#[derive(Debug)] +pub struct InlayHint { + pub range: TextRange, + pub text: SmolStr, + pub inlay_kind: InlayKind, +} + +pub(crate) fn inlay_hints(file: &SourceFile) -> Vec { + file.syntax().descendants().map(|node| get_inlay_hints(&node)).flatten().collect() +} + +fn get_inlay_hints(node: &SyntaxNode) -> Vec { + visitor() + .visit(|let_statement: ast::LetStmt| { + let let_syntax = let_statement.syntax(); + + if let_statement.ascribed_type().is_some() { + return Vec::new(); + } + + let pat_range = match let_statement.pat().map(|pat| pat.kind()) { + Some(PatKind::BindPat(bind_pat)) => bind_pat.syntax().text_range(), + Some(PatKind::TuplePat(tuple_pat)) => tuple_pat.syntax().text_range(), + _ => return Vec::new(), + }; + + vec![InlayHint { + range: pat_range, + text: let_syntax.text().to_smol_string(), + inlay_kind: InlayKind::LetBinding, + }] + }) + .visit(|closure_parameter: ast::LambdaExpr| { + if let Some(param_list) = closure_parameter.param_list() { + param_list + .params() + .filter(|closure_param| closure_param.ascribed_type().is_none()) + .map(|closure_param| { + let closure_param_syntax = closure_param.syntax(); + InlayHint { + range: closure_param_syntax.text_range(), + text: closure_param_syntax.text().to_smol_string(), + inlay_kind: InlayKind::ClosureParameter, + } + }) + .collect() + } else { + Vec::new() + } + }) + .accept(&node) + .unwrap_or_else(Vec::new) +} + +#[cfg(test)] +mod tests { + use super::*; + use insta::assert_debug_snapshot_matches; + + #[test] + fn test_inlay_hints() { + let file = SourceFile::parse( + r#" +struct OuterStruct {} + +fn main() { + struct InnerStruct {} + + let test = 54; + let test = InnerStruct {}; + let test = OuterStruct {}; + let test = vec![222]; + let mut test = Vec::new(); + test.push(333); + let test = test.into_iter().map(|i| i * i).collect::>(); + let mut test = 33; + let _ = 22; + let test: Vec<_> = (0..3).collect(); + + let _ = (0..23).map(|i: u32| { + let i_squared = i * i; + i_squared + }); + + let test: i32 = 33; + + let (x, c) = (42, 'a'); + let test = (42, 'a'); +} + +"#, + ) + .ok() + .unwrap(); + let hints = inlay_hints(&file); + assert_debug_snapshot_matches!("inlay_hints", hints); + } +} diff --git a/crates/ra_ide_api/src/lib.rs b/crates/ra_ide_api/src/lib.rs index c54d574bc..af163088a 100644 --- a/crates/ra_ide_api/src/lib.rs +++ b/crates/ra_ide_api/src/lib.rs @@ -38,6 +38,7 @@ mod join_lines; mod typing; mod matching_brace; mod display; +mod inlay_hints; #[cfg(test)] mod marks; @@ -64,6 +65,7 @@ pub use crate::{ display::{file_structure, FunctionSignature, NavigationTarget, StructureNode}, folding_ranges::{Fold, FoldKind}, hover::HoverResult, + inlay_hints::{InlayHint, InlayKind}, line_index::{LineCol, LineIndex}, line_index_utils::translate_offset_with_edit, references::ReferenceSearchResult, @@ -396,6 +398,11 @@ impl Analysis { file_structure(&parse.tree()) } + /// Returns a list of the places in the file where type hints can be displayed. + pub fn inlay_hints(&self, file_id: FileId) -> Vec { + inlay_hints::inlay_hints(&self.db.parse(file_id).tree()) + } + /// Returns the set of folding ranges. pub fn folding_ranges(&self, file_id: FileId) -> Vec { let parse = self.db.parse(file_id); diff --git a/crates/ra_ide_api/src/snapshots/tests__inlay_hints.snap b/crates/ra_ide_api/src/snapshots/tests__inlay_hints.snap new file mode 100644 index 000000000..f4d562314 --- /dev/null +++ b/crates/ra_ide_api/src/snapshots/tests__inlay_hints.snap @@ -0,0 +1,63 @@ +--- +created: "2019-07-20T20:13:53.385368Z" +creator: insta@0.8.1 +source: crates/ra_ide_api/src/inlay_hints.rs +expression: hints +--- +[ + InlayHint { + range: [71; 75), + text: "let test = 54;", + inlay_kind: LetBinding, + }, + InlayHint { + range: [90; 94), + text: "let test = InnerStruct {};", + inlay_kind: LetBinding, + }, + InlayHint { + range: [121; 125), + text: "let test = OuterStruct {};", + inlay_kind: LetBinding, + }, + InlayHint { + range: [152; 156), + text: "let test = vec![222];", + inlay_kind: LetBinding, + }, + InlayHint { + range: [178; 186), + text: "let mut test = Vec::new();", + inlay_kind: LetBinding, + }, + InlayHint { + range: [229; 233), + text: "let test = test.into_iter().map(|i| i * i).collect::>();", + inlay_kind: LetBinding, + }, + InlayHint { + range: [258; 259), + text: "i", + inlay_kind: ClosureParameter, + }, + InlayHint { + range: [297; 305), + text: "let mut test = 33;", + inlay_kind: LetBinding, + }, + InlayHint { + range: [417; 426), + text: "let i_squared = i * i;", + inlay_kind: LetBinding, + }, + InlayHint { + range: [500; 506), + text: "let (x, c) = (42, \'a\');", + inlay_kind: LetBinding, + }, + InlayHint { + range: [528; 532), + text: "let test = (42, \'a\');", + inlay_kind: LetBinding, + }, +] -- cgit v1.2.3 From 24784c60df583d1807faa889a84312df1d2e3b22 Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Sun, 21 Jul 2019 20:51:27 +0300 Subject: Code review fixes --- crates/ra_ide_api/src/inlay_hints.rs | 64 ++++++++++++++++++++-- crates/ra_ide_api/src/lib.rs | 4 +- .../src/snapshots/tests__inlay_hints.snap | 63 --------------------- 3 files changed, 62 insertions(+), 69 deletions(-) delete mode 100644 crates/ra_ide_api/src/snapshots/tests__inlay_hints.snap (limited to 'crates/ra_ide_api') diff --git a/crates/ra_ide_api/src/inlay_hints.rs b/crates/ra_ide_api/src/inlay_hints.rs index 4d1df65d0..739a44b19 100644 --- a/crates/ra_ide_api/src/inlay_hints.rs +++ b/crates/ra_ide_api/src/inlay_hints.rs @@ -61,7 +61,7 @@ fn get_inlay_hints(node: &SyntaxNode) -> Vec { } }) .accept(&node) - .unwrap_or_else(Vec::new) + .unwrap_or_default() } #[cfg(test)] @@ -93,7 +93,7 @@ fn main() { let i_squared = i * i; i_squared }); - + let test: i32 = 33; let (x, c) = (42, 'a'); @@ -104,7 +104,63 @@ fn main() { ) .ok() .unwrap(); - let hints = inlay_hints(&file); - assert_debug_snapshot_matches!("inlay_hints", hints); + assert_debug_snapshot_matches!(inlay_hints(&file), @r#"[ + InlayHint { + range: [71; 75), + text: "let test = 54;", + inlay_kind: LetBinding, + }, + InlayHint { + range: [90; 94), + text: "let test = InnerStruct {};", + inlay_kind: LetBinding, + }, + InlayHint { + range: [121; 125), + text: "let test = OuterStruct {};", + inlay_kind: LetBinding, + }, + InlayHint { + range: [152; 156), + text: "let test = vec![222];", + inlay_kind: LetBinding, + }, + InlayHint { + range: [178; 186), + text: "let mut test = Vec::new();", + inlay_kind: LetBinding, + }, + InlayHint { + range: [229; 233), + text: "let test = test.into_iter().map(|i| i * i).collect::>();", + inlay_kind: LetBinding, + }, + InlayHint { + range: [258; 259), + text: "i", + inlay_kind: ClosureParameter, + }, + InlayHint { + range: [297; 305), + text: "let mut test = 33;", + inlay_kind: LetBinding, + }, + InlayHint { + range: [417; 426), + text: "let i_squared = i * i;", + inlay_kind: LetBinding, + }, + InlayHint { + range: [496; 502), + text: "let (x, c) = (42, \'a\');", + inlay_kind: LetBinding, + }, + InlayHint { + range: [524; 528), + text: "let test = (42, \'a\');", + inlay_kind: LetBinding, + }, +]"# + ); } } diff --git a/crates/ra_ide_api/src/lib.rs b/crates/ra_ide_api/src/lib.rs index af163088a..32168f12d 100644 --- a/crates/ra_ide_api/src/lib.rs +++ b/crates/ra_ide_api/src/lib.rs @@ -399,8 +399,8 @@ impl Analysis { } /// Returns a list of the places in the file where type hints can be displayed. - pub fn inlay_hints(&self, file_id: FileId) -> Vec { - inlay_hints::inlay_hints(&self.db.parse(file_id).tree()) + pub fn inlay_hints(&self, file_id: FileId) -> Cancelable> { + self.with_db(|db| inlay_hints::inlay_hints(&db.parse(file_id).tree())) } /// Returns the set of folding ranges. diff --git a/crates/ra_ide_api/src/snapshots/tests__inlay_hints.snap b/crates/ra_ide_api/src/snapshots/tests__inlay_hints.snap deleted file mode 100644 index f4d562314..000000000 --- a/crates/ra_ide_api/src/snapshots/tests__inlay_hints.snap +++ /dev/null @@ -1,63 +0,0 @@ ---- -created: "2019-07-20T20:13:53.385368Z" -creator: insta@0.8.1 -source: crates/ra_ide_api/src/inlay_hints.rs -expression: hints ---- -[ - InlayHint { - range: [71; 75), - text: "let test = 54;", - inlay_kind: LetBinding, - }, - InlayHint { - range: [90; 94), - text: "let test = InnerStruct {};", - inlay_kind: LetBinding, - }, - InlayHint { - range: [121; 125), - text: "let test = OuterStruct {};", - inlay_kind: LetBinding, - }, - InlayHint { - range: [152; 156), - text: "let test = vec![222];", - inlay_kind: LetBinding, - }, - InlayHint { - range: [178; 186), - text: "let mut test = Vec::new();", - inlay_kind: LetBinding, - }, - InlayHint { - range: [229; 233), - text: "let test = test.into_iter().map(|i| i * i).collect::>();", - inlay_kind: LetBinding, - }, - InlayHint { - range: [258; 259), - text: "i", - inlay_kind: ClosureParameter, - }, - InlayHint { - range: [297; 305), - text: "let mut test = 33;", - inlay_kind: LetBinding, - }, - InlayHint { - range: [417; 426), - text: "let i_squared = i * i;", - inlay_kind: LetBinding, - }, - InlayHint { - range: [500; 506), - text: "let (x, c) = (42, \'a\');", - inlay_kind: LetBinding, - }, - InlayHint { - range: [528; 532), - text: "let test = (42, \'a\');", - inlay_kind: LetBinding, - }, -] -- cgit v1.2.3 From 09c7c86696eb8289c9a8ab30bdbb824824c51eb1 Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Sun, 21 Jul 2019 23:28:05 +0300 Subject: Resolve types on the server --- crates/ra_ide_api/src/inlay_hints.rs | 126 ++++++++++++++++++++--------------- crates/ra_ide_api/src/lib.rs | 2 +- 2 files changed, 74 insertions(+), 54 deletions(-) (limited to 'crates/ra_ide_api') diff --git a/crates/ra_ide_api/src/inlay_hints.rs b/crates/ra_ide_api/src/inlay_hints.rs index 739a44b19..7d49ebcf4 100644 --- a/crates/ra_ide_api/src/inlay_hints.rs +++ b/crates/ra_ide_api/src/inlay_hints.rs @@ -1,3 +1,6 @@ +use crate::{db::RootDatabase, FileId}; +use hir::{HirDisplay, Ty}; +use ra_syntax::ast::Pat; use ra_syntax::{ algo::visit::{visitor, Visitor}, ast::{self, PatKind, TypeAscriptionOwner}, @@ -15,63 +18,101 @@ pub struct InlayHint { pub range: TextRange, pub text: SmolStr, pub inlay_kind: InlayKind, + pub inlay_type_string: String, } -pub(crate) fn inlay_hints(file: &SourceFile) -> Vec { - file.syntax().descendants().map(|node| get_inlay_hints(&node)).flatten().collect() +pub(crate) fn inlay_hints(db: &RootDatabase, file_id: FileId, file: &SourceFile) -> Vec { + file.syntax() + .descendants() + .map(|node| get_inlay_hints(db, file_id, &node).unwrap_or_default()) + .flatten() + .collect() } -fn get_inlay_hints(node: &SyntaxNode) -> Vec { +fn get_inlay_hints( + db: &RootDatabase, + file_id: FileId, + node: &SyntaxNode, +) -> Option> { visitor() .visit(|let_statement: ast::LetStmt| { let let_syntax = let_statement.syntax(); if let_statement.ascribed_type().is_some() { - return Vec::new(); + return None; } - let pat_range = match let_statement.pat().map(|pat| pat.kind()) { - Some(PatKind::BindPat(bind_pat)) => bind_pat.syntax().text_range(), - Some(PatKind::TuplePat(tuple_pat)) => tuple_pat.syntax().text_range(), - _ => return Vec::new(), + let let_pat = let_statement.pat()?; + let inlay_type_string = get_node_displayable_type(db, file_id, let_syntax, &let_pat)? + .display(db) + .to_string();; + + let pat_range = match let_pat.kind() { + PatKind::BindPat(bind_pat) => bind_pat.syntax().text_range(), + PatKind::TuplePat(tuple_pat) => tuple_pat.syntax().text_range(), + _ => return None, }; - vec![InlayHint { + Some(vec![InlayHint { range: pat_range, text: let_syntax.text().to_smol_string(), inlay_kind: InlayKind::LetBinding, - }] + inlay_type_string, + }]) }) - .visit(|closure_parameter: ast::LambdaExpr| { - if let Some(param_list) = closure_parameter.param_list() { + .visit(|closure_parameter: ast::LambdaExpr| match closure_parameter.param_list() { + Some(param_list) => Some( param_list .params() .filter(|closure_param| closure_param.ascribed_type().is_none()) - .map(|closure_param| { + .filter_map(|closure_param| { let closure_param_syntax = closure_param.syntax(); - InlayHint { + let inlay_type_string = get_node_displayable_type( + db, + file_id, + closure_param_syntax, + &closure_param.pat()?, + )? + .display(db) + .to_string(); + Some(InlayHint { range: closure_param_syntax.text_range(), text: closure_param_syntax.text().to_smol_string(), inlay_kind: InlayKind::ClosureParameter, - } + inlay_type_string, + }) }) - .collect() - } else { - Vec::new() - } + .collect(), + ), + None => None, }) - .accept(&node) - .unwrap_or_default() + .accept(&node)? +} + +fn get_node_displayable_type( + db: &RootDatabase, + file_id: FileId, + node_syntax: &SyntaxNode, + node_pat: &Pat, +) -> Option { + let analyzer = hir::SourceAnalyzer::new(db, file_id, node_syntax, None); + analyzer.type_of_pat(db, node_pat).and_then(|resolved_type| { + if let Ty::Apply(_) = resolved_type { + Some(resolved_type) + } else { + None + } + }) } #[cfg(test)] mod tests { - use super::*; + use crate::mock_analysis::single_file; use insta::assert_debug_snapshot_matches; #[test] fn test_inlay_hints() { - let file = SourceFile::parse( + let (analysis, file_id) = single_file( r#" struct OuterStruct {} @@ -99,66 +140,45 @@ fn main() { let (x, c) = (42, 'a'); let test = (42, 'a'); } - "#, - ) - .ok() - .unwrap(); - assert_debug_snapshot_matches!(inlay_hints(&file), @r#"[ + ); + + assert_debug_snapshot_matches!(analysis.inlay_hints(file_id).unwrap(), @r#"[ InlayHint { range: [71; 75), text: "let test = 54;", inlay_kind: LetBinding, - }, - InlayHint { - range: [90; 94), - text: "let test = InnerStruct {};", - inlay_kind: LetBinding, + inlay_type_string: "i32", }, InlayHint { range: [121; 125), text: "let test = OuterStruct {};", inlay_kind: LetBinding, - }, - InlayHint { - range: [152; 156), - text: "let test = vec![222];", - inlay_kind: LetBinding, - }, - InlayHint { - range: [178; 186), - text: "let mut test = Vec::new();", - inlay_kind: LetBinding, - }, - InlayHint { - range: [229; 233), - text: "let test = test.into_iter().map(|i| i * i).collect::>();", - inlay_kind: LetBinding, - }, - InlayHint { - range: [258; 259), - text: "i", - inlay_kind: ClosureParameter, + inlay_type_string: "OuterStruct", }, InlayHint { range: [297; 305), text: "let mut test = 33;", inlay_kind: LetBinding, + inlay_type_string: "i32", }, InlayHint { range: [417; 426), text: "let i_squared = i * i;", inlay_kind: LetBinding, + inlay_type_string: "u32", }, InlayHint { range: [496; 502), text: "let (x, c) = (42, \'a\');", inlay_kind: LetBinding, + inlay_type_string: "(i32, char)", }, InlayHint { range: [524; 528), text: "let test = (42, \'a\');", inlay_kind: LetBinding, + inlay_type_string: "(i32, char)", }, ]"# ); diff --git a/crates/ra_ide_api/src/lib.rs b/crates/ra_ide_api/src/lib.rs index 32168f12d..16ffb03ce 100644 --- a/crates/ra_ide_api/src/lib.rs +++ b/crates/ra_ide_api/src/lib.rs @@ -400,7 +400,7 @@ impl Analysis { /// Returns a list of the places in the file where type hints can be displayed. pub fn inlay_hints(&self, file_id: FileId) -> Cancelable> { - self.with_db(|db| inlay_hints::inlay_hints(&db.parse(file_id).tree())) + self.with_db(|db| inlay_hints::inlay_hints(db, file_id, &db.parse(file_id).tree())) } /// Returns the set of folding ranges. -- cgit v1.2.3 From d32774b4640daf708832e7e5c3b3c62031198beb Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Mon, 22 Jul 2019 00:16:07 +0300 Subject: Fix the string conversions --- crates/ra_ide_api/src/inlay_hints.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'crates/ra_ide_api') diff --git a/crates/ra_ide_api/src/inlay_hints.rs b/crates/ra_ide_api/src/inlay_hints.rs index 7d49ebcf4..4f959f4d3 100644 --- a/crates/ra_ide_api/src/inlay_hints.rs +++ b/crates/ra_ide_api/src/inlay_hints.rs @@ -55,7 +55,7 @@ fn get_inlay_hints( Some(vec![InlayHint { range: pat_range, - text: let_syntax.text().to_smol_string(), + text: let_syntax.text().to_string().into(), inlay_kind: InlayKind::LetBinding, inlay_type_string, }]) @@ -77,7 +77,7 @@ fn get_inlay_hints( .to_string(); Some(InlayHint { range: closure_param_syntax.text_range(), - text: closure_param_syntax.text().to_smol_string(), + text: closure_param_syntax.text().to_string().into(), inlay_kind: InlayKind::ClosureParameter, inlay_type_string, }) -- cgit v1.2.3 From 25398ad30d6ffc07b3ca9ceee6a55f301973c155 Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Mon, 22 Jul 2019 00:47:44 +0300 Subject: Use SmolStr for the type text --- crates/ra_ide_api/src/inlay_hints.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'crates/ra_ide_api') diff --git a/crates/ra_ide_api/src/inlay_hints.rs b/crates/ra_ide_api/src/inlay_hints.rs index 4f959f4d3..1b1f28951 100644 --- a/crates/ra_ide_api/src/inlay_hints.rs +++ b/crates/ra_ide_api/src/inlay_hints.rs @@ -18,7 +18,7 @@ pub struct InlayHint { pub range: TextRange, pub text: SmolStr, pub inlay_kind: InlayKind, - pub inlay_type_string: String, + pub inlay_type_string: SmolStr, } pub(crate) fn inlay_hints(db: &RootDatabase, file_id: FileId, file: &SourceFile) -> Vec { @@ -45,7 +45,8 @@ fn get_inlay_hints( let let_pat = let_statement.pat()?; let inlay_type_string = get_node_displayable_type(db, file_id, let_syntax, &let_pat)? .display(db) - .to_string();; + .to_string() + .into(); let pat_range = match let_pat.kind() { PatKind::BindPat(bind_pat) => bind_pat.syntax().text_range(), @@ -74,7 +75,9 @@ fn get_inlay_hints( &closure_param.pat()?, )? .display(db) - .to_string(); + .to_string() + .into(); + Some(InlayHint { range: closure_param_syntax.text_range(), text: closure_param_syntax.text().to_string().into(), -- cgit v1.2.3 From 8f3377d9f93a256f8e68ae183808fd767b529d18 Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Mon, 22 Jul 2019 21:52:47 +0300 Subject: Code review fixes --- crates/ra_ide_api/src/inlay_hints.rs | 49 +++++++++++++++--------------------- 1 file changed, 20 insertions(+), 29 deletions(-) (limited to 'crates/ra_ide_api') diff --git a/crates/ra_ide_api/src/inlay_hints.rs b/crates/ra_ide_api/src/inlay_hints.rs index 1b1f28951..174662beb 100644 --- a/crates/ra_ide_api/src/inlay_hints.rs +++ b/crates/ra_ide_api/src/inlay_hints.rs @@ -9,16 +9,15 @@ use ra_syntax::{ #[derive(Debug, PartialEq, Eq)] pub enum InlayKind { - LetBinding, - ClosureParameter, + LetBindingType, + ClosureParameterType, } #[derive(Debug)] pub struct InlayHint { pub range: TextRange, - pub text: SmolStr, - pub inlay_kind: InlayKind, - pub inlay_type_string: SmolStr, + pub kind: InlayKind, + pub label: SmolStr, } pub(crate) fn inlay_hints(db: &RootDatabase, file_id: FileId, file: &SourceFile) -> Vec { @@ -56,9 +55,8 @@ fn get_inlay_hints( Some(vec![InlayHint { range: pat_range, - text: let_syntax.text().to_string().into(), - inlay_kind: InlayKind::LetBinding, - inlay_type_string, + kind: InlayKind::LetBindingType, + label: inlay_type_string, }]) }) .visit(|closure_parameter: ast::LambdaExpr| match closure_parameter.param_list() { @@ -80,9 +78,8 @@ fn get_inlay_hints( Some(InlayHint { range: closure_param_syntax.text_range(), - text: closure_param_syntax.text().to_string().into(), - inlay_kind: InlayKind::ClosureParameter, - inlay_type_string, + kind: InlayKind::ClosureParameterType, + label: inlay_type_string, }) }) .collect(), @@ -149,39 +146,33 @@ fn main() { assert_debug_snapshot_matches!(analysis.inlay_hints(file_id).unwrap(), @r#"[ InlayHint { range: [71; 75), - text: "let test = 54;", - inlay_kind: LetBinding, - inlay_type_string: "i32", + kind: LetBindingType, + label: "i32", }, InlayHint { range: [121; 125), - text: "let test = OuterStruct {};", - inlay_kind: LetBinding, - inlay_type_string: "OuterStruct", + kind: LetBindingType, + label: "OuterStruct", }, InlayHint { range: [297; 305), - text: "let mut test = 33;", - inlay_kind: LetBinding, - inlay_type_string: "i32", + kind: LetBindingType, + label: "i32", }, InlayHint { range: [417; 426), - text: "let i_squared = i * i;", - inlay_kind: LetBinding, - inlay_type_string: "u32", + kind: LetBindingType, + label: "u32", }, InlayHint { range: [496; 502), - text: "let (x, c) = (42, \'a\');", - inlay_kind: LetBinding, - inlay_type_string: "(i32, char)", + kind: LetBindingType, + label: "(i32, char)", }, InlayHint { range: [524; 528), - text: "let test = (42, \'a\');", - inlay_kind: LetBinding, - inlay_type_string: "(i32, char)", + kind: LetBindingType, + label: "(i32, char)", }, ]"# ); -- cgit v1.2.3