Skip to content

Commit

Permalink
Remove unnecessary Err from get_canonicalized_path (#8009)
Browse files Browse the repository at this point in the history
Co-authored-by: Michael Davis <[email protected]>
  • Loading branch information
nkitsaini and the-mikedavis authored Aug 20, 2023
1 parent 2767459 commit 22f4f31
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 29 deletions.
4 changes: 2 additions & 2 deletions helix-core/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,15 @@ pub fn get_normalized_path(path: &Path) -> PathBuf {
///
/// This function is used instead of `std::fs::canonicalize` because we don't want to verify
/// here if the path exists, just normalize it's components.
pub fn get_canonicalized_path(path: &Path) -> std::io::Result<PathBuf> {
pub fn get_canonicalized_path(path: &Path) -> PathBuf {
let path = expand_tilde(path);
let path = if path.is_relative() {
helix_loader::current_working_dir().join(path)
} else {
path
};

Ok(get_normalized_path(path.as_path()))
get_normalized_path(path.as_path())
}

pub fn get_relative_path(path: &Path) -> PathBuf {
Expand Down
13 changes: 2 additions & 11 deletions helix-term/src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ impl Application {
let path = helix_loader::runtime_file(Path::new("tutor"));
editor.open(&path, Action::VerticalSplit)?;
// Unset path to prevent accidentally saving to the original tutor file.
doc_mut!(editor).set_path(None)?;
doc_mut!(editor).set_path(None);
} else if !args.files.is_empty() {
let first = &args.files[0].0; // we know it's not empty
if first.is_dir() {
Expand Down Expand Up @@ -554,16 +554,7 @@ impl Application {
let bytes = doc_save_event.text.len_bytes();

if doc.path() != Some(&doc_save_event.path) {
if let Err(err) = doc.set_path(Some(&doc_save_event.path)) {
log::error!(
"error setting path for doc '{:?}': {}",
doc.path(),
err.to_string(),
);

self.editor.set_error(err.to_string());
return;
}
doc.set_path(Some(&doc_save_event.path));

let loader = self.editor.syn_loader.clone();

Expand Down
2 changes: 1 addition & 1 deletion helix-term/src/commands/typed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1679,7 +1679,7 @@ fn tutor(
let path = helix_loader::runtime_file(Path::new("tutor"));
cx.editor.open(&path, Action::Replace)?;
// Unset path to prevent accidentally saving to the original tutor file.
doc_mut!(cx.editor).set_path(None)?;
doc_mut!(cx.editor).set_path(None);
Ok(())
}

Expand Down
10 changes: 5 additions & 5 deletions helix-term/src/ui/picker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ pub enum PathOrId {
}

impl PathOrId {
fn get_canonicalized(self) -> std::io::Result<Self> {
fn get_canonicalized(self) -> Self {
use PathOrId::*;
Ok(match self {
Path(path) => Path(helix_core::path::get_canonicalized_path(&path)?),
match self {
Path(path) => Path(helix_core::path::get_canonicalized_path(&path)),
Id(id) => Id(id),
})
}
}
}

Expand Down Expand Up @@ -375,7 +375,7 @@ impl<T: Item + 'static> Picker<T> {
fn current_file(&self, editor: &Editor) -> Option<FileLocation> {
self.selection()
.and_then(|current| (self.file_fn.as_ref()?)(editor, current))
.and_then(|(path_or_id, line)| path_or_id.get_canonicalized().ok().zip(Some(line)))
.map(|(path_or_id, line)| (path_or_id.get_canonicalized(), line))
}

/// Get (cached) preview for a given path. If a document corresponding
Expand Down
2 changes: 1 addition & 1 deletion helix-term/tests/test/picker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ async fn test_picker_alt_ret() -> anyhow::Result<()> {
];
let paths = files
.iter()
.map(|f| get_canonicalized_path(f.path()).unwrap())
.map(|f| get_canonicalized_path(f.path()))
.collect::<Vec<_>>();

fs::write(&paths[0], "1\n2\n3\n4")?;
Expand Down
12 changes: 4 additions & 8 deletions helix-view/src/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -708,7 +708,7 @@ impl Document {
let mut doc = Self::from(rope, Some((encoding, has_bom)), config);

// set the path and try detecting the language
doc.set_path(Some(path))?;
doc.set_path(Some(path));
if let Some(loader) = config_loader {
doc.detect_language(loader);
}
Expand Down Expand Up @@ -853,7 +853,7 @@ impl Document {
let text = self.text().clone();

let path = match path {
Some(path) => helix_core::path::get_canonicalized_path(&path)?,
Some(path) => helix_core::path::get_canonicalized_path(&path),
None => {
if self.path.is_none() {
bail!("Can't save with no path set!");
Expand Down Expand Up @@ -1047,18 +1047,14 @@ impl Document {
self.encoding
}

pub fn set_path(&mut self, path: Option<&Path>) -> Result<(), std::io::Error> {
let path = path
.map(helix_core::path::get_canonicalized_path)
.transpose()?;
pub fn set_path(&mut self, path: Option<&Path>) {
let path = path.map(helix_core::path::get_canonicalized_path);

// if parent doesn't exist we still want to open the document
// and error out when document is saved
self.path = path;

self.detect_readonly();

Ok(())
}

/// Set the programming language for the file and load associated data (e.g. highlighting)
Expand Down
2 changes: 1 addition & 1 deletion helix-view/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1436,7 +1436,7 @@ impl Editor {

// ??? possible use for integration tests
pub fn open(&mut self, path: &Path, action: Action) -> Result<DocumentId, Error> {
let path = helix_core::path::get_canonicalized_path(path)?;
let path = helix_core::path::get_canonicalized_path(path);
let id = self.document_by_path(&path).map(|doc| doc.id);

let id = if let Some(id) = id {
Expand Down

0 comments on commit 22f4f31

Please sign in to comment.