on master help sign in
Add more context; Editor::open doesn't need to own path
commited950fcc56c480dc5a54c7e07918dca9192db200 authorSkyler Hawthorne <skyler@dead10ck.com> dateTue 26 Apr 2022, 19:18 (5 years ago) parent1533f48
8 files changed+28−25
helix-term/src/application.rs+10−7 helix-term/src/commands.rs+2−2 helix-term/src/commands/lsp.rs+3−3 helix-term/src/commands/typed.rs+6−6 helix-term/src/compositor.rs+3−3 helix-term/src/ui/mod.rs+1−1 helix-view/src/editor.rs+2−2 helix-view/src/handlers/dap.rs+1−1
helix-term/src/application.rs+10−7
@@ -25,7 +25,7 @@ use std::{
2525 time::{Duration, Instant},
2626};
2727
28−use anyhow::Error;
28+use anyhow::{Context, Error};
2929
3030use crossterm::{
3131 event::{DisableMouseCapture, EnableMouseCapture, Event},
@@ -122,7 +122,7 @@ impl Application {
122122 });
123123 let syn_loader = std::sync::Arc::new(syntax::Loader::new(syn_loader_conf));
124124
125− let mut compositor = Compositor::new()?;
125+ let mut compositor = Compositor::new().context("build compositor")?;
126126 let config = Arc::new(ArcSwap::from_pointee(config));
127127 let mut editor = Editor::new(
128128 compositor.size(),
@@ -141,26 +141,28 @@ impl Application {
141141
142142 if args.load_tutor {
143143 let path = helix_loader::runtime_dir().join("tutor.txt");
144− editor.open(path, Action::VerticalSplit)?;
144+ editor.open(&path, Action::VerticalSplit)?;
145145 // Unset path to prevent accidentally saving to the original tutor file.
146146 doc_mut!(editor).set_path(None)?;
147147 } else if !args.files.is_empty() {
148148 let first = &args.files[0].0; // we know it's not empty
149149 if first.is_dir() {
150− std::env::set_current_dir(&first)?;
150+ std::env::set_current_dir(&first).context("set current dir")?;
151151 editor.new_file(Action::VerticalSplit);
152152 let picker = ui::file_picker(".".into(), &config.load().editor);
153153 compositor.push(Box::new(overlayed(picker)));
154154 } else {
155155 let nr_of_files = args.files.len();
156− editor.open(first.to_path_buf(), Action::VerticalSplit)?;
156+ editor.open(first, Action::VerticalSplit)?;
157157 for (file, pos) in args.files {
158158 if file.is_dir() {
159159 return Err(anyhow::anyhow!(
160160 "expected a path to file, found a directory. (to open a directory pass it as first argument)"
161161 ));
162162 } else {
163− let doc_id = editor.open(file, Action::Load)?;
163+ let doc_id = editor
164+ .open(&file, Action::Load)
165+ .context(format!("open '{}'", file.to_string_lossy()))?;
164166 // with Action::Load all documents have the same view
165167 let view_id = editor.tree.focus;
166168 let doc = editor.document_mut(doc_id).unwrap();
@@ -192,7 +194,8 @@ impl Application {
192194 #[cfg(windows)]
193195 let signals = futures_util::stream::empty();
194196 #[cfg(not(windows))]
195− let signals = Signals::new(&[signal::SIGTSTP, signal::SIGCONT])?;
197+ let signals =
198+ Signals::new(&[signal::SIGTSTP, signal::SIGCONT]).context("build signal handler")?;
196199
197200 let app = Self {
198201 compositor,
helix-term/src/commands.rs+2−2
@@ -1024,7 +1024,7 @@ fn goto_file_impl(cx: &mut Context, action: Action) {
10241024 for sel in paths {
10251025 let p = sel.trim();
10261026 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) {
10281028 cx.editor.set_error(format!("Open file failed: {:?}", e));
10291029 }
10301030 }
@@ -1849,7 +1849,7 @@ fn global_search(cx: &mut Context) {
18491849 }
18501850 },
18511851 move |cx, (line_num, path), action| {
1852− match cx.editor.open(path.into(), action) {
1852+ match cx.editor.open(path, action) {
18531853 Ok(_) => {}
18541854 Err(e) => {
18551855 cx.editor.set_error(format!(
helix-term/src/commands/lsp.rs+3−3
@@ -61,7 +61,7 @@ fn jump_to_location(
6161 return;
6262 }
6363 };
64− let _id = editor.open(path, action).expect("editor.open failed");
64+ let _id = editor.open(&path, action).expect("editor.open failed");
6565 let (view, doc) = current!(editor);
6666 let definition_pos = location.range.start;
6767 // TODO: convert inside server
@@ -114,7 +114,7 @@ fn sym_picker(
114114 return;
115115 }
116116 };
117− if let Err(err) = cx.editor.open(path, action) {
117+ if let Err(err) = cx.editor.open(&path, action) {
118118 let err = format!("failed to open document: {}: {}", uri, err);
119119 log::error!("{}", err);
120120 cx.editor.set_error(err);
@@ -385,7 +385,7 @@ pub fn apply_workspace_edit(
385385 };
386386
387387 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) {
389389 Ok(doc_id) => doc_id,
390390 Err(err) => {
391391 let err = format!("failed to open document: {}: {}", uri, err);
helix-term/src/commands/typed.rs+6−6
@@ -50,7 +50,7 @@ fn open(
5050 ensure!(!args.is_empty(), "wrong argument count");
5151 for arg in args {
5252 let (path, pos) = args::parse_file(arg);
53− let _ = cx.editor.open(path, Action::Replace)?;
53+ let _ = cx.editor.open(&path, Action::Replace)?;
5454 let (view, doc) = current!(cx.editor);
5555 let pos = Selection::point(pos_at_coords(doc.text().slice(..), pos, true));
5656 doc.set_selection(view.id, pos);
@@ -819,7 +819,7 @@ fn vsplit(
819819 } else {
820820 for arg in args {
821821 cx.editor
822− .open(PathBuf::from(arg.as_ref()), Action::VerticalSplit)?;
822+ .open(&PathBuf::from(arg.as_ref()), Action::VerticalSplit)?;
823823 }
824824 }
825825
@@ -838,7 +838,7 @@ fn hsplit(
838838 } else {
839839 for arg in args {
840840 cx.editor
841− .open(PathBuf::from(arg.as_ref()), Action::HorizontalSplit)?;
841+ .open(&PathBuf::from(arg.as_ref()), Action::HorizontalSplit)?;
842842 }
843843 }
844844
@@ -923,7 +923,7 @@ fn tutor(
923923 _event: PromptEvent,
924924) -> anyhow::Result<()> {
925925 let path = helix_loader::runtime_dir().join("tutor.txt");
926− cx.editor.open(path, Action::Replace)?;
926+ cx.editor.open(&path, Action::Replace)?;
927927 // Unset path to prevent accidentally saving to the original tutor file.
928928 doc_mut!(cx.editor).set_path(None)?;
929929 Ok(())
@@ -1150,7 +1150,7 @@ fn open_config(
11501150 _event: PromptEvent,
11511151) -> anyhow::Result<()> {
11521152 cx.editor
1153− .open(helix_loader::config_file(), Action::Replace)?;
1153+ .open(&helix_loader::config_file(), Action::Replace)?;
11541154 Ok(())
11551155}
11561156
@@ -1159,7 +1159,7 @@ fn open_log(
11591159 _args: &[Cow<str>],
11601160 _event: PromptEvent,
11611161) -> anyhow::Result<()> {
1162− cx.editor.open(helix_loader::log_file(), Action::Replace)?;
1162+ cx.editor.open(&helix_loader::log_file(), Action::Replace)?;
11631163 Ok(())
11641164}
11651165
helix-term/src/compositor.rs+3−3
@@ -63,7 +63,7 @@ pub trait Component: Any + AnyComponent {
6363 }
6464}
6565
66−use anyhow::Error;
66+use anyhow::Context as AnyhowContext;
6767use std::io::stdout;
6868use tui::backend::{Backend, CrosstermBackend};
6969type Terminal = tui::terminal::Terminal<CrosstermBackend<std::io::Stdout>>;
@@ -76,9 +76,9 @@ pub struct Compositor {
7676}
7777
7878impl Compositor {
79− pub fn new() -> Result<Self, Error> {
79+ pub fn new() -> anyhow::Result<Self> {
8080 let backend = CrosstermBackend::new(stdout());
81− let terminal = Terminal::new(backend)?;
81+ let terminal = Terminal::new(backend).context("build terminal")?;
8282 Ok(Self {
8383 layers: Vec::new(),
8484 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
175175 path.strip_prefix(&root).unwrap_or(path).to_string_lossy()
176176 },
177177 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) {
179179 let err = if let Some(err) = e.source() {
180180 format!("{}", err)
181181 } else {
helix-view/src/editor.rs+2−2
@@ -779,8 +779,8 @@ impl Editor {
779779 }
780780
781781 // ??? possible use for integration tests
782− pub fn open(&mut self, path: PathBuf, action: Action) -> Result<DocumentId, Error> {
783− let path = helix_core::path::get_canonicalized_path(&path)?;
782+ pub fn open(&mut self, path: &Path, action: Action) -> Result<DocumentId, Error> {
783+ let path = helix_core::path::get_canonicalized_path(path)?;
784784 let id = self.document_by_path(&path).map(|doc| doc.id);
785785
786786 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) {
6262 return;
6363 };
6464
65− if let Err(e) = editor.open(path, Action::Replace) {
65+ if let Err(e) = editor.open(&path, Action::Replace) {
6666 editor.set_error(format!("Unable to jump to stack frame: {}", e));
6767 return;
6868 }