Skip to content

Commit

Permalink
Further simplify error handling in :commands
Browse files Browse the repository at this point in the history
  • Loading branch information
archseer committed Jul 11, 2021
1 parent 9c02a1b commit d530d6e
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 80 deletions.
108 changes: 36 additions & 72 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use helix_view::{
Document, DocumentId, Editor, ViewId,
};

use anyhow::{anyhow, bail};
use anyhow::{anyhow, bail, Context as _};
use helix_lsp::{
lsp,
util::{lsp_pos_to_pos, lsp_range_to_range, pos_to_lsp_pos, range_to_lsp_range},
Expand Down Expand Up @@ -1197,16 +1197,9 @@ mod cmd {
args: &[&str],
_event: PromptEvent,
) -> anyhow::Result<()> {
match args.get(0) {
Some(path) => {
let _ = cx.editor.open(path.into(), Action::Replace)?;

Ok(())
}
None => {
bail!("wrong argument count");
}
}
let path = args.get(0).context("wrong argument count")?;
let _ = cx.editor.open(path.into(), Action::Replace)?;
Ok(())
}

fn write_impl<P: AsRef<Path>>(
Expand All @@ -1217,9 +1210,7 @@ mod cmd {
let (_, doc) = current!(cx.editor);

if let Some(path) = path {
if let Err(err) = doc.set_path(path.as_ref()) {
bail!("invalid filepath: {}", err);
};
doc.set_path(path.as_ref()).context("invalid filepath")?;
}
if doc.path().is_none() {
bail!("cannot write a buffer without a filename");
Expand Down Expand Up @@ -1306,14 +1297,11 @@ mod cmd {
_ => None,
};

if let Some(s) = style {
let doc = doc_mut!(cx.editor);
doc.indent_style = s;
let style = style.context("invalid indent style")?;
let doc = doc_mut!(cx.editor);
doc.indent_style = style;

Ok(())
} else {
bail!("invalid indent style '{}'", args[0]);
}
Ok(())
}

/// Sets or reports the current document's line ending setting.
Expand Down Expand Up @@ -1352,12 +1340,9 @@ mod cmd {
_ => None,
};

if let Some(le) = line_ending {
doc_mut!(cx.editor).line_ending = le;
Ok(())
} else {
bail!("invalid line ending '{}'", args[0]);
}
let line_ending = line_ending.context("invalid line ending")?;
doc_mut!(cx.editor).line_ending = line_ending;
Ok(())
}

fn earlier(
Expand Down Expand Up @@ -1397,7 +1382,7 @@ mod cmd {
event: PromptEvent,
) -> anyhow::Result<()> {
let handle = write_impl(cx, args.first())?;
helix_lsp::block_on(handle)?;
let _ = helix_lsp::block_on(handle)?;
quit(cx, &[], event)
}

Expand All @@ -1407,7 +1392,7 @@ mod cmd {
event: PromptEvent,
) -> anyhow::Result<()> {
let handle = write_impl(cx, args.first())?;
helix_lsp::block_on(handle)?;
let _ = helix_lsp::block_on(handle)?;
force_quit(cx, &[], event)
}

Expand Down Expand Up @@ -1532,12 +1517,7 @@ mod cmd {
args: &[&str],
_event: PromptEvent,
) -> anyhow::Result<()> {
let theme = if let Some(theme) = args.first() {
theme
} else {
bail!("theme name not provided");
};

let theme = args.first().context("theme not provided")?;
cx.editor.set_theme_from_name(theme)
}

Expand Down Expand Up @@ -1598,10 +1578,7 @@ mod cmd {
doc.append_changes_to_history(view.id);
Ok(())
}
Err(e) => {
log::error!("Couldn't get system clipboard contents: {:?}", e);
bail!("Couldn't get system clipboard contents: {:?}", e);
}
Err(e) => Err(e.context("Couldn't get system clipboard contents")),
}
}

Expand All @@ -1620,39 +1597,29 @@ mod cmd {
args: &[&str],
_event: PromptEvent,
) -> anyhow::Result<()> {
let dir = args
.first()
.ok_or_else(|| anyhow!("target directory not provided"))?;
let dir = args.first().context("target directory not provided")?;

if let Err(e) = std::env::set_current_dir(dir) {
bail!("Couldn't change the current working directory: {:?}", e);
}

match std::env::current_dir() {
Ok(cwd) => {
cx.editor.set_status(format!(
"Current working directory is now {}",
cwd.display()
));
Ok(())
}
Err(e) => bail!("Couldn't get the new working directory: {}", e),
}
let cwd = std::env::current_dir().context("Couldn't get the new working directory")?;
cx.editor.set_status(format!(
"Current working directory is now {}",
cwd.display()
));
Ok(())
}

fn show_current_directory(
cx: &mut compositor::Context,
_args: &[&str],
_event: PromptEvent,
) -> anyhow::Result<()> {
match std::env::current_dir() {
Ok(cwd) => {
cx.editor
.set_status(format!("Current working directory is {}", cwd.display()));
Ok(())
}
Err(e) => bail!("Couldn't get the current working directory: {}", e),
}
let cwd = std::env::current_dir().context("Couldn't get the new working directory")?;
cx.editor
.set_status(format!("Current working directory is {}", cwd.display()));
Ok(())
}

/// Sets the [`Document`]'s encoding..
Expand Down Expand Up @@ -2870,10 +2837,10 @@ fn yank_joined_to_clipboard_impl(editor: &mut Editor, separator: &str) -> anyhow

let joined = values.join(separator);

if let Err(e) = editor.clipboard_provider.set_contents(joined) {
log::error!("Couldn't set system clipboard content: {:?}", e);
bail!("Couldn't set system clipboard content: {:?}", e);
}
editor
.clipboard_provider
.set_contents(joined)
.context("Couldn't set system clipboard content")?;

editor.set_status(msg);

Expand All @@ -2894,7 +2861,6 @@ fn yank_main_selection_to_clipboard_impl(editor: &mut Editor) -> anyhow::Result<
.fragment(doc.text().slice(..));

if let Err(e) = editor.clipboard_provider.set_contents(value.into_owned()) {
log::error!("Couldn't set system clipboard content: {:?}", e);
bail!("Couldn't set system clipboard content: {:?}", e);
}

Expand Down Expand Up @@ -2965,10 +2931,7 @@ fn paste_clipboard_impl(editor: &mut Editor, action: Paste) -> anyhow::Result<()
Ok(())
}
Ok(None) => Ok(()),
Err(e) => {
log::error!("Couldn't get system clipboard contents: {:?}", e);
bail!("Couldn't get system clipboard contents: {:?}", e);
}
Err(e) => Err(e.context("Couldn't get system clipboard contents")),
}
}

Expand Down Expand Up @@ -3000,7 +2963,7 @@ fn replace_with_yanked(cx: &mut Context) {
}
}

fn replace_selections_with_clipboard_impl(editor: &mut Editor) {
fn replace_selections_with_clipboard_impl(editor: &mut Editor) -> anyhow::Result<()> {
let (view, doc) = current!(editor);

match editor.clipboard_provider.get_contents() {
Expand All @@ -3014,13 +2977,14 @@ fn replace_selections_with_clipboard_impl(editor: &mut Editor) {

doc.apply(&transaction, view.id);
doc.append_changes_to_history(view.id);
Ok(())
}
Err(e) => log::error!("Couldn't get system clipboard contents: {:?}", e),
Err(e) => Err(e.context("Couldn't get system clipboard contents")),
}
}

fn replace_selections_with_clipboard(cx: &mut Context) {
replace_selections_with_clipboard_impl(&mut cx.editor);
let _ = replace_selections_with_clipboard_impl(&mut cx.editor);
}

fn paste_after(cx: &mut Context) {
Expand Down
13 changes: 5 additions & 8 deletions helix-view/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,11 @@ impl Editor {
}

pub fn set_theme_from_name(&mut self, theme: &str) -> anyhow::Result<()> {
let theme = match self.theme_loader.load(theme.as_ref()) {
Ok(theme) => theme,
Err(e) => {
log::warn!("failed setting theme `{}` - {}", theme, e);
anyhow::bail!("failed setting theme `{}` - {}", theme, e);
}
};

use anyhow::Context;
let theme = self
.theme_loader
.load(theme.as_ref())
.with_context(|| format!("failed setting theme `{}`", theme))?;
self.set_theme(theme);
Ok(())
}
Expand Down

0 comments on commit d530d6e

Please sign in to comment.