on master help sign in
fix: Allow multi-line prompt documentation
commit2af04325d83cd0141400951252574666cffdf1af authorBlaž Hrastnik <blaz@mxxn.io> dateSun 20 Feb 2022, 14:44 (5 years ago) parenta449156
3 files changed+30−19
helix-term/src/ui/markdown.rs+2−13 helix-term/src/ui/prompt.rs+11−5 helix-term/src/ui/text.rs+17−1
helix-term/src/ui/markdown.rs+2−13
@@ -247,19 +247,8 @@ impl Component for Markdown {
247247
248248 // TODO: account for tab width
249249 let max_text_width = (viewport.0 - padding).min(120);
250− let mut text_width = 0;
251− let mut height = padding;
252− for content in contents {
253− height += 1;
254− let content_width = content.width() as u16;
255− if content_width > max_text_width {
256− text_width = max_text_width;
257− height += content_width / max_text_width;
258− } else if content_width > text_width {
259− text_width = content_width;
260− }
261− }
250+ let (width, height) = crate::ui::text::required_size(&contents, max_text_width);
262251
263− Some((text_width + padding, height))
252+ Some((width + padding * 2, height))
264253 }
265254}
helix-term/src/ui/prompt.rs+11−5
@@ -389,12 +389,18 @@ impl Prompt {
389389 if let Some(doc) = (self.doc_fn)(&self.line) {
390390 let mut text = ui::Text::new(doc.to_string());
391391
392+ let max_width = BASE_WIDTH * 3;
393+ let padding = 1;
394+
392395 let viewport = area;
396+
397+ let (_width, height) = ui::text::required_size(&text.contents, max_width);
398+
393399 let area = viewport.intersection(Rect::new(
394400 completion_area.x,
395− completion_area.y.saturating_sub(3),
396− BASE_WIDTH * 3,
397− 3,
401+ completion_area.y.saturating_sub(height + padding * 2),
402+ max_width,
403+ height + padding * 2,
398404 ));
399405
400406 let background = theme.get("ui.help");
@@ -402,8 +408,8 @@ impl Prompt {
402408
403409 text.render(
404410 area.inner(&Margin {
405− vertical: 1,
406− horizontal: 1,
411+ vertical: padding,
412+ horizontal: padding,
407413 }),
408414 surface,
409415 cx,
helix-term/src/ui/text.rs+17−1
@@ -4,7 +4,7 @@ use tui::buffer::Buffer as Surface;
44use helix_view::graphics::Rect;
55
66pub struct Text {
7− contents: tui::text::Text<'static>,
7+ pub(crate) contents: tui::text::Text<'static>,
88 size: (u16, u16),
99 viewport: (u16, u16),
1010}
@@ -49,3 +49,19 @@ impl Component for Text {
4949 Some(self.size)
5050 }
5151}
52+
53+pub fn required_size(text: &tui::text::Text, max_text_width: u16) -> (u16, u16) {
54+ let mut text_width = 0;
55+ let mut height = 0;
56+ for content in &text.lines {
57+ height += 1;
58+ let content_width = content.width() as u16;
59+ if content_width > max_text_width {
60+ text_width = max_text_width;
61+ height += content_width / max_text_width;
62+ } else if content_width > text_width {
63+ text_width = content_width;
64+ }
65+ }
66+ (text_width, height)
67+}