on master help sign in
ui: prompt: Avoid allocating a prompt name if it's a static string
commita3bd80a6fa4f07b0fa581eae416376763ba94345 authorBlaž Hrastnik <blaz@mxxn.io> dateTue 31 Aug 2021, 18:29 (5 years ago) parent9b96bb5
4 files changed+21−26
helix-term/src/commands.rs+17−22 helix-term/src/ui/mod.rs+1−1 helix-term/src/ui/picker.rs+1−1 helix-term/src/ui/prompt.rs+2−2
helix-term/src/commands.rs+17−22
@@ -1079,7 +1079,7 @@ fn select_all(cx: &mut Context) {
10791079}
10801080
10811081fn 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| {
10831083 let text = doc.text().slice(..);
10841084 if let Some(selection) = selection::select_on_matches(text, doc.selection(view.id), &regex)
10851085 {
@@ -1091,7 +1091,7 @@ fn select_regex(cx: &mut Context) {
10911091}
10921092
10931093fn 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| {
10951095 let text = doc.text().slice(..);
10961096 let selection = selection::split_on_matches(text, doc.selection(view.id), &regex);
10971097 doc.set_selection(view.id, selection);
@@ -1157,15 +1157,11 @@ fn search(cx: &mut Context) {
11571157 // feed chunks into the regex yet
11581158 let contents = doc.text().slice(..).to_string();
11591159
1160− let prompt = ui::regex_prompt(
1161− cx,
1162− "search:".to_string(),
1163− move |view, doc, registers, regex| {
1164− search_impl(doc, view, &contents, &regex, false);
1165− // TODO: only store on enter (accept), not update
1166− registers.write('/', vec![regex.as_str().to_string()]);
1167− },
1168− );
1160+ let prompt = ui::regex_prompt(cx, "search:".into(), move |view, doc, registers, regex| {
1161+ search_impl(doc, view, &contents, &regex, false);
1162+ // TODO: only store on enter (accept), not update
1163+ registers.write('/', vec![regex.as_str().to_string()]);
1164+ });
11691165
11701166 cx.push_layer(Box::new(prompt));
11711167}
@@ -2210,7 +2206,7 @@ mod cmd {
22102206
22112207fn command_mode(cx: &mut Context) {
22122208 let mut prompt = Prompt::new(
2213− ":".to_owned(),
2209+ ":".into(),
22142210 Some(':'),
22152211 |input: &str| {
22162212 // we use .this over split_whitespace() because we care about empty segments
@@ -3819,7 +3815,7 @@ fn join_selections(cx: &mut Context) {
38193815
38203816fn keep_selections(cx: &mut Context) {
38213817 // 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| {
38233819 let text = doc.text().slice(..);
38243820
38253821 if let Some(selection) = selection::keep_matches(text, doc.selection(view.id), &regex) {
@@ -4307,26 +4303,26 @@ enum ShellBehavior {
43074303}
43084304
43094305fn shell_pipe(cx: &mut Context) {
4310− shell(cx, "pipe:", ShellBehavior::Replace);
4306+ shell(cx, "pipe:".into(), ShellBehavior::Replace);
43114307}
43124308
43134309fn shell_pipe_to(cx: &mut Context) {
4314− shell(cx, "pipe-to:", ShellBehavior::Ignore);
4310+ shell(cx, "pipe-to:".into(), ShellBehavior::Ignore);
43154311}
43164312
43174313fn shell_insert_output(cx: &mut Context) {
4318− shell(cx, "insert-output:", ShellBehavior::Insert);
4314+ shell(cx, "insert-output:".into(), ShellBehavior::Insert);
43194315}
43204316
43214317fn shell_append_output(cx: &mut Context) {
4322− shell(cx, "append-output:", ShellBehavior::Append);
4318+ shell(cx, "append-output:".into(), ShellBehavior::Append);
43234319}
43244320
43254321fn shell_keep_pipe(cx: &mut Context) {
4326− shell(cx, "keep-pipe:", ShellBehavior::Filter);
4322+ shell(cx, "keep-pipe:".into(), ShellBehavior::Filter);
43274323}
43284324
4329−fn shell(cx: &mut Context, prompt: &str, behavior: ShellBehavior) {
4325+fn shell(cx: &mut Context, prompt: Cow<'static, str>, behavior: ShellBehavior) {
43304326 use std::io::Write;
43314327 use std::process::{Command, Stdio};
43324328 if cx.editor.config.shell.is_empty() {
@@ -4338,7 +4334,7 @@ fn shell(cx: &mut Context, prompt: &str, behavior: ShellBehavior) {
43384334 ShellBehavior::Insert | ShellBehavior::Append => false,
43394335 };
43404336 let prompt = Prompt::new(
4341− prompt.to_owned(),
4337+ prompt,
43424338 Some('|'),
43434339 |_input: &str| Vec::new(),
43444340 move |cx: &mut compositor::Context, input: &str, event: PromptEvent| {
@@ -4408,9 +4404,8 @@ fn shell(cx: &mut Context, prompt: &str, behavior: ShellBehavior) {
44084404 }
44094405 }
44104406
4411− let transaction = Transaction::change(doc.text(), changes.into_iter());
4412−
44134407 if behavior != ShellBehavior::Ignore {
4408+ let transaction = Transaction::change(doc.text(), changes.into_iter());
44144409 doc.apply(&transaction, view.id);
44154410 doc.append_changes_to_history(view.id);
44164411 }
helix-term/src/ui/mod.rs+1−1
@@ -27,7 +27,7 @@ use std::path::PathBuf;
2727
2828pub fn regex_prompt(
2929 cx: &mut crate::commands::Context,
30− prompt: String,
30+ prompt: std::borrow::Cow<'static, str>,
3131 fun: impl Fn(&mut View, &mut Document, &mut Registers, Regex) + 'static,
3232) -> Prompt {
3333 let (view, doc) = current!(cx.editor);
helix-term/src/ui/picker.rs+1−1
@@ -202,7 +202,7 @@ impl<T> Picker<T> {
202202 callback_fn: impl Fn(&mut Editor, &T, Action) + 'static,
203203 ) -> Self {
204204 let prompt = Prompt::new(
205− "".to_string(),
205+ "".into(),
206206 None,
207207 |_pattern: &str| Vec::new(),
208208 |_editor: &mut Context, _pattern: &str, _event: PromptEvent| {
helix-term/src/ui/prompt.rs+2−2
@@ -15,7 +15,7 @@ use helix_view::{
1515pub type Completion = (RangeFrom<usize>, Cow<'static, str>);
1616
1717pub struct Prompt {
18− prompt: String,
18+ prompt: Cow<'static, str>,
1919 pub line: String,
2020 cursor: usize,
2121 completion: Vec<Completion>,
@@ -55,7 +55,7 @@ pub enum Movement {
5555
5656impl Prompt {
5757 pub fn new(
58− prompt: String,
58+ prompt: Cow<'static, str>,
5959 history_register: Option<char>,
6060 mut completion_fn: impl FnMut(&str) -> Vec<Completion> + 'static,
6161 callback_fn: impl FnMut(&mut Context, &str, PromptEvent) + 'static,