Skip to content

Commit b874a40

Browse files
authored
Fix Lombok path on Windows (#58)
Resolves #57.
1 parent 5f37a48 commit b874a40

File tree

1 file changed

+36
-22
lines changed

1 file changed

+36
-22
lines changed

src/lib.rs

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use std::{
22
collections::BTreeSet,
3+
env::current_dir,
34
fs::{self, create_dir},
4-
path::Path,
5+
path::{Path, PathBuf},
56
};
67

78
use zed_extension_api::{
@@ -15,17 +16,19 @@ use zed_extension_api::{
1516
settings::LspSettings,
1617
};
1718

19+
const PATH_TO_STR_ERROR: &str = "failed to convert path to string";
20+
1821
struct Java {
19-
cached_binary_path: Option<String>,
20-
cached_lombok_path: Option<String>,
22+
cached_binary_path: Option<PathBuf>,
23+
cached_lombok_path: Option<PathBuf>,
2124
}
2225

2326
impl Java {
2427
fn language_server_binary_path(
2528
&mut self,
2629
language_server_id: &LanguageServerId,
2730
worktree: &Worktree,
28-
) -> zed::Result<String> {
31+
) -> zed::Result<PathBuf> {
2932
// Use cached path if exists
3033

3134
if let Some(path) = &self.cached_binary_path {
@@ -43,7 +46,7 @@ impl Java {
4346
};
4447

4548
if let Some(path_binary) = worktree.which(binary_name) {
46-
return Ok(path_binary);
49+
return Ok(PathBuf::from(path_binary));
4750
}
4851

4952
// Check for latest version
@@ -128,11 +131,11 @@ impl Java {
128131
format!("attempt to get latest version's build resulted in a malformed response: {err}")
129132
})?;
130133
let latest_version_build = latest_version_build.trim_end();
131-
let prefix = "jdtls";
134+
let prefix = PathBuf::from("jdtls");
132135
// Exclude ".tar.gz"
133136
let build_directory = &latest_version_build[..latest_version_build.len() - 7];
134-
let build_path = format!("{prefix}/{build_directory}");
135-
let binary_path = format!("{build_path}/bin/{binary_name}");
137+
let build_path = prefix.join(build_directory);
138+
let binary_path = build_path.join("bin").join(binary_name);
136139

137140
// If latest version isn't installed,
138141
if !fs::metadata(&binary_path).is_ok_and(|stat| stat.is_file()) {
@@ -146,10 +149,10 @@ impl Java {
146149
&format!(
147150
"https://www.eclipse.org/downloads/download.php?file=/jdtls/milestones/{latest_version}/{latest_version_build}",
148151
),
149-
&build_path,
152+
build_path.to_str().ok_or(PATH_TO_STR_ERROR)?,
150153
DownloadedFileType::GzipTar,
151154
)?;
152-
make_file_executable(&binary_path)?;
155+
make_file_executable(binary_path.to_str().ok_or(PATH_TO_STR_ERROR)?)?;
153156

154157
// ...and delete other versions
155158

@@ -182,7 +185,7 @@ impl Java {
182185
Ok(binary_path)
183186
}
184187

185-
fn lombok_jar_path(&mut self, language_server_id: &LanguageServerId) -> zed::Result<String> {
188+
fn lombok_jar_path(&mut self, language_server_id: &LanguageServerId) -> zed::Result<PathBuf> {
186189
// Use cached path if exists
187190

188191
if let Some(path) = &self.cached_lombok_path {
@@ -222,10 +225,7 @@ impl Java {
222225
.ok_or("malformed GitHub tags response")?[1..];
223226
let prefix = "lombok";
224227
let jar_name = format!("lombok-{latest_version}.jar");
225-
let jar_path = Path::new(prefix)
226-
.join(&jar_name)
227-
.to_string_lossy()
228-
.into_owned();
228+
let jar_path = Path::new(prefix).join(&jar_name);
229229

230230
// If latest version isn't installed,
231231
if !fs::metadata(&jar_path).is_ok_and(|stat| stat.is_file()) {
@@ -238,7 +238,7 @@ impl Java {
238238
create_dir(prefix).map_err(|err| err.to_string())?;
239239
download_file(
240240
&format!("https://projectlombok.org/downloads/{jar_name}"),
241-
&jar_path,
241+
jar_path.to_str().ok_or(PATH_TO_STR_ERROR)?,
242242
DownloadedFileType::Uncompressed,
243243
)?;
244244

@@ -318,18 +318,32 @@ impl Extension for Java {
318318
.unwrap_or(false);
319319

320320
if lombok_enabled {
321+
let mut current_dir =
322+
current_dir().map_err(|err| format!("could not get current dir: {err}"))?;
323+
324+
if current_platform().0 == Os::Windows {
325+
current_dir = current_dir
326+
.strip_prefix("/")
327+
.map_err(|err| err.to_string())?
328+
.to_path_buf();
329+
}
330+
321331
let lombok_jar_path = self.lombok_jar_path(language_server_id)?;
322-
let lombok_jar_full_path = std::env::current_dir()
323-
.map_err(|e| format!("could not get current dir: {e}"))?
324-
.join(&lombok_jar_path)
325-
.to_string_lossy()
332+
let canonical_lombok_jar_path = current_dir
333+
.join(lombok_jar_path)
334+
.to_str()
335+
.ok_or(PATH_TO_STR_ERROR)?
326336
.to_string();
327337

328-
args.push(format!("--jvm-arg=-javaagent:{lombok_jar_full_path}"));
338+
args.push(format!("--jvm-arg=-javaagent:{canonical_lombok_jar_path}"));
329339
}
330340

331341
Ok(zed::Command {
332-
command: self.language_server_binary_path(language_server_id, worktree)?,
342+
command: self
343+
.language_server_binary_path(language_server_id, worktree)?
344+
.to_str()
345+
.ok_or(PATH_TO_STR_ERROR)?
346+
.to_string(),
333347
args,
334348
env,
335349
})

0 commit comments

Comments
 (0)