Skip to content

Commit

Permalink
merge 12208: NikitaRevenco/hover_buffer
Browse files Browse the repository at this point in the history
feat: add new `hover_dump` command

docs: for hover_dump

chore: appease clippy

docs: generate entry for `hover_dump`

refactor: use doc_mut! macro
  • Loading branch information
Axlefublr committed Jan 23, 2025
1 parent 5e2322a commit efda706
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 8 deletions.
1 change: 1 addition & 0 deletions book/src/generated/static-cmd.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@
| `remove_primary_selection` | Remove primary selection | normal: `` <A-,> ``, select: `` <A-,> `` |
| `completion` | Invoke completion popup | insert: `` <C-x> `` |
| `hover` | Show docs for item under cursor | normal: `` <space>k ``, select: `` <space>k `` |
| `hover_dump` | Show docs for item under cursor in a new buffer | normal: `` <space>K ``, select: `` <space>K `` |
| `toggle_comments` | Comment/uncomment selections | normal: `` <C-c> ``, `` <space>c ``, select: `` <C-c> ``, `` <space>c `` |
| `toggle_line_comments` | Line comment/uncomment selections | normal: `` <space><A-c> ``, select: `` <space><A-c> `` |
| `toggle_block_comments` | Block comment/uncomment selections | normal: `` <space>C ``, select: `` <space>C `` |
Expand Down
1 change: 1 addition & 0 deletions book/src/keymap.md
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@ This layer is a kludge of mappings, mostly pickers.
| `g` | Open changed file picker | `changed_file_picker` |
| `G` | Debug (experimental) | N/A |
| `k` | Show documentation for item under cursor in a [popup](#popup) (**LSP**) | `hover` |
| `K` | Show documentation for item under cursor in a new buffer (**LSP**) | `hover_dump` |
| `s` | Open document symbol picker (**LSP**) | `symbol_picker` |
| `S` | Open workspace symbol picker (**LSP**) | `workspace_symbol_picker` |
| `d` | Open document diagnostics picker (**LSP**) | `diagnostics_picker` |
Expand Down
1 change: 1 addition & 0 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,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",
Expand Down
32 changes: 25 additions & 7 deletions helix-term/src/commands/lsp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -1008,7 +1008,10 @@ pub fn signature_help(cx: &mut Context) {
.trigger_signature_help(SignatureHelpInvoked::Manual, cx.editor)
}

pub fn hover(cx: &mut Context) {
pub fn hover_impl<T>(cx: &mut Context, callback: T)
where
T: FnOnce(String, &mut Editor, &mut Compositor) + std::marker::Send + 'static,
{
let (view, doc) = current!(cx.editor);

// TODO support multiple language servers (merge UI somehow)
Expand Down Expand Up @@ -1049,16 +1052,31 @@ pub fn hover(cx: &mut Context) {
lsp::HoverContents::Markup(contents) => contents.value,
};

// skip if contents empty

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);
callback(contents, editor, compositor);
}
},
);
}

pub fn hover_dump(cx: &mut Context) {
hover_impl(cx, |contents, editor, _compositor| {
editor.new_file_from_document(
Action::HorizontalSplit,
Document::from(Rope::from(contents), None, editor.config.clone()),
);
let hover_doc = doc_mut!(editor);
let _ = hover_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);
Expand Down
1 change: 1 addition & 0 deletions helix-term/src/keymap/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ pub fn default() -> HashMap<Mode, KeyTrie> {
"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,
Expand Down
2 changes: 1 addition & 1 deletion helix-view/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1771,7 +1771,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
Expand Down

0 comments on commit efda706

Please sign in to comment.