diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2019-07-08 10:55:23 +0100 |
---|---|---|
committer | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2019-07-08 10:55:23 +0100 |
commit | 5ce2b4819ec37faa6b7ac1afe006ae03865ad544 (patch) | |
tree | e627fd15f070cd210c379f033b54ea004309d7ab /crates | |
parent | 5b19825e376b67aabbe8d5b163bf69b1acd92f04 (diff) | |
parent | 9c6e93cd6c108536015272b6a363ed5cc1d4fb44 (diff) |
Merge #1504
1504: Simplify LSP handlers r=matklad a=kjeremy
Takes advantage of protocol inheritance via composition and simplifies some responses via the `From`/`Into` traits.
Co-authored-by: Jeremy Kolb <[email protected]>
Diffstat (limited to 'crates')
-rw-r--r-- | crates/gen_lsp_server/Cargo.toml | 2 | ||||
-rw-r--r-- | crates/ra_lsp_server/Cargo.toml | 2 | ||||
-rw-r--r-- | crates/ra_lsp_server/src/main_loop/handlers.rs | 58 | ||||
-rw-r--r-- | crates/ra_lsp_server/tests/heavy_tests/main.rs | 6 |
4 files changed, 30 insertions, 38 deletions
diff --git a/crates/gen_lsp_server/Cargo.toml b/crates/gen_lsp_server/Cargo.toml index bf57df81f..36a29265c 100644 --- a/crates/gen_lsp_server/Cargo.toml +++ b/crates/gen_lsp_server/Cargo.toml | |||
@@ -8,7 +8,7 @@ license = "MIT OR Apache-2.0" | |||
8 | description = "Generic LSP server scaffold." | 8 | description = "Generic LSP server scaffold." |
9 | 9 | ||
10 | [dependencies] | 10 | [dependencies] |
11 | lsp-types = "0.58.0" | 11 | lsp-types = "0.59.0" |
12 | log = "0.4.3" | 12 | log = "0.4.3" |
13 | serde_json = "1.0.34" | 13 | serde_json = "1.0.34" |
14 | serde = { version = "1.0.83", features = ["derive"] } | 14 | serde = { version = "1.0.83", features = ["derive"] } |
diff --git a/crates/ra_lsp_server/Cargo.toml b/crates/ra_lsp_server/Cargo.toml index 2c4b3789e..50bc8111b 100644 --- a/crates/ra_lsp_server/Cargo.toml +++ b/crates/ra_lsp_server/Cargo.toml | |||
@@ -13,7 +13,7 @@ crossbeam-channel = "0.3.5" | |||
13 | flexi_logger = "0.13.0" | 13 | flexi_logger = "0.13.0" |
14 | log = "0.4.3" | 14 | log = "0.4.3" |
15 | url_serde = "0.2.0" | 15 | url_serde = "0.2.0" |
16 | lsp-types = { version = "0.58.0", features = ["proposed"] } | 16 | lsp-types = { version = "0.59.0", features = ["proposed"] } |
17 | rustc-hash = "1.0" | 17 | rustc-hash = "1.0" |
18 | parking_lot = "0.8.0" | 18 | parking_lot = "0.8.0" |
19 | 19 | ||
diff --git a/crates/ra_lsp_server/src/main_loop/handlers.rs b/crates/ra_lsp_server/src/main_loop/handlers.rs index a8d6f7c23..62c8cbf71 100644 --- a/crates/ra_lsp_server/src/main_loop/handlers.rs +++ b/crates/ra_lsp_server/src/main_loop/handlers.rs | |||
@@ -2,11 +2,11 @@ use std::{fmt::Write as _, io::Write as _}; | |||
2 | 2 | ||
3 | use gen_lsp_server::ErrorCode; | 3 | use gen_lsp_server::ErrorCode; |
4 | use lsp_types::{ | 4 | use lsp_types::{ |
5 | CodeAction, CodeActionResponse, CodeLens, Command, Diagnostic, DiagnosticSeverity, | 5 | CodeAction, CodeActionResponse, CodeLens, Command, CompletionItem, Diagnostic, |
6 | DocumentFormattingParams, DocumentHighlight, DocumentSymbol, FoldingRange, FoldingRangeKind, | 6 | DiagnosticSeverity, DocumentFormattingParams, DocumentHighlight, DocumentSymbol, FoldingRange, |
7 | FoldingRangeParams, Hover, HoverContents, Location, MarkupContent, MarkupKind, Position, | 7 | FoldingRangeKind, FoldingRangeParams, Hover, HoverContents, Location, MarkupContent, |
8 | PrepareRenameResponse, Range, RenameParams, SymbolInformation, TextDocumentIdentifier, | 8 | MarkupKind, Position, PrepareRenameResponse, Range, RenameParams, SymbolInformation, |
9 | TextEdit, WorkspaceEdit, | 9 | TextDocumentIdentifier, TextEdit, WorkspaceEdit, |
10 | }; | 10 | }; |
11 | use ra_ide_api::{ | 11 | use ra_ide_api::{ |
12 | AssistId, Cancelable, FileId, FilePosition, FileRange, FoldKind, Query, RangeInfo, | 12 | AssistId, Cancelable, FileId, FilePosition, FileRange, FoldKind, Query, RangeInfo, |
@@ -153,14 +153,12 @@ pub fn handle_on_type_formatting( | |||
153 | params: req::DocumentOnTypeFormattingParams, | 153 | params: req::DocumentOnTypeFormattingParams, |
154 | ) -> Result<Option<Vec<TextEdit>>> { | 154 | ) -> Result<Option<Vec<TextEdit>>> { |
155 | let _p = profile("handle_on_type_formatting"); | 155 | let _p = profile("handle_on_type_formatting"); |
156 | let file_id = params.text_document.try_conv_with(&world)?; | 156 | let mut position = params.text_document_position.try_conv_with(&world)?; |
157 | let line_index = world.analysis().file_line_index(file_id); | 157 | let line_index = world.analysis().file_line_index(position.file_id); |
158 | let position = FilePosition { | 158 | |
159 | file_id, | 159 | // in `ra_ide_api`, the `on_type` invariant is that |
160 | /// in `ra_ide_api`, the `on_type` invariant is that | 160 | // `text.char_at(position) == typed_char`. |
161 | /// `text.char_at(position) == typed_char`. | 161 | position.offset = position.offset - TextUnit::of_char('.'); |
162 | offset: params.position.conv_with(&line_index) - TextUnit::of_char('.'), | ||
163 | }; | ||
164 | 162 | ||
165 | let edit = match params.ch.as_str() { | 163 | let edit = match params.ch.as_str() { |
166 | "=" => world.analysis().on_eq_typed(position), | 164 | "=" => world.analysis().on_eq_typed(position), |
@@ -214,7 +212,7 @@ pub fn handle_document_symbol( | |||
214 | } | 212 | } |
215 | } | 213 | } |
216 | 214 | ||
217 | Ok(Some(req::DocumentSymbolResponse::Nested(res))) | 215 | Ok(Some(res.into())) |
218 | } | 216 | } |
219 | 217 | ||
220 | pub fn handle_workspace_symbol( | 218 | pub fn handle_workspace_symbol( |
@@ -277,7 +275,7 @@ pub fn handle_goto_definition( | |||
277 | .map(|nav| RangeInfo::new(nav_range, nav)) | 275 | .map(|nav| RangeInfo::new(nav_range, nav)) |
278 | .map(|nav| to_location_link(&nav, &world, &line_index)) | 276 | .map(|nav| to_location_link(&nav, &world, &line_index)) |
279 | .collect::<Result<Vec<_>>>()?; | 277 | .collect::<Result<Vec<_>>>()?; |
280 | Ok(Some(req::GotoDefinitionResponse::Link(res))) | 278 | Ok(Some(res.into())) |
281 | } | 279 | } |
282 | 280 | ||
283 | pub fn handle_goto_implementation( | 281 | pub fn handle_goto_implementation( |
@@ -297,7 +295,7 @@ pub fn handle_goto_implementation( | |||
297 | .map(|nav| RangeInfo::new(nav_range, nav)) | 295 | .map(|nav| RangeInfo::new(nav_range, nav)) |
298 | .map(|nav| to_location_link(&nav, &world, &line_index)) | 296 | .map(|nav| to_location_link(&nav, &world, &line_index)) |
299 | .collect::<Result<Vec<_>>>()?; | 297 | .collect::<Result<Vec<_>>>()?; |
300 | Ok(Some(req::GotoDefinitionResponse::Link(res))) | 298 | Ok(Some(res.into())) |
301 | } | 299 | } |
302 | 300 | ||
303 | pub fn handle_goto_type_definition( | 301 | pub fn handle_goto_type_definition( |
@@ -317,7 +315,7 @@ pub fn handle_goto_type_definition( | |||
317 | .map(|nav| RangeInfo::new(nav_range, nav)) | 315 | .map(|nav| RangeInfo::new(nav_range, nav)) |
318 | .map(|nav| to_location_link(&nav, &world, &line_index)) | 316 | .map(|nav| to_location_link(&nav, &world, &line_index)) |
319 | .collect::<Result<Vec<_>>>()?; | 317 | .collect::<Result<Vec<_>>>()?; |
320 | Ok(Some(req::GotoDefinitionResponse::Link(res))) | 318 | Ok(Some(res.into())) |
321 | } | 319 | } |
322 | 320 | ||
323 | pub fn handle_parent_module( | 321 | pub fn handle_parent_module( |
@@ -407,12 +405,7 @@ pub fn handle_completion( | |||
407 | params: req::CompletionParams, | 405 | params: req::CompletionParams, |
408 | ) -> Result<Option<req::CompletionResponse>> { | 406 | ) -> Result<Option<req::CompletionResponse>> { |
409 | let _p = profile("handle_completion"); | 407 | let _p = profile("handle_completion"); |
410 | let position = { | 408 | let position = params.text_document_position.try_conv_with(&world)?; |
411 | let file_id = params.text_document.try_conv_with(&world)?; | ||
412 | let line_index = world.analysis().file_line_index(file_id); | ||
413 | let offset = params.position.conv_with(&line_index); | ||
414 | FilePosition { file_id, offset } | ||
415 | }; | ||
416 | let completion_triggered_after_single_colon = { | 409 | let completion_triggered_after_single_colon = { |
417 | let mut res = false; | 410 | let mut res = false; |
418 | if let Some(ctx) = params.context { | 411 | if let Some(ctx) = params.context { |
@@ -440,9 +433,10 @@ pub fn handle_completion( | |||
440 | Some(items) => items, | 433 | Some(items) => items, |
441 | }; | 434 | }; |
442 | let line_index = world.analysis().file_line_index(position.file_id); | 435 | let line_index = world.analysis().file_line_index(position.file_id); |
443 | let items = items.into_iter().map(|item| item.conv_with(&line_index)).collect(); | 436 | let items: Vec<CompletionItem> = |
437 | items.into_iter().map(|item| item.conv_with(&line_index)).collect(); | ||
444 | 438 | ||
445 | Ok(Some(req::CompletionResponse::Array(items))) | 439 | Ok(Some(items.into())) |
446 | } | 440 | } |
447 | 441 | ||
448 | pub fn handle_folding_range( | 442 | pub fn handle_folding_range( |
@@ -543,9 +537,7 @@ pub fn handle_prepare_rename( | |||
543 | } | 537 | } |
544 | 538 | ||
545 | pub fn handle_rename(world: WorldSnapshot, params: RenameParams) -> Result<Option<WorkspaceEdit>> { | 539 | pub fn handle_rename(world: WorldSnapshot, params: RenameParams) -> Result<Option<WorkspaceEdit>> { |
546 | let file_id = params.text_document.try_conv_with(&world)?; | 540 | let position = params.text_document_position.try_conv_with(&world)?; |
547 | let line_index = world.analysis().file_line_index(file_id); | ||
548 | let offset = params.position.conv_with(&line_index); | ||
549 | 541 | ||
550 | if params.new_name.is_empty() { | 542 | if params.new_name.is_empty() { |
551 | return Err(LspError::new( | 543 | return Err(LspError::new( |
@@ -555,8 +547,7 @@ pub fn handle_rename(world: WorldSnapshot, params: RenameParams) -> Result<Optio | |||
555 | .into()); | 547 | .into()); |
556 | } | 548 | } |
557 | 549 | ||
558 | let optional_change = | 550 | let optional_change = world.analysis().rename(position, &*params.new_name)?; |
559 | world.analysis().rename(FilePosition { file_id, offset }, &*params.new_name)?; | ||
560 | let change = match optional_change { | 551 | let change = match optional_change { |
561 | None => return Ok(None), | 552 | None => return Ok(None), |
562 | Some(it) => it, | 553 | Some(it) => it, |
@@ -571,11 +562,10 @@ pub fn handle_references( | |||
571 | world: WorldSnapshot, | 562 | world: WorldSnapshot, |
572 | params: req::ReferenceParams, | 563 | params: req::ReferenceParams, |
573 | ) -> Result<Option<Vec<Location>>> { | 564 | ) -> Result<Option<Vec<Location>>> { |
574 | let file_id = params.text_document.try_conv_with(&world)?; | 565 | let position = params.text_document_position.try_conv_with(&world)?; |
575 | let line_index = world.analysis().file_line_index(file_id); | 566 | let line_index = world.analysis().file_line_index(position.file_id); |
576 | let offset = params.position.conv_with(&line_index); | ||
577 | 567 | ||
578 | let refs = match world.analysis().find_all_refs(FilePosition { file_id, offset })? { | 568 | let refs = match world.analysis().find_all_refs(position)? { |
579 | None => return Ok(None), | 569 | None => return Ok(None), |
580 | Some(refs) => refs, | 570 | Some(refs) => refs, |
581 | }; | 571 | }; |
diff --git a/crates/ra_lsp_server/tests/heavy_tests/main.rs b/crates/ra_lsp_server/tests/heavy_tests/main.rs index d271b02fc..451be32a8 100644 --- a/crates/ra_lsp_server/tests/heavy_tests/main.rs +++ b/crates/ra_lsp_server/tests/heavy_tests/main.rs | |||
@@ -37,9 +37,11 @@ use std::collections::Spam; | |||
37 | eprintln!("loading took {:?}", project_start.elapsed()); | 37 | eprintln!("loading took {:?}", project_start.elapsed()); |
38 | let completion_start = Instant::now(); | 38 | let completion_start = Instant::now(); |
39 | let res = server.send_request::<Completion>(CompletionParams { | 39 | let res = server.send_request::<Completion>(CompletionParams { |
40 | text_document: server.doc_id("src/lib.rs"), | 40 | text_document_position: TextDocumentPositionParams::new( |
41 | server.doc_id("src/lib.rs"), | ||
42 | Position::new(0, 23), | ||
43 | ), | ||
41 | context: None, | 44 | context: None, |
42 | position: Position::new(0, 23), | ||
43 | }); | 45 | }); |
44 | assert!(format!("{}", res).contains("HashMap")); | 46 | assert!(format!("{}", res).contains("HashMap")); |
45 | eprintln!("completion took {:?}", completion_start.elapsed()); | 47 | eprintln!("completion took {:?}", completion_start.elapsed()); |