Skip to content

Commit

Permalink
fix: Allow multi-line prompt documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
archseer committed Feb 20, 2022
1 parent a449156 commit 2af0432
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 19 deletions.
15 changes: 2 additions & 13 deletions helix-term/src/ui/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,19 +247,8 @@ impl Component for Markdown {

// TODO: account for tab width
let max_text_width = (viewport.0 - padding).min(120);
let mut text_width = 0;
let mut height = padding;
for content in contents {
height += 1;
let content_width = content.width() as u16;
if content_width > max_text_width {
text_width = max_text_width;
height += content_width / max_text_width;
} else if content_width > text_width {
text_width = content_width;
}
}
let (width, height) = crate::ui::text::required_size(&contents, max_text_width);

Some((text_width + padding, height))
Some((width + padding * 2, height))
}
}
16 changes: 11 additions & 5 deletions helix-term/src/ui/prompt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -389,21 +389,27 @@ impl Prompt {
if let Some(doc) = (self.doc_fn)(&self.line) {
let mut text = ui::Text::new(doc.to_string());

let max_width = BASE_WIDTH * 3;
let padding = 1;

let viewport = area;

let (_width, height) = ui::text::required_size(&text.contents, max_width);

let area = viewport.intersection(Rect::new(
completion_area.x,
completion_area.y.saturating_sub(3),
BASE_WIDTH * 3,
3,
completion_area.y.saturating_sub(height + padding * 2),
max_width,
height + padding * 2,
));

let background = theme.get("ui.help");
surface.clear_with(area, background);

text.render(
area.inner(&Margin {
vertical: 1,
horizontal: 1,
vertical: padding,
horizontal: padding,
}),
surface,
cx,
Expand Down
18 changes: 17 additions & 1 deletion helix-term/src/ui/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use tui::buffer::Buffer as Surface;
use helix_view::graphics::Rect;

pub struct Text {
contents: tui::text::Text<'static>,
pub(crate) contents: tui::text::Text<'static>,
size: (u16, u16),
viewport: (u16, u16),
}
Expand Down Expand Up @@ -49,3 +49,19 @@ impl Component for Text {
Some(self.size)
}
}

pub fn required_size(text: &tui::text::Text, max_text_width: u16) -> (u16, u16) {
let mut text_width = 0;
let mut height = 0;
for content in &text.lines {
height += 1;
let content_width = content.width() as u16;
if content_width > max_text_width {
text_width = max_text_width;
height += content_width / max_text_width;
} else if content_width > text_width {
text_width = content_width;
}
}
(text_width, height)
}

0 comments on commit 2af0432

Please sign in to comment.