Skip to content

Commit

Permalink
feat(lsp): code lens
Browse files Browse the repository at this point in the history
Initial implementation of LSP code lense.
  • Loading branch information
matoous committed Dec 8, 2022
1 parent 96ff64a commit 1ae7c86
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 0 deletions.
21 changes: 21 additions & 0 deletions helix-lsp/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,9 @@ impl Client {
publish_diagnostics: Some(lsp::PublishDiagnosticsClientCapabilities {
..Default::default()
}),
code_lens: Some(lsp::CodeLensClientCapabilities {
..Default::default()
}),
..Default::default()
}),
window: Some(lsp::WindowClientCapabilities {
Expand Down Expand Up @@ -1077,4 +1080,22 @@ impl Client {

Some(self.call::<lsp::request::ExecuteCommand>(params))
}

pub fn code_lens(
&self,
text_document: lsp::TextDocumentIdentifier,
) -> Option<impl Future<Output = Result<Value>>> {
let capabilities = self.capabilities.get().unwrap();

// Return early if the server does not support code lens.
capabilities.code_lens_provider.as_ref()?;

let params = lsp::CodeLensParams {
text_document,
work_done_progress_params: lsp::WorkDoneProgressParams::default(),
partial_result_params: lsp::PartialResultParams::default(),
};

Some(self.call::<lsp::request::CodeLensRequest>(params))
}
}
1 change: 1 addition & 0 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,7 @@ impl MappableCommand {
paste_after, "Paste after selection",
paste_before, "Paste before selection",
paste_clipboard_after, "Paste clipboard after selections",
code_lenses_picker, "Show code lense picker",
paste_clipboard_before, "Paste clipboard before selections",
paste_primary_clipboard_after, "Paste primary clipboard after selections",
paste_primary_clipboard_before, "Paste primary clipboard before selections",
Expand Down
75 changes: 75 additions & 0 deletions helix-term/src/commands/lsp.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub use helix_lsp::lsp::Command;
use helix_lsp::{
block_on,
lsp::{self, CodeAction, CodeActionOrCommand, DiagnosticSeverity, NumberOrString},
Expand Down Expand Up @@ -1256,3 +1257,77 @@ pub fn select_references_to_symbol_under_cursor(cx: &mut Context) {
},
);
}

impl ui::menu::Item for lsp::CodeLens {
type Data = ();

fn label(&self, _: &Self::Data) -> Spans {
match self.command.clone() {
Some(cmd) => cmd.title.into(),
None => "unresolved".into(),
}
}
}

pub fn code_lenses_picker(cx: &mut Context) {
let doc = doc!(cx.editor);

let language_server = match doc.language_server() {
Some(language_server) => language_server,
None => return,
};

let request = match language_server.code_lens(doc.identifier()) {
Some(future) => future,
None => {
cx.editor
.set_error("Language server does not support code lense");
return;
}
};

let doc_id = doc.id();
cx.callback(
request,
move |editor, compositor, lenses: Option<Vec<lsp::CodeLens>>| {
if let Some(lenses) = lenses {
log::error!("lenses got: {:?}", lenses);
let picker = FilePicker::new(
lenses,
(),
|cx, meta, _action| {
let doc = doc!(cx.editor);
let language_server = language_server!(cx.editor, doc);

if let Some(cmd) = meta.command.clone() {
match language_server.command(cmd) {
Some(future) => tokio::spawn(async move {
let res = future.await;

if let Err(e) = res {
log::error!("execute LSP command: {}", e);
}
}),
None => {
cx.editor.set_error(
"Language server does not support executing commands",
);
return;
}
};
}
},
move |_editor, meta| {
Some((
doc_id.into(),
Some((meta.range.start.line as usize, meta.range.end.line as usize)),
))
},
);
compositor.push(Box::new(overlayed(picker)));
} else {
editor.set_status("no lense found");
}
},
)
}
2 changes: 2 additions & 0 deletions helix-term/src/keymap/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,8 @@ pub fn default() -> HashMap<Mode, Keymap> {
"D" => workspace_diagnostics_picker,
"a" => code_action,
"'" => last_picker,
// "l" => execute_lense_under_cursor,
"L" => code_lenses_picker,
"g" => { "Debug (experimental)" sticky=true
"l" => dap_launch,
"b" => dap_toggle_breakpoint,
Expand Down

0 comments on commit 1ae7c86

Please sign in to comment.