1use std::path::{Path, PathBuf};
2
3use serde::{Deserialize, Serialize};
4
5lazy_static::lazy_static! {
6 pub static ref HOME: PathBuf = dirs::home_dir().expect("failed to determine home directory");
7 pub static ref CONFIG_DIR: PathBuf = HOME.join(".config").join("zed");
8 pub static ref CONVERSATIONS_DIR: PathBuf = HOME.join(".config/zed/conversations");
9 pub static ref EMBEDDINGS_DIR: PathBuf = HOME.join(".config/zed/embeddings");
10 pub static ref LOGS_DIR: PathBuf = HOME.join("Library/Logs/Zed");
11 pub static ref SUPPORT_DIR: PathBuf = HOME.join("Library/Application Support/Zed");
12 pub static ref LANGUAGES_DIR: PathBuf = HOME.join("Library/Application Support/Zed/languages");
13 pub static ref COPILOT_DIR: PathBuf = HOME.join("Library/Application Support/Zed/copilot");
14 pub static ref DB_DIR: PathBuf = HOME.join("Library/Application Support/Zed/db");
15 pub static ref SETTINGS: PathBuf = CONFIG_DIR.join("settings.json");
16 pub static ref KEYMAP: PathBuf = CONFIG_DIR.join("keymap.json");
17 pub static ref LAST_USERNAME: PathBuf = CONFIG_DIR.join("last-username.txt");
18 pub static ref LOG: PathBuf = LOGS_DIR.join("Zed.log");
19 pub static ref OLD_LOG: PathBuf = LOGS_DIR.join("Zed.log.old");
20 pub static ref LOCAL_SETTINGS_RELATIVE_PATH: &'static Path = Path::new(".zed/settings.json");
21}
22
23pub mod legacy {
24 use std::path::PathBuf;
25
26 lazy_static::lazy_static! {
27 static ref CONFIG_DIR: PathBuf = super::HOME.join(".zed");
28 pub static ref SETTINGS: PathBuf = CONFIG_DIR.join("settings.json");
29 pub static ref KEYMAP: PathBuf = CONFIG_DIR.join("keymap.json");
30 }
31}
32
33/// Compacts a given file path by replacing the user's home directory
34/// prefix with a tilde (`~`).
35///
36/// # Arguments
37///
38/// * `path` - A reference to a `Path` representing the file path to compact.
39///
40/// # Examples
41///
42/// ```
43/// use std::path::{Path, PathBuf};
44/// use util::paths::compact;
45/// let path: PathBuf = [
46/// util::paths::HOME.to_string_lossy().to_string(),
47/// "some_file.txt".to_string(),
48/// ]
49/// .iter()
50/// .collect();
51/// if cfg!(target_os = "linux") || cfg!(target_os = "macos") {
52/// assert_eq!(compact(&path).to_str(), Some("~/some_file.txt"));
53/// } else {
54/// assert_eq!(compact(&path).to_str(), path.to_str());
55/// }
56/// ```
57///
58/// # Returns
59///
60/// * A `PathBuf` containing the compacted file path. If the input path
61/// does not have the user's home directory prefix, or if we are not on
62/// Linux or macOS, the original path is returned unchanged.
63pub fn compact(path: &Path) -> PathBuf {
64 if cfg!(target_os = "linux") || cfg!(target_os = "macos") {
65 match path.strip_prefix(HOME.as_path()) {
66 Ok(relative_path) => {
67 let mut shortened_path = PathBuf::new();
68 shortened_path.push("~");
69 shortened_path.push(relative_path);
70 shortened_path
71 }
72 Err(_) => path.to_path_buf(),
73 }
74 } else {
75 path.to_path_buf()
76 }
77}
78
79/// A delimiter to use in `path_query:row_number:column_number` strings parsing.
80pub const FILE_ROW_COLUMN_DELIMITER: char = ':';
81
82/// A representation of a path-like string with optional row and column numbers.
83/// Matching values example: `te`, `test.rs:22`, `te:22:5`, etc.
84#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
85pub struct PathLikeWithPosition<P> {
86 pub path_like: P,
87 pub row: Option<u32>,
88 // Absent if row is absent.
89 pub column: Option<u32>,
90}
91
92impl<P> PathLikeWithPosition<P> {
93 /// Parses a string that possibly has `:row:column` suffix.
94 /// Ignores trailing `:`s, so `test.rs:22:` is parsed as `test.rs:22`.
95 /// If any of the row/column component parsing fails, the whole string is then parsed as a path like.
96 pub fn parse_str<E>(
97 s: &str,
98 parse_path_like_str: impl Fn(&str) -> Result<P, E>,
99 ) -> Result<Self, E> {
100 let fallback = |fallback_str| {
101 Ok(Self {
102 path_like: parse_path_like_str(fallback_str)?,
103 row: None,
104 column: None,
105 })
106 };
107
108 match s.trim().split_once(FILE_ROW_COLUMN_DELIMITER) {
109 Some((path_like_str, maybe_row_and_col_str)) => {
110 let path_like_str = path_like_str.trim();
111 let maybe_row_and_col_str = maybe_row_and_col_str.trim();
112 if path_like_str.is_empty() {
113 fallback(s)
114 } else if maybe_row_and_col_str.is_empty() {
115 fallback(path_like_str)
116 } else {
117 let (row_parse_result, maybe_col_str) =
118 match maybe_row_and_col_str.split_once(FILE_ROW_COLUMN_DELIMITER) {
119 Some((maybe_row_str, maybe_col_str)) => {
120 (maybe_row_str.parse::<u32>(), maybe_col_str.trim())
121 }
122 None => (maybe_row_and_col_str.parse::<u32>(), ""),
123 };
124
125 match row_parse_result {
126 Ok(row) => {
127 if maybe_col_str.is_empty() {
128 Ok(Self {
129 path_like: parse_path_like_str(path_like_str)?,
130 row: Some(row),
131 column: None,
132 })
133 } else {
134 match maybe_col_str.parse::<u32>() {
135 Ok(col) => Ok(Self {
136 path_like: parse_path_like_str(path_like_str)?,
137 row: Some(row),
138 column: Some(col),
139 }),
140 Err(_) => fallback(s),
141 }
142 }
143 }
144 Err(_) => fallback(s),
145 }
146 }
147 }
148 None => fallback(s),
149 }
150 }
151
152 pub fn map_path_like<P2, E>(
153 self,
154 mapping: impl FnOnce(P) -> Result<P2, E>,
155 ) -> Result<PathLikeWithPosition<P2>, E> {
156 Ok(PathLikeWithPosition {
157 path_like: mapping(self.path_like)?,
158 row: self.row,
159 column: self.column,
160 })
161 }
162
163 pub fn to_string(&self, path_like_to_string: impl Fn(&P) -> String) -> String {
164 let path_like_string = path_like_to_string(&self.path_like);
165 if let Some(row) = self.row {
166 if let Some(column) = self.column {
167 format!("{path_like_string}:{row}:{column}")
168 } else {
169 format!("{path_like_string}:{row}")
170 }
171 } else {
172 path_like_string
173 }
174 }
175}
176
177#[cfg(test)]
178mod tests {
179 use super::*;
180
181 type TestPath = PathLikeWithPosition<String>;
182
183 fn parse_str(s: &str) -> TestPath {
184 TestPath::parse_str(s, |s| Ok::<_, std::convert::Infallible>(s.to_string()))
185 .expect("infallible")
186 }
187
188 #[test]
189 fn path_with_position_parsing_positive() {
190 let input_and_expected = [
191 (
192 "test_file.rs",
193 PathLikeWithPosition {
194 path_like: "test_file.rs".to_string(),
195 row: None,
196 column: None,
197 },
198 ),
199 (
200 "test_file.rs:1",
201 PathLikeWithPosition {
202 path_like: "test_file.rs".to_string(),
203 row: Some(1),
204 column: None,
205 },
206 ),
207 (
208 "test_file.rs:1:2",
209 PathLikeWithPosition {
210 path_like: "test_file.rs".to_string(),
211 row: Some(1),
212 column: Some(2),
213 },
214 ),
215 ];
216
217 for (input, expected) in input_and_expected {
218 let actual = parse_str(input);
219 assert_eq!(
220 actual, expected,
221 "For positive case input str '{input}', got a parse mismatch"
222 );
223 }
224 }
225
226 #[test]
227 fn path_with_position_parsing_negative() {
228 for input in [
229 "test_file.rs:a",
230 "test_file.rs:a:b",
231 "test_file.rs::",
232 "test_file.rs::1",
233 "test_file.rs:1::",
234 "test_file.rs::1:2",
235 "test_file.rs:1::2",
236 "test_file.rs:1:2:",
237 "test_file.rs:1:2:3",
238 ] {
239 let actual = parse_str(input);
240 assert_eq!(
241 actual,
242 PathLikeWithPosition {
243 path_like: input.to_string(),
244 row: None,
245 column: None,
246 },
247 "For negative case input str '{input}', got a parse mismatch"
248 );
249 }
250 }
251
252 // Trim off trailing `:`s for otherwise valid input.
253 #[test]
254 fn path_with_position_parsing_special() {
255 let input_and_expected = [
256 (
257 "test_file.rs:",
258 PathLikeWithPosition {
259 path_like: "test_file.rs".to_string(),
260 row: None,
261 column: None,
262 },
263 ),
264 (
265 "test_file.rs:1:",
266 PathLikeWithPosition {
267 path_like: "test_file.rs".to_string(),
268 row: Some(1),
269 column: None,
270 },
271 ),
272 ];
273
274 for (input, expected) in input_and_expected {
275 let actual = parse_str(input);
276 assert_eq!(
277 actual, expected,
278 "For special case input str '{input}', got a parse mismatch"
279 );
280 }
281 }
282}