@@ -23,7 +23,7 @@ use helix_view::{
23 Document, DocumentId, Editor, ViewId,
23 Document, DocumentId, Editor, ViewId,
26−use anyhow::{anyhow, bail};
26+use anyhow::{anyhow, bail, Context as _};
27use helix_lsp::{
27use helix_lsp::{
29 util::{lsp_pos_to_pos, lsp_range_to_range, pos_to_lsp_pos, range_to_lsp_range},
29 util::{lsp_pos_to_pos, lsp_range_to_range, pos_to_lsp_pos, range_to_lsp_range},
@@ -1197,16 +1197,9 @@ mod cmd {
1197 args: &[&str],
1197 args: &[&str],
1198 _event: PromptEvent,
1198 _event: PromptEvent,
1199 ) -> anyhow::Result<()> {
1199 ) -> anyhow::Result<()> {
1200− match args.get(0) {
1200+ let path = args.get(0).context("wrong argument count")?;
1202− let _ = cx.editor.open(path.into(), Action::Replace)?;
1201+ let _ = cx.editor.open(path.into(), Action::Replace)?;
1207− bail!("wrong argument count");
1212 fn write_impl<P: AsRef<Path>>(
1205 fn write_impl<P: AsRef<Path>>(
@@ -1217,9 +1210,7 @@ mod cmd {
1217 let (_, doc) = current!(cx.editor);
1210 let (_, doc) = current!(cx.editor);
1219 if let Some(path) = path {
1212 if let Some(path) = path {
1220− if let Err(err) = doc.set_path(path.as_ref()) {
1213+ doc.set_path(path.as_ref()).context("invalid filepath")?;
1221− bail!("invalid filepath: {}", err);
1224 if doc.path().is_none() {
1215 if doc.path().is_none() {
1225 bail!("cannot write a buffer without a filename");
1216 bail!("cannot write a buffer without a filename");
@@ -1306,14 +1297,11 @@ mod cmd {
1306 _ => None,
1297 _ => None,
1309− if let Some(s) = style {
1300+ let style = style.context("invalid indent style")?;
1310− let doc = doc_mut!(cx.editor);
1301+ let doc = doc_mut!(cx.editor);
1311− doc.indent_style = s;
1302+ doc.indent_style = style;
1315− bail!("invalid indent style '{}'", args[0]);
1319 /// Sets or reports the current document's line ending setting.
1307 /// Sets or reports the current document's line ending setting.
@@ -1352,12 +1340,9 @@ mod cmd {
1352 _ => None,
1340 _ => None,
1355− if let Some(le) = line_ending {
1343+ let line_ending = line_ending.context("invalid line ending")?;
1356− doc_mut!(cx.editor).line_ending = le;
1344+ doc_mut!(cx.editor).line_ending = line_ending;
1359− bail!("invalid line ending '{}'", args[0]);
1363 fn earlier(
1348 fn earlier(
@@ -1397,7 +1382,7 @@ mod cmd {
1397 event: PromptEvent,
1382 event: PromptEvent,
1398 ) -> anyhow::Result<()> {
1383 ) -> anyhow::Result<()> {
1399 let handle = write_impl(cx, args.first())?;
1384 let handle = write_impl(cx, args.first())?;
1400− helix_lsp::block_on(handle)?;
1385+ let _ = helix_lsp::block_on(handle)?;
1401 quit(cx, &[], event)
1386 quit(cx, &[], event)
@@ -1407,7 +1392,7 @@ mod cmd {
1407 event: PromptEvent,
1392 event: PromptEvent,
1408 ) -> anyhow::Result<()> {
1393 ) -> anyhow::Result<()> {
1409 let handle = write_impl(cx, args.first())?;
1394 let handle = write_impl(cx, args.first())?;
1410− helix_lsp::block_on(handle)?;
1395+ let _ = helix_lsp::block_on(handle)?;
1411 force_quit(cx, &[], event)
1396 force_quit(cx, &[], event)
@@ -1532,12 +1517,7 @@ mod cmd {
1532 args: &[&str],
1517 args: &[&str],
1533 _event: PromptEvent,
1518 _event: PromptEvent,
1534 ) -> anyhow::Result<()> {
1519 ) -> anyhow::Result<()> {
1535− let theme = if let Some(theme) = args.first() {
1538− bail!("theme name not provided");
1520+ let theme = args.first().context("theme not provided")?;
1541 cx.editor.set_theme_from_name(theme)
1521 cx.editor.set_theme_from_name(theme)
@@ -1598,10 +1578,7 @@ mod cmd {
1598 doc.append_changes_to_history(view.id);
1578 doc.append_changes_to_history(view.id);
1602− log::error!("Couldn't get system clipboard contents: {:?}", e);
1603− bail!("Couldn't get system clipboard contents: {:?}", e);
1581+ Err(e) => Err(e.context("Couldn't get system clipboard contents")),
@@ -1620,24 +1597,18 @@ mod cmd {
1620 args: &[&str],
1597 args: &[&str],
1621 _event: PromptEvent,
1598 _event: PromptEvent,
1622 ) -> anyhow::Result<()> {
1599 ) -> anyhow::Result<()> {
1625− .ok_or_else(|| anyhow!("target directory not provided"))?;
1600+ let dir = args.first().context("target directory not provided")?;
1627 if let Err(e) = std::env::set_current_dir(dir) {
1602 if let Err(e) = std::env::set_current_dir(dir) {
1628 bail!("Couldn't change the current working directory: {:?}", e);
1603 bail!("Couldn't change the current working directory: {:?}", e);
1631− match std::env::current_dir() {
1606+ let cwd = std::env::current_dir().context("Couldn't get the new working directory")?;
1633− cx.editor.set_status(format!(
1607+ cx.editor.set_status(format!(
1634− "Current working directory is now {}",
1608+ "Current working directory is now {}",
1635− cwd.display()
1609+ cwd.display()
1639− Err(e) => bail!("Couldn't get the new working directory: {}", e),
1643 fn show_current_directory(
1614 fn show_current_directory(
@@ -1645,14 +1616,10 @@ mod cmd {
1645 _args: &[&str],
1616 _args: &[&str],
1646 _event: PromptEvent,
1617 _event: PromptEvent,
1647 ) -> anyhow::Result<()> {
1618 ) -> anyhow::Result<()> {
1648− match std::env::current_dir() {
1619+ let cwd = std::env::current_dir().context("Couldn't get the new working directory")?;
1650− cx.editor
1620+ cx.editor
1651− .set_status(format!("Current working directory is {}", cwd.display()));
1621+ .set_status(format!("Current working directory is {}", cwd.display()));
1654− Err(e) => bail!("Couldn't get the current working directory: {}", e),
1658 /// Sets the [`Document`]'s encoding..
1625 /// Sets the [`Document`]'s encoding..
@@ -2870,10 +2837,10 @@ fn yank_joined_to_clipboard_impl(editor: &mut Editor, separator: &str) -> anyhow
2871 let joined = values.join(separator);
2838 let joined = values.join(separator);
2873− if let Err(e) = editor.clipboard_provider.set_contents(joined) {
2841+ .clipboard_provider
2842+ .set_contents(joined)
2874− log::error!("Couldn't set system clipboard content: {:?}", e);
2843+ .context("Couldn't set system clipboard content")?;
2875− bail!("Couldn't set system clipboard content: {:?}", e);
2878 editor.set_status(msg);
2845 editor.set_status(msg);
@@ -2894,7 +2861,6 @@ fn yank_main_selection_to_clipboard_impl(editor: &mut Editor) -> anyhow::Result<
2894 .fragment(doc.text().slice(..));
2861 .fragment(doc.text().slice(..));
2896 if let Err(e) = editor.clipboard_provider.set_contents(value.into_owned()) {
2863 if let Err(e) = editor.clipboard_provider.set_contents(value.into_owned()) {
2897− log::error!("Couldn't set system clipboard content: {:?}", e);
2898 bail!("Couldn't set system clipboard content: {:?}", e);
2864 bail!("Couldn't set system clipboard content: {:?}", e);
@@ -2965,10 +2931,7 @@ fn paste_clipboard_impl(editor: &mut Editor, action: Paste) -> anyhow::Result<()
2967 Ok(None) => Ok(()),
2933 Ok(None) => Ok(()),
2969− log::error!("Couldn't get system clipboard contents: {:?}", e);
2970− bail!("Couldn't get system clipboard contents: {:?}", e);
2934+ Err(e) => Err(e.context("Couldn't get system clipboard contents")),
@@ -3000,7 +2963,7 @@ fn replace_with_yanked(cx: &mut Context) {
3003−fn replace_selections_with_clipboard_impl(editor: &mut Editor) {
2966+fn replace_selections_with_clipboard_impl(editor: &mut Editor) -> anyhow::Result<()> {
3004 let (view, doc) = current!(editor);
2967 let (view, doc) = current!(editor);
3006 match editor.clipboard_provider.get_contents() {
2969 match editor.clipboard_provider.get_contents() {
@@ -3014,13 +2977,14 @@ fn replace_selections_with_clipboard_impl(editor: &mut Editor) {
3015 doc.apply(&transaction, view.id);
2978 doc.apply(&transaction, view.id);
3016 doc.append_changes_to_history(view.id);
2979 doc.append_changes_to_history(view.id);
3018− Err(e) => log::error!("Couldn't get system clipboard contents: {:?}", e),
2982+ Err(e) => Err(e.context("Couldn't get system clipboard contents")),
3022fn replace_selections_with_clipboard(cx: &mut Context) {
2986fn replace_selections_with_clipboard(cx: &mut Context) {
3023− replace_selections_with_clipboard_impl(&mut cx.editor);
2987+ let _ = replace_selections_with_clipboard_impl(&mut cx.editor);
3026fn paste_after(cx: &mut Context) {
2990fn paste_after(cx: &mut Context) {