on master help sign in
Remove unnecessary `Err` from `get_canonicalized_path` (#8009)

Co-authored-by: Michael Davis <mcarsondavis@gmail.com>

commit22f4f313f1cbdaadafcc3dd471f5a0bb4f7034e1 authornkitsaini <74284503+nkitsaini@users.noreply.github.com> dateMon 21 Aug 2023, 00:41 (3 years ago) parent2767459
7 files changed+16−29
helix-core/src/path.rs+2−2 helix-term/src/application.rs+2−11 helix-term/src/commands/typed.rs+1−1 helix-term/src/ui/picker.rs+5−5 helix-term/tests/test/picker.rs+1−1 helix-view/src/document.rs+4−8 helix-view/src/editor.rs+1−1
helix-core/src/path.rs+2−2
@@ -85,7 +85,7 @@ pub fn get_normalized_path(path: &Path) -> PathBuf {
8585///
8686/// This function is used instead of `std::fs::canonicalize` because we don't want to verify
8787/// here if the path exists, just normalize it's components.
88−pub fn get_canonicalized_path(path: &Path) -> std::io::Result<PathBuf> {
88+pub fn get_canonicalized_path(path: &Path) -> PathBuf {
8989 let path = expand_tilde(path);
9090 let path = if path.is_relative() {
9191 helix_loader::current_working_dir().join(path)
@@ -93,7 +93,7 @@ pub fn get_canonicalized_path(path: &Path) -> std::io::Result<PathBuf> {
9393 path
9494 };
9595
96− Ok(get_normalized_path(path.as_path()))
96+ get_normalized_path(path.as_path())
9797}
9898
9999pub fn get_relative_path(path: &Path) -> PathBuf {
helix-term/src/application.rs+2−11
@@ -160,7 +160,7 @@ impl Application {
160160 let path = helix_loader::runtime_file(Path::new("tutor"));
161161 editor.open(&path, Action::VerticalSplit)?;
162162 // Unset path to prevent accidentally saving to the original tutor file.
163− doc_mut!(editor).set_path(None)?;
163+ doc_mut!(editor).set_path(None);
164164 } else if !args.files.is_empty() {
165165 let first = &args.files[0].0; // we know it's not empty
166166 if first.is_dir() {
@@ -554,16 +554,7 @@ impl Application {
554554 let bytes = doc_save_event.text.len_bytes();
555555
556556 if doc.path() != Some(&doc_save_event.path) {
557− if let Err(err) = doc.set_path(Some(&doc_save_event.path)) {
558− log::error!(
559− "error setting path for doc '{:?}': {}",
560− doc.path(),
561− err.to_string(),
562− );
563−
564− self.editor.set_error(err.to_string());
565− return;
566− }
557+ doc.set_path(Some(&doc_save_event.path));
567558
568559 let loader = self.editor.syn_loader.clone();
569560
helix-term/src/commands/typed.rs+1−1
@@ -1679,7 +1679,7 @@ fn tutor(
16791679 let path = helix_loader::runtime_file(Path::new("tutor"));
16801680 cx.editor.open(&path, Action::Replace)?;
16811681 // Unset path to prevent accidentally saving to the original tutor file.
1682− doc_mut!(cx.editor).set_path(None)?;
1682+ doc_mut!(cx.editor).set_path(None);
16831683 Ok(())
16841684}
16851685
helix-term/src/ui/picker.rs+5−5
@@ -51,12 +51,12 @@ pub enum PathOrId {
5151}
5252
5353impl PathOrId {
54− fn get_canonicalized(self) -> std::io::Result<Self> {
54+ fn get_canonicalized(self) -> Self {
5555 use PathOrId::*;
56− Ok(match self {
57− Path(path) => Path(helix_core::path::get_canonicalized_path(&path)?),
56+ match self {
57+ Path(path) => Path(helix_core::path::get_canonicalized_path(&path)),
5858 Id(id) => Id(id),
59− })
59+ }
6060 }
6161}
6262
@@ -375,7 +375,7 @@ impl<T: Item + 'static> Picker<T> {
375375 fn current_file(&self, editor: &Editor) -> Option<FileLocation> {
376376 self.selection()
377377 .and_then(|current| (self.file_fn.as_ref()?)(editor, current))
378− .and_then(|(path_or_id, line)| path_or_id.get_canonicalized().ok().zip(Some(line)))
378+ .map(|(path_or_id, line)| (path_or_id.get_canonicalized(), line))
379379 }
380380
381381 /// Get (cached) preview for a given path. If a document corresponding
helix-term/tests/test/picker.rs+1−1
@@ -30,7 +30,7 @@ async fn test_picker_alt_ret() -> anyhow::Result<()> {
3030 ];
3131 let paths = files
3232 .iter()
33− .map(|f| get_canonicalized_path(f.path()).unwrap())
33+ .map(|f| get_canonicalized_path(f.path()))
3434 .collect::<Vec<_>>();
3535
3636 fs::write(&paths[0], "1\n2\n3\n4")?;
helix-view/src/document.rs+4−8
@@ -708,7 +708,7 @@ impl Document {
708708 let mut doc = Self::from(rope, Some((encoding, has_bom)), config);
709709
710710 // set the path and try detecting the language
711− doc.set_path(Some(path))?;
711+ doc.set_path(Some(path));
712712 if let Some(loader) = config_loader {
713713 doc.detect_language(loader);
714714 }
@@ -853,7 +853,7 @@ impl Document {
853853 let text = self.text().clone();
854854
855855 let path = match path {
856− Some(path) => helix_core::path::get_canonicalized_path(&path)?,
856+ Some(path) => helix_core::path::get_canonicalized_path(&path),
857857 None => {
858858 if self.path.is_none() {
859859 bail!("Can't save with no path set!");
@@ -1047,18 +1047,14 @@ impl Document {
10471047 self.encoding
10481048 }
10491049
1050− pub fn set_path(&mut self, path: Option<&Path>) -> Result<(), std::io::Error> {
1051− let path = path
1052− .map(helix_core::path::get_canonicalized_path)
1053− .transpose()?;
1050+ pub fn set_path(&mut self, path: Option<&Path>) {
1051+ let path = path.map(helix_core::path::get_canonicalized_path);
10541052
10551053 // if parent doesn't exist we still want to open the document
10561054 // and error out when document is saved
10571055 self.path = path;
10581056
10591057 self.detect_readonly();
1060−
1061− Ok(())
10621058 }
10631059
10641060 /// Set the programming language for the file and load associated data (e.g. highlighting)
helix-view/src/editor.rs+1−1
@@ -1436,7 +1436,7 @@ impl Editor {
14361436
14371437 // ??? possible use for integration tests
14381438 pub fn open(&mut self, path: &Path, action: Action) -> Result<DocumentId, Error> {
1439− let path = helix_core::path::get_canonicalized_path(path)?;
1439+ let path = helix_core::path::get_canonicalized_path(path);
14401440 let id = self.document_by_path(&path).map(|doc| doc.id);
14411441
14421442 let id = if let Some(id) = id {