on master help sign in
ui: prompt: Avoid allocating a prompt name if it's a static string
a3bd80aBlaž Hrastnik
helix-term/src/commands.rs+17−22
@@ -1079,7 +1079,7 @@ fn select_all(cx: &mut Context) {
1079}
1079}
1080
1080
1081fn select_regex(cx: &mut Context) {
1081fn select_regex(cx: &mut Context) {
1082− let prompt = ui::regex_prompt(cx, "select:".to_string(), move |view, doc, _, regex| {
1082+ let prompt = ui::regex_prompt(cx, "select:".into(), move |view, doc, _, regex| {
1083 let text = doc.text().slice(..);
1083 let text = doc.text().slice(..);
1084 if let Some(selection) = selection::select_on_matches(text, doc.selection(view.id), &regex)
1084 if let Some(selection) = selection::select_on_matches(text, doc.selection(view.id), &regex)
1085 {
1085 {
@@ -1091,7 +1091,7 @@ fn select_regex(cx: &mut Context) {
1091}
1091}
1092
1092
1093fn split_selection(cx: &mut Context) {
1093fn split_selection(cx: &mut Context) {
1094− let prompt = ui::regex_prompt(cx, "split:".to_string(), move |view, doc, _, regex| {
1094+ let prompt = ui::regex_prompt(cx, "split:".into(), move |view, doc, _, regex| {
1095 let text = doc.text().slice(..);
1095 let text = doc.text().slice(..);
1096 let selection = selection::split_on_matches(text, doc.selection(view.id), &regex);
1096 let selection = selection::split_on_matches(text, doc.selection(view.id), &regex);
1097 doc.set_selection(view.id, selection);
1097 doc.set_selection(view.id, selection);
@@ -1157,15 +1157,11 @@ fn search(cx: &mut Context) {
1157 // feed chunks into the regex yet
1157 // feed chunks into the regex yet
1158 let contents = doc.text().slice(..).to_string();
1158 let contents = doc.text().slice(..).to_string();
1159
1159
1160− let prompt = ui::regex_prompt(
1161− cx,
1162− "search:".to_string(),
1163− move |view, doc, registers, regex| {
1160+ let prompt = ui::regex_prompt(cx, "search:".into(), move |view, doc, registers, regex| {
1164− search_impl(doc, view, &contents, &regex, false);
1161+ search_impl(doc, view, &contents, &regex, false);
1165− // TODO: only store on enter (accept), not update
1162+ // TODO: only store on enter (accept), not update
1166− registers.write('/', vec![regex.as_str().to_string()]);
1163+ registers.write('/', vec![regex.as_str().to_string()]);
1167− },
1168− );
1164+ });
1169
1165
1170 cx.push_layer(Box::new(prompt));
1166 cx.push_layer(Box::new(prompt));
1171}
1167}
@@ -2210,7 +2206,7 @@ mod cmd {
2210
2206
2211fn command_mode(cx: &mut Context) {
2207fn command_mode(cx: &mut Context) {
2212 let mut prompt = Prompt::new(
2208 let mut prompt = Prompt::new(
2213− ":".to_owned(),
2209+ ":".into(),
2214 Some(':'),
2210 Some(':'),
2215 |input: &str| {
2211 |input: &str| {
2216 // we use .this over split_whitespace() because we care about empty segments
2212 // we use .this over split_whitespace() because we care about empty segments
@@ -3819,7 +3815,7 @@ fn join_selections(cx: &mut Context) {
3819
3815
3820fn keep_selections(cx: &mut Context) {
3816fn keep_selections(cx: &mut Context) {
3821 // keep selections matching regex
3817 // keep selections matching regex
3822− let prompt = ui::regex_prompt(cx, "keep:".to_string(), move |view, doc, _, regex| {
3818+ let prompt = ui::regex_prompt(cx, "keep:".into(), move |view, doc, _, regex| {
3823 let text = doc.text().slice(..);
3819 let text = doc.text().slice(..);
3824
3820
3825 if let Some(selection) = selection::keep_matches(text, doc.selection(view.id), &regex) {
3821 if let Some(selection) = selection::keep_matches(text, doc.selection(view.id), &regex) {
@@ -4307,26 +4303,26 @@ enum ShellBehavior {
4307}
4303}
4308
4304
4309fn shell_pipe(cx: &mut Context) {
4305fn shell_pipe(cx: &mut Context) {
4310− shell(cx, "pipe:", ShellBehavior::Replace);
4306+ shell(cx, "pipe:".into(), ShellBehavior::Replace);
4311}
4307}
4312
4308
4313fn shell_pipe_to(cx: &mut Context) {
4309fn shell_pipe_to(cx: &mut Context) {
4314− shell(cx, "pipe-to:", ShellBehavior::Ignore);
4310+ shell(cx, "pipe-to:".into(), ShellBehavior::Ignore);
4315}
4311}
4316
4312
4317fn shell_insert_output(cx: &mut Context) {
4313fn shell_insert_output(cx: &mut Context) {
4318− shell(cx, "insert-output:", ShellBehavior::Insert);
4314+ shell(cx, "insert-output:".into(), ShellBehavior::Insert);
4319}
4315}
4320
4316
4321fn shell_append_output(cx: &mut Context) {
4317fn shell_append_output(cx: &mut Context) {
4322− shell(cx, "append-output:", ShellBehavior::Append);
4318+ shell(cx, "append-output:".into(), ShellBehavior::Append);
4323}
4319}
4324
4320
4325fn shell_keep_pipe(cx: &mut Context) {
4321fn shell_keep_pipe(cx: &mut Context) {
4326− shell(cx, "keep-pipe:", ShellBehavior::Filter);
4322+ shell(cx, "keep-pipe:".into(), ShellBehavior::Filter);
4327}
4323}
4328
4324
4329−fn shell(cx: &mut Context, prompt: &str, behavior: ShellBehavior) {
4325+fn shell(cx: &mut Context, prompt: Cow<'static, str>, behavior: ShellBehavior) {
4330 use std::io::Write;
4326 use std::io::Write;
4331 use std::process::{Command, Stdio};
4327 use std::process::{Command, Stdio};
4332 if cx.editor.config.shell.is_empty() {
4328 if cx.editor.config.shell.is_empty() {
@@ -4338,7 +4334,7 @@ fn shell(cx: &mut Context, prompt: &str, behavior: ShellBehavior) {
4338 ShellBehavior::Insert | ShellBehavior::Append => false,
4334 ShellBehavior::Insert | ShellBehavior::Append => false,
4339 };
4335 };
4340 let prompt = Prompt::new(
4336 let prompt = Prompt::new(
4341− prompt.to_owned(),
4337+ prompt,
4342 Some('|'),
4338 Some('|'),
4343 |_input: &str| Vec::new(),
4339 |_input: &str| Vec::new(),
4344 move |cx: &mut compositor::Context, input: &str, event: PromptEvent| {
4340 move |cx: &mut compositor::Context, input: &str, event: PromptEvent| {
@@ -4408,9 +4404,8 @@ fn shell(cx: &mut Context, prompt: &str, behavior: ShellBehavior) {
4408 }
4404 }
4409 }
4405 }
4410
4406
4411− let transaction = Transaction::change(doc.text(), changes.into_iter());
4412−
4413 if behavior != ShellBehavior::Ignore {
4407 if behavior != ShellBehavior::Ignore {
4408+ let transaction = Transaction::change(doc.text(), changes.into_iter());
4414 doc.apply(&transaction, view.id);
4409 doc.apply(&transaction, view.id);
4415 doc.append_changes_to_history(view.id);
4410 doc.append_changes_to_history(view.id);
4416 }
4411 }
helix-term/src/ui/mod.rs+1−1
@@ -27,7 +27,7 @@ use std::path::PathBuf;
27
27
28pub fn regex_prompt(
28pub fn regex_prompt(
29 cx: &mut crate::commands::Context,
29 cx: &mut crate::commands::Context,
30− prompt: String,
30+ prompt: std::borrow::Cow<'static, str>,
31 fun: impl Fn(&mut View, &mut Document, &mut Registers, Regex) + 'static,
31 fun: impl Fn(&mut View, &mut Document, &mut Registers, Regex) + 'static,
32) -> Prompt {
32) -> Prompt {
33 let (view, doc) = current!(cx.editor);
33 let (view, doc) = current!(cx.editor);
helix-term/src/ui/picker.rs+1−1
@@ -202,7 +202,7 @@ impl<T> Picker<T> {
202 callback_fn: impl Fn(&mut Editor, &T, Action) + 'static,
202 callback_fn: impl Fn(&mut Editor, &T, Action) + 'static,
203 ) -> Self {
203 ) -> Self {
204 let prompt = Prompt::new(
204 let prompt = Prompt::new(
205− "".to_string(),
205+ "".into(),
206 None,
206 None,
207 |_pattern: &str| Vec::new(),
207 |_pattern: &str| Vec::new(),
208 |_editor: &mut Context, _pattern: &str, _event: PromptEvent| {
208 |_editor: &mut Context, _pattern: &str, _event: PromptEvent| {
helix-term/src/ui/prompt.rs+2−2
@@ -15,7 +15,7 @@ use helix_view::{
15pub type Completion = (RangeFrom<usize>, Cow<'static, str>);
15pub type Completion = (RangeFrom<usize>, Cow<'static, str>);
16
16
17pub struct Prompt {
17pub struct Prompt {
18− prompt: String,
18+ prompt: Cow<'static, str>,
19 pub line: String,
19 pub line: String,
20 cursor: usize,
20 cursor: usize,
21 completion: Vec<Completion>,
21 completion: Vec<Completion>,
@@ -55,7 +55,7 @@ pub enum Movement {
55
55
56impl Prompt {
56impl Prompt {
57 pub fn new(
57 pub fn new(
58− prompt: String,
58+ prompt: Cow<'static, str>,
59 history_register: Option<char>,
59 history_register: Option<char>,
60 mut completion_fn: impl FnMut(&str) -> Vec<Completion> + 'static,
60 mut completion_fn: impl FnMut(&str) -> Vec<Completion> + 'static,
61 callback_fn: impl FnMut(&mut Context, &str, PromptEvent) + 'static,
61 callback_fn: impl FnMut(&mut Context, &str, PromptEvent) + 'static,