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_lsp_server/src/main_loop/handlers.rs | 42 ++++++++++++++++---------- 1 file changed, 26 insertions(+), 16 deletions(-) (limited to 'crates/ra_lsp_server/src') diff --git a/crates/ra_lsp_server/src/main_loop/handlers.rs b/crates/ra_lsp_server/src/main_loop/handlers.rs index 68865b755..ea947417f 100644 --- a/crates/ra_lsp_server/src/main_loop/handlers.rs +++ b/crates/ra_lsp_server/src/main_loop/handlers.rs @@ -726,29 +726,39 @@ pub fn handle_code_lens( } } - // Handle impls - lenses.extend( - world - .analysis() - .file_structure(file_id) - .into_iter() - .filter(|it| match it.kind { - SyntaxKind::TRAIT_DEF | SyntaxKind::STRUCT_DEF | SyntaxKind::ENUM_DEF => true, - _ => false, - }) - .map(|it| { + lenses.extend(world.analysis().file_structure(file_id).into_iter().filter_map(|it| { + match it.kind { + // Handle impls + SyntaxKind::TRAIT_DEF | SyntaxKind::STRUCT_DEF | SyntaxKind::ENUM_DEF => { let range = it.node_range.conv_with(&line_index); let pos = range.start; let lens_params = req::TextDocumentPositionParams::new(params.text_document.clone(), pos); - CodeLens { + Some(CodeLens { range, command: None, data: Some(to_value(CodeLensResolveData::Impls(lens_params)).unwrap()), - } - }), - ); - + }) + } + // handle let statements + SyntaxKind::LET_STMT => world + .analysis() + .type_of(FileRange { range: it.navigation_range, file_id }) + .ok() + .and_then(std::convert::identity) + .filter(|resolved_type| "{unknown}" != resolved_type) + .map(|resolved_type| CodeLens { + range: it.node_range.conv_with(&line_index), + command: Some(Command { + title: resolved_type, + command: String::new(), + arguments: None, + }), + data: None, + }), + _ => None, + } + })); Ok(Some(lenses)) } -- 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_lsp_server/src/main_loop/handlers.rs | 70 +++++++++++++++----------- 1 file changed, 42 insertions(+), 28 deletions(-) (limited to 'crates/ra_lsp_server/src') diff --git a/crates/ra_lsp_server/src/main_loop/handlers.rs b/crates/ra_lsp_server/src/main_loop/handlers.rs index ea947417f..1077aafd8 100644 --- a/crates/ra_lsp_server/src/main_loop/handlers.rs +++ b/crates/ra_lsp_server/src/main_loop/handlers.rs @@ -9,7 +9,8 @@ use lsp_types::{ TextDocumentIdentifier, TextEdit, WorkspaceEdit, }; use ra_ide_api::{ - AssistId, Cancelable, FileId, FilePosition, FileRange, FoldKind, Query, RunnableKind, Severity, + AssistId, Cancelable, FileId, FilePosition, FileRange, FoldKind, InlayKind, Query, + RunnableKind, Severity, }; use ra_prof::profile; use ra_syntax::{AstNode, SyntaxKind, TextRange, TextUnit}; @@ -685,13 +686,14 @@ pub fn handle_code_lens( params: req::CodeLensParams, ) -> Result>> { let file_id = params.text_document.try_conv_with(&world)?; - let line_index = world.analysis().file_line_index(file_id); + let analysis = world.analysis(); + let line_index = analysis.file_line_index(file_id); let mut lenses: Vec = Default::default(); let workspace_root = world.workspace_root_for(file_id); // Gather runnables - for runnable in world.analysis().runnables(file_id)? { + for runnable in analysis.runnables(file_id)? { let title = match &runnable.kind { RunnableKind::Test { .. } | RunnableKind::TestMod { .. } => Some("▶️Run Test"), RunnableKind::Bench { .. } => Some("Run Bench"), @@ -726,39 +728,51 @@ pub fn handle_code_lens( } } - lenses.extend(world.analysis().file_structure(file_id).into_iter().filter_map(|it| { - match it.kind { - // Handle impls - SyntaxKind::TRAIT_DEF | SyntaxKind::STRUCT_DEF | SyntaxKind::ENUM_DEF => { + // Handle impls + lenses.extend( + analysis + .file_structure(file_id) + .into_iter() + .filter(|it| match it.kind { + SyntaxKind::TRAIT_DEF | SyntaxKind::STRUCT_DEF | SyntaxKind::ENUM_DEF => true, + _ => false, + }) + .map(|it| { let range = it.node_range.conv_with(&line_index); let pos = range.start; let lens_params = req::TextDocumentPositionParams::new(params.text_document.clone(), pos); - Some(CodeLens { + CodeLens { range, command: None, data: Some(to_value(CodeLensResolveData::Impls(lens_params)).unwrap()), - }) - } - // handle let statements - SyntaxKind::LET_STMT => world - .analysis() - .type_of(FileRange { range: it.navigation_range, file_id }) - .ok() - .and_then(std::convert::identity) - .filter(|resolved_type| "{unknown}" != resolved_type) - .map(|resolved_type| CodeLens { - range: it.node_range.conv_with(&line_index), - command: Some(Command { - title: resolved_type, - command: String::new(), - arguments: None, - }), - data: None, + } + }), + ); + + lenses.extend( + analysis + .inlay_hints(file_id) + .into_iter() + .filter(|hint| hint.inlay_kind == InlayKind::LetBinding) + .filter_map(|inlay_hint| { + let resolved_type = analysis + .type_of(FileRange { range: inlay_hint.range, file_id }) + .ok() + .and_then(std::convert::identity) + .filter(|resolved_type| "{unknown}" != resolved_type); + resolved_type.map(|resolved_type| (resolved_type, inlay_hint.range)) + }) + .map(|(resolved_type, range)| CodeLens { + range: range.conv_with(&line_index), + command: Some(Command { + title: resolved_type, + command: String::new(), + arguments: None, }), - _ => None, - } - })); + data: None, + }), + ); Ok(Some(lenses)) } -- 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_lsp_server/src/main_loop/handlers.rs | 35 ++++++++------------------ 1 file changed, 10 insertions(+), 25 deletions(-) (limited to 'crates/ra_lsp_server/src') diff --git a/crates/ra_lsp_server/src/main_loop/handlers.rs b/crates/ra_lsp_server/src/main_loop/handlers.rs index 1077aafd8..e5d2ff832 100644 --- a/crates/ra_lsp_server/src/main_loop/handlers.rs +++ b/crates/ra_lsp_server/src/main_loop/handlers.rs @@ -9,8 +9,7 @@ use lsp_types::{ TextDocumentIdentifier, TextEdit, WorkspaceEdit, }; use ra_ide_api::{ - AssistId, Cancelable, FileId, FilePosition, FileRange, FoldKind, InlayKind, Query, - RunnableKind, Severity, + AssistId, Cancelable, FileId, FilePosition, FileRange, FoldKind, Query, RunnableKind, Severity, }; use ra_prof::profile; use ra_syntax::{AstNode, SyntaxKind, TextRange, TextUnit}; @@ -750,29 +749,15 @@ pub fn handle_code_lens( }), ); - lenses.extend( - analysis - .inlay_hints(file_id) - .into_iter() - .filter(|hint| hint.inlay_kind == InlayKind::LetBinding) - .filter_map(|inlay_hint| { - let resolved_type = analysis - .type_of(FileRange { range: inlay_hint.range, file_id }) - .ok() - .and_then(std::convert::identity) - .filter(|resolved_type| "{unknown}" != resolved_type); - resolved_type.map(|resolved_type| (resolved_type, inlay_hint.range)) - }) - .map(|(resolved_type, range)| CodeLens { - range: range.conv_with(&line_index), - command: Some(Command { - title: resolved_type, - command: String::new(), - arguments: None, - }), - data: None, - }), - ); + lenses.extend(analysis.inlay_hints(file_id)?.into_iter().map(|inlay_hint| CodeLens { + range: inlay_hint.range.conv_with(&line_index), + command: Some(Command { + title: inlay_hint.inlay_type_string, + command: String::new(), + arguments: None, + }), + data: None, + })); Ok(Some(lenses)) } -- cgit v1.2.3 From ba76017d2eb1b7606106c15478ac658dc32b6dbd Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Sun, 21 Jul 2019 23:48:54 +0300 Subject: Do not show the lens with type hints --- crates/ra_lsp_server/src/main_loop/handlers.rs | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) (limited to 'crates/ra_lsp_server/src') diff --git a/crates/ra_lsp_server/src/main_loop/handlers.rs b/crates/ra_lsp_server/src/main_loop/handlers.rs index e5d2ff832..68865b755 100644 --- a/crates/ra_lsp_server/src/main_loop/handlers.rs +++ b/crates/ra_lsp_server/src/main_loop/handlers.rs @@ -685,14 +685,13 @@ pub fn handle_code_lens( params: req::CodeLensParams, ) -> Result>> { let file_id = params.text_document.try_conv_with(&world)?; - let analysis = world.analysis(); - let line_index = analysis.file_line_index(file_id); + let line_index = world.analysis().file_line_index(file_id); let mut lenses: Vec = Default::default(); let workspace_root = world.workspace_root_for(file_id); // Gather runnables - for runnable in analysis.runnables(file_id)? { + for runnable in world.analysis().runnables(file_id)? { let title = match &runnable.kind { RunnableKind::Test { .. } | RunnableKind::TestMod { .. } => Some("▶️Run Test"), RunnableKind::Bench { .. } => Some("Run Bench"), @@ -729,7 +728,8 @@ pub fn handle_code_lens( // Handle impls lenses.extend( - analysis + world + .analysis() .file_structure(file_id) .into_iter() .filter(|it| match it.kind { @@ -749,15 +749,6 @@ pub fn handle_code_lens( }), ); - lenses.extend(analysis.inlay_hints(file_id)?.into_iter().map(|inlay_hint| CodeLens { - range: inlay_hint.range.conv_with(&line_index), - command: Some(Command { - title: inlay_hint.inlay_type_string, - command: String::new(), - arguments: None, - }), - data: None, - })); Ok(Some(lenses)) } -- 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_lsp_server/src/main_loop.rs | 1 + crates/ra_lsp_server/src/main_loop/handlers.rs | 23 +++++++++++++++++++++- crates/ra_lsp_server/src/req.rs | 27 ++++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 1 deletion(-) (limited to 'crates/ra_lsp_server/src') diff --git a/crates/ra_lsp_server/src/main_loop.rs b/crates/ra_lsp_server/src/main_loop.rs index 668d2fd72..8e830c8b8 100644 --- a/crates/ra_lsp_server/src/main_loop.rs +++ b/crates/ra_lsp_server/src/main_loop.rs @@ -362,6 +362,7 @@ fn on_request( .on::(handlers::handle_references)? .on::(handlers::handle_formatting)? .on::(handlers::handle_document_highlight)? + .on::(handlers::handle_inlay_hints)? .finish(); Ok(()) } diff --git a/crates/ra_lsp_server/src/main_loop/handlers.rs b/crates/ra_lsp_server/src/main_loop/handlers.rs index 68865b755..5bf950a53 100644 --- a/crates/ra_lsp_server/src/main_loop/handlers.rs +++ b/crates/ra_lsp_server/src/main_loop/handlers.rs @@ -21,7 +21,7 @@ use url_serde::Ser; use crate::{ cargo_target_spec::{runnable_args, CargoTargetSpec}, conv::{to_location, Conv, ConvWith, MapConvWith, TryConvWith, TryConvWithToVec}, - req::{self, Decoration}, + req::{self, Decoration, InlayHint, InlayHintsParams, InlayKind}, world::WorldSnapshot, LspError, Result, }; @@ -874,3 +874,24 @@ fn to_diagnostic_severity(severity: Severity) -> DiagnosticSeverity { WeakWarning => DiagnosticSeverity::Hint, } } + +pub fn handle_inlay_hints( + world: WorldSnapshot, + params: InlayHintsParams, +) -> Result> { + let file_id = params.text_document.try_conv_with(&world)?; + let analysis = world.analysis(); + let line_index = analysis.file_line_index(file_id); + Ok(analysis + .inlay_hints(file_id)? + .into_iter() + .map(|api_type| InlayHint { + label: api_type.label.to_string(), + range: api_type.range.conv_with(&line_index), + kind: match api_type.kind { + ra_ide_api::InlayKind::LetBindingType => InlayKind::LetBindingType, + ra_ide_api::InlayKind::ClosureParameterType => InlayKind::ClosureParameterType, + }, + }) + .collect()) +} diff --git a/crates/ra_lsp_server/src/req.rs b/crates/ra_lsp_server/src/req.rs index 8d39b04a7..916185f99 100644 --- a/crates/ra_lsp_server/src/req.rs +++ b/crates/ra_lsp_server/src/req.rs @@ -196,3 +196,30 @@ pub struct SourceChange { pub workspace_edit: WorkspaceEdit, pub cursor_position: Option, } + +pub enum InlayHints {} + +impl Request for InlayHints { + type Params = InlayHintsParams; + type Result = Vec; + const METHOD: &'static str = "rust-analyzer/inlayHints"; +} + +#[derive(Serialize, Deserialize, Debug)] +#[serde(rename_all = "camelCase")] +pub struct InlayHintsParams { + pub text_document: TextDocumentIdentifier, +} + +#[derive(Debug, PartialEq, Eq, Deserialize, Serialize)] +pub enum InlayKind { + LetBindingType, + ClosureParameterType, +} + +#[derive(Debug, Deserialize, Serialize)] +pub struct InlayHint { + pub range: Range, + pub kind: InlayKind, + pub label: String, +} -- cgit v1.2.3