mirror of https://github.com/helix-editor/helix
Further simplify error handling in :commands
parent
9c02a1b070
commit
d530d6e39d
|
@ -23,7 +23,7 @@ use helix_view::{
|
||||||
Document, DocumentId, Editor, ViewId,
|
Document, DocumentId, Editor, ViewId,
|
||||||
};
|
};
|
||||||
|
|
||||||
use anyhow::{anyhow, bail};
|
use anyhow::{anyhow, bail, Context as _};
|
||||||
use helix_lsp::{
|
use helix_lsp::{
|
||||||
lsp,
|
lsp,
|
||||||
util::{lsp_pos_to_pos, lsp_range_to_range, pos_to_lsp_pos, range_to_lsp_range},
|
util::{lsp_pos_to_pos, lsp_range_to_range, pos_to_lsp_pos, range_to_lsp_range},
|
||||||
|
@ -1197,16 +1197,9 @@ mod cmd {
|
||||||
args: &[&str],
|
args: &[&str],
|
||||||
_event: PromptEvent,
|
_event: PromptEvent,
|
||||||
) -> anyhow::Result<()> {
|
) -> anyhow::Result<()> {
|
||||||
match args.get(0) {
|
let path = args.get(0).context("wrong argument count")?;
|
||||||
Some(path) => {
|
let _ = cx.editor.open(path.into(), Action::Replace)?;
|
||||||
let _ = cx.editor.open(path.into(), Action::Replace)?;
|
Ok(())
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
None => {
|
|
||||||
bail!("wrong argument count");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn write_impl<P: AsRef<Path>>(
|
fn write_impl<P: AsRef<Path>>(
|
||||||
|
@ -1217,9 +1210,7 @@ mod cmd {
|
||||||
let (_, doc) = current!(cx.editor);
|
let (_, doc) = current!(cx.editor);
|
||||||
|
|
||||||
if let Some(path) = path {
|
if let Some(path) = path {
|
||||||
if let Err(err) = doc.set_path(path.as_ref()) {
|
doc.set_path(path.as_ref()).context("invalid filepath")?;
|
||||||
bail!("invalid filepath: {}", err);
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
if doc.path().is_none() {
|
if doc.path().is_none() {
|
||||||
bail!("cannot write a buffer without a filename");
|
bail!("cannot write a buffer without a filename");
|
||||||
|
@ -1306,14 +1297,11 @@ mod cmd {
|
||||||
_ => None,
|
_ => None,
|
||||||
};
|
};
|
||||||
|
|
||||||
if let Some(s) = style {
|
let style = style.context("invalid indent style")?;
|
||||||
let doc = doc_mut!(cx.editor);
|
let doc = doc_mut!(cx.editor);
|
||||||
doc.indent_style = s;
|
doc.indent_style = style;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
} else {
|
|
||||||
bail!("invalid indent style '{}'", args[0]);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sets or reports the current document's line ending setting.
|
/// Sets or reports the current document's line ending setting.
|
||||||
|
@ -1352,12 +1340,9 @@ mod cmd {
|
||||||
_ => None,
|
_ => None,
|
||||||
};
|
};
|
||||||
|
|
||||||
if let Some(le) = line_ending {
|
let line_ending = line_ending.context("invalid line ending")?;
|
||||||
doc_mut!(cx.editor).line_ending = le;
|
doc_mut!(cx.editor).line_ending = line_ending;
|
||||||
Ok(())
|
Ok(())
|
||||||
} else {
|
|
||||||
bail!("invalid line ending '{}'", args[0]);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn earlier(
|
fn earlier(
|
||||||
|
@ -1397,7 +1382,7 @@ mod cmd {
|
||||||
event: PromptEvent,
|
event: PromptEvent,
|
||||||
) -> anyhow::Result<()> {
|
) -> anyhow::Result<()> {
|
||||||
let handle = write_impl(cx, args.first())?;
|
let handle = write_impl(cx, args.first())?;
|
||||||
helix_lsp::block_on(handle)?;
|
let _ = helix_lsp::block_on(handle)?;
|
||||||
quit(cx, &[], event)
|
quit(cx, &[], event)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1407,7 +1392,7 @@ mod cmd {
|
||||||
event: PromptEvent,
|
event: PromptEvent,
|
||||||
) -> anyhow::Result<()> {
|
) -> anyhow::Result<()> {
|
||||||
let handle = write_impl(cx, args.first())?;
|
let handle = write_impl(cx, args.first())?;
|
||||||
helix_lsp::block_on(handle)?;
|
let _ = helix_lsp::block_on(handle)?;
|
||||||
force_quit(cx, &[], event)
|
force_quit(cx, &[], event)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1532,12 +1517,7 @@ mod cmd {
|
||||||
args: &[&str],
|
args: &[&str],
|
||||||
_event: PromptEvent,
|
_event: PromptEvent,
|
||||||
) -> anyhow::Result<()> {
|
) -> anyhow::Result<()> {
|
||||||
let theme = if let Some(theme) = args.first() {
|
let theme = args.first().context("theme not provided")?;
|
||||||
theme
|
|
||||||
} else {
|
|
||||||
bail!("theme name not provided");
|
|
||||||
};
|
|
||||||
|
|
||||||
cx.editor.set_theme_from_name(theme)
|
cx.editor.set_theme_from_name(theme)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1598,10 +1578,7 @@ mod cmd {
|
||||||
doc.append_changes_to_history(view.id);
|
doc.append_changes_to_history(view.id);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => Err(e.context("Couldn't get system clipboard contents")),
|
||||||
log::error!("Couldn't get system clipboard contents: {:?}", e);
|
|
||||||
bail!("Couldn't get system clipboard contents: {:?}", e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1620,24 +1597,18 @@ mod cmd {
|
||||||
args: &[&str],
|
args: &[&str],
|
||||||
_event: PromptEvent,
|
_event: PromptEvent,
|
||||||
) -> anyhow::Result<()> {
|
) -> anyhow::Result<()> {
|
||||||
let dir = args
|
let dir = args.first().context("target directory not provided")?;
|
||||||
.first()
|
|
||||||
.ok_or_else(|| anyhow!("target directory not provided"))?;
|
|
||||||
|
|
||||||
if let Err(e) = std::env::set_current_dir(dir) {
|
if let Err(e) = std::env::set_current_dir(dir) {
|
||||||
bail!("Couldn't change the current working directory: {:?}", e);
|
bail!("Couldn't change the current working directory: {:?}", e);
|
||||||
}
|
}
|
||||||
|
|
||||||
match std::env::current_dir() {
|
let cwd = std::env::current_dir().context("Couldn't get the new working directory")?;
|
||||||
Ok(cwd) => {
|
cx.editor.set_status(format!(
|
||||||
cx.editor.set_status(format!(
|
"Current working directory is now {}",
|
||||||
"Current working directory is now {}",
|
cwd.display()
|
||||||
cwd.display()
|
));
|
||||||
));
|
Ok(())
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
Err(e) => bail!("Couldn't get the new working directory: {}", e),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn show_current_directory(
|
fn show_current_directory(
|
||||||
|
@ -1645,14 +1616,10 @@ mod cmd {
|
||||||
_args: &[&str],
|
_args: &[&str],
|
||||||
_event: PromptEvent,
|
_event: PromptEvent,
|
||||||
) -> anyhow::Result<()> {
|
) -> anyhow::Result<()> {
|
||||||
match std::env::current_dir() {
|
let cwd = std::env::current_dir().context("Couldn't get the new working directory")?;
|
||||||
Ok(cwd) => {
|
cx.editor
|
||||||
cx.editor
|
.set_status(format!("Current working directory is {}", cwd.display()));
|
||||||
.set_status(format!("Current working directory is {}", cwd.display()));
|
Ok(())
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
Err(e) => bail!("Couldn't get the current working directory: {}", e),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sets the [`Document`]'s encoding..
|
/// Sets the [`Document`]'s encoding..
|
||||||
|
@ -2870,10 +2837,10 @@ fn yank_joined_to_clipboard_impl(editor: &mut Editor, separator: &str) -> anyhow
|
||||||
|
|
||||||
let joined = values.join(separator);
|
let joined = values.join(separator);
|
||||||
|
|
||||||
if let Err(e) = editor.clipboard_provider.set_contents(joined) {
|
editor
|
||||||
log::error!("Couldn't set system clipboard content: {:?}", e);
|
.clipboard_provider
|
||||||
bail!("Couldn't set system clipboard content: {:?}", e);
|
.set_contents(joined)
|
||||||
}
|
.context("Couldn't set system clipboard content")?;
|
||||||
|
|
||||||
editor.set_status(msg);
|
editor.set_status(msg);
|
||||||
|
|
||||||
|
@ -2894,7 +2861,6 @@ fn yank_main_selection_to_clipboard_impl(editor: &mut Editor) -> anyhow::Result<
|
||||||
.fragment(doc.text().slice(..));
|
.fragment(doc.text().slice(..));
|
||||||
|
|
||||||
if let Err(e) = editor.clipboard_provider.set_contents(value.into_owned()) {
|
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);
|
bail!("Couldn't set system clipboard content: {:?}", e);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2965,10 +2931,7 @@ fn paste_clipboard_impl(editor: &mut Editor, action: Paste) -> anyhow::Result<()
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
Ok(None) => Ok(()),
|
Ok(None) => Ok(()),
|
||||||
Err(e) => {
|
Err(e) => Err(e.context("Couldn't get system clipboard contents")),
|
||||||
log::error!("Couldn't get system clipboard contents: {:?}", e);
|
|
||||||
bail!("Couldn't get system clipboard contents: {:?}", e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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);
|
let (view, doc) = current!(editor);
|
||||||
|
|
||||||
match editor.clipboard_provider.get_contents() {
|
match editor.clipboard_provider.get_contents() {
|
||||||
|
@ -3014,13 +2977,14 @@ fn replace_selections_with_clipboard_impl(editor: &mut Editor) {
|
||||||
|
|
||||||
doc.apply(&transaction, view.id);
|
doc.apply(&transaction, view.id);
|
||||||
doc.append_changes_to_history(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) {
|
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) {
|
fn paste_after(cx: &mut Context) {
|
||||||
|
|
|
@ -98,14 +98,11 @@ impl Editor {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_theme_from_name(&mut self, theme: &str) -> anyhow::Result<()> {
|
pub fn set_theme_from_name(&mut self, theme: &str) -> anyhow::Result<()> {
|
||||||
let theme = match self.theme_loader.load(theme.as_ref()) {
|
use anyhow::Context;
|
||||||
Ok(theme) => theme,
|
let theme = self
|
||||||
Err(e) => {
|
.theme_loader
|
||||||
log::warn!("failed setting theme `{}` - {}", theme, e);
|
.load(theme.as_ref())
|
||||||
anyhow::bail!("failed setting theme `{}` - {}", theme, e);
|
.with_context(|| format!("failed setting theme `{}`", theme))?;
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
self.set_theme(theme);
|
self.set_theme(theme);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue