Skip to content

Commit

Permalink
editor gutter refactor
Browse files Browse the repository at this point in the history
Use code actions popup for CodeLens
  • Loading branch information
dzhou121 committed Aug 7, 2024
1 parent d9e0411 commit 103206f
Show file tree
Hide file tree
Showing 17 changed files with 622 additions and 554 deletions.
13 changes: 7 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ lapce-core = { path = "./lapce-core" }
lapce-rpc = { path = "./lapce-rpc" }
lapce-proxy = { path = "./lapce-proxy" }

floem = { git = "https://github.com/lapce/floem", rev = "48d2b6eb22a02a100d67868052ecc35fa3c42e10", features = ["editor", "serde", "default-image-formats", "rfd-async-std"] }
floem = { git = "https://github.com/lapce/floem", rev = "54f0d1bcf0e1a91d82492ee7300a526adb60eb5c", features = ["editor", "serde", "default-image-formats", "rfd-async-std"] }
# floem = { path = "../floem", features = ["editor", "serde", "default-image-formats", "rfd-async-std"] }
floem-editor-core = { git = "https://github.com/lapce/floem", rev = "48d2b6eb22a02a100d67868052ecc35fa3c42e10", features = ["serde"] }
floem-editor-core = { git = "https://github.com/lapce/floem", rev = "54f0d1bcf0e1a91d82492ee7300a526adb60eb5c", features = ["serde"] }
# floem-editor-core = { path = "../floem/editor-core/", features = ["serde"] }

[patch.crates-io]
Expand Down
2 changes: 1 addition & 1 deletion lapce-app/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3103,7 +3103,7 @@ fn code_action(window_tab_data: Rc<WindowTabData>) -> impl View {
.align_items(Some(AlignItems::Center))
.min_width(0.0)
.width_full()
.line_height(1.6)
.line_height(1.8)
.border_radius(6.0)
.cursor(CursorStyle::Pointer)
.apply_if(active.get() == i, |s| {
Expand Down
12 changes: 6 additions & 6 deletions lapce-app/src/code_action.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{rc::Rc, sync::Arc};
use std::rc::Rc;

use floem::{
keyboard::Modifiers,
Expand Down Expand Up @@ -166,7 +166,8 @@ impl CodeActionData {

pub fn show(
&mut self,
code_actions: Arc<(PluginId, Vec<CodeActionOrCommand>)>,
plugin_id: PluginId,
code_actions: im::Vector<CodeActionOrCommand>,
offset: usize,
mouse_click: bool,
) {
Expand All @@ -176,11 +177,10 @@ impl CodeActionData {
self.mouse_click = mouse_click;
self.request_id += 1;
self.items = code_actions
.1
.iter()
.into_iter()
.map(|code_action| ScoredCodeActionItem {
item: code_action.clone(),
plugin_id: code_actions.0,
item: code_action,
plugin_id,
score: 0,
indices: Vec::new(),
})
Expand Down
94 changes: 94 additions & 0 deletions lapce-app/src/code_lens.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
use std::rc::Rc;

use lapce_rpc::dap_types::RunDebugConfig;
use serde::{Deserialize, Serialize};
use serde_json::Value;

use crate::{command::InternalCommand, debug::RunDebugMode, window_tab::CommonData};

#[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,
}

#[derive(Clone)]
pub struct CodeLensData {
common: Rc<CommonData>,
}

impl CodeLensData {
pub fn new(common: Rc<CommonData>) -> Self {
Self { common }
}

pub fn run(&self, command: &str, args: Vec<Value>) {
match command {
"rust-analyzer.runSingle" | "rust-analyzer.debugSingle" => {
if let Some(config) = self.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 });
}
}
_ => {
tracing::debug!("todo {:}", command);
}
}
}

fn get_rust_command_config(&self, args: &[Value]) -> Option<RunDebugConfig> {
if let Some(args) = args.first() {
let Ok(mut cargo_args) =
serde_json::from_value::<RustArgs>(args.clone())
else {
tracing::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 {
tracing::error!("no args");
None
}
}
}
5 changes: 3 additions & 2 deletions lapce-app/src/command.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{path::PathBuf, rc::Rc, sync::Arc};
use std::{path::PathBuf, rc::Rc};

pub use floem::views::editor::command::CommandExecuted;
use floem::{
Expand Down Expand Up @@ -649,7 +649,8 @@ pub enum InternalCommand {
ShowCodeActions {
offset: usize,
mouse_click: bool,
code_actions: Arc<(PluginId, Vec<CodeActionOrCommand>)>,
plugin_id: PluginId,
code_actions: im::Vector<CodeActionOrCommand>,
},
RunCodeAction {
plugin_id: PluginId,
Expand Down
66 changes: 32 additions & 34 deletions lapce-app/src/doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ use lapce_xi_rope::{
Interval, Rope, RopeDelta, Transformer,
};
use lsp_types::{
CodeActionResponse, Diagnostic, DiagnosticSeverity, InlayHint, InlayHintLabel,
CodeActionOrCommand, CodeLens, Diagnostic, DiagnosticSeverity, InlayHint,
InlayHintLabel,
};
use serde::{Deserialize, Serialize};
use smallvec::SmallVec;
Expand All @@ -76,7 +77,7 @@ use crate::{
find::{Find, FindProgress, FindResult},
history::DocumentHistory,
keypress::KeyPressFocus,
main_split::{Editors, ScoredCodeLensItem},
main_split::Editors,
panel::kind::PanelKind,
window_tab::{CommonData, Focus},
workspace::LapceWorkspace,
Expand Down Expand Up @@ -150,9 +151,10 @@ pub struct DocInfo {
}

/// (Offset -> (Plugin the code actions are from, Code Actions))
pub type CodeActions = im::HashMap<usize, Arc<(PluginId, CodeActionResponse)>>;
pub type CodeActions =
im::HashMap<usize, (PluginId, im::Vector<CodeActionOrCommand>)>;

pub type CodeLens = im::HashMap<PluginId, im::Vector<Arc<ScoredCodeLensItem>>>;
pub type AllCodeLens = im::HashMap<usize, (PluginId, usize, im::Vector<CodeLens>)>;

#[derive(Clone)]
pub struct Doc {
Expand Down Expand Up @@ -182,7 +184,7 @@ pub struct Doc {
/// (Offset -> (Plugin the code actions are from, Code Actions))
pub code_actions: RwSignal<CodeActions>,

pub code_lens: RwSignal<CodeLens>,
pub code_lens: RwSignal<AllCodeLens>,

/// Stores information about different versions of the document from source control.
histories: RwSignal<im::HashMap<String, DocumentHistory>>,
Expand Down Expand Up @@ -861,43 +863,39 @@ impl Doc {
pub fn get_code_lens(&self) {
let cx = self.scope;
let doc = self.clone();
self.code_lens.update(|code_lens| {
code_lens.clear();
});
let rev = self.rev();
if let DocContent::File { path, .. } = doc.content.get_untracked() {
let send = create_ext_action(cx, move |result| {
if rev != doc.rev() {
return;
}
if let Ok(ProxyResponse::GetCodeLensResponse { plugin_id, resp }) =
result
{
let Some(codelens) = resp else {
return;
};
if codelens.is_empty() {
doc.code_lens.update(|x| {
x.remove(&plugin_id);
});
} else {
let codelens = codelens
.into_iter()
.filter_map(|x| {
tracing::debug!("{:?}", x);
if let Some(command) = x.command {
if let Some(args) = command.arguments {
Some(Arc::new(ScoredCodeLensItem {
range: x.range,
title: command.title,
command: command.command,
args,
}))
} else {
None
}
} else {
None
}
})
.collect();
doc.code_lens.update(|x| {
x.insert(plugin_id, codelens);
});
}
doc.code_lens.update(|code_lens| {
for codelens in codelens {
let entry = code_lens
.entry(codelens.range.start.line as usize)
.or_insert_with(|| {
(
plugin_id,
doc.buffer.with_untracked(|b| {
b.offset_of_line(
codelens.range.start.line as usize,
)
}),
im::Vector::new(),
)
});
entry.2.push_back(codelens);
}
});
}
});
self.common.proxy.get_code_lens(path, move |result| {
Expand Down
30 changes: 17 additions & 13 deletions lapce-app/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ use lapce_core::{
use lapce_rpc::{buffer::BufferId, plugin::PluginId, proxy::ProxyResponse};
use lapce_xi_rope::{Rope, RopeDelta, Transformer};
use lsp_types::{
CompletionItem, CompletionTextEdit, GotoDefinitionResponse, HoverContents,
InlayHint, InlayHintLabel, InlineCompletionTriggerKind, Location, MarkedString,
MarkupKind, Range, TextEdit,
CodeActionResponse, CompletionItem, CompletionTextEdit, GotoDefinitionResponse,
HoverContents, InlayHint, InlayHintLabel, InlineCompletionTriggerKind, Location,
MarkedString, MarkupKind, Range, TextEdit,
};
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -2019,7 +2019,7 @@ impl EditorData {

// insert some empty data, so that we won't make the request again
doc.code_actions().update(|c| {
c.insert(offset, Arc::new((PluginId(0), Vec::new())));
c.insert(offset, (PluginId(0), im::Vector::new()));
});

let (position, rev, diagnostics) = doc.buffer.with_untracked(|buffer| {
Expand All @@ -2041,13 +2041,16 @@ impl EditorData {
(position, rev, diagnostics)
});

let send = create_ext_action(self.scope, move |resp| {
if doc.rev() == rev {
doc.code_actions().update(|c| {
c.insert(offset, Arc::new(resp));
});
}
});
let send = create_ext_action(
self.scope,
move |resp: (PluginId, CodeActionResponse)| {
if doc.rev() == rev {
doc.code_actions().update(|c| {
c.insert(offset, (resp.0, resp.1.into()));
});
}
},
);

self.common.proxy.get_code_actions(
path,
Expand All @@ -2071,12 +2074,13 @@ impl EditorData {
let code_actions = doc
.code_actions()
.with_untracked(|c| c.get(&offset).cloned());
if let Some(code_actions) = code_actions {
if !code_actions.1.is_empty() {
if let Some((plugin_id, code_actions)) = code_actions {
if !code_actions.is_empty() {
self.common.internal_command.send(
InternalCommand::ShowCodeActions {
offset,
mouse_click,
plugin_id,
code_actions,
},
);
Expand Down
Loading

0 comments on commit 103206f

Please sign in to comment.