-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathdebugger.rs
More file actions
342 lines (282 loc) · 11.2 KB
/
Copy pathdebugger.rs
File metadata and controls
342 lines (282 loc) · 11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
use std::{collections::HashMap, env, fs, path::PathBuf};
use serde::{Deserialize, Serialize};
use zed_extension_api::{
self as zed, DownloadedFileType, LanguageServerId, LanguageServerInstallationStatus, Os,
TcpArgumentsTemplate, Worktree, current_platform, download_file,
http_client::{HttpMethod, HttpRequest, fetch},
serde_json::{self, Value, json},
set_language_server_installation_status,
};
use crate::lsp::LspWrapper;
#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
struct JavaDebugLaunchConfig {
request: String,
#[serde(skip_serializing_if = "Option::is_none")]
project_name: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
main_class: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
args: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
vm_args: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
encoding: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
class_paths: Option<Vec<String>>,
#[serde(skip_serializing_if = "Option::is_none")]
module_paths: Option<Vec<String>>,
#[serde(skip_serializing_if = "Option::is_none")]
cwd: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
env: Option<HashMap<String, String>>,
#[serde(skip_serializing_if = "Option::is_none")]
stop_on_entry: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
no_debug: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
console: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
shorten_command_line: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
launcher_script: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
java_exec: Option<String>,
}
const TEST_SCOPE: &str = "$Test";
const AUTO_SCOPE: &str = "$Auto";
const RUNTIME_SCOPE: &str = "$Runtime";
const SCOPES: [&str; 3] = [TEST_SCOPE, AUTO_SCOPE, RUNTIME_SCOPE];
const PATH_TO_STR_ERROR: &str = "Failed to convert path to string";
const MAVEN_METADATA_URL: &str = "https://repo1.maven.org/maven2/com/microsoft/java/com.microsoft.java.debug.plugin/maven-metadata.xml";
pub struct Debugger {
lsp: LspWrapper,
plugin_path: Option<PathBuf>,
}
impl Debugger {
pub fn new(lsp: LspWrapper) -> Debugger {
Debugger {
plugin_path: None,
lsp,
}
}
pub fn loaded(&self) -> bool {
self.plugin_path.is_some()
}
pub fn get_or_download(
&mut self,
language_server_id: &LanguageServerId,
) -> zed::Result<PathBuf> {
let prefix = "debugger";
if let Some(path) = &self.plugin_path
&& fs::metadata(path).is_ok_and(|stat| stat.is_file())
{
return Ok(path.clone());
}
set_language_server_installation_status(
language_server_id,
&LanguageServerInstallationStatus::CheckingForUpdate,
);
let res = fetch(
&HttpRequest::builder()
.method(HttpMethod::Get)
.url(MAVEN_METADATA_URL)
.build()?,
);
// Maven loves to be down, trying to resolve it gracefully
if let Err(err) = &res {
if !fs::metadata(prefix).is_ok_and(|stat| stat.is_dir()) {
return Err(err.to_owned());
}
// If it's not a 5xx code, then return an error.
if !err.contains("status code 5") {
return Err(err.to_owned());
}
let exists = fs::read_dir(prefix)
.ok()
.and_then(|dir| dir.last().map(|v| v.ok()))
.flatten();
if let Some(file) = exists {
if !file.metadata().is_ok_and(|stat| stat.is_file()) {
return Err(err.to_owned());
}
if !file
.file_name()
.to_str()
.is_some_and(|name| name.ends_with(".jar"))
{
return Err(err.to_owned());
}
let jar_path = PathBuf::from(prefix).join(file.file_name());
self.plugin_path = Some(jar_path.clone());
return Ok(jar_path);
}
}
let xml = String::from_utf8(res?.body).map_err(|err| {
format!("could not get string from maven metadata response body: {err}")
})?;
let start_tag = "<latest>";
let end_tag = "</latest>";
let latest_version = xml
.split_once(start_tag)
.and_then(|(_, rest)| rest.split_once(end_tag))
.map(|(content, _)| content.trim())
.ok_or(format!("Failed to parse maven-metadata.xml response {xml}"))?;
let artifact = "com.microsoft.java.debug.plugin";
let jar_name = format!("{artifact}-{latest_version}.jar");
let jar_path = PathBuf::from(prefix).join(&jar_name);
if !fs::metadata(&jar_path).is_ok_and(|stat| stat.is_file()) {
if let Err(err) = fs::remove_dir_all(prefix) {
println!("failed to remove directory entry: {err}");
}
set_language_server_installation_status(
language_server_id,
&LanguageServerInstallationStatus::Downloading,
);
fs::create_dir(prefix).map_err(|err| err.to_string())?;
let url = format!(
"https://repo1.maven.org/maven2/com/microsoft/java/{artifact}/{latest_version}/{jar_name}"
);
download_file(
url.as_str(),
jar_path.to_str().ok_or(PATH_TO_STR_ERROR)?,
DownloadedFileType::Uncompressed,
)
.map_err(|err| format!("Failed to download {url} {err}"))?;
}
self.plugin_path = Some(jar_path.clone());
Ok(jar_path)
}
pub fn start_session(&self) -> zed::Result<TcpArgumentsTemplate> {
let port = self.lsp.get()?.request::<u16>(
"workspace/executeCommand",
json!({ "command": "vscode.java.startDebugSession" }),
)?;
Ok(TcpArgumentsTemplate {
host: None,
port: Some(port),
timeout: None,
})
}
pub fn inject_config(&self, worktree: &Worktree, config_string: String) -> zed::Result<String> {
let config: Value = serde_json::from_str(&config_string)
.map_err(|err| format!("Failed to parse debug config {err}"))?;
if config
.get("request")
.and_then(Value::as_str)
.is_some_and(|req| req != "launch")
{
return Ok(config_string);
}
let mut config = serde_json::from_value::<JavaDebugLaunchConfig>(config)
.map_err(|err| format!("Failed to parse java debug config {err}"))?;
let workspace_folder = worktree.root_path();
let (main_class, project_name) = {
let arguments = [config.main_class.clone(), config.project_name.clone()]
.iter()
.flatten()
.cloned()
.collect::<Vec<String>>();
let entries = self
.lsp
.get()?
.resolve_main_class(arguments)?
.into_iter()
.filter(|entry| {
config
.main_class
.as_ref()
.map(|class| &entry.main_class == class)
.unwrap_or(true)
})
.filter(|entry| {
config
.project_name
.as_ref()
.map(|class| &entry.project_name == class)
.unwrap_or(true)
})
.collect::<Vec<_>>();
if entries.len() > 1 {
return Err("Project have multiple entry points, you must explicitly specify \"mainClass\" or \"projectName\"".to_owned());
}
match entries.first() {
None => (config.main_class, config.project_name),
Some(entry) => (
Some(entry.main_class.to_owned()),
Some(entry.project_name.to_owned()),
),
}
};
let mut classpaths = config.class_paths.unwrap_or(vec![AUTO_SCOPE.to_string()]);
if classpaths
.iter()
.any(|class| SCOPES.contains(&class.as_str()))
{
// https://github.com/microsoft/vscode-java-debug/blob/main/src/configurationProvider.ts#L518
let scope = {
if classpaths.iter().any(|class| class == TEST_SCOPE) {
Some("test".to_string())
} else if classpaths.iter().any(|class| class == AUTO_SCOPE) {
None
} else if classpaths.iter().any(|class| class == RUNTIME_SCOPE) {
Some("runtime".to_string())
} else {
None
}
};
let arguments = vec![main_class.clone(), project_name.clone(), scope.clone()];
let result = self.lsp.get()?.resolve_class_path(arguments)?;
for resolved in result {
classpaths.extend(resolved);
}
}
classpaths.retain(|class| !SCOPES.contains(&class.as_str()));
classpaths.dedup();
config.class_paths = Some(classpaths);
config.main_class = main_class;
config.project_name = project_name;
config.cwd = config.cwd.or(Some(workspace_folder.to_string()));
let config = serde_json::to_string(&config)
.map_err(|err| format!("Failed to stringify debug config {err}"))?
.replace("${workspaceFolder}", &workspace_folder);
Ok(config)
}
pub fn inject_plugin_into_options(
&self,
initialization_options: Option<Value>,
) -> zed::Result<Value> {
let current_dir =
env::current_dir().map_err(|err| format!("could not get current dir: {err}"))?;
let canonical_path = Value::String(
current_dir
.join(
self.plugin_path
.as_ref()
.ok_or("Debugger is not loaded yet")?,
)
.to_string_lossy()
.to_string(),
);
match initialization_options {
None => Ok(json!({
"bundles": [canonical_path]
})),
Some(options) => {
let mut options = options.clone();
let mut bundles = options
.get_mut("bundles")
.unwrap_or(&mut Value::Array(vec![]))
.take();
let bundles_vec = bundles
.as_array_mut()
.ok_or("Invalid initialization_options format")?;
if !bundles_vec.contains(&canonical_path) {
bundles_vec.push(canonical_path);
}
options["bundles"] = bundles;
Ok(options)
}
}
}
}