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