Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get code lens and store in doc #96

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions lapce-app/src/doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ pub struct DocInfo {
/// (Offset -> (Plugin the code actions are from, Code Actions))
pub type CodeActions = im::HashMap<usize, Arc<(PluginId, CodeActionResponse)>>;

pub type CodeLens = im::HashMap<PluginId, Arc<Vec<lsp_types::CodeLens>>>;

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

pub code_lens: RwSignal<CodeLens>,

/// Stores information about different versions of the document from source control.
histories: RwSignal<im::HashMap<String, DocumentHistory>>,
pub head_changes: RwSignal<im::Vector<DiffLines>>,
Expand Down Expand Up @@ -242,6 +246,7 @@ impl Doc {
preedit: PreeditData::new(cx),
editors,
common,
code_lens: cx.create_rw_signal(im::HashMap::new()),
}
}

Expand Down Expand Up @@ -290,6 +295,7 @@ impl Doc {
preedit: PreeditData::new(cx),
editors,
common,
code_lens: cx.create_rw_signal(im::HashMap::new()),
}
}

Expand Down Expand Up @@ -338,6 +344,7 @@ impl Doc {
preedit: PreeditData::new(cx),
editors,
common,
code_lens: cx.create_rw_signal(im::HashMap::new()),
}
}

Expand Down
60 changes: 60 additions & 0 deletions lapce-app/src/editor/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1466,6 +1466,32 @@ fn editor_gutter(
})
};

let code_lens_view = move |line: usize| {
let line_y = screen_lines
.with_untracked(|s| s.info_for_line(line))
.map(|l| l.y)
.unwrap_or_default();
container(svg(move || config.get().ui_svg(LapceIcons::START)).style(
move |s| {
let config = config.get();
let size = config.ui.icon_size() as f32 + 2.0;
s.size(size, size)
.color(config.color(LapceColor::LAPCE_ICON_ACTIVE))
},
))
.on_click_stop(move |_| {
// todo
})
.style(move |s| {
s.width(padding_right)
.height(config.get().editor.line_height() as f32)
.justify_center()
.items_center()
.cursor(CursorStyle::Pointer)
.margin_top(line_y as f32 - viewport.get().y0 as f32)
})
};

stack((
stack((
empty().style(move |s| s.width(padding_left)),
Expand Down Expand Up @@ -1564,6 +1590,40 @@ fn editor_gutter(
}
})
.style(|s| s.size_pct(100.0, 100.0)),
dyn_stack(
move || {
let doc = doc.get();
let lines = doc.code_lens.with(|x| {
let lines: Vec<usize> = x
.iter()
.map(|(_, lens)| {
lens.iter()
.map(|len| len.range.start.line as usize)
.collect()
})
.fold(
Vec::new(),
|mut x: Vec<usize>, mut y: Vec<usize>| {
x.append(&mut y);
x
},
);
lines
});
lines
},
move |i| *i,
code_lens_view,
)
.style(move |s| {
let config = config.get();
let gutter_width = gutter_width.get();
let size = config.ui.icon_size() as f32;
let margin_left =
gutter_width as f32 + (padding_right - size) / 2.0 - 4.0;
s.absolute().size_pct(100.0, 100.0).margin_left(margin_left)
})
.debug_name("CodeLens Stack"),
container(
svg(move || config.get().ui_svg(LapceIcons::LIGHTBULB)).style(
move |s| {
Expand Down
31 changes: 31 additions & 0 deletions lapce-app/src/main_split.rs
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,37 @@ impl MainSplitData {
send(result);
});
}
{
let doc = doc.clone();
if let DocContent::File { path, .. } = doc.content.get_untracked() {
let send = create_ext_action(cx, move |result| {
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 {
doc.code_lens.update(|x| {
x.insert(
plugin_id,
std::sync::Arc::new(codelens),
);
});
}
}
});
self.common.proxy.get_code_lens(path, move |result| {
send(result);
});
}
}

(doc, true)
}
Expand Down
10 changes: 10 additions & 0 deletions lapce-proxy/src/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1034,6 +1034,16 @@ impl ProxyHandler for Dispatcher {
);
});
}
GetCodeLens { path } => {
let proxy_rpc = self.proxy_rpc.clone();
self.catalog_rpc
.get_code_lens(&path, move |plugin_id, result| {
let result = result.map(|resp| {
ProxyResponse::GetCodeLensResponse { plugin_id, resp }
});
proxy_rpc.handle_response(id, result);
});
}
}
}
}
Expand Down
43 changes: 36 additions & 7 deletions lapce-proxy/src/plugin/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use lapce_rpc::{
use lapce_xi_rope::{Rope, RopeDelta};
use lsp_types::{
request::{
CodeActionRequest, CodeActionResolveRequest, Completion,
CodeActionRequest, CodeActionResolveRequest, CodeLensRequest, Completion,
DocumentSymbolRequest, Formatting, GotoDefinition, GotoTypeDefinition,
GotoTypeDefinitionParams, GotoTypeDefinitionResponse, HoverRequest,
InlayHintRequest, InlineCompletionRequest, PrepareRenameRequest, References,
Expand All @@ -43,12 +43,13 @@ use lsp_types::{
ClientCapabilities, CodeAction, CodeActionCapabilityResolveSupport,
CodeActionClientCapabilities, CodeActionContext, CodeActionKind,
CodeActionKindLiteralSupport, CodeActionLiteralSupport, CodeActionParams,
CodeActionResponse, CompletionClientCapabilities, CompletionItem,
CompletionItemCapability, CompletionItemCapabilityResolveSupport,
CompletionParams, CompletionResponse, Diagnostic, DocumentFormattingParams,
DocumentSymbolParams, DocumentSymbolResponse, FormattingOptions, GotoCapability,
GotoDefinitionParams, GotoDefinitionResponse, Hover, HoverClientCapabilities,
HoverParams, InlayHint, InlayHintClientCapabilities, InlayHintParams,
CodeActionResponse, CodeLens, CodeLensParams, CompletionClientCapabilities,
CompletionItem, CompletionItemCapability,
CompletionItemCapabilityResolveSupport, CompletionParams, CompletionResponse,
Diagnostic, DocumentFormattingParams, DocumentSymbolParams,
DocumentSymbolResponse, FormattingOptions, GotoCapability, GotoDefinitionParams,
GotoDefinitionResponse, Hover, HoverClientCapabilities, HoverParams, InlayHint,
InlayHintClientCapabilities, InlayHintParams,
InlineCompletionClientCapabilities, InlineCompletionParams,
InlineCompletionResponse, InlineCompletionTriggerKind, Location, MarkupKind,
MessageActionItemCapabilities, ParameterInformationSettings,
Expand Down Expand Up @@ -648,6 +649,34 @@ impl PluginCatalogRpcHandler {
);
}

pub fn get_code_lens(
&self,
path: &Path,
cb: impl FnOnce(PluginId, Result<Option<Vec<CodeLens>>, RpcError>)
+ Clone
+ Send
+ 'static,
) {
let uri = Url::from_file_path(path).unwrap();
let method = CodeLensRequest::METHOD;
let params = CodeLensParams {
text_document: TextDocumentIdentifier { uri },
work_done_progress_params: Default::default(),
partial_result_params: Default::default(),
};

let language_id =
Some(language_id_from_path(path).unwrap_or("").to_string());

self.send_request_to_all_plugins(
method,
params,
language_id,
Some(path.to_path_buf()),
cb,
);
}

pub fn get_inlay_hints(
&self,
path: &Path,
Expand Down
5 changes: 4 additions & 1 deletion lapce-proxy/src/plugin/psp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use lsp_types::{
ShowMessage,
},
request::{
CodeActionRequest, CodeActionResolveRequest, Completion,
CodeActionRequest, CodeActionResolveRequest, CodeLensRequest, Completion,
DocumentSymbolRequest, Formatting, GotoDefinition, GotoTypeDefinition,
HoverRequest, Initialize, InlayHintRequest, InlineCompletionRequest,
PrepareRenameRequest, References, RegisterCapability, Rename,
Expand Down Expand Up @@ -780,6 +780,9 @@ impl PluginHostHandler {
CodeActionResolveRequest::METHOD => {
self.server_capabilities.code_action_provider.is_some()
}
CodeLensRequest::METHOD => {
self.server_capabilities.code_lens_provider.is_some()
}
_ => false,
}
}
Expand Down
13 changes: 12 additions & 1 deletion lapce-rpc/src/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crossbeam_channel::{Receiver, Sender};
use indexmap::IndexMap;
use lapce_xi_rope::RopeDelta;
use lsp_types::{
request::GotoTypeDefinitionResponse, CodeAction, CodeActionResponse,
request::GotoTypeDefinitionResponse, CodeAction, CodeActionResponse, CodeLens,
CompletionItem, Diagnostic, DocumentSymbolResponse, GotoDefinitionResponse,
Hover, InlayHint, InlineCompletionResponse, InlineCompletionTriggerKind,
Location, Position, PrepareRenameResponse, SelectionRange, SymbolInformation,
Expand Down Expand Up @@ -134,6 +134,9 @@ pub enum ProxyRequest {
position: Position,
diagnostics: Vec<Diagnostic>,
},
GetCodeLens {
path: PathBuf,
},
GetDocumentSymbols {
path: PathBuf,
},
Expand Down Expand Up @@ -366,6 +369,10 @@ pub enum ProxyResponse {
plugin_id: PluginId,
resp: CodeActionResponse,
},
GetCodeLensResponse {
plugin_id: PluginId,
resp: Option<Vec<CodeLens>>,
},
GetFilesResponse {
items: Vec<PathBuf>,
},
Expand Down Expand Up @@ -874,6 +881,10 @@ impl ProxyRpcHandler {
);
}

pub fn get_code_lens(&self, path: PathBuf, f: impl ProxyCallback + 'static) {
self.request_async(ProxyRequest::GetCodeLens { path }, f);
}

pub fn get_document_formatting(
&self,
path: PathBuf,
Expand Down
Loading