From 1450ec8733916e442a1bf46d99b9042bc961031b Mon Sep 17 00:00:00 2001 From: Nikita Revenco <154856872+NikitaRevenco@users.noreply.github.com> Date: Fri, 6 Dec 2024 21:02:09 +0000 Subject: [PATCH] feat: add new `hover_dump` command --- helix-term/src/commands.rs | 1 + helix-term/src/commands/lsp.rs | 61 +++++++++++++++++++++++++++++++- helix-term/src/keymap/default.rs | 1 + helix-view/src/editor.rs | 2 +- 4 files changed, 63 insertions(+), 2 deletions(-) diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 23d2b246d..4131a65d3 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -496,6 +496,7 @@ impl MappableCommand { remove_primary_selection, "Remove primary selection", completion, "Invoke completion popup", hover, "Show docs for item under cursor", + hover_dump, "Show docs for item under cursor in a new buffer", toggle_comments, "Comment/uncomment selections", toggle_line_comments, "Line comment/uncomment selections", toggle_block_comments, "Block comment/uncomment selections", diff --git a/helix-term/src/commands/lsp.rs b/helix-term/src/commands/lsp.rs index 130428d4d..2b6cf124a 100644 --- a/helix-term/src/commands/lsp.rs +++ b/helix-term/src/commands/lsp.rs @@ -14,7 +14,7 @@ use tui::{text::Span, widgets::Row}; use super::{align_view, push_jump, Align, Context, Editor}; use helix_core::{ - syntax::LanguageServerFeature, text_annotations::InlineAnnotation, Selection, Uri, + syntax::LanguageServerFeature, text_annotations::InlineAnnotation, Rope, Selection, Uri, }; use helix_stdx::path; use helix_view::{ @@ -1036,9 +1036,16 @@ pub fn signature_help(cx: &mut Context) { .trigger_signature_help(SignatureHelpInvoked::Manual, cx.editor) } +// <<<(cx: &mut Context, callback: T) + // where + // T: FnOnce(String, &mut Editor, &mut Compositor) + std::marker::Send + 'static, + // { + // >>>>a>>> 0e043a81 (feat: add new `hover_dump` command) let (view, doc) = current!(cx.editor); if doc .language_servers_with_feature(LanguageServerFeature::Hover) @@ -1062,10 +1069,43 @@ pub fn hover(cx: &mut Context) { .text_document_hover(doc.identifier(), pos, None) .unwrap(); + // <<<<<<< HEAD async move { let json = request.await?; let response = serde_json::from_value::>(json)?; anyhow::Ok((server_name, response)) + // ======= + // cx.callback( + // future, + // move |editor, compositor, response: Option| { + // if let Some(hover) = response { + // // hover.contents / .range <- used for visualizing + + // fn marked_string_to_markdown(contents: lsp::MarkedString) -> String { + // match contents { + // lsp::MarkedString::String(contents) => contents, + // lsp::MarkedString::LanguageString(string) => { + // if string.language == "markdown" { + // string.value + // } else { + // format!("```{}\n{}\n```", string.language, string.value) + // } + // } + // } + // } + + // let contents = match hover.contents { + // lsp::HoverContents::Scalar(contents) => marked_string_to_markdown(contents), + // lsp::HoverContents::Array(contents) => contents + // .into_iter() + // .map(marked_string_to_markdown) + // .collect::>() + // .join("\n\n"), + // lsp::HoverContents::Markup(contents) => contents.value, + // }; + + // callback(contents, editor, compositor); + // >>>>>>> 0e043a81 (feat: add new `hover_dump` command) } }) .collect(); @@ -1094,6 +1134,25 @@ pub fn hover(cx: &mut Context) { }); } +pub fn hover_dump(cx: &mut Context) { + hover_impl(cx, |contents, editor, _compositor| { + editor.new_file_from_document( + Action::VerticalSplit, + Document::from(Rope::from(contents), None, editor.config.clone()), + ); + let (_view, doc) = current!(editor); + if let Ok(_) = doc.set_language_by_language_id("markdown", editor.syn_loader.clone()) {}; + }) +} + +pub fn hover(cx: &mut Context) { + hover_impl(cx, |contents, editor, compositor| { + let contents = ui::Markdown::new(contents, editor.syn_loader.clone()); + let popup = Popup::new("hover", contents).auto_close(true); + compositor.replace_or_push("hover", popup); + }) +} + pub fn rename_symbol(cx: &mut Context) { fn get_prefill_from_word_boundary(editor: &Editor) -> String { let (view, doc) = current_ref!(editor); diff --git a/helix-term/src/keymap/default.rs b/helix-term/src/keymap/default.rs index e160b2246..af9bb2329 100644 --- a/helix-term/src/keymap/default.rs +++ b/helix-term/src/keymap/default.rs @@ -283,6 +283,7 @@ pub fn default() -> HashMap { "R" => replace_selections_with_clipboard, "/" => global_search, "k" => hover, + "K" => hover_dump, "r" => rename_symbol, "h" => select_references_to_symbol_under_cursor, "c" => toggle_comments, diff --git a/helix-view/src/editor.rs b/helix-view/src/editor.rs index 00fe719d9..249862073 100644 --- a/helix-view/src/editor.rs +++ b/helix-view/src/editor.rs @@ -1708,7 +1708,7 @@ impl Editor { id } - fn new_file_from_document(&mut self, action: Action, doc: Document) -> DocumentId { + pub fn new_file_from_document(&mut self, action: Action, doc: Document) -> DocumentId { let id = self.new_document(doc); self.switch(id, action); id