From 9c02a1b070b90668c97968b848421ad2de9d459b Mon Sep 17 00:00:00 2001 From: Lionel Flandrin Date: Sat, 26 Jun 2021 18:50:44 +0100 Subject: Make command implementation return a Result<()> The error message is displayed with cx.editor.set_error. --- helix-view/src/editor.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'helix-view/src') diff --git a/helix-view/src/editor.rs b/helix-view/src/editor.rs index 4f01cce4..c80535ed 100644 --- a/helix-view/src/editor.rs +++ b/helix-view/src/editor.rs @@ -97,16 +97,17 @@ impl Editor { self._refresh(); } - pub fn set_theme_from_name(&mut self, theme: &str) { + 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); - return; + anyhow::bail!("failed setting theme `{}` - {}", theme, e); } }; self.set_theme(theme); + Ok(()) } fn _refresh(&mut self) { -- cgit v1.2.3-70-g09d2 From d530d6e39d58f5759d2db0a9bda1ea5e21154f83 Mon Sep 17 00:00:00 2001 From: Blaž Hrastnik Date: Sun, 11 Jul 2021 16:35:57 +0900 Subject: Further simplify error handling in :commands --- helix-term/src/commands.rs | 108 +++++++++++++++------------------------------ helix-view/src/editor.rs | 13 +++--- 2 files changed, 41 insertions(+), 80 deletions(-) (limited to 'helix-view/src') diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 8a9ffb91..8bbdfdfa 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -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}, @@ -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>( @@ -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"); @@ -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. @@ -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( @@ -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) } @@ -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) } @@ -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) } @@ -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")), } } @@ -1620,24 +1597,18 @@ 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( @@ -1645,14 +1616,10 @@ mod cmd { _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.. @@ -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); @@ -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); } @@ -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")), } } @@ -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() { @@ -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) { diff --git a/helix-view/src/editor.rs b/helix-view/src/editor.rs index c80535ed..a468b87c 100644 --- a/helix-view/src/editor.rs +++ b/helix-view/src/editor.rs @@ -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(()) } -- cgit v1.2.3-70-g09d2 From 0b1ed8656db35397122c1c2779e31bf86e38c430 Mon Sep 17 00:00:00 2001 From: Kirawi Date: Wed, 14 Jul 2021 22:22:34 -0400 Subject: Fix #442 (#446) * fix #442 fix #442 fmt * create Rope from default line ending * Fix use of encoding in Document::open()--- helix-view/src/document.rs | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) (limited to 'helix-view/src') diff --git a/helix-view/src/document.rs b/helix-view/src/document.rs index a2bd1c41..8fdf7d98 100644 --- a/helix-view/src/document.rs +++ b/helix-view/src/document.rs @@ -456,14 +456,16 @@ impl Document { theme: Option<&Theme>, config_loader: Option<&syntax::Loader>, ) -> Result { - if !path.exists() { - return Ok(Self::default()); - } + let (mut rope, encoding) = if path.exists() { + let mut file = + std::fs::File::open(&path).context(format!("unable to open {:?}", path))?; + from_reader(&mut file, encoding)? + } else { + let encoding = encoding.unwrap_or(encoding_rs::UTF_8); + (Rope::from(DEFAULT_LINE_ENDING.as_str()), encoding) + }; - let mut file = std::fs::File::open(&path).context(format!("unable to open {:?}", path))?; - let (mut rope, encoding) = from_reader(&mut file, encoding)?; let line_ending = with_line_ending(&mut rope); - let mut doc = Self::from(rope, Some(encoding)); // set the path and try detecting the language -- cgit v1.2.3-70-g09d2 From 9fcbbfa46762484fc132383d9c2855ce37f60d6f Mon Sep 17 00:00:00 2001 From: Cor Date: Wed, 14 Jul 2021 21:29:39 +0200 Subject: Changed startup behaviour to only open a single view when multiple files are specified on the commandline. Changed the behaviour; the first argument on the commandline is the file on display --- helix-term/src/application.rs | 5 ++++- helix-view/src/editor.rs | 4 ++++ 2 files changed, 8 insertions(+), 1 deletion(-) (limited to 'helix-view/src') diff --git a/helix-term/src/application.rs b/helix-term/src/application.rs index 79dd7c3b..c55d4c98 100644 --- a/helix-term/src/application.rs +++ b/helix-term/src/application.rs @@ -83,15 +83,18 @@ impl Application { editor.new_file(Action::VerticalSplit); compositor.push(Box::new(ui::file_picker(first.clone()))); } else { + let nr_of_files = args.files.len(); + editor.open(first.to_path_buf(), Action::VerticalSplit)?; for file in args.files { if file.is_dir() { return Err(anyhow::anyhow!( "expected a path to file, found a directory. (to open a directory pass it as first argument)" )); } else { - editor.open(file, Action::VerticalSplit)?; + editor.open(file.to_path_buf(), Action::Load)?; } } + editor.set_status(format!("Loaded {} files.", nr_of_files)); } } else { editor.new_file(Action::VerticalSplit); diff --git a/helix-view/src/editor.rs b/helix-view/src/editor.rs index a468b87c..cd9d0a92 100644 --- a/helix-view/src/editor.rs +++ b/helix-view/src/editor.rs @@ -39,6 +39,7 @@ pub struct Editor { #[derive(Debug, Copy, Clone)] pub enum Action { + Load, Replace, HorizontalSplit, VerticalSplit, @@ -151,6 +152,9 @@ impl Editor { return; } + Action::Load => { + return; + } Action::HorizontalSplit => { let view = View::new(id); let view_id = self.tree.split(view, Layout::Horizontal); -- cgit v1.2.3-70-g09d2