on master help sign in
Further simplify error handling in :commands
commitd530d6e39d58f5759d2db0a9bda1ea5e21154f83 authorBlaž Hrastnik <blaz@mxxn.io> dateSun 11 Jul 2021, 16:35 (5 years ago) parent9c02a1b
2 files changed+41−80
helix-term/src/commands.rs+36−72 helix-view/src/editor.rs+5−8
helix-term/src/commands.rs+36−72
@@ -23,7 +23,7 @@ use helix_view::{
2323 Document, DocumentId, Editor, ViewId,
2424};
2525
26−use anyhow::{anyhow, bail};
26+use anyhow::{anyhow, bail, Context as _};
2727use helix_lsp::{
2828 lsp,
2929 util::{lsp_pos_to_pos, lsp_range_to_range, pos_to_lsp_pos, range_to_lsp_range},
@@ -1197,16 +1197,9 @@ mod cmd {
11971197 args: &[&str],
11981198 _event: PromptEvent,
11991199 ) -> anyhow::Result<()> {
1200− match args.get(0) {
1201− Some(path) => {
1202− let _ = cx.editor.open(path.into(), Action::Replace)?;
1203−
1204− Ok(())
1205− }
1206− None => {
1207− bail!("wrong argument count");
1208− }
1209− }
1200+ let path = args.get(0).context("wrong argument count")?;
1201+ let _ = cx.editor.open(path.into(), Action::Replace)?;
1202+ Ok(())
12101203 }
12111204
12121205 fn write_impl<P: AsRef<Path>>(
@@ -1217,9 +1210,7 @@ mod cmd {
12171210 let (_, doc) = current!(cx.editor);
12181211
12191212 if let Some(path) = path {
1220− if let Err(err) = doc.set_path(path.as_ref()) {
1221− bail!("invalid filepath: {}", err);
1222− };
1213+ doc.set_path(path.as_ref()).context("invalid filepath")?;
12231214 }
12241215 if doc.path().is_none() {
12251216 bail!("cannot write a buffer without a filename");
@@ -1306,14 +1297,11 @@ mod cmd {
13061297 _ => None,
13071298 };
13081299
1309− if let Some(s) = style {
1310− let doc = doc_mut!(cx.editor);
1311− doc.indent_style = s;
1300+ let style = style.context("invalid indent style")?;
1301+ let doc = doc_mut!(cx.editor);
1302+ doc.indent_style = style;
13121303
1313− Ok(())
1314− } else {
1315− bail!("invalid indent style '{}'", args[0]);
1316− }
1304+ Ok(())
13171305 }
13181306
13191307 /// Sets or reports the current document's line ending setting.
@@ -1352,12 +1340,9 @@ mod cmd {
13521340 _ => None,
13531341 };
13541342
1355− if let Some(le) = line_ending {
1356− doc_mut!(cx.editor).line_ending = le;
1357− Ok(())
1358− } else {
1359− bail!("invalid line ending '{}'", args[0]);
1360− }
1343+ let line_ending = line_ending.context("invalid line ending")?;
1344+ doc_mut!(cx.editor).line_ending = line_ending;
1345+ Ok(())
13611346 }
13621347
13631348 fn earlier(
@@ -1397,7 +1382,7 @@ mod cmd {
13971382 event: PromptEvent,
13981383 ) -> anyhow::Result<()> {
13991384 let handle = write_impl(cx, args.first())?;
1400− helix_lsp::block_on(handle)?;
1385+ let _ = helix_lsp::block_on(handle)?;
14011386 quit(cx, &[], event)
14021387 }
14031388
@@ -1407,7 +1392,7 @@ mod cmd {
14071392 event: PromptEvent,
14081393 ) -> anyhow::Result<()> {
14091394 let handle = write_impl(cx, args.first())?;
1410− helix_lsp::block_on(handle)?;
1395+ let _ = helix_lsp::block_on(handle)?;
14111396 force_quit(cx, &[], event)
14121397 }
14131398
@@ -1532,12 +1517,7 @@ mod cmd {
15321517 args: &[&str],
15331518 _event: PromptEvent,
15341519 ) -> anyhow::Result<()> {
1535− let theme = if let Some(theme) = args.first() {
1536− theme
1537− } else {
1538− bail!("theme name not provided");
1539− };
1540−
1520+ let theme = args.first().context("theme not provided")?;
15411521 cx.editor.set_theme_from_name(theme)
15421522 }
15431523
@@ -1598,10 +1578,7 @@ mod cmd {
15981578 doc.append_changes_to_history(view.id);
15991579 Ok(())
16001580 }
1601− Err(e) => {
1602− log::error!("Couldn't get system clipboard contents: {:?}", e);
1603− bail!("Couldn't get system clipboard contents: {:?}", e);
1604− }
1581+ Err(e) => Err(e.context("Couldn't get system clipboard contents")),
16051582 }
16061583 }
16071584
@@ -1620,24 +1597,18 @@ mod cmd {
16201597 args: &[&str],
16211598 _event: PromptEvent,
16221599 ) -> 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")?;
16261601
16271602 if let Err(e) = std::env::set_current_dir(dir) {
16281603 bail!("Couldn't change the current working directory: {:?}", e);
16291604 }
16301605
1631− match std::env::current_dir() {
1632− Ok(cwd) => {
1633− cx.editor.set_status(format!(
1634− "Current working directory is now {}",
1635− cwd.display()
1636− ));
1637− Ok(())
1638− }
1639− Err(e) => bail!("Couldn't get the new working directory: {}", e),
1640− }
1606+ let cwd = std::env::current_dir().context("Couldn't get the new working directory")?;
1607+ cx.editor.set_status(format!(
1608+ "Current working directory is now {}",
1609+ cwd.display()
1610+ ));
1611+ Ok(())
16411612 }
16421613
16431614 fn show_current_directory(
@@ -1645,14 +1616,10 @@ mod cmd {
16451616 _args: &[&str],
16461617 _event: PromptEvent,
16471618 ) -> anyhow::Result<()> {
1648− match std::env::current_dir() {
1649− Ok(cwd) => {
1650− cx.editor
1651− .set_status(format!("Current working directory is {}", cwd.display()));
1652− Ok(())
1653− }
1654− Err(e) => bail!("Couldn't get the current working directory: {}", e),
1655− }
1619+ let cwd = std::env::current_dir().context("Couldn't get the new working directory")?;
1620+ cx.editor
1621+ .set_status(format!("Current working directory is {}", cwd.display()));
1622+ Ok(())
16561623 }
16571624
16581625 /// Sets the [`Document`]'s encoding..
@@ -2870,10 +2837,10 @@ fn yank_joined_to_clipboard_impl(editor: &mut Editor, separator: &str) -> anyhow
28702837
28712838 let joined = values.join(separator);
28722839
2873− if let Err(e) = editor.clipboard_provider.set_contents(joined) {
2874− log::error!("Couldn't set system clipboard content: {:?}", e);
2875− bail!("Couldn't set system clipboard content: {:?}", e);
2876− }
2840+ editor
2841+ .clipboard_provider
2842+ .set_contents(joined)
2843+ .context("Couldn't set system clipboard content")?;
28772844
28782845 editor.set_status(msg);
28792846
@@ -2894,7 +2861,6 @@ fn yank_main_selection_to_clipboard_impl(editor: &mut Editor) -> anyhow::Result<
28942861 .fragment(doc.text().slice(..));
28952862
28962863 if let Err(e) = editor.clipboard_provider.set_contents(value.into_owned()) {
2897− log::error!("Couldn't set system clipboard content: {:?}", e);
28982864 bail!("Couldn't set system clipboard content: {:?}", e);
28992865 }
29002866
@@ -2965,10 +2931,7 @@ fn paste_clipboard_impl(editor: &mut Editor, action: Paste) -> anyhow::Result<()
29652931 Ok(())
29662932 }
29672933 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);
2971− }
2934+ Err(e) => Err(e.context("Couldn't get system clipboard contents")),
29722935 }
29732936}
29742937
@@ -3000,7 +2963,7 @@ fn replace_with_yanked(cx: &mut Context) {
30002963 }
30012964}
30022965
3003−fn replace_selections_with_clipboard_impl(editor: &mut Editor) {
2966+fn replace_selections_with_clipboard_impl(editor: &mut Editor) -> anyhow::Result<()> {
30042967 let (view, doc) = current!(editor);
30052968
30062969 match editor.clipboard_provider.get_contents() {
@@ -3014,13 +2977,14 @@ fn replace_selections_with_clipboard_impl(editor: &mut Editor) {
30142977
30152978 doc.apply(&transaction, view.id);
30162979 doc.append_changes_to_history(view.id);
2980+ Ok(())
30172981 }
3018− Err(e) => log::error!("Couldn't get system clipboard contents: {:?}", e),
2982+ Err(e) => Err(e.context("Couldn't get system clipboard contents")),
30192983 }
30202984}
30212985
30222986fn 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);
30242988}
30252989
30262990fn paste_after(cx: &mut Context) {
helix-view/src/editor.rs+5−8
@@ -98,14 +98,11 @@ impl Editor {
9898 }
9999
100100 pub fn set_theme_from_name(&mut self, theme: &str) -> anyhow::Result<()> {
101− let theme = match self.theme_loader.load(theme.as_ref()) {
102− Ok(theme) => theme,
103− Err(e) => {
104− log::warn!("failed setting theme `{}` - {}", theme, e);
105− anyhow::bail!("failed setting theme `{}` - {}", theme, e);
106− }
107− };
108−
101+ use anyhow::Context;
102+ let theme = self
103+ .theme_loader
104+ .load(theme.as_ref())
105+ .with_context(|| format!("failed setting theme `{}`", theme))?;
109106 self.set_theme(theme);
110107 Ok(())
111108 }