on master help sign in
Further simplify error handling in :commands
d530d6eBlaž Hrastnik
helix-term/src/commands.rs+36−72
@@ -23,7 +23,7 @@ use helix_view::{
23 Document, DocumentId, Editor, ViewId,
23 Document, DocumentId, Editor, ViewId,
24};
24};
25
25
26−use anyhow::{anyhow, bail};
26+use anyhow::{anyhow, bail, Context as _};
27use helix_lsp::{
27use helix_lsp::{
28 lsp,
28 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")?;
1201− Some(path) => {
1202− let _ = cx.editor.open(path.into(), Action::Replace)?;
1201+ let _ = cx.editor.open(path.into(), Action::Replace)?;
1203−
1204− Ok(())
1202+ Ok(())
1205− }
1206− None => {
1207− bail!("wrong argument count");
1208− }
1209− }
1210 }
1203 }
1211
1204
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);
1218
1211
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);
1222− };
1223 }
1214 }
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,
1307 };
1298 };
1308
1299
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;
1312
1303
1313− Ok(())
1304+ Ok(())
1314− } else {
1315− bail!("invalid indent style '{}'", args[0]);
1316− }
1317 }
1305 }
1318
1306
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,
1353 };
1341 };
1354
1342
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;
1357− Ok(())
1345+ Ok(())
1358− } else {
1359− bail!("invalid line ending '{}'", args[0]);
1360− }
1361 }
1346 }
1362
1347
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)
1402 }
1387 }
1403
1388
@@ -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)
1412 }
1397 }
1413
1398
@@ -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() {
1536− theme
1537− } else {
1538− bail!("theme name not provided");
1520+ let theme = args.first().context("theme not provided")?;
1539− };
1540−
1541 cx.editor.set_theme_from_name(theme)
1521 cx.editor.set_theme_from_name(theme)
1542 }
1522 }
1543
1523
@@ -1598,10 +1578,7 @@ mod cmd {
1598 doc.append_changes_to_history(view.id);
1578 doc.append_changes_to_history(view.id);
1599 Ok(())
1579 Ok(())
1600 }
1580 }
1601− Err(e) => {
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")),
1604− }
1605 }
1582 }
1606 }
1583 }
1607
1584
@@ -1620,24 +1597,18 @@ mod cmd {
1620 args: &[&str],
1597 args: &[&str],
1621 _event: PromptEvent,
1598 _event: PromptEvent,
1622 ) -> anyhow::Result<()> {
1599 ) -> anyhow::Result<()> {
1623− let dir = args
1624− .first()
1625− .ok_or_else(|| anyhow!("target directory not provided"))?;
1600+ let dir = args.first().context("target directory not provided")?;
1626
1601
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);
1629 }
1604 }
1630
1605
1631− match std::env::current_dir() {
1606+ let cwd = std::env::current_dir().context("Couldn't get the new working directory")?;
1632− Ok(cwd) => {
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()
1636− ));
1610+ ));
1637− Ok(())
1611+ Ok(())
1638− }
1639− Err(e) => bail!("Couldn't get the new working directory: {}", e),
1640− }
1641 }
1612 }
1642
1613
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")?;
1649− Ok(cwd) => {
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()));
1652− Ok(())
1622+ Ok(())
1653− }
1654− Err(e) => bail!("Couldn't get the current working directory: {}", e),
1655− }
1656 }
1623 }
1657
1624
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
2870
2837
2871 let joined = values.join(separator);
2838 let joined = values.join(separator);
2872
2839
2840+ editor
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);
2876− }
2877
2844
2878 editor.set_status(msg);
2845 editor.set_status(msg);
2879
2846
@@ -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(..));
2895
2862
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);
2899 }
2865 }
2900
2866
@@ -2965,10 +2931,7 @@ fn paste_clipboard_impl(editor: &mut Editor, action: Paste) -> anyhow::Result<()
2965 Ok(())
2931 Ok(())
2966 }
2932 }
2967 Ok(None) => Ok(()),
2933 Ok(None) => Ok(()),
2968− Err(e) => {
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")),
2971− }
2972 }
2935 }
2973}
2936}
2974
2937
@@ -3000,7 +2963,7 @@ fn replace_with_yanked(cx: &mut Context) {
3000 }
2963 }
3001}
2964}
3002
2965
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);
3005
2968
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) {
3014
2977
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);
2980+ Ok(())
3017 }
2981 }
3018− Err(e) => log::error!("Couldn't get system clipboard contents: {:?}", e),
2982+ Err(e) => Err(e.context("Couldn't get system clipboard contents")),
3019 }
2983 }
3020}
2984}
3021
2985
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);
3024}
2988}
3025
2989
3026fn paste_after(cx: &mut Context) {
2990fn paste_after(cx: &mut Context) {
helix-view/src/editor.rs+5−8
@@ -98,14 +98,11 @@ impl Editor {
98 }
98 }
99
99
100 pub fn set_theme_from_name(&mut self, theme: &str) -> anyhow::Result<()> {
100 pub fn set_theme_from_name(&mut self, theme: &str) -> anyhow::Result<()> {
101+ use anyhow::Context;
102+ let theme = self
103+ .theme_loader
101− let theme = match self.theme_loader.load(theme.as_ref()) {
104+ .load(theme.as_ref())
102− Ok(theme) => theme,
103− Err(e) => {
104− log::warn!("failed setting theme `{}` - {}", theme, e);
105+ .with_context(|| format!("failed setting theme `{}`", theme))?;
105− anyhow::bail!("failed setting theme `{}` - {}", theme, e);
106− }
107− };
108−
109 self.set_theme(theme);
106 self.set_theme(theme);
110 Ok(())
107 Ok(())
111 }
108 }