use std::path::{Path, PathBuf}; use serde::{Deserialize, Serialize}; lazy_static::lazy_static! { pub static ref HOME: PathBuf = dirs::home_dir().expect("failed to determine home directory"); pub static ref CONFIG_DIR: PathBuf = HOME.join(".config").join("zed"); pub static ref CONVERSATIONS_DIR: PathBuf = HOME.join(".config/zed/conversations"); pub static ref EMBEDDINGS_DIR: PathBuf = HOME.join(".config/zed/embeddings"); pub static ref LOGS_DIR: PathBuf = HOME.join("Library/Logs/Zed"); pub static ref SUPPORT_DIR: PathBuf = HOME.join("Library/Application Support/Zed"); pub static ref LANGUAGES_DIR: PathBuf = HOME.join("Library/Application Support/Zed/languages"); pub static ref COPILOT_DIR: PathBuf = HOME.join("Library/Application Support/Zed/copilot"); pub static ref DB_DIR: PathBuf = HOME.join("Library/Application Support/Zed/db"); pub static ref SETTINGS: PathBuf = CONFIG_DIR.join("settings.json"); pub static ref KEYMAP: PathBuf = CONFIG_DIR.join("keymap.json"); pub static ref LAST_USERNAME: PathBuf = CONFIG_DIR.join("last-username.txt"); pub static ref LOG: PathBuf = LOGS_DIR.join("Zed.log"); pub static ref OLD_LOG: PathBuf = LOGS_DIR.join("Zed.log.old"); pub static ref LOCAL_SETTINGS_RELATIVE_PATH: &'static Path = Path::new(".zed/settings.json"); } pub mod legacy { use std::path::PathBuf; lazy_static::lazy_static! { static ref CONFIG_DIR: PathBuf = super::HOME.join(".zed"); pub static ref SETTINGS: PathBuf = CONFIG_DIR.join("settings.json"); pub static ref KEYMAP: PathBuf = CONFIG_DIR.join("keymap.json"); } } /// Compacts a given file path by replacing the user's home directory /// prefix with a tilde (`~`). /// /// # Arguments /// /// * `path` - A reference to a `Path` representing the file path to compact. /// /// # Examples /// /// ``` /// use std::path::{Path, PathBuf}; /// use util::paths::compact; /// let path: PathBuf = [ /// util::paths::HOME.to_string_lossy().to_string(), /// "some_file.txt".to_string(), /// ] /// .iter() /// .collect(); /// if cfg!(target_os = "linux") || cfg!(target_os = "macos") { /// assert_eq!(compact(&path).to_str(), Some("~/some_file.txt")); /// } else { /// assert_eq!(compact(&path).to_str(), path.to_str()); /// } /// ``` /// /// # Returns /// /// * A `PathBuf` containing the compacted file path. If the input path /// does not have the user's home directory prefix, or if we are not on /// Linux or macOS, the original path is returned unchanged. pub fn compact(path: &Path) -> PathBuf { if cfg!(target_os = "linux") || cfg!(target_os = "macos") { match path.strip_prefix(HOME.as_path()) { Ok(relative_path) => { let mut shortened_path = PathBuf::new(); shortened_path.push("~"); shortened_path.push(relative_path); shortened_path } Err(_) => path.to_path_buf(), } } else { path.to_path_buf() } } /// A delimiter to use in `path_query:row_number:column_number` strings parsing. pub const FILE_ROW_COLUMN_DELIMITER: char = ':'; /// A representation of a path-like string with optional row and column numbers. /// Matching values example: `te`, `test.rs:22`, `te:22:5`, etc. #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] pub struct PathLikeWithPosition
{
pub path_like: P,
pub row: Option PathLikeWithPosition {
/// Parses a string that possibly has `:row:column` suffix.
/// Ignores trailing `:`s, so `test.rs:22:` is parsed as `test.rs:22`.
/// If any of the row/column component parsing fails, the whole string is then parsed as a path like.
pub fn parse_str ,
) -> Result