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

feat: new command to open hover documentation in a new buffer instead of a popup #12208

Open
wants to merge 5 commits 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
1 change: 1 addition & 0 deletions book/src/generated/static-cmd.md
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,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 @@ -476,6 +476,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::VerticalSplit,
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 @@ -281,6 +281,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 @@ -1689,7 +1689,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
Loading