Skip to content

Commit

Permalink
Avoid cloning the whole paragraph content just for rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
mo8it committed Feb 27, 2024
1 parent 6db666f commit 065ffe2
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 12 deletions.
3 changes: 2 additions & 1 deletion helix-term/src/ui/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,8 @@ impl EditorView {
}
}

let paragraph = Paragraph::new(lines)
let text = Text::from(lines);
let paragraph = Paragraph::new(&text)
.alignment(Alignment::Right)
.wrap(Wrap { trim: true });
let width = 100.min(viewport.width);
Expand Down
3 changes: 2 additions & 1 deletion helix-term/src/ui/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::compositor::{Component, Context};
use helix_view::graphics::{Margin, Rect};
use helix_view::info::Info;
use tui::buffer::Buffer as Surface;
use tui::text::Text;
use tui::widgets::{Block, Borders, Paragraph, Widget};

impl Component for Info {
Expand Down Expand Up @@ -31,7 +32,7 @@ impl Component for Info {
let inner = block.inner(area).inner(&margin);
block.render(area, surface);

Paragraph::new(self.text.as_str())
Paragraph::new(&Text::from(self.text.as_str()))
.style(text_style)
.render(inner, surface);
}
Expand Down
4 changes: 2 additions & 2 deletions helix-term/src/ui/lsp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ impl Component for SignatureHelp {
let (_, sig_text_height) = crate::ui::text::required_size(&sig_text, area.width);
let sig_text_area = area.clip_top(1).with_height(sig_text_height);
let sig_text_area = sig_text_area.inner(&margin).intersection(surface.area);
let sig_text_para = Paragraph::new(sig_text).wrap(Wrap { trim: false });
let sig_text_para = Paragraph::new(&sig_text).wrap(Wrap { trim: false });
sig_text_para.render(sig_text_area, surface);

if self.signature_doc.is_none() {
Expand All @@ -100,7 +100,7 @@ impl Component for SignatureHelp {
let sig_doc_area = area
.clip_top(sig_text_area.height + 2)
.clip_bottom(u16::from(cx.editor.popup_border()));
let sig_doc_para = Paragraph::new(sig_doc)
let sig_doc_para = Paragraph::new(&sig_doc)
.wrap(Wrap { trim: false })
.scroll((cx.scroll.unwrap_or_default() as u16, 0));
sig_doc_para.render(sig_doc_area.inner(&margin), surface);
Expand Down
2 changes: 1 addition & 1 deletion helix-term/src/ui/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ impl Component for Markdown {

let text = self.parse(Some(&cx.editor.theme));

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

Expand Down
2 changes: 1 addition & 1 deletion helix-term/src/ui/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl Component for Text {
fn render(&mut self, area: Rect, surface: &mut Surface, _cx: &mut Context) {
use tui::widgets::{Paragraph, Widget, Wrap};

let par = Paragraph::new(self.contents.clone()).wrap(Wrap { trim: false });
let par = Paragraph::new(&self.contents).wrap(Wrap { trim: false });
// .scroll(x, y) offsets

par.render(area, surface);
Expand Down
9 changes: 3 additions & 6 deletions helix-tui/src/widgets/paragraph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ pub struct Paragraph<'a> {
/// How to wrap the text
wrap: Option<Wrap>,
/// The text to display
text: Text<'a>,
text: &'a Text<'a>,
/// Scroll
scroll: (u16, u16),
/// Alignment of the text
Expand Down Expand Up @@ -92,15 +92,12 @@ pub struct Wrap {
}

impl<'a> Paragraph<'a> {
pub fn new<T>(text: T) -> Paragraph<'a>
where
T: Into<Text<'a>>,
{
pub fn new(text: &'a Text) -> Paragraph<'a> {
Paragraph {
block: None,
style: Default::default(),
wrap: None,
text: text.into(),
text,
scroll: (0, 0),
alignment: Alignment::Left,
}
Expand Down

0 comments on commit 065ffe2

Please sign in to comment.