on master help sign in
fix: remove unneeded allocations when calling helix_view::Info::new
b58899bAlexis (Poliorcetics) Bourget
helix-term/src/commands.rs+1−7
@@ -4455,13 +4455,7 @@ fn select_textobject(cx: &mut Context, objtype: textobject::TextObject) {
4455 (" ", "... or any character acting as a pair"),
4455 (" ", "... or any character acting as a pair"),
4456 ];
4456 ];
4457
4457
4458− cx.editor.autoinfo = Some(Info::new(
4458+ cx.editor.autoinfo = Some(Info::new(title, &help_text));
4459− title,
4460− help_text
4461− .into_iter()
4462− .map(|(col1, col2)| (col1.to_string(), col2.to_string()))
4463− .collect(),
4464− ));
4465}
4459}
4466
4460
4467fn surround_add(cx: &mut Context) {
4461fn surround_add(cx: &mut Context) {
helix-view/src/info.rs+23−10
@@ -16,7 +16,11 @@ pub struct Info {
16}
16}
17
17
18impl Info {
18impl Info {
19− pub fn new(title: &str, body: Vec<(String, String)>) -> Self {
19+ pub fn new<T, U>(title: &str, body: &[(T, U)]) -> Self
20+ where
21+ T: AsRef<str>,
22+ U: AsRef<str>,
23+ {
20 if body.is_empty() {
24 if body.is_empty() {
21 return Self {
25 return Self {
22 title: title.to_string(),
26 title: title.to_string(),
@@ -26,11 +30,21 @@ impl Info {
26 };
30 };
27 }
31 }
28
32
29− let item_width = body.iter().map(|(item, _)| item.width()).max().unwrap();
33+ let item_width = body
34+ .iter()
35+ .map(|(item, _)| item.as_ref().width())
36+ .max()
37+ .unwrap();
30 let mut text = String::new();
38 let mut text = String::new();
31
39
32− for (item, desc) in &body {
40+ for (item, desc) in body {
41+ let _ = writeln!(
42+ text,
43+ "{:width$} {}",
44+ item.as_ref(),
45+ desc.as_ref(),
33− let _ = writeln!(text, "{:width$} {}", item, desc, width = item_width);
46+ width = item_width
47+ );
34 }
48 }
35
49
36 Self {
50 Self {
@@ -42,19 +56,19 @@ impl Info {
42 }
56 }
43
57
44 pub fn from_keymap(title: &str, body: Vec<(&str, BTreeSet<KeyEvent>)>) -> Self {
58 pub fn from_keymap(title: &str, body: Vec<(&str, BTreeSet<KeyEvent>)>) -> Self {
45− let body = body
59+ let body: Vec<_> = body
46 .into_iter()
60 .into_iter()
47 .map(|(desc, events)| {
61 .map(|(desc, events)| {
48 let events = events.iter().map(ToString::to_string).collect::<Vec<_>>();
62 let events = events.iter().map(ToString::to_string).collect::<Vec<_>>();
49− (events.join(", "), desc.to_string())
63+ (events.join(", "), desc)
50 })
64 })
51 .collect();
65 .collect();
52
66
53− Self::new(title, body)
67+ Self::new(title, &body)
54 }
68 }
55
69
56 pub fn from_registers(registers: &Registers) -> Self {
70 pub fn from_registers(registers: &Registers) -> Self {
57− let body = registers
71+ let body: Vec<_> = registers
58 .inner()
72 .inner()
59 .iter()
73 .iter()
60 .map(|(ch, reg)| {
74 .map(|(ch, reg)| {
@@ -62,13 +76,12 @@ impl Info {
62 .read()
76 .read()
63 .get(0)
77 .get(0)
64 .and_then(|s| s.lines().next())
78 .and_then(|s| s.lines().next())
65− .map(String::from)
66 .unwrap_or_default();
79 .unwrap_or_default();
67 (ch.to_string(), content)
80 (ch.to_string(), content)
68 })
81 })
69 .collect();
82 .collect();
70
83
71− let mut infobox = Self::new("Registers", body);
84+ let mut infobox = Self::new("Registers", &body);
72 infobox.width = 30; // copied content could be very long
85 infobox.width = 30; // copied content could be very long
73 infobox
86 infobox
74 }
87 }