Skip to content

Commit

Permalink
feat: colored text for message popup
Browse files Browse the repository at this point in the history
  • Loading branch information
NikitaRevenco committed Dec 14, 2024
1 parent f488f4d commit d11550b
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
7 changes: 4 additions & 3 deletions helix-term/src/ui/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ use crate::{
keymap::{KeymapResult, Keymaps},
ui::{
document::{render_document, LinePos, TextRenderer},
markdown::StyledText,
statusline,
text_decorations::{self, Decoration, DecorationManager, InlineDiagnostics},
Completion, Markdown, Popup, ProgressSpinners,
Completion, Popup, ProgressSpinners,
},
};

Expand Down Expand Up @@ -1538,8 +1539,8 @@ impl Component for EditorView {
let call: job::Callback = Callback::EditorCompositor(Box::new(
move |editor: &mut Editor, compositor: &mut Compositor| {
if let Some((contents, _)) = &editor.status_msg {
let contents =
Markdown::new(contents.to_string(), editor.syn_loader.clone());
let contents = StyledText::new(contents.to_string(), style);

let popup = Popup::new("hover", contents).auto_close(true);
compositor.replace_or_push("hover", popup);
}
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

0 comments on commit d11550b

Please sign in to comment.