Skip to content

Commit

Permalink
render lens symbol in gutter
Browse files Browse the repository at this point in the history
  • Loading branch information
matoous committed Nov 26, 2023
1 parent 89336f5 commit 4d7203d
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 70 deletions.
2 changes: 1 addition & 1 deletion helix-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,5 @@ pub use syntax::Syntax;

pub use diagnostic::Diagnostic;

pub use line_ending::{LineEnding, NATIVE_LINE_ENDING};
pub use line_ending::{line_end_char_index, LineEnding, NATIVE_LINE_ENDING};
pub use transaction::{Assoc, Change, ChangeSet, Deletion, Operation, Transaction};
23 changes: 0 additions & 23 deletions helix-lsp/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1562,27 +1562,4 @@ impl Client {

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

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

// Return early if the server does not support resolving code lens.
match capabilities.code_lens_provider {
Some(lsp::CodeLensOptions {
resolve_provider: Some(true),
..
}) => (),
_ => return None,
}

let request = self.call::<lsp::request::CodeLensResolve>(code_lens);
Some(async move {
let json = request.await?;
let response: Option<lsp::CodeLens> = serde_json::from_value(json)?;
Ok(response)
})
}
}
2 changes: 1 addition & 1 deletion helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ impl MappableCommand {
paste_before, "Paste before selection",
paste_clipboard_after, "Paste clipboard after selections",
code_lens_under_cursor, "Show code lenses under cursor",
code_lenses_picker, "Show code lense picker",
request_code_lenses, "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
48 changes: 7 additions & 41 deletions helix-term/src/commands/lsp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1708,17 +1708,7 @@ pub fn code_lens_under_cursor(cx: &mut Context) {
// TODO: fix the check
cl.range.start.line == pos.line
})
.map(|cl| {
// if cl.command.is_none() {
// if let Some(req) = language_server.code_lens_resolve(cl.clone()) {
// if let Some(code_lens) = block_on(req).ok().unwrap() {
// log::info!("code_lense: resolved {:?} into {:?}", cl, code_lens);
// return map_code_lens(doc, &code_lens);
// }
// }
// }
map_code_lens(doc_text, cl, offset_encoding, language_server.id())
})
.map(|cl| map_code_lens(doc_text, cl, offset_encoding, language_server.id()))
.collect();

if lenses.is_empty() {
Expand Down Expand Up @@ -1764,7 +1754,9 @@ pub fn code_lens_under_cursor(cx: &mut Context) {
};
}

pub fn code_lenses_picker(cx: &mut Context) {
// TODO: should be run the same way as diagnostic - shouldn't require manual
// trigger to set lenses.
pub fn request_code_lenses(cx: &mut Context) {
let doc = doc!(cx.editor);

let language_server =
Expand All @@ -1787,6 +1779,8 @@ pub fn code_lenses_picker(cx: &mut Context) {
request,
move |editor, compositor, lenses: Option<Vec<lsp::CodeLens>>| {
if let Some(lenses) = lenses {
log::error!("lenses got: {:?}", lenses);

let doc = doc_mut!(editor, &doc_id);
let doc_text = doc.text();
if let Some(current_url) = doc.url() {
Expand All @@ -1797,36 +1791,8 @@ pub fn code_lenses_picker(cx: &mut Context) {
.iter()
.map(|l| map_code_lens(doc_text, l, offset_enc, language_server_id))
.collect();
log::error!("lenses got: {:?}", lenses);
doc.set_code_lens(lenses.clone());
let picker = Picker::new(lenses, (), |cx, meta, _action| {
let doc = doc!(cx.editor);
let language_server = language_server_with_feature!(
cx.editor,
doc,
LanguageServerFeature::CodeLens
);

if let Some(cmd) = meta.command.clone() {
let future = match language_server.command(cmd) {
Some(future) => future,
None => {
cx.editor.set_error(
"Language server does not support executing commands",
);
return;
}
};
tokio::spawn(async move {
let res = future.await;

if let Err(e) = res {
log::error!("execute LSP command: {}", e);
}
});
}
});
compositor.push(Box::new(picker));
doc.set_code_lens(lenses.clone());
} else {
editor.set_status("no lens found");
}
Expand Down
2 changes: 1 addition & 1 deletion helix-term/src/keymap/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ pub fn default() -> HashMap<Mode, KeyTrie> {
"a" => code_action,
"'" => last_picker,
"l" => code_lens_under_cursor,
"L" => code_lenses_picker,
"L" => request_code_lenses,
"g" => { "Debug (experimental)" sticky=true
"l" => dap_launch,
"r" => dap_restart,
Expand Down
12 changes: 10 additions & 2 deletions helix-view/src/gutter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ pub fn diagnostic<'doc>(
let info = theme.get("info");
let hint = theme.get("hint");
let diagnostics = &doc.diagnostics;
let lenses = &doc.code_lens;

Box::new(
move |line: usize, _selected: bool, first_visual_line: bool, out: &mut String| {
Expand All @@ -73,15 +74,22 @@ pub fn diagnostic<'doc>(
.language_servers_with_feature(LanguageServerFeature::Diagnostics)
.any(|ls| ls.id() == d.language_server_id)
});
diagnostics_on_line.max_by_key(|d| d.severity).map(|d| {
if let Some(style) = diagnostics_on_line.max_by_key(|d| d.severity).map(|d| {
write!(out, "●").ok();
match d.severity {
Some(Severity::Error) => error,
Some(Severity::Warning) | None => warning,
Some(Severity::Info) => info,
Some(Severity::Hint) => hint,
}
})
}) {
return Some(style);
};
if let Some(_) = lenses.iter().find(|l| l.line == line) {
write!(out, "▶").ok();
return Some(info);
}
None
},
)
}
Expand Down
2 changes: 2 additions & 0 deletions helix-view/src/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,8 @@ impl View {
add_annotations(other_inlay_hints, other_style);
add_annotations(padding_after_inlay_hints, None);

// TODO: lens line annotation

text_annotations
}

Expand Down
2 changes: 2 additions & 0 deletions runtime/themes/dracula_at_night.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@
"markup.quote" = { fg = "yellow", modifiers = ["italic"] }
"markup.raw" = { fg = "foreground" }

"code_lens" = { underline = { color = "purple", style = "curl" } }

[palette]
background = "#0e1419"
background_dark = "#21222c"
Expand Down
2 changes: 1 addition & 1 deletion theme.toml
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ label = "honey"
"diagnostic.warning" = { underline = { color = "lightning", style = "curl" } }
"diagnostic.error" = { underline = { color = "apricot", style = "curl" } }

"code_lens" = { modifiers = ["underline"] }
"code_lens" = { underline = { color = "delta", style = "curl" } }

warning = "lightning"
error = "apricot"
Expand Down

0 comments on commit 4d7203d

Please sign in to comment.