aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_lsp_server
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_lsp_server')
-rw-r--r--crates/ra_lsp_server/src/main_loop/handlers.rs95
1 files changed, 43 insertions, 52 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..4ac051c96 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, Cancelable, 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};
@@ -325,27 +325,7 @@ pub fn handle_runnables(
325 continue; 325 continue;
326 } 326 }
327 } 327 }
328 328 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 } 329 }
350 let mut check_args = vec!["check".to_string()]; 330 let mut check_args = vec!["check".to_string()];
351 let label; 331 let label;
@@ -693,42 +673,27 @@ pub fn handle_code_lens(
693 let line_index = world.analysis().file_line_index(file_id)?; 673 let line_index = world.analysis().file_line_index(file_id)?;
694 674
695 let mut lenses: Vec<CodeLens> = Default::default(); 675 let mut lenses: Vec<CodeLens> = Default::default();
696 let workspace_root = world.workspace_root_for(file_id);
697 676
698 // Gather runnables 677 // Gather runnables
699 for runnable in world.analysis().runnables(file_id)? { 678 for runnable in world.analysis().runnables(file_id)? {
700 let title = match &runnable.kind { 679 let title = match &runnable.kind {
701 RunnableKind::Test { .. } | RunnableKind::TestMod { .. } => Some("▶️Run Test"), 680 RunnableKind::Test { .. } | RunnableKind::TestMod { .. } => "▶️Run Test",
702 RunnableKind::Bench { .. } => Some("Run Bench"), 681 RunnableKind::Bench { .. } => "Run Bench",
703 RunnableKind::Bin => Some("️Run"), 682 RunnableKind::Bin => "Run",
683 }
684 .to_string();
685 let r = to_lsp_runnable(&world, file_id, runnable)?;
686 let lens = CodeLens {
687 range: r.range,
688 command: Some(Command {
689 title,
690 command: "rust-analyzer.runSingle".into(),
691 arguments: Some(vec![to_value(r).unwrap()]),
692 }),
693 data: None,
704 }; 694 };
705 695
706 if let Some(title) = title { 696 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 } 697 }
733 698
734 // Handle impls 699 // Handle impls
@@ -856,6 +821,32 @@ pub fn publish_decorations(
856 Ok(req::PublishDecorationsParams { uri, decorations: highlight(&world, file_id)? }) 821 Ok(req::PublishDecorationsParams { uri, decorations: highlight(&world, file_id)? })
857} 822}
858 823
824fn to_lsp_runnable(
825 world: &WorldSnapshot,
826 file_id: FileId,
827 runnable: Runnable,
828) -> Result<req::Runnable> {
829 let args = runnable_args(world, file_id, &runnable.kind)?;
830 let line_index = world.analysis().file_line_index(file_id)?;
831 let label = match &runnable.kind {
832 RunnableKind::Test { name } => format!("test {}", name),
833 RunnableKind::TestMod { path } => format!("test-mod {}", path),
834 RunnableKind::Bench { name } => format!("bench {}", name),
835 RunnableKind::Bin => "run binary".to_string(),
836 };
837 Ok(req::Runnable {
838 range: runnable.range.conv_with(&line_index),
839 label,
840 bin: "cargo".to_string(),
841 args,
842 env: {
843 let mut m = FxHashMap::default();
844 m.insert("RUST_BACKTRACE".to_string(), "short".to_string());
845 m
846 },
847 cwd: world.workspace_root_for(file_id).map(|root| root.to_string_lossy().to_string()),
848 })
849}
859fn highlight(world: &WorldSnapshot, file_id: FileId) -> Result<Vec<Decoration>> { 850fn highlight(world: &WorldSnapshot, file_id: FileId) -> Result<Vec<Decoration>> {
860 let line_index = world.analysis().file_line_index(file_id)?; 851 let line_index = world.analysis().file_line_index(file_id)?;
861 let res = world 852 let res = world