Skip to content

Commit 168ff5e

Browse files
committed
Normalize path quoting
- Quote paths centrally in path_to_string and drop OS-specific helpers - Pass unescaped JDTLS shared config path and simplify Lombok -javaagent arg formatting - Move the path escaping helper and its tests into the shared utilities `src/util.rs`
1 parent cc22d5c commit 168ff5e

3 files changed

Lines changed: 56 additions & 67 deletions

File tree

src/java.rs

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -143,37 +143,6 @@ impl Java {
143143
}
144144
}
145145

146-
fn format_lombok_agent_arg(path: &str, os: zed::Os) -> String {
147-
if os == zed::Os::Windows {
148-
format!("-javaagent:\"{path}\"")
149-
} else {
150-
format!("-javaagent:{path}")
151-
}
152-
}
153-
154-
#[cfg(test)]
155-
mod tests {
156-
use super::*;
157-
use zed_extension_api::Os;
158-
159-
#[test]
160-
fn test_format_lombok_agent_arg_windows() {
161-
let path = "C:\\Users\\User Name\\lombok.jar";
162-
let arg = format_lombok_agent_arg(path, Os::Windows);
163-
assert_eq!(arg, format!("-javaagent:\"{path}\""));
164-
}
165-
166-
#[test]
167-
fn test_format_lombok_agent_arg_unix() {
168-
let path = "/Users/username/lombok.jar";
169-
let arg = format_lombok_agent_arg(path, Os::Mac);
170-
assert_eq!(arg, format!("-javaagent:{path}"));
171-
172-
let arg_linux = format_lombok_agent_arg(path, Os::Linux);
173-
assert_eq!(arg_linux, format!("-javaagent:{path}"));
174-
}
175-
}
176-
177146
impl Extension for Java {
178147
fn new() -> Self
179148
where
@@ -319,10 +288,7 @@ impl Extension for Java {
319288
self.lombok_jar_path(language_server_id, &configuration, worktree)?;
320289
let canonical_lombok_jar_path = path_to_string(current_dir.join(lombok_jar_path))?;
321290

322-
Some(format_lombok_agent_arg(
323-
&canonical_lombok_jar_path,
324-
zed::current_platform().0,
325-
))
291+
Some(format!("-javaagent:{canonical_lombok_jar_path}"))
326292
} else {
327293
None
328294
};

src/jdtls.rs

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ pub fn build_jdtls_launch_args(
7272
"-Dosgi.checkConfiguration=true".to_string(),
7373
format!(
7474
"-Dosgi.sharedConfiguration.area={}",
75-
escape_path_if_needed(path_to_string(shared_config_path)?, current_platform().0)
75+
path_to_string(shared_config_path)?
7676
),
7777
"-Dosgi.sharedConfiguration.area.readOnly=true".to_string(),
7878
"-Dosgi.configuration.cascaded=true".to_string(),
@@ -353,34 +353,3 @@ fn get_shared_config_path(jdtls_base_directory: &Path) -> PathBuf {
353353
};
354354
jdtls_base_directory.join(config_to_use)
355355
}
356-
357-
fn escape_path_if_needed(path: String, os: Os) -> String {
358-
if os == Os::Windows {
359-
format!("\"{}\"", path)
360-
} else {
361-
path
362-
}
363-
}
364-
365-
#[cfg(test)]
366-
mod tests {
367-
use super::*;
368-
use zed_extension_api::Os;
369-
370-
#[test]
371-
fn test_escape_path_if_needed_windows() {
372-
let path = "C:\\Users\\User Name\\Projects\\zed-extension-java".to_string();
373-
let escaped = escape_path_if_needed(path.clone(), Os::Windows);
374-
assert_eq!(escaped, format!("\"{}\"", path));
375-
}
376-
377-
#[test]
378-
fn test_escape_path_if_needed_unix() {
379-
let path = "/Users/username/Projects/zed-extension-java".to_string();
380-
let escaped = escape_path_if_needed(path.clone(), Os::Mac);
381-
assert_eq!(escaped, path);
382-
383-
let escaped_linux = escape_path_if_needed(path.clone(), Os::Linux);
384-
assert_eq!(escaped_linux, path);
385-
}
386-
}

src/util.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@ pub fn path_to_string<P: AsRef<Path>>(path: P) -> zed::Result<String> {
264264
.to_path_buf()
265265
.into_os_string()
266266
.into_string()
267+
.map(escape_path_if_needed)
267268
.map_err(|_| PATH_TO_STR_ERROR.to_string())
268269
}
269270

@@ -345,3 +346,56 @@ pub fn should_use_local_or_download(
345346
CheckUpdates::Always => Ok(None),
346347
}
347348
}
349+
350+
// Escape path if included spaces
351+
// Add quotes to path if needed for avoid errors with spaces in path
352+
//
353+
// # Arguments
354+
// * `path` - The path to escape
355+
//
356+
// # Returns
357+
// * `String` - The escaped path
358+
//
359+
fn escape_path_if_needed(path: String) -> String {
360+
if path.contains(' ') {
361+
format!("\"{}\"", path)
362+
} else {
363+
path
364+
}
365+
}
366+
367+
#[cfg(test)]
368+
mod tests {
369+
use super::*;
370+
371+
#[test]
372+
fn test_path_to_string_with_spaces_windows() {
373+
let path = PathBuf::from("C:\\Users\\User Name\\Projects\\zed-extension-java");
374+
let escaped = path_to_string(&path).unwrap();
375+
assert_eq!(
376+
escaped,
377+
"\"C:\\Users\\User Name\\Projects\\zed-extension-java\""
378+
);
379+
}
380+
381+
#[test]
382+
fn test_path_to_string_without_spaces_windows() {
383+
let path = PathBuf::from("C:\\Users\\UserName\\Projects\\zed-extension-java");
384+
let escaped = path_to_string(&path).unwrap();
385+
assert_eq!(escaped, "C:\\Users\\UserName\\Projects\\zed-extension-java");
386+
}
387+
388+
#[test]
389+
fn test_path_to_string_with_spaces_unix() {
390+
let path = PathBuf::from("/home/username/Projects/zed extension java");
391+
let escaped = path_to_string(&path).unwrap();
392+
assert_eq!(escaped, "\"/home/username/Projects/zed extension java\"");
393+
}
394+
395+
#[test]
396+
fn test_path_to_string_without_spaces_unix() {
397+
let path = PathBuf::from("/home/username/Projects/zed-extension-java");
398+
let escaped = path_to_string(&path).unwrap();
399+
assert_eq!(escaped, "/home/username/Projects/zed-extension-java");
400+
}
401+
}

0 commit comments

Comments
 (0)