on master help sign in
Allow static strings in set_status/set_error so API is nicer
commitd11b6521399f41cb4c0b75ebaa5770972f550ebb authorBlaž Hrastnik <blaz@mxxn.io> dateTue 15 Feb 2022, 16:45 (5 years ago) parentfd0e4b1
4 files changed+51−53
helix-term/src/application.rs+1−2 helix-term/src/commands.rs+31−33 helix-term/src/commands/dap.rs+10−12 helix-view/src/editor.rs+9−6
helix-term/src/application.rs+1−2
@@ -468,8 +468,7 @@ impl Application {
468468 // TODO: fetch breakpoints (in case we're attaching)
469469
470470 if debugger.configuration_done().await.is_ok() {
471− self.editor
472− .set_status("Debugged application started".to_owned());
471+ self.editor.set_status("Debugged application started");
473472 }; // TODO: do we need to handle error?
474473 }
475474 ev => {
helix-term/src/commands.rs+31−33
@@ -776,9 +776,8 @@ fn trim_selections(cx: &mut Context) {
776776fn align_selections(cx: &mut Context) {
777777 let align_style = cx.count();
778778 if align_style > 3 {
779− cx.editor.set_error(
780− "align only accept 1,2,3 as count to set left/center/right align".to_string(),
781− );
779+ cx.editor
780+ .set_error("align only accept 1,2,3 as count to set left/center/right align");
782781 return;
783782 }
784783
@@ -793,7 +792,7 @@ fn align_selections(cx: &mut Context) {
793792 let (l1, l2) = sel.line_range(text);
794793 if l1 != l2 {
795794 cx.editor
796− .set_error("align cannot work with multi line selections".to_string());
795+ .set_error("align cannot work with multi line selections");
797796 return;
798797 }
799798 // if the selection is not in the same line with last selection, we set the column to 0
@@ -1790,7 +1789,7 @@ fn global_search(cx: &mut Context) {
17901789 let call: job::Callback =
17911790 Box::new(move |editor: &mut Editor, compositor: &mut Compositor| {
17921791 if all_matches.is_empty() {
1793− editor.set_status("No matches found".to_string());
1792+ editor.set_status("No matches found");
17941793 return;
17951794 }
17961795
@@ -2177,10 +2176,10 @@ pub mod cmd {
21772176 if args.is_empty() {
21782177 let style = doc!(cx.editor).indent_style;
21792178 cx.editor.set_status(match style {
2180− Tabs => "tabs".into(),
2181− Spaces(1) => "1 space".into(),
2179+ Tabs => "tabs".to_owned(),
2180+ Spaces(1) => "1 space".to_owned(),
21822181 Spaces(n) if (2..=8).contains(&n) => format!("{} spaces", n),
2183− _ => "error".into(), // Shouldn't happen.
2182+ _ => unreachable!(), // Shouldn't happen.
21842183 });
21852184 return Ok(());
21862185 }
@@ -2216,14 +2215,14 @@ pub mod cmd {
22162215 if args.is_empty() {
22172216 let line_ending = doc!(cx.editor).line_ending;
22182217 cx.editor.set_status(match line_ending {
2219− Crlf => "crlf".into(),
2220− LF => "line feed".into(),
2221− FF => "form feed".into(),
2222− CR => "carriage return".into(),
2223− Nel => "next line".into(),
2218+ Crlf => "crlf",
2219+ LF => "line feed",
2220+ FF => "form feed",
2221+ CR => "carriage return",
2222+ Nel => "next line",
22242223
22252224 // These should never be a document's default line ending.
2226− VT | LS | PS => "error".into(),
2225+ VT | LS | PS => "error",
22272226 });
22282227
22292228 return Ok(());
@@ -2259,7 +2258,7 @@ pub mod cmd {
22592258 let (view, doc) = current!(cx.editor);
22602259 let success = doc.earlier(view.id, uk);
22612260 if !success {
2262− cx.editor.set_status("Already at oldest change".to_owned());
2261+ cx.editor.set_status("Already at oldest change");
22632262 }
22642263
22652264 Ok(())
@@ -2274,7 +2273,7 @@ pub mod cmd {
22742273 let (view, doc) = current!(cx.editor);
22752274 let success = doc.later(view.id, uk);
22762275 if !success {
2277− cx.editor.set_status("Already at newest change".to_owned());
2276+ cx.editor.set_status("Already at newest change");
22782277 }
22792278
22802279 Ok(())
@@ -2634,7 +2633,7 @@ pub mod cmd {
26342633 if let Some(label) = args.first() {
26352634 doc.set_encoding(label)
26362635 } else {
2637− let encoding = doc.encoding().name().to_string();
2636+ let encoding = doc.encoding().name().to_owned();
26382637 cx.editor.set_status(encoding);
26392638 Ok(())
26402639 }
@@ -3612,7 +3611,7 @@ pub fn code_action(cx: &mut Context) {
36123611 None => return,
36133612 };
36143613 if actions.is_empty() {
3615− editor.set_status("No code actions available".to_owned());
3614+ editor.set_status("No code actions available");
36163615 return;
36173616 }
36183617
@@ -3836,7 +3835,7 @@ fn last_picker(cx: &mut Context) {
38363835 compositor.push(picker);
38373836 }
38383837 // XXX: figure out how to show error when no last picker lifetime
3839− // cx.editor.set_error("no last picker".to_owned())
3838+ // cx.editor.set_error("no last picker")
38403839 }));
38413840}
38423841
@@ -4098,7 +4097,7 @@ fn goto_last_accessed_file(cx: &mut Context) {
40984097 if let Some(alt) = alternate_file {
40994098 cx.editor.switch(alt, Action::Replace);
41004099 } else {
4101− cx.editor.set_error("no last accessed buffer".to_owned())
4100+ cx.editor.set_error("no last accessed buffer")
41024101 }
41034102}
41044103
@@ -4125,7 +4124,7 @@ fn goto_last_modified_file(cx: &mut Context) {
41254124 if let Some(alt) = alternate_file {
41264125 cx.editor.switch(alt, Action::Replace);
41274126 } else {
4128− cx.editor.set_error("no last modified buffer".to_owned())
4127+ cx.editor.set_error("no last modified buffer")
41294128 }
41304129}
41314130
@@ -4196,7 +4195,7 @@ fn goto_impl(
41964195 jump_to(editor, location, offset_encoding, Action::Replace);
41974196 }
41984197 [] => {
4199− editor.set_error("No definition found.".to_string());
4198+ editor.set_error("No definition found.");
42004199 }
42014200 _locations => {
42024201 let picker = FilePicker::new(
@@ -4841,7 +4840,7 @@ fn undo(cx: &mut Context) {
48414840 let (view, doc) = current!(cx.editor);
48424841 for _ in 0..count {
48434842 if !doc.undo(view.id) {
4844− cx.editor.set_status("Already at oldest change".to_owned());
4843+ cx.editor.set_status("Already at oldest change");
48454844 break;
48464845 }
48474846 }
@@ -4852,7 +4851,7 @@ fn redo(cx: &mut Context) {
48524851 let (view, doc) = current!(cx.editor);
48534852 for _ in 0..count {
48544853 if !doc.redo(view.id) {
4855− cx.editor.set_status("Already at newest change".to_owned());
4854+ cx.editor.set_status("Already at newest change");
48564855 break;
48574856 }
48584857 }
@@ -4864,7 +4863,7 @@ fn earlier(cx: &mut Context) {
48644863 for _ in 0..count {
48654864 // rather than doing in batch we do this so get error halfway
48664865 if !doc.earlier(view.id, UndoKind::Steps(1)) {
4867− cx.editor.set_status("Already at oldest change".to_owned());
4866+ cx.editor.set_status("Already at oldest change");
48684867 break;
48694868 }
48704869 }
@@ -4876,7 +4875,7 @@ fn later(cx: &mut Context) {
48764875 for _ in 0..count {
48774876 // rather than doing in batch we do this so get error halfway
48784877 if !doc.later(view.id, UndoKind::Steps(1)) {
4879− cx.editor.set_status("Already at newest change".to_owned());
4878+ cx.editor.set_status("Already at newest change");
48804879 break;
48814880 }
48824881 }
@@ -4962,7 +4961,7 @@ fn yank_main_selection_to_clipboard_impl(
49624961 bail!("Couldn't set system clipboard content: {}", e);
49634962 }
49644963
4965− editor.set_status("yanked main selection to system clipboard".to_owned());
4964+ editor.set_status("yanked main selection to system clipboard");
49664965 Ok(())
49674966}
49684967
@@ -5399,7 +5398,7 @@ fn remove_primary_selection(cx: &mut Context) {
53995398
54005399 let selection = doc.selection(view.id);
54015400 if selection.len() == 1 {
5402− cx.editor.set_error("no selections remaining".to_owned());
5401+ cx.editor.set_error("no selections remaining");
54035402 return;
54045403 }
54055404 let index = selection.primary_index();
@@ -5506,7 +5505,7 @@ pub fn completion(cx: &mut Context) {
55065505 }
55075506
55085507 if items.is_empty() {
5509− // editor.set_error("No completion available".to_string());
5508+ // editor.set_error("No completion available");
55105509 return;
55115510 }
55125511 let size = compositor.size();
@@ -5787,8 +5786,7 @@ fn jump_backward(cx: &mut Context) {
57875786
57885787fn save_selection(cx: &mut Context) {
57895788 push_jump(cx.editor);
5790− cx.editor
5791− .set_status("Selection saved to jumplist".to_owned());
5789+ cx.editor.set_status("Selection saved to jumplist");
57925790}
57935791
57945792fn rotate_view(cx: &mut Context) {
@@ -6161,7 +6159,7 @@ fn shell_keep_pipe(cx: &mut Context) {
61616159 }
61626160
61636161 if ranges.is_empty() {
6164− cx.editor.set_error("No selections remaining".to_string());
6162+ cx.editor.set_error("No selections remaining");
61656163 return;
61666164 }
61676165
@@ -6247,7 +6245,7 @@ fn shell(cx: &mut Context, prompt: Cow<'static, str>, behavior: ShellBehavior) {
62476245 };
62486246
62496247 if !success {
6250− cx.editor.set_error("Command failed".to_string());
6248+ cx.editor.set_error("Command failed");
62516249 return;
62526250 }
62536251
helix-term/src/commands/dap.rs+10−12
@@ -298,8 +298,7 @@ pub fn dap_start_impl(
298298
299299pub fn dap_launch(cx: &mut Context) {
300300 if cx.editor.debugger.is_some() {
301− cx.editor
302− .set_error("Debugger is already running".to_string());
301+ cx.editor.set_error("Debugger is already running");
303302 return;
304303 }
305304
@@ -312,7 +311,7 @@ pub fn dap_launch(cx: &mut Context) {
312311 Some(c) => c,
313312 None => {
314313 cx.editor
315− .set_error("No debug adapter available for language".to_string());
314+ .set_error("No debug adapter available for language");
316315 return;
317316 }
318317 };
@@ -410,7 +409,7 @@ pub fn dap_toggle_breakpoint(cx: &mut Context) {
410409 Some(path) => path.clone(),
411410 None => {
412411 cx.editor
413− .set_error("Can't set breakpoint: document has no path".to_string());
412+ .set_error("Can't set breakpoint: document has no path");
414413 return;
415414 }
416415 };
@@ -517,7 +516,7 @@ pub fn dap_continue(cx: &mut Context) {
517516 );
518517 } else {
519518 cx.editor
520− .set_error("Currently active thread is not stopped. Switch the thread.".into());
519+ .set_error("Currently active thread is not stopped. Switch the thread.");
521520 }
522521}
523522
@@ -543,7 +542,7 @@ pub fn dap_step_in(cx: &mut Context) {
543542 });
544543 } else {
545544 cx.editor
546− .set_error("Currently active thread is not stopped. Switch the thread.".into());
545+ .set_error("Currently active thread is not stopped. Switch the thread.");
547546 }
548547}
549548
@@ -557,7 +556,7 @@ pub fn dap_step_out(cx: &mut Context) {
557556 });
558557 } else {
559558 cx.editor
560− .set_error("Currently active thread is not stopped. Switch the thread.".into());
559+ .set_error("Currently active thread is not stopped. Switch the thread.");
561560 }
562561}
563562
@@ -571,7 +570,7 @@ pub fn dap_next(cx: &mut Context) {
571570 });
572571 } else {
573572 cx.editor
574− .set_error("Currently active thread is not stopped. Switch the thread.".into());
573+ .set_error("Currently active thread is not stopped. Switch the thread.");
575574 }
576575}
577576
@@ -580,14 +579,14 @@ pub fn dap_variables(cx: &mut Context) {
580579
581580 if debugger.thread_id.is_none() {
582581 cx.editor
583− .set_status("Cannot access variables while target is running".to_owned());
582+ .set_status("Cannot access variables while target is running");
584583 return;
585584 }
586585 let (frame, thread_id) = match (debugger.active_frame, debugger.thread_id) {
587586 (Some(frame), Some(thread_id)) => (frame, thread_id),
588587 _ => {
589588 cx.editor
590− .set_status("Cannot find current stack frame to access variables".to_owned());
589+ .set_status("Cannot find current stack frame to access variables");
591590 return;
592591 }
593592 };
@@ -783,8 +782,7 @@ pub fn dap_switch_stack_frame(cx: &mut Context) {
783782 let thread_id = match debugger.thread_id {
784783 Some(thread_id) => thread_id,
785784 None => {
786− cx.editor
787− .set_error("No thread is currently active".to_owned());
785+ cx.editor.set_error("No thread is currently active");
788786 return;
789787 }
790788 };
helix-view/src/editor.rs+9−6
@@ -14,6 +14,7 @@ use futures_util::stream::select_all::SelectAll;
1414use tokio_stream::wrappers::UnboundedReceiverStream;
1515
1616use std::{
17+ borrow::Cow,
1718 collections::{BTreeMap, HashMap},
1819 io::stdin,
1920 num::NonZeroUsize,
@@ -284,7 +285,7 @@ pub struct Editor {
284285 pub syn_loader: Arc<syntax::Loader>,
285286 pub theme_loader: Arc<theme::Loader>,
286287
287− pub status_msg: Option<(String, Severity)>,
288+ pub status_msg: Option<(Cow<'static, str>, Severity)>,
288289 pub autoinfo: Option<Info>,
289290
290291 pub config: Config,
@@ -357,18 +358,20 @@ impl Editor {
357358 self.status_msg = None;
358359 }
359360
360− pub fn set_status(&mut self, status: String) {
361− self.status_msg = Some((status, Severity::Info));
361+ #[inline]
362+ pub fn set_status<T: Into<Cow<'static, str>>>(&mut self, status: T) {
363+ self.status_msg = Some((status.into(), Severity::Info));
362364 }
363365
364− pub fn set_error(&mut self, error: String) {
365− self.status_msg = Some((error, Severity::Error));
366+ #[inline]
367+ pub fn set_error<T: Into<Cow<'static, str>>>(&mut self, error: T) {
368+ self.status_msg = Some((error.into(), Severity::Error));
366369 }
367370
368371 pub fn set_theme(&mut self, theme: Theme) {
369372 // `ui.selection` is the only scope required to be able to render a theme.
370373 if theme.find_scope_index("ui.selection").is_none() {
371− self.set_error("Invalid theme: `ui.selection` required".to_owned());
374+ self.set_error("Invalid theme: `ui.selection` required");
372375 return;
373376 }
374377