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.rs116
1 files changed, 43 insertions, 73 deletions
diff --git a/crates/ra_lsp_server/src/main_loop/handlers.rs b/crates/ra_lsp_server/src/main_loop/handlers.rs
index 686ee5d12..a3d3f167c 100644
--- a/crates/ra_lsp_server/src/main_loop/handlers.rs
+++ b/crates/ra_lsp_server/src/main_loop/handlers.rs
@@ -9,7 +9,7 @@ use lsp_types::{
9 TextEdit, WorkspaceEdit, 9 TextEdit, WorkspaceEdit,
10}; 10};
11use ra_ide_api::{ 11use ra_ide_api::{
12 AssistId, Cancelable, FileId, FilePosition, FileRange, FoldKind, Query, RunnableKind, 12 AssistId, FileId, FilePosition, FileRange, FoldKind, Query, Runnable, RunnableKind,
13}; 13};
14use ra_prof::profile; 14use ra_prof::profile;
15use ra_syntax::{AstNode, SyntaxKind, TextRange, TextUnit}; 15use ra_syntax::{AstNode, SyntaxKind, TextRange, TextUnit};
@@ -45,27 +45,6 @@ pub fn handle_syntax_tree(world: WorldSnapshot, params: req::SyntaxTreeParams) -
45 Ok(res) 45 Ok(res)
46} 46}
47 47
48// FIXME: drop this API
49pub fn handle_extend_selection(
50 world: WorldSnapshot,
51 params: req::ExtendSelectionParams,
52) -> Result<req::ExtendSelectionResult> {
53 log::error!(
54 "extend selection is deprecated and will be removed soon,
55 use the new selection range API in LSP",
56 );
57 let file_id = params.text_document.try_conv_with(&world)?;
58 let line_index = world.analysis().file_line_index(file_id)?;
59 let selections = params
60 .selections
61 .into_iter()
62 .map_conv_with(&line_index)
63 .map(|range| FileRange { file_id, range })
64 .map(|frange| world.analysis().extend_selection(frange).map(|it| it.conv_with(&line_index)))
65 .collect::<Cancelable<Vec<_>>>()?;
66 Ok(req::ExtendSelectionResult { selections })
67}
68
69pub fn handle_selection_range( 48pub fn handle_selection_range(
70 world: WorldSnapshot, 49 world: WorldSnapshot,
71 params: req::SelectionRangeParams, 50 params: req::SelectionRangeParams,
@@ -325,27 +304,7 @@ pub fn handle_runnables(
325 continue; 304 continue;
326 } 305 }
327 } 306 }
328 307 res.push(to_lsp_runnable(&world, file_id, runnable)?);
329 let args = runnable_args(&world, file_id, &runnable.kind)?;
330
331 let r = req::Runnable {
332 range: runnable.range.conv_with(&line_index),
333 label: match &runnable.kind {
334 RunnableKind::Test { name } => format!("test {}", name),
335 RunnableKind::TestMod { path } => format!("test-mod {}", path),
336 RunnableKind::Bench { name } => format!("bench {}", name),
337 RunnableKind::Bin => "run binary".to_string(),
338 },
339 bin: "cargo".to_string(),
340 args,
341 env: {
342 let mut m = FxHashMap::default();
343 m.insert("RUST_BACKTRACE".to_string(), "short".to_string());
344 m
345 },
346 cwd: workspace_root.map(|root| root.to_string_lossy().to_string()),
347 };
348 res.push(r);
349 } 308 }
350 let mut check_args = vec!["check".to_string()]; 309 let mut check_args = vec!["check".to_string()];
351 let label; 310 let label;
@@ -693,42 +652,27 @@ pub fn handle_code_lens(
693 let line_index = world.analysis().file_line_index(file_id)?; 652 let line_index = world.analysis().file_line_index(file_id)?;
694 653
695 let mut lenses: Vec<CodeLens> = Default::default(); 654 let mut lenses: Vec<CodeLens> = Default::default();
696 let workspace_root = world.workspace_root_for(file_id);
697 655
698 // Gather runnables 656 // Gather runnables
699 for runnable in world.analysis().runnables(file_id)? { 657 for runnable in world.analysis().runnables(file_id)? {
700 let title = match &runnable.kind { 658 let title = match &runnable.kind {
701 RunnableKind::Test { .. } | RunnableKind::TestMod { .. } => Some("▶️Run Test"), 659 RunnableKind::Test { .. } | RunnableKind::TestMod { .. } => "▶️Run Test",
702 RunnableKind::Bench { .. } => Some("Run Bench"), 660 RunnableKind::Bench { .. } => "Run Bench",
703 RunnableKind::Bin => Some("️Run"), 661 RunnableKind::Bin => "Run",
662 }
663 .to_string();
664 let r = to_lsp_runnable(&world, file_id, runnable)?;
665 let lens = CodeLens {
666 range: r.range,
667 command: Some(Command {
668 title,
669 command: "rust-analyzer.runSingle".into(),
670 arguments: Some(vec![to_value(r).unwrap()]),
671 }),
672 data: None,
704 }; 673 };
705 674
706 if let Some(title) = title { 675 lenses.push(lens);
707 let args = runnable_args(&world, file_id, &runnable.kind)?;
708 let range = runnable.range.conv_with(&line_index);
709
710 // This represents the actual command that will be run.
711 let r: req::Runnable = req::Runnable {
712 range,
713 label: Default::default(),
714 bin: "cargo".into(),
715 args,
716 env: Default::default(),
717 cwd: workspace_root.map(|root| root.to_string_lossy().to_string()),
718 };
719
720 let lens = CodeLens {
721 range,
722 command: Some(Command {
723 title: title.into(),
724 command: "rust-analyzer.runSingle".into(),
725 arguments: Some(vec![to_value(r).unwrap()]),
726 }),
727 data: None,
728 };
729
730 lenses.push(lens);
731 }
732 } 676 }
733 677
734 // Handle impls 678 // Handle impls
@@ -856,6 +800,32 @@ pub fn publish_decorations(
856 Ok(req::PublishDecorationsParams { uri, decorations: highlight(&world, file_id)? }) 800 Ok(req::PublishDecorationsParams { uri, decorations: highlight(&world, file_id)? })
857} 801}
858 802
803fn to_lsp_runnable(
804 world: &WorldSnapshot,
805 file_id: FileId,
806 runnable: Runnable,
807) -> Result<req::Runnable> {
808 let args = runnable_args(world, file_id, &runnable.kind)?;
809 let line_index = world.analysis().file_line_index(file_id)?;
810 let label = match &runnable.kind {
811 RunnableKind::Test { name } => format!("test {}", name),
812 RunnableKind::TestMod { path } => format!("test-mod {}", path),
813 RunnableKind::Bench { name } => format!("bench {}", name),
814 RunnableKind::Bin => "run binary".to_string(),
815 };
816 Ok(req::Runnable {
817 range: runnable.range.conv_with(&line_index),
818 label,
819 bin: "cargo".to_string(),
820 args,
821 env: {
822 let mut m = FxHashMap::default();
823 m.insert("RUST_BACKTRACE".to_string(), "short".to_string());
824 m
825 },
826 cwd: world.workspace_root_for(file_id).map(|root| root.to_string_lossy().to_string()),
827 })
828}
859fn highlight(world: &WorldSnapshot, file_id: FileId) -> Result<Vec<Decoration>> { 829fn highlight(world: &WorldSnapshot, file_id: FileId) -> Result<Vec<Decoration>> {
860 let line_index = world.analysis().file_line_index(file_id)?; 830 let line_index = world.analysis().file_line_index(file_id)?;
861 let res = world 831 let res = world