on master help sign in
Add more context; Editor::open doesn't need to own path
ed950fcSkyler Hawthorne
helix-term/src/application.rs+10−7
@@ -25,7 +25,7 @@ use std::{
25 time::{Duration, Instant},
25 time::{Duration, Instant},
26};
26};
27
27
28−use anyhow::Error;
28+use anyhow::{Context, Error};
29
29
30use crossterm::{
30use crossterm::{
31 event::{DisableMouseCapture, EnableMouseCapture, Event},
31 event::{DisableMouseCapture, EnableMouseCapture, Event},
@@ -122,7 +122,7 @@ impl Application {
122 });
122 });
123 let syn_loader = std::sync::Arc::new(syntax::Loader::new(syn_loader_conf));
123 let syn_loader = std::sync::Arc::new(syntax::Loader::new(syn_loader_conf));
124
124
125− let mut compositor = Compositor::new()?;
125+ let mut compositor = Compositor::new().context("build compositor")?;
126 let config = Arc::new(ArcSwap::from_pointee(config));
126 let config = Arc::new(ArcSwap::from_pointee(config));
127 let mut editor = Editor::new(
127 let mut editor = Editor::new(
128 compositor.size(),
128 compositor.size(),
@@ -141,26 +141,28 @@ impl Application {
141
141
142 if args.load_tutor {
142 if args.load_tutor {
143 let path = helix_loader::runtime_dir().join("tutor.txt");
143 let path = helix_loader::runtime_dir().join("tutor.txt");
144− editor.open(path, Action::VerticalSplit)?;
144+ editor.open(&path, Action::VerticalSplit)?;
145 // Unset path to prevent accidentally saving to the original tutor file.
145 // Unset path to prevent accidentally saving to the original tutor file.
146 doc_mut!(editor).set_path(None)?;
146 doc_mut!(editor).set_path(None)?;
147 } else if !args.files.is_empty() {
147 } else if !args.files.is_empty() {
148 let first = &args.files[0].0; // we know it's not empty
148 let first = &args.files[0].0; // we know it's not empty
149 if first.is_dir() {
149 if first.is_dir() {
150− std::env::set_current_dir(&first)?;
150+ std::env::set_current_dir(&first).context("set current dir")?;
151 editor.new_file(Action::VerticalSplit);
151 editor.new_file(Action::VerticalSplit);
152 let picker = ui::file_picker(".".into(), &config.load().editor);
152 let picker = ui::file_picker(".".into(), &config.load().editor);
153 compositor.push(Box::new(overlayed(picker)));
153 compositor.push(Box::new(overlayed(picker)));
154 } else {
154 } else {
155 let nr_of_files = args.files.len();
155 let nr_of_files = args.files.len();
156− editor.open(first.to_path_buf(), Action::VerticalSplit)?;
156+ editor.open(first, Action::VerticalSplit)?;
157 for (file, pos) in args.files {
157 for (file, pos) in args.files {
158 if file.is_dir() {
158 if file.is_dir() {
159 return Err(anyhow::anyhow!(
159 return Err(anyhow::anyhow!(
160 "expected a path to file, found a directory. (to open a directory pass it as first argument)"
160 "expected a path to file, found a directory. (to open a directory pass it as first argument)"
161 ));
161 ));
162 } else {
162 } else {
163+ let doc_id = editor
163− let doc_id = editor.open(file, Action::Load)?;
164+ .open(&file, Action::Load)
165+ .context(format!("open '{}'", file.to_string_lossy()))?;
164 // with Action::Load all documents have the same view
166 // with Action::Load all documents have the same view
165 let view_id = editor.tree.focus;
167 let view_id = editor.tree.focus;
166 let doc = editor.document_mut(doc_id).unwrap();
168 let doc = editor.document_mut(doc_id).unwrap();
@@ -192,7 +194,8 @@ impl Application {
192 #[cfg(windows)]
194 #[cfg(windows)]
193 let signals = futures_util::stream::empty();
195 let signals = futures_util::stream::empty();
194 #[cfg(not(windows))]
196 #[cfg(not(windows))]
197+ let signals =
195− let signals = Signals::new(&[signal::SIGTSTP, signal::SIGCONT])?;
198+ Signals::new(&[signal::SIGTSTP, signal::SIGCONT]).context("build signal handler")?;
196
199
197 let app = Self {
200 let app = Self {
198 compositor,
201 compositor,
helix-term/src/commands.rs+2−2
@@ -1024,7 +1024,7 @@ fn goto_file_impl(cx: &mut Context, action: Action) {
1024 for sel in paths {
1024 for sel in paths {
1025 let p = sel.trim();
1025 let p = sel.trim();
1026 if !p.is_empty() {
1026 if !p.is_empty() {
1027− if let Err(e) = cx.editor.open(PathBuf::from(p), action) {
1027+ if let Err(e) = cx.editor.open(&PathBuf::from(p), action) {
1028 cx.editor.set_error(format!("Open file failed: {:?}", e));
1028 cx.editor.set_error(format!("Open file failed: {:?}", e));
1029 }
1029 }
1030 }
1030 }
@@ -1849,7 +1849,7 @@ fn global_search(cx: &mut Context) {
1849 }
1849 }
1850 },
1850 },
1851 move |cx, (line_num, path), action| {
1851 move |cx, (line_num, path), action| {
1852− match cx.editor.open(path.into(), action) {
1852+ match cx.editor.open(path, action) {
1853 Ok(_) => {}
1853 Ok(_) => {}
1854 Err(e) => {
1854 Err(e) => {
1855 cx.editor.set_error(format!(
1855 cx.editor.set_error(format!(
helix-term/src/commands/lsp.rs+3−3
@@ -61,7 +61,7 @@ fn jump_to_location(
61 return;
61 return;
62 }
62 }
63 };
63 };
64− let _id = editor.open(path, action).expect("editor.open failed");
64+ let _id = editor.open(&path, action).expect("editor.open failed");
65 let (view, doc) = current!(editor);
65 let (view, doc) = current!(editor);
66 let definition_pos = location.range.start;
66 let definition_pos = location.range.start;
67 // TODO: convert inside server
67 // TODO: convert inside server
@@ -114,7 +114,7 @@ fn sym_picker(
114 return;
114 return;
115 }
115 }
116 };
116 };
117− if let Err(err) = cx.editor.open(path, action) {
117+ if let Err(err) = cx.editor.open(&path, action) {
118 let err = format!("failed to open document: {}: {}", uri, err);
118 let err = format!("failed to open document: {}: {}", uri, err);
119 log::error!("{}", err);
119 log::error!("{}", err);
120 cx.editor.set_error(err);
120 cx.editor.set_error(err);
@@ -385,7 +385,7 @@ pub fn apply_workspace_edit(
385 };
385 };
386
386
387 let current_view_id = view!(editor).id;
387 let current_view_id = view!(editor).id;
388− let doc_id = match editor.open(path, Action::Load) {
388+ let doc_id = match editor.open(&path, Action::Load) {
389 Ok(doc_id) => doc_id,
389 Ok(doc_id) => doc_id,
390 Err(err) => {
390 Err(err) => {
391 let err = format!("failed to open document: {}: {}", uri, err);
391 let err = format!("failed to open document: {}: {}", uri, err);
helix-term/src/commands/typed.rs+6−6
@@ -50,7 +50,7 @@ fn open(
50 ensure!(!args.is_empty(), "wrong argument count");
50 ensure!(!args.is_empty(), "wrong argument count");
51 for arg in args {
51 for arg in args {
52 let (path, pos) = args::parse_file(arg);
52 let (path, pos) = args::parse_file(arg);
53− let _ = cx.editor.open(path, Action::Replace)?;
53+ let _ = cx.editor.open(&path, Action::Replace)?;
54 let (view, doc) = current!(cx.editor);
54 let (view, doc) = current!(cx.editor);
55 let pos = Selection::point(pos_at_coords(doc.text().slice(..), pos, true));
55 let pos = Selection::point(pos_at_coords(doc.text().slice(..), pos, true));
56 doc.set_selection(view.id, pos);
56 doc.set_selection(view.id, pos);
@@ -819,7 +819,7 @@ fn vsplit(
819 } else {
819 } else {
820 for arg in args {
820 for arg in args {
821 cx.editor
821 cx.editor
822− .open(PathBuf::from(arg.as_ref()), Action::VerticalSplit)?;
822+ .open(&PathBuf::from(arg.as_ref()), Action::VerticalSplit)?;
823 }
823 }
824 }
824 }
825
825
@@ -838,7 +838,7 @@ fn hsplit(
838 } else {
838 } else {
839 for arg in args {
839 for arg in args {
840 cx.editor
840 cx.editor
841− .open(PathBuf::from(arg.as_ref()), Action::HorizontalSplit)?;
841+ .open(&PathBuf::from(arg.as_ref()), Action::HorizontalSplit)?;
842 }
842 }
843 }
843 }
844
844
@@ -923,7 +923,7 @@ fn tutor(
923 _event: PromptEvent,
923 _event: PromptEvent,
924) -> anyhow::Result<()> {
924) -> anyhow::Result<()> {
925 let path = helix_loader::runtime_dir().join("tutor.txt");
925 let path = helix_loader::runtime_dir().join("tutor.txt");
926− cx.editor.open(path, Action::Replace)?;
926+ cx.editor.open(&path, Action::Replace)?;
927 // Unset path to prevent accidentally saving to the original tutor file.
927 // Unset path to prevent accidentally saving to the original tutor file.
928 doc_mut!(cx.editor).set_path(None)?;
928 doc_mut!(cx.editor).set_path(None)?;
929 Ok(())
929 Ok(())
@@ -1150,7 +1150,7 @@ fn open_config(
1150 _event: PromptEvent,
1150 _event: PromptEvent,
1151) -> anyhow::Result<()> {
1151) -> anyhow::Result<()> {
1152 cx.editor
1152 cx.editor
1153− .open(helix_loader::config_file(), Action::Replace)?;
1153+ .open(&helix_loader::config_file(), Action::Replace)?;
1154 Ok(())
1154 Ok(())
1155}
1155}
1156
1156
@@ -1159,7 +1159,7 @@ fn open_log(
1159 _args: &[Cow<str>],
1159 _args: &[Cow<str>],
1160 _event: PromptEvent,
1160 _event: PromptEvent,
1161) -> anyhow::Result<()> {
1161) -> anyhow::Result<()> {
1162− cx.editor.open(helix_loader::log_file(), Action::Replace)?;
1162+ cx.editor.open(&helix_loader::log_file(), Action::Replace)?;
1163 Ok(())
1163 Ok(())
1164}
1164}
1165
1165
helix-term/src/compositor.rs+3−3
@@ -63,7 +63,7 @@ pub trait Component: Any + AnyComponent {
63 }
63 }
64}
64}
65
65
66−use anyhow::Error;
66+use anyhow::Context as AnyhowContext;
67use std::io::stdout;
67use std::io::stdout;
68use tui::backend::{Backend, CrosstermBackend};
68use tui::backend::{Backend, CrosstermBackend};
69type Terminal = tui::terminal::Terminal<CrosstermBackend<std::io::Stdout>>;
69type Terminal = tui::terminal::Terminal<CrosstermBackend<std::io::Stdout>>;
@@ -76,9 +76,9 @@ pub struct Compositor {
76}
76}
77
77
78impl Compositor {
78impl Compositor {
79− pub fn new() -> Result<Self, Error> {
79+ pub fn new() -> anyhow::Result<Self> {
80 let backend = CrosstermBackend::new(stdout());
80 let backend = CrosstermBackend::new(stdout());
81− let terminal = Terminal::new(backend)?;
81+ let terminal = Terminal::new(backend).context("build terminal")?;
82 Ok(Self {
82 Ok(Self {
83 layers: Vec::new(),
83 layers: Vec::new(),
84 terminal,
84 terminal,
helix-term/src/ui/mod.rs+1−1
@@ -175,7 +175,7 @@ pub fn file_picker(root: PathBuf, config: &helix_view::editor::Config) -> FilePi
175 path.strip_prefix(&root).unwrap_or(path).to_string_lossy()
175 path.strip_prefix(&root).unwrap_or(path).to_string_lossy()
176 },
176 },
177 move |cx, path: &PathBuf, action| {
177 move |cx, path: &PathBuf, action| {
178− if let Err(e) = cx.editor.open(path.into(), action) {
178+ if let Err(e) = cx.editor.open(path, action) {
179 let err = if let Some(err) = e.source() {
179 let err = if let Some(err) = e.source() {
180 format!("{}", err)
180 format!("{}", err)
181 } else {
181 } else {
helix-view/src/editor.rs+2−2
@@ -779,8 +779,8 @@ impl Editor {
779 }
779 }
780
780
781 // ??? possible use for integration tests
781 // ??? possible use for integration tests
782− pub fn open(&mut self, path: PathBuf, action: Action) -> Result<DocumentId, Error> {
782+ pub fn open(&mut self, path: &Path, action: Action) -> Result<DocumentId, Error> {
783− let path = helix_core::path::get_canonicalized_path(&path)?;
783+ let path = helix_core::path::get_canonicalized_path(path)?;
784 let id = self.document_by_path(&path).map(|doc| doc.id);
784 let id = self.document_by_path(&path).map(|doc| doc.id);
785
785
786 let id = if let Some(id) = id {
786 let id = if let Some(id) = id {
helix-view/src/handlers/dap.rs+1−1
@@ -62,7 +62,7 @@ pub fn jump_to_stack_frame(editor: &mut Editor, frame: &helix_dap::StackFrame) {
62 return;
62 return;
63 };
63 };
64
64
65− if let Err(e) = editor.open(path, Action::Replace) {
65+ if let Err(e) = editor.open(&path, Action::Replace) {
66 editor.set_error(format!("Unable to jump to stack frame: {}", e));
66 editor.set_error(format!("Unable to jump to stack frame: {}", e));
67 return;
67 return;
68 }
68 }