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: display long status messages as popups #12241

Open
wants to merge 2 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
36 changes: 28 additions & 8 deletions helix-term/src/ui/editor.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
use crate::{
commands::{self, OnKeyCallback, OnKeyCallbackKind},
compositor::{Component, Context, Event, EventResult},
compositor::{Component, Compositor, Context, Event, EventResult},
events::{OnModeSwitch, PostCommand},
handlers::completion::CompletionItem,
job::{self, Callback},
key,
keymap::{KeymapResult, Keymaps},
ui::{
document::{render_document, LinePos, TextRenderer},
markdown::StyledText,
statusline,
text_decorations::{self, Decoration, DecorationManager, InlineDiagnostics},
Completion, ProgressSpinners,
Completion, Popup, ProgressSpinners,
},
};

Expand Down Expand Up @@ -1588,12 +1590,30 @@ impl Component for EditorView {
cx.editor.theme.get("ui.text")
};

surface.set_string(
area.x,
area.y + area.height.saturating_sub(1),
status_msg,
style,
);
if status_msg.len() <= area.width.into() {
surface.set_string(
area.x,
area.y + area.height.saturating_sub(1),
status_msg,
style,
);
} else {
let status_msg_box = async move {
let call: job::Callback = Callback::EditorCompositor(Box::new(
move |editor: &mut Editor, compositor: &mut Compositor| {
if let Some((contents, _)) = &editor.status_msg {
let contents = StyledText::new(contents.to_string(), style);

let popup = Popup::new("hover", contents).auto_close(true);
compositor.replace_or_push("hover", popup);
}
},
));
Ok(call)
};

cx.jobs.callback(status_msg_box)
}
}

if area.width.saturating_sub(status_msg_width as u16) > key_width {
Expand Down
41 changes: 40 additions & 1 deletion helix-term/src/ui/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use tui::{
text::{Span, Spans, Text},
};

use std::sync::Arc;
use std::{borrow::Cow, sync::Arc};

use pulldown_cmark::{CodeBlockKind, Event, HeadingLevel, Options, Parser, Tag, TagEnd};

Expand Down Expand Up @@ -126,6 +126,45 @@ pub struct Markdown {
config_loader: Arc<ArcSwap<syntax::Loader>>,
}

pub struct StyledText {
text: tui::text::Text<'static>,
}

impl StyledText {
pub fn new(contents: String, style: Style) -> Self {
let spans: Vec<Spans<'static>> = contents
.lines()
.map(|line| Spans::from(vec![Span::styled(Cow::Owned(line.to_string()), style)]))
.collect();

let text = Text::from(spans);

Self { text }
}
}

impl Component for StyledText {
fn render(&mut self, area: Rect, surface: &mut Surface, cx: &mut Context) {
use tui::widgets::{Paragraph, Widget, Wrap};

let par = Paragraph::new(&self.text)
.wrap(Wrap { trim: false })
.scroll((cx.scroll.unwrap_or_default() as u16, 0));

let margin = Margin::all(1);
par.render(area.inner(margin), surface);
}

fn required_size(&mut self, viewport: (u16, u16)) -> Option<(u16, u16)> {
let padding = 2;

let max_text_width = (viewport.0.saturating_sub(padding)).min(120);
let (width, height) = crate::ui::text::required_size(&self.text, max_text_width);

Some((width + padding, height + padding))
}
}

// TODO: pre-render and self reference via Pin
// better yet, just use Tendril + subtendril for references

Expand Down
Loading