Skip to content

Commit

Permalink
run code lens
Browse files Browse the repository at this point in the history
  • Loading branch information
jm-observer committed Aug 1, 2024
1 parent 1662ac3 commit 751fc46
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 13 deletions.
15 changes: 11 additions & 4 deletions lapce-app/src/doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions lapce-app/src/editor/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2447,14 +2447,14 @@ fn code_lens(
move |(_, item): (usize, Arc<ScoredCodeLensItem>)| {
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, |_| {})
Expand Down
89 changes: 82 additions & 7 deletions lapce-app/src/main_split.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -2162,8 +2164,24 @@ impl MainSplitData {
}
}

pub fn run_code_lens(&self, _action: Command) {
debug!("todo");
pub fn run_code_lens(&self, args: &Vec<Value>, 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
Expand Down Expand Up @@ -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<Value>,
}

#[derive(Serialize, Deserialize)]
struct CargoArgs {
#[serde(rename = "cargoArgs")]
pub cargo_args: Vec<String>,

#[serde(rename = "cargoExtraArgs")]
pub cargo_extra_args: Vec<String>,

#[serde(rename = "executableArgs")]
pub executable_args: Vec<String>,
}

#[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<Value>) -> Option<RunDebugConfig> {
if let Some(args) = args.get(0) {
let Ok(mut cargo_args) = serde_json::from_value::<RustArgs>(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;
}
}
1 change: 1 addition & 0 deletions lapce-app/src/window_tab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2490,6 +2490,7 @@ impl WindowTabData {
mode: &RunDebugMode,
config: &RunDebugConfig,
) {
debug!("{:?}", config);
match mode {
RunDebugMode::Run => {
self.run_in_terminal(cx, mode, config, false);
Expand Down

0 comments on commit 751fc46

Please sign in to comment.