diff options
Diffstat (limited to 'crates')
-rw-r--r-- | crates/ra_ide_api/src/inlay_hints.rs | 49 | ||||
-rw-r--r-- | crates/ra_lsp_server/src/main_loop.rs | 1 | ||||
-rw-r--r-- | crates/ra_lsp_server/src/main_loop/handlers.rs | 23 | ||||
-rw-r--r-- | crates/ra_lsp_server/src/req.rs | 27 |
4 files changed, 70 insertions, 30 deletions
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::{ | |||
9 | 9 | ||
10 | #[derive(Debug, PartialEq, Eq)] | 10 | #[derive(Debug, PartialEq, Eq)] |
11 | pub enum InlayKind { | 11 | pub enum InlayKind { |
12 | LetBinding, | 12 | LetBindingType, |
13 | ClosureParameter, | 13 | ClosureParameterType, |
14 | } | 14 | } |
15 | 15 | ||
16 | #[derive(Debug)] | 16 | #[derive(Debug)] |
17 | pub struct InlayHint { | 17 | pub struct InlayHint { |
18 | pub range: TextRange, | 18 | pub range: TextRange, |
19 | pub text: SmolStr, | 19 | pub kind: InlayKind, |
20 | pub inlay_kind: InlayKind, | 20 | pub label: SmolStr, |
21 | pub inlay_type_string: SmolStr, | ||
22 | } | 21 | } |
23 | 22 | ||
24 | pub(crate) fn inlay_hints(db: &RootDatabase, file_id: FileId, file: &SourceFile) -> Vec<InlayHint> { | 23 | pub(crate) fn inlay_hints(db: &RootDatabase, file_id: FileId, file: &SourceFile) -> Vec<InlayHint> { |
@@ -56,9 +55,8 @@ fn get_inlay_hints( | |||
56 | 55 | ||
57 | Some(vec![InlayHint { | 56 | Some(vec![InlayHint { |
58 | range: pat_range, | 57 | range: pat_range, |
59 | text: let_syntax.text().to_string().into(), | 58 | kind: InlayKind::LetBindingType, |
60 | inlay_kind: InlayKind::LetBinding, | 59 | label: inlay_type_string, |
61 | inlay_type_string, | ||
62 | }]) | 60 | }]) |
63 | }) | 61 | }) |
64 | .visit(|closure_parameter: ast::LambdaExpr| match closure_parameter.param_list() { | 62 | .visit(|closure_parameter: ast::LambdaExpr| match closure_parameter.param_list() { |
@@ -80,9 +78,8 @@ fn get_inlay_hints( | |||
80 | 78 | ||
81 | Some(InlayHint { | 79 | Some(InlayHint { |
82 | range: closure_param_syntax.text_range(), | 80 | range: closure_param_syntax.text_range(), |
83 | text: closure_param_syntax.text().to_string().into(), | 81 | kind: InlayKind::ClosureParameterType, |
84 | inlay_kind: InlayKind::ClosureParameter, | 82 | label: inlay_type_string, |
85 | inlay_type_string, | ||
86 | }) | 83 | }) |
87 | }) | 84 | }) |
88 | .collect(), | 85 | .collect(), |
@@ -149,39 +146,33 @@ fn main() { | |||
149 | assert_debug_snapshot_matches!(analysis.inlay_hints(file_id).unwrap(), @r#"[ | 146 | assert_debug_snapshot_matches!(analysis.inlay_hints(file_id).unwrap(), @r#"[ |
150 | InlayHint { | 147 | InlayHint { |
151 | range: [71; 75), | 148 | range: [71; 75), |
152 | text: "let test = 54;", | 149 | kind: LetBindingType, |
153 | inlay_kind: LetBinding, | 150 | label: "i32", |
154 | inlay_type_string: "i32", | ||
155 | }, | 151 | }, |
156 | InlayHint { | 152 | InlayHint { |
157 | range: [121; 125), | 153 | range: [121; 125), |
158 | text: "let test = OuterStruct {};", | 154 | kind: LetBindingType, |
159 | inlay_kind: LetBinding, | 155 | label: "OuterStruct", |
160 | inlay_type_string: "OuterStruct", | ||
161 | }, | 156 | }, |
162 | InlayHint { | 157 | InlayHint { |
163 | range: [297; 305), | 158 | range: [297; 305), |
164 | text: "let mut test = 33;", | 159 | kind: LetBindingType, |
165 | inlay_kind: LetBinding, | 160 | label: "i32", |
166 | inlay_type_string: "i32", | ||
167 | }, | 161 | }, |
168 | InlayHint { | 162 | InlayHint { |
169 | range: [417; 426), | 163 | range: [417; 426), |
170 | text: "let i_squared = i * i;", | 164 | kind: LetBindingType, |
171 | inlay_kind: LetBinding, | 165 | label: "u32", |
172 | inlay_type_string: "u32", | ||
173 | }, | 166 | }, |
174 | InlayHint { | 167 | InlayHint { |
175 | range: [496; 502), | 168 | range: [496; 502), |
176 | text: "let (x, c) = (42, \'a\');", | 169 | kind: LetBindingType, |
177 | inlay_kind: LetBinding, | 170 | label: "(i32, char)", |
178 | inlay_type_string: "(i32, char)", | ||
179 | }, | 171 | }, |
180 | InlayHint { | 172 | InlayHint { |
181 | range: [524; 528), | 173 | range: [524; 528), |
182 | text: "let test = (42, \'a\');", | 174 | kind: LetBindingType, |
183 | inlay_kind: LetBinding, | 175 | label: "(i32, char)", |
184 | inlay_type_string: "(i32, char)", | ||
185 | }, | 176 | }, |
186 | ]"# | 177 | ]"# |
187 | ); | 178 | ); |
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( | |||
362 | .on::<req::References>(handlers::handle_references)? | 362 | .on::<req::References>(handlers::handle_references)? |
363 | .on::<req::Formatting>(handlers::handle_formatting)? | 363 | .on::<req::Formatting>(handlers::handle_formatting)? |
364 | .on::<req::DocumentHighlightRequest>(handlers::handle_document_highlight)? | 364 | .on::<req::DocumentHighlightRequest>(handlers::handle_document_highlight)? |
365 | .on::<req::InlayHints>(handlers::handle_inlay_hints)? | ||
365 | .finish(); | 366 | .finish(); |
366 | Ok(()) | 367 | Ok(()) |
367 | } | 368 | } |
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; | |||
21 | use crate::{ | 21 | use crate::{ |
22 | cargo_target_spec::{runnable_args, CargoTargetSpec}, | 22 | cargo_target_spec::{runnable_args, CargoTargetSpec}, |
23 | conv::{to_location, Conv, ConvWith, MapConvWith, TryConvWith, TryConvWithToVec}, | 23 | conv::{to_location, Conv, ConvWith, MapConvWith, TryConvWith, TryConvWithToVec}, |
24 | req::{self, Decoration}, | 24 | req::{self, Decoration, InlayHint, InlayHintsParams, InlayKind}, |
25 | world::WorldSnapshot, | 25 | world::WorldSnapshot, |
26 | LspError, Result, | 26 | LspError, Result, |
27 | }; | 27 | }; |
@@ -874,3 +874,24 @@ fn to_diagnostic_severity(severity: Severity) -> DiagnosticSeverity { | |||
874 | WeakWarning => DiagnosticSeverity::Hint, | 874 | WeakWarning => DiagnosticSeverity::Hint, |
875 | } | 875 | } |
876 | } | 876 | } |
877 | |||
878 | pub fn handle_inlay_hints( | ||
879 | world: WorldSnapshot, | ||
880 | params: InlayHintsParams, | ||
881 | ) -> Result<Vec<InlayHint>> { | ||
882 | let file_id = params.text_document.try_conv_with(&world)?; | ||
883 | let analysis = world.analysis(); | ||
884 | let line_index = analysis.file_line_index(file_id); | ||
885 | Ok(analysis | ||
886 | .inlay_hints(file_id)? | ||
887 | .into_iter() | ||
888 | .map(|api_type| InlayHint { | ||
889 | label: api_type.label.to_string(), | ||
890 | range: api_type.range.conv_with(&line_index), | ||
891 | kind: match api_type.kind { | ||
892 | ra_ide_api::InlayKind::LetBindingType => InlayKind::LetBindingType, | ||
893 | ra_ide_api::InlayKind::ClosureParameterType => InlayKind::ClosureParameterType, | ||
894 | }, | ||
895 | }) | ||
896 | .collect()) | ||
897 | } | ||
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 { | |||
196 | pub workspace_edit: WorkspaceEdit, | 196 | pub workspace_edit: WorkspaceEdit, |
197 | pub cursor_position: Option<TextDocumentPositionParams>, | 197 | pub cursor_position: Option<TextDocumentPositionParams>, |
198 | } | 198 | } |
199 | |||
200 | pub enum InlayHints {} | ||
201 | |||
202 | impl Request for InlayHints { | ||
203 | type Params = InlayHintsParams; | ||
204 | type Result = Vec<InlayHint>; | ||
205 | const METHOD: &'static str = "rust-analyzer/inlayHints"; | ||
206 | } | ||
207 | |||
208 | #[derive(Serialize, Deserialize, Debug)] | ||
209 | #[serde(rename_all = "camelCase")] | ||
210 | pub struct InlayHintsParams { | ||
211 | pub text_document: TextDocumentIdentifier, | ||
212 | } | ||
213 | |||
214 | #[derive(Debug, PartialEq, Eq, Deserialize, Serialize)] | ||
215 | pub enum InlayKind { | ||
216 | LetBindingType, | ||
217 | ClosureParameterType, | ||
218 | } | ||
219 | |||
220 | #[derive(Debug, Deserialize, Serialize)] | ||
221 | pub struct InlayHint { | ||
222 | pub range: Range, | ||
223 | pub kind: InlayKind, | ||
224 | pub label: String, | ||
225 | } | ||