From 751fc46df57c632ef915a01c05fe435abee5996c Mon Sep 17 00:00:00 2001 From: fengqi <362254883@qq.com> Date: Thu, 1 Aug 2024 17:29:38 +0800 Subject: [PATCH] run code lens --- lapce-app/src/doc.rs | 15 ++++-- lapce-app/src/editor/view.rs | 4 +- lapce-app/src/main_split.rs | 89 +++++++++++++++++++++++++++++++++--- lapce-app/src/window_tab.rs | 1 + 4 files changed, 96 insertions(+), 13 deletions(-) diff --git a/lapce-app/src/doc.rs b/lapce-app/src/doc.rs index fa23ed816c..177b3b7563 100644 --- a/lapce-app/src/doc.rs +++ b/lapce-app/src/doc.rs @@ -873,11 +873,18 @@ impl Doc { let codelens = codelens .into_iter() .filter_map(|x| { + tracing::debug!("{:?}", x); if let Some(command) = x.command { - Some(Arc::new(ScoredCodeLensItem { - item: command, - range: x.range, - })) + if let Some(args) = command.arguments { + Some(Arc::new(ScoredCodeLensItem { + range: x.range, + title: command.title, + command: command.command, + args, + })) + } else { + None + } } else { None } diff --git a/lapce-app/src/editor/view.rs b/lapce-app/src/editor/view.rs index 7569c1cb27..dc3e02c444 100644 --- a/lapce-app/src/editor/view.rs +++ b/lapce-app/src/editor/view.rs @@ -2447,14 +2447,14 @@ fn code_lens( move |(_, item): (usize, Arc)| { let value = main_split.clone(); container( - text(item.item.title.replace('\n', " ")) + text(item.title.replace('\n', " ")) .style(|s| s.text_ellipsis().min_width(0.0)), ) .on_click_stop({ let window_tab_data = window_tab_data.clone(); move |_| { window_tab_data.update_code_lens_id(None); - value.run_code_lens(item.item.clone()); + value.run_code_lens(&item.args, &item.command); } }) .on_event_stop(EventListener::PointerDown, |_| {}) diff --git a/lapce-app/src/main_split.rs b/lapce-app/src/main_split.rs index cf06d90bea..9adc1c06c2 100644 --- a/lapce-app/src/main_split.rs +++ b/lapce-app/src/main_split.rs @@ -20,21 +20,23 @@ use lapce_core::{ }; use lapce_rpc::{ buffer::BufferId, + dap_types::RunDebugConfig, plugin::{PluginId, VoltID}, proxy::ProxyResponse, }; use lapce_xi_rope::{spans::SpansBuilder, Rope}; use lsp_types::{ - CodeAction, CodeActionOrCommand, Command, DiagnosticSeverity, - DocumentChangeOperation, DocumentChanges, OneOf, Position, Range, TextEdit, Url, - WorkspaceEdit, + CodeAction, CodeActionOrCommand, DiagnosticSeverity, DocumentChangeOperation, + DocumentChanges, OneOf, Position, Range, TextEdit, Url, WorkspaceEdit, }; use serde::{Deserialize, Serialize}; -use tracing::{debug, event, Level}; +use serde_json::Value; +use tracing::{debug, error, event, Level}; use crate::{ alert::AlertButton, command::InternalCommand, + debug::RunDebugMode, doc::{DiagnosticData, Doc, DocContent, DocHistory, EditorDiagnostic}, editor::{ diff::DiffEditorData, @@ -2162,8 +2164,24 @@ impl MainSplitData { } } - pub fn run_code_lens(&self, _action: Command) { - debug!("todo"); + pub fn run_code_lens(&self, args: &Vec, command: &str) { + match command { + "rust-analyzer.runSingle" | "rust-analyzer.debugSingle" => { + if let Some(config) = get_rust_command_config(args) { + let mode = if command == "rust-analyzer.runSingle" { + RunDebugMode::Run + } else { + RunDebugMode::Debug + }; + self.common + .internal_command + .send(InternalCommand::RunAndDebug { mode, config }); + } + } + _ => { + debug!("todo {:}", command); + } + } } /// Resolve a code action and apply its held workspace edit @@ -3018,6 +3036,63 @@ pub enum TabCloseKind { #[derive(Clone, Debug, PartialEq)] pub struct ScoredCodeLensItem { - pub item: Command, pub range: Range, + pub title: String, + pub command: String, + pub args: Vec, +} + +#[derive(Serialize, Deserialize)] +struct CargoArgs { + #[serde(rename = "cargoArgs")] + pub cargo_args: Vec, + + #[serde(rename = "cargoExtraArgs")] + pub cargo_extra_args: Vec, + + #[serde(rename = "executableArgs")] + pub executable_args: Vec, +} + +#[derive(Serialize, Deserialize)] +struct RustArgs { + pub args: CargoArgs, + pub kind: String, + pub label: String, + pub location: lsp_types::LocationLink, +} + +fn get_rust_command_config(args: &Vec) -> Option { + if let Some(args) = args.get(0) { + let Ok(mut cargo_args) = serde_json::from_value::(args.clone()) + else { + error!("serde error"); + return None; + }; + cargo_args + .args + .cargo_args + .extend(cargo_args.args.cargo_extra_args); + if !cargo_args.args.executable_args.is_empty() { + cargo_args.args.cargo_args.push("--".to_string()); + cargo_args + .args + .cargo_args + .extend(cargo_args.args.executable_args); + } + Some(RunDebugConfig { + ty: None, + name: cargo_args.label, + program: cargo_args.kind, + args: Some(cargo_args.args.cargo_args), + cwd: None, + env: None, + prelaunch: None, + debug_command: None, + dap_id: Default::default(), + }) + } else { + error!("no args"); + return None; + } } diff --git a/lapce-app/src/window_tab.rs b/lapce-app/src/window_tab.rs index 7457dde45d..9aecba1798 100644 --- a/lapce-app/src/window_tab.rs +++ b/lapce-app/src/window_tab.rs @@ -2490,6 +2490,7 @@ impl WindowTabData { mode: &RunDebugMode, config: &RunDebugConfig, ) { + debug!("{:?}", config); match mode { RunDebugMode::Run => { self.run_in_terminal(cx, mode, config, false);