diff --git a/crates/journal/src/journal.rs b/crates/journal/src/journal.rs index 9e73e0da550e806b4a642942766414a4b28249ae..2e30b91dab833d18f5fc9c35ad7ea4934d197fa8 100644 --- a/crates/journal/src/journal.rs +++ b/crates/journal/src/journal.rs @@ -173,9 +173,15 @@ pub fn new_journal_entry(workspace: &Workspace, window: &mut Window, cx: &mut Ap } fn journal_dir(path: &str) -> Option { - shellexpand::full(path) //TODO handle this better - .ok() - .map(|dir| Path::new(&dir.to_string()).to_path_buf().join("journal")) + let expanded = shellexpand::full(path).ok()?; + let base_path = Path::new(expanded.as_ref()); + let absolute_path = if base_path.is_absolute() { + base_path.to_path_buf() + } else { + log::warn!("Invalid journal path {path:?} (not absolute), falling back to home directory",); + std::env::home_dir()? + }; + Some(absolute_path.join("journal")) } fn heading_entry(now: NaiveTime, hour_format: &HourFormat) -> String { @@ -224,4 +230,65 @@ mod tests { assert_eq!(actual_heading_entry, expected_heading_entry); } } + + mod journal_dir_tests { + use super::super::*; + + #[test] + #[cfg(target_family = "unix")] + fn test_absolute_unix_path() { + let result = journal_dir("/home/user"); + assert!(result.is_some()); + let path = result.unwrap(); + assert!(path.is_absolute()); + assert_eq!(path, PathBuf::from("/home/user/journal")); + } + + #[test] + fn test_tilde_expansion() { + let result = journal_dir("~/documents"); + assert!(result.is_some()); + let path = result.unwrap(); + + assert!(path.is_absolute(), "Tilde should expand to absolute path"); + + if let Some(home) = std::env::home_dir() { + assert_eq!(path, home.join("documents").join("journal")); + } + } + + #[test] + fn test_relative_path_falls_back_to_home() { + for relative_path in ["relative/path", "NONEXT/some/path", "../some/path"] { + let result = journal_dir(relative_path); + assert!(result.is_some(), "Failed for path: {}", relative_path); + let path = result.unwrap(); + + assert!( + path.is_absolute(), + "Path should be absolute for input '{}', got: {:?}", + relative_path, + path + ); + + if let Some(home) = std::env::home_dir() { + assert_eq!( + path, + home.join("journal"), + "Should fall back to home directory for input '{}'", + relative_path + ); + } + } + } + + #[test] + #[cfg(target_os = "windows")] + fn test_absolute_path_windows_style() { + let result = journal_dir("C:\\Users\\user\\Documents"); + assert!(result.is_some()); + let path = result.unwrap(); + assert_eq!(path, PathBuf::from("C:\\Users\\user\\Documents\\journal")); + } + } } diff --git a/crates/project/src/lsp_store.rs b/crates/project/src/lsp_store.rs index 90675e364b4d962b5c67cafb941b2b6cb9e1df9b..540a1a8eb0ac205d5f777e1728bbe7322bbe6187 100644 --- a/crates/project/src/lsp_store.rs +++ b/crates/project/src/lsp_store.rs @@ -7654,14 +7654,13 @@ impl LspStore { let uri = lsp::Uri::from_file_path(&abs_path) .ok() .with_context(|| format!("Failed to convert path to URI: {}", abs_path.display())) - .unwrap(); + .log_err()?; let next_snapshot = buffer.text_snapshot(); for language_server in language_servers { let language_server = language_server.clone(); let buffer_snapshots = self - .as_local_mut() - .unwrap() + .as_local_mut()? .buffer_snapshots .get_mut(&buffer.remote_id()) .and_then(|m| m.get_mut(&language_server.server_id()))?; diff --git a/docs/src/configuring-zed.md b/docs/src/configuring-zed.md index 14f11df256167928931280cb906cd996615b101b..07d93fd6d167bafeb0a8e4bc72f80f52265edee1 100644 --- a/docs/src/configuring-zed.md +++ b/docs/src/configuring-zed.md @@ -2519,11 +2519,12 @@ Unspecified values have a `false` value, hints won't be toggled if all the modif "path": "~", "hour_format": "hour12" } + ``` ### Path -- Description: The path of the directory where journal entries are stored. +- Description: The path of the directory where journal entries are stored. If an invalid path is specified, the journal will fall back to using `~` (the home directory). - Setting: `path` - Default: `~`