aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_lsp_server/src/main_loop/handlers.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_lsp_server/src/main_loop/handlers.rs')
-rw-r--r--crates/ra_lsp_server/src/main_loop/handlers.rs78
1 files changed, 78 insertions, 0 deletions
diff --git a/crates/ra_lsp_server/src/main_loop/handlers.rs b/crates/ra_lsp_server/src/main_loop/handlers.rs
index c25b63852..639fe4553 100644
--- a/crates/ra_lsp_server/src/main_loop/handlers.rs
+++ b/crates/ra_lsp_server/src/main_loop/handlers.rs
@@ -4,6 +4,7 @@ use languageserver_types::{
4 CodeActionResponse, Command, CompletionItem, CompletionItemKind, Diagnostic, 4 CodeActionResponse, Command, CompletionItem, CompletionItemKind, Diagnostic,
5 DiagnosticSeverity, DocumentSymbol, FoldingRange, FoldingRangeKind, FoldingRangeParams, 5 DiagnosticSeverity, DocumentSymbol, FoldingRange, FoldingRangeKind, FoldingRangeParams,
6 InsertTextFormat, Location, Position, SymbolInformation, TextDocumentIdentifier, TextEdit, 6 InsertTextFormat, Location, Position, SymbolInformation, TextDocumentIdentifier, TextEdit,
7 RenameParams, WorkspaceEdit, PrepareRenameResponse
7}; 8};
8use ra_analysis::{FileId, FoldKind, JobToken, Query, RunnableKind}; 9use ra_analysis::{FileId, FoldKind, JobToken, Query, RunnableKind};
9use ra_syntax::text_utils::contains_offset_nonstrict; 10use ra_syntax::text_utils::contains_offset_nonstrict;
@@ -17,6 +18,8 @@ use crate::{
17 Result, 18 Result,
18}; 19};
19 20
21use std::collections::HashMap;
22
20pub fn handle_syntax_tree( 23pub fn handle_syntax_tree(
21 world: ServerWorld, 24 world: ServerWorld,
22 params: req::SyntaxTreeParams, 25 params: req::SyntaxTreeParams,
@@ -460,6 +463,81 @@ pub fn handle_signature_help(
460 } 463 }
461} 464}
462 465
466pub fn handle_prepare_rename(
467 world: ServerWorld,
468 params: req::TextDocumentPositionParams,
469 token: JobToken,
470) -> Result<Option<PrepareRenameResponse>> {
471 let file_id = params.text_document.try_conv_with(&world)?;
472 let line_index = world.analysis().file_line_index(file_id);
473 let offset = params.position.conv_with(&line_index);
474
475 // We support renaming references like handle_rename does.
476 // In the future we may want to reject the renaming of things like keywords here too.
477 let refs = world.analysis().find_all_refs(file_id, offset, &token);
478 if refs.is_empty() {
479 return Ok(None);
480 }
481
482 let r = refs.first().unwrap();
483 let loc = to_location(r.0, r.1, &world, &line_index)?;
484
485 Ok(Some(PrepareRenameResponse::Range(loc.range)))
486}
487
488pub fn handle_rename(
489 world: ServerWorld,
490 params: RenameParams,
491 token: JobToken,
492) -> Result<Option<WorkspaceEdit>> {
493 let file_id = params.text_document.try_conv_with(&world)?;
494 let line_index = world.analysis().file_line_index(file_id);
495 let offset = params.position.conv_with(&line_index);
496
497 if params.new_name.is_empty() {
498 return Ok(None);
499 }
500
501 let refs = world.analysis().find_all_refs(file_id, offset, &token);
502 if refs.is_empty() {
503 return Ok(None);
504 }
505
506 let mut changes = HashMap::new();
507 for r in refs {
508 if let Ok(loc) = to_location(r.0, r.1, &world, &line_index) {
509 changes.entry(loc.uri).or_insert(Vec::new()).push(
510 TextEdit {
511 range: loc.range,
512 new_text: params.new_name.clone()
513 });
514 }
515 }
516
517 Ok(Some(WorkspaceEdit {
518 changes: Some(changes),
519
520 // TODO: return this instead if client/server support it. See #144
521 document_changes : None,
522 }))
523}
524
525pub fn handle_references(
526 world: ServerWorld,
527 params: req::ReferenceParams,
528 token: JobToken,
529) -> Result<Option<Vec<Location>>> {
530 let file_id = params.text_document.try_conv_with(&world)?;
531 let line_index = world.analysis().file_line_index(file_id);
532 let offset = params.position.conv_with(&line_index);
533
534 let refs = world.analysis().find_all_refs(file_id, offset, &token);
535
536 Ok(Some(refs.into_iter()
537 .filter_map(|r| to_location(r.0, r.1, &world, &line_index).ok())
538 .collect()))
539}
540
463pub fn handle_code_action( 541pub fn handle_code_action(
464 world: ServerWorld, 542 world: ServerWorld,
465 params: req::CodeActionParams, 543 params: req::CodeActionParams,