Allow static strings in set_status/set_error so API is nicer

imgbot
Blaž Hrastnik 2 years ago
parent fd0e4b1159
commit d11b652139

@ -468,8 +468,7 @@ impl Application {
// TODO: fetch breakpoints (in case we're attaching) // TODO: fetch breakpoints (in case we're attaching)
if debugger.configuration_done().await.is_ok() { if debugger.configuration_done().await.is_ok() {
self.editor self.editor.set_status("Debugged application started");
.set_status("Debugged application started".to_owned());
}; // TODO: do we need to handle error? }; // TODO: do we need to handle error?
} }
ev => { ev => {

@ -776,9 +776,8 @@ fn trim_selections(cx: &mut Context) {
fn align_selections(cx: &mut Context) { fn align_selections(cx: &mut Context) {
let align_style = cx.count(); let align_style = cx.count();
if align_style > 3 { if align_style > 3 {
cx.editor.set_error( cx.editor
"align only accept 1,2,3 as count to set left/center/right align".to_string(), .set_error("align only accept 1,2,3 as count to set left/center/right align");
);
return; return;
} }
@ -793,7 +792,7 @@ fn align_selections(cx: &mut Context) {
let (l1, l2) = sel.line_range(text); let (l1, l2) = sel.line_range(text);
if l1 != l2 { if l1 != l2 {
cx.editor cx.editor
.set_error("align cannot work with multi line selections".to_string()); .set_error("align cannot work with multi line selections");
return; return;
} }
// if the selection is not in the same line with last selection, we set the column to 0 // 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) {
let call: job::Callback = let call: job::Callback =
Box::new(move |editor: &mut Editor, compositor: &mut Compositor| { Box::new(move |editor: &mut Editor, compositor: &mut Compositor| {
if all_matches.is_empty() { if all_matches.is_empty() {
editor.set_status("No matches found".to_string()); editor.set_status("No matches found");
return; return;
} }
@ -2177,10 +2176,10 @@ pub mod cmd {
if args.is_empty() { if args.is_empty() {
let style = doc!(cx.editor).indent_style; let style = doc!(cx.editor).indent_style;
cx.editor.set_status(match style { cx.editor.set_status(match style {
Tabs => "tabs".into(), Tabs => "tabs".to_owned(),
Spaces(1) => "1 space".into(), Spaces(1) => "1 space".to_owned(),
Spaces(n) if (2..=8).contains(&n) => format!("{} spaces", n), Spaces(n) if (2..=8).contains(&n) => format!("{} spaces", n),
_ => "error".into(), // Shouldn't happen. _ => unreachable!(), // Shouldn't happen.
}); });
return Ok(()); return Ok(());
} }
@ -2216,14 +2215,14 @@ pub mod cmd {
if args.is_empty() { if args.is_empty() {
let line_ending = doc!(cx.editor).line_ending; let line_ending = doc!(cx.editor).line_ending;
cx.editor.set_status(match line_ending { cx.editor.set_status(match line_ending {
Crlf => "crlf".into(), Crlf => "crlf",
LF => "line feed".into(), LF => "line feed",
FF => "form feed".into(), FF => "form feed",
CR => "carriage return".into(), CR => "carriage return",
Nel => "next line".into(), Nel => "next line",
// These should never be a document's default line ending. // These should never be a document's default line ending.
VT | LS | PS => "error".into(), VT | LS | PS => "error",
}); });
return Ok(()); return Ok(());
@ -2259,7 +2258,7 @@ pub mod cmd {
let (view, doc) = current!(cx.editor); let (view, doc) = current!(cx.editor);
let success = doc.earlier(view.id, uk); let success = doc.earlier(view.id, uk);
if !success { if !success {
cx.editor.set_status("Already at oldest change".to_owned()); cx.editor.set_status("Already at oldest change");
} }
Ok(()) Ok(())
@ -2274,7 +2273,7 @@ pub mod cmd {
let (view, doc) = current!(cx.editor); let (view, doc) = current!(cx.editor);
let success = doc.later(view.id, uk); let success = doc.later(view.id, uk);
if !success { if !success {
cx.editor.set_status("Already at newest change".to_owned()); cx.editor.set_status("Already at newest change");
} }
Ok(()) Ok(())
@ -2634,7 +2633,7 @@ pub mod cmd {
if let Some(label) = args.first() { if let Some(label) = args.first() {
doc.set_encoding(label) doc.set_encoding(label)
} else { } else {
let encoding = doc.encoding().name().to_string(); let encoding = doc.encoding().name().to_owned();
cx.editor.set_status(encoding); cx.editor.set_status(encoding);
Ok(()) Ok(())
} }
@ -3612,7 +3611,7 @@ pub fn code_action(cx: &mut Context) {
None => return, None => return,
}; };
if actions.is_empty() { if actions.is_empty() {
editor.set_status("No code actions available".to_owned()); editor.set_status("No code actions available");
return; return;
} }
@ -3836,7 +3835,7 @@ fn last_picker(cx: &mut Context) {
compositor.push(picker); compositor.push(picker);
} }
// XXX: figure out how to show error when no last picker lifetime // XXX: figure out how to show error when no last picker lifetime
// cx.editor.set_error("no last picker".to_owned()) // cx.editor.set_error("no last picker")
})); }));
} }
@ -4098,7 +4097,7 @@ fn goto_last_accessed_file(cx: &mut Context) {
if let Some(alt) = alternate_file { if let Some(alt) = alternate_file {
cx.editor.switch(alt, Action::Replace); cx.editor.switch(alt, Action::Replace);
} else { } else {
cx.editor.set_error("no last accessed buffer".to_owned()) cx.editor.set_error("no last accessed buffer")
} }
} }
@ -4125,7 +4124,7 @@ fn goto_last_modified_file(cx: &mut Context) {
if let Some(alt) = alternate_file { if let Some(alt) = alternate_file {
cx.editor.switch(alt, Action::Replace); cx.editor.switch(alt, Action::Replace);
} else { } else {
cx.editor.set_error("no last modified buffer".to_owned()) cx.editor.set_error("no last modified buffer")
} }
} }
@ -4196,7 +4195,7 @@ fn goto_impl(
jump_to(editor, location, offset_encoding, Action::Replace); jump_to(editor, location, offset_encoding, Action::Replace);
} }
[] => { [] => {
editor.set_error("No definition found.".to_string()); editor.set_error("No definition found.");
} }
_locations => { _locations => {
let picker = FilePicker::new( let picker = FilePicker::new(
@ -4841,7 +4840,7 @@ fn undo(cx: &mut Context) {
let (view, doc) = current!(cx.editor); let (view, doc) = current!(cx.editor);
for _ in 0..count { for _ in 0..count {
if !doc.undo(view.id) { if !doc.undo(view.id) {
cx.editor.set_status("Already at oldest change".to_owned()); cx.editor.set_status("Already at oldest change");
break; break;
} }
} }
@ -4852,7 +4851,7 @@ fn redo(cx: &mut Context) {
let (view, doc) = current!(cx.editor); let (view, doc) = current!(cx.editor);
for _ in 0..count { for _ in 0..count {
if !doc.redo(view.id) { if !doc.redo(view.id) {
cx.editor.set_status("Already at newest change".to_owned()); cx.editor.set_status("Already at newest change");
break; break;
} }
} }
@ -4864,7 +4863,7 @@ fn earlier(cx: &mut Context) {
for _ in 0..count { for _ in 0..count {
// rather than doing in batch we do this so get error halfway // rather than doing in batch we do this so get error halfway
if !doc.earlier(view.id, UndoKind::Steps(1)) { if !doc.earlier(view.id, UndoKind::Steps(1)) {
cx.editor.set_status("Already at oldest change".to_owned()); cx.editor.set_status("Already at oldest change");
break; break;
} }
} }
@ -4876,7 +4875,7 @@ fn later(cx: &mut Context) {
for _ in 0..count { for _ in 0..count {
// rather than doing in batch we do this so get error halfway // rather than doing in batch we do this so get error halfway
if !doc.later(view.id, UndoKind::Steps(1)) { if !doc.later(view.id, UndoKind::Steps(1)) {
cx.editor.set_status("Already at newest change".to_owned()); cx.editor.set_status("Already at newest change");
break; break;
} }
} }
@ -4962,7 +4961,7 @@ fn yank_main_selection_to_clipboard_impl(
bail!("Couldn't set system clipboard content: {}", e); bail!("Couldn't set system clipboard content: {}", e);
} }
editor.set_status("yanked main selection to system clipboard".to_owned()); editor.set_status("yanked main selection to system clipboard");
Ok(()) Ok(())
} }
@ -5399,7 +5398,7 @@ fn remove_primary_selection(cx: &mut Context) {
let selection = doc.selection(view.id); let selection = doc.selection(view.id);
if selection.len() == 1 { if selection.len() == 1 {
cx.editor.set_error("no selections remaining".to_owned()); cx.editor.set_error("no selections remaining");
return; return;
} }
let index = selection.primary_index(); let index = selection.primary_index();
@ -5506,7 +5505,7 @@ pub fn completion(cx: &mut Context) {
} }
if items.is_empty() { if items.is_empty() {
// editor.set_error("No completion available".to_string()); // editor.set_error("No completion available");
return; return;
} }
let size = compositor.size(); let size = compositor.size();
@ -5787,8 +5786,7 @@ fn jump_backward(cx: &mut Context) {
fn save_selection(cx: &mut Context) { fn save_selection(cx: &mut Context) {
push_jump(cx.editor); push_jump(cx.editor);
cx.editor cx.editor.set_status("Selection saved to jumplist");
.set_status("Selection saved to jumplist".to_owned());
} }
fn rotate_view(cx: &mut Context) { fn rotate_view(cx: &mut Context) {
@ -6161,7 +6159,7 @@ fn shell_keep_pipe(cx: &mut Context) {
} }
if ranges.is_empty() { if ranges.is_empty() {
cx.editor.set_error("No selections remaining".to_string()); cx.editor.set_error("No selections remaining");
return; return;
} }
@ -6247,7 +6245,7 @@ fn shell(cx: &mut Context, prompt: Cow<'static, str>, behavior: ShellBehavior) {
}; };
if !success { if !success {
cx.editor.set_error("Command failed".to_string()); cx.editor.set_error("Command failed");
return; return;
} }

@ -298,8 +298,7 @@ pub fn dap_start_impl(
pub fn dap_launch(cx: &mut Context) { pub fn dap_launch(cx: &mut Context) {
if cx.editor.debugger.is_some() { if cx.editor.debugger.is_some() {
cx.editor cx.editor.set_error("Debugger is already running");
.set_error("Debugger is already running".to_string());
return; return;
} }
@ -312,7 +311,7 @@ pub fn dap_launch(cx: &mut Context) {
Some(c) => c, Some(c) => c,
None => { None => {
cx.editor cx.editor
.set_error("No debug adapter available for language".to_string()); .set_error("No debug adapter available for language");
return; return;
} }
}; };
@ -410,7 +409,7 @@ pub fn dap_toggle_breakpoint(cx: &mut Context) {
Some(path) => path.clone(), Some(path) => path.clone(),
None => { None => {
cx.editor cx.editor
.set_error("Can't set breakpoint: document has no path".to_string()); .set_error("Can't set breakpoint: document has no path");
return; return;
} }
}; };
@ -517,7 +516,7 @@ pub fn dap_continue(cx: &mut Context) {
); );
} else { } else {
cx.editor cx.editor
.set_error("Currently active thread is not stopped. Switch the thread.".into()); .set_error("Currently active thread is not stopped. Switch the thread.");
} }
} }
@ -543,7 +542,7 @@ pub fn dap_step_in(cx: &mut Context) {
}); });
} else { } else {
cx.editor cx.editor
.set_error("Currently active thread is not stopped. Switch the thread.".into()); .set_error("Currently active thread is not stopped. Switch the thread.");
} }
} }
@ -557,7 +556,7 @@ pub fn dap_step_out(cx: &mut Context) {
}); });
} else { } else {
cx.editor cx.editor
.set_error("Currently active thread is not stopped. Switch the thread.".into()); .set_error("Currently active thread is not stopped. Switch the thread.");
} }
} }
@ -571,7 +570,7 @@ pub fn dap_next(cx: &mut Context) {
}); });
} else { } else {
cx.editor cx.editor
.set_error("Currently active thread is not stopped. Switch the thread.".into()); .set_error("Currently active thread is not stopped. Switch the thread.");
} }
} }
@ -580,14 +579,14 @@ pub fn dap_variables(cx: &mut Context) {
if debugger.thread_id.is_none() { if debugger.thread_id.is_none() {
cx.editor cx.editor
.set_status("Cannot access variables while target is running".to_owned()); .set_status("Cannot access variables while target is running");
return; return;
} }
let (frame, thread_id) = match (debugger.active_frame, debugger.thread_id) { let (frame, thread_id) = match (debugger.active_frame, debugger.thread_id) {
(Some(frame), Some(thread_id)) => (frame, thread_id), (Some(frame), Some(thread_id)) => (frame, thread_id),
_ => { _ => {
cx.editor cx.editor
.set_status("Cannot find current stack frame to access variables".to_owned()); .set_status("Cannot find current stack frame to access variables");
return; return;
} }
}; };
@ -783,8 +782,7 @@ pub fn dap_switch_stack_frame(cx: &mut Context) {
let thread_id = match debugger.thread_id { let thread_id = match debugger.thread_id {
Some(thread_id) => thread_id, Some(thread_id) => thread_id,
None => { None => {
cx.editor cx.editor.set_error("No thread is currently active");
.set_error("No thread is currently active".to_owned());
return; return;
} }
}; };

@ -14,6 +14,7 @@ use futures_util::stream::select_all::SelectAll;
use tokio_stream::wrappers::UnboundedReceiverStream; use tokio_stream::wrappers::UnboundedReceiverStream;
use std::{ use std::{
borrow::Cow,
collections::{BTreeMap, HashMap}, collections::{BTreeMap, HashMap},
io::stdin, io::stdin,
num::NonZeroUsize, num::NonZeroUsize,
@ -284,7 +285,7 @@ pub struct Editor {
pub syn_loader: Arc<syntax::Loader>, pub syn_loader: Arc<syntax::Loader>,
pub theme_loader: Arc<theme::Loader>, pub theme_loader: Arc<theme::Loader>,
pub status_msg: Option<(String, Severity)>, pub status_msg: Option<(Cow<'static, str>, Severity)>,
pub autoinfo: Option<Info>, pub autoinfo: Option<Info>,
pub config: Config, pub config: Config,
@ -357,18 +358,20 @@ impl Editor {
self.status_msg = None; self.status_msg = None;
} }
pub fn set_status(&mut self, status: String) { #[inline]
self.status_msg = Some((status, Severity::Info)); pub fn set_status<T: Into<Cow<'static, str>>>(&mut self, status: T) {
self.status_msg = Some((status.into(), Severity::Info));
} }
pub fn set_error(&mut self, error: String) { #[inline]
self.status_msg = Some((error, Severity::Error)); pub fn set_error<T: Into<Cow<'static, str>>>(&mut self, error: T) {
self.status_msg = Some((error.into(), Severity::Error));
} }
pub fn set_theme(&mut self, theme: Theme) { pub fn set_theme(&mut self, theme: Theme) {
// `ui.selection` is the only scope required to be able to render a theme. // `ui.selection` is the only scope required to be able to render a theme.
if theme.find_scope_index("ui.selection").is_none() { if theme.find_scope_index("ui.selection").is_none() {
self.set_error("Invalid theme: `ui.selection` required".to_owned()); self.set_error("Invalid theme: `ui.selection` required");
return; return;
} }

Loading…
Cancel
Save