windjammer-lsp 0.45.0

Language Server Protocol implementation for Windjammer
Documentation
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
//! Debug Adapter Protocol support (planned feature for v0.35.0)
#![allow(dead_code)]
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::collections::HashMap;
use std::process::{Child, Command, Stdio};
use tokio::sync::mpsc;

/// Debug Adapter Protocol implementation for Windjammer
///
/// This adapter translates between the DAP protocol and rust-lldb/gdb
/// to enable debugging of Windjammer programs.
#[cfg_attr(test, allow(dead_code))]
pub struct DebugAdapter {
    /// The underlying debugger process (lldb or gdb)
    debugger: Option<Child>,
    /// Source map: Windjammer file -> Rust file
    source_map: HashMap<String, String>,
    /// Breakpoints
    breakpoints: HashMap<String, Vec<Breakpoint>>,
    /// Request/response channel
    response_tx: mpsc::UnboundedSender<DapMessage>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(test, allow(dead_code))]
pub struct DapMessage {
    #[serde(rename = "type")]
    pub msg_type: String,
    pub seq: i64,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub command: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub arguments: Option<Value>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub body: Option<Value>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub success: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub message: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(test, allow(dead_code))]
pub struct Breakpoint {
    pub line: i32,
    pub verified: bool,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub source: Option<Source>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(test, allow(dead_code))]
pub struct Source {
    pub path: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(test, allow(dead_code))]
pub struct StackFrame {
    pub id: i32,
    pub name: String,
    pub source: Source,
    pub line: i32,
    pub column: i32,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(test, allow(dead_code))]
pub struct Variable {
    pub name: String,
    pub value: String,
    #[serde(rename = "type")]
    pub var_type: Option<String>,
    pub variables_reference: i32,
}

impl DebugAdapter {
    pub fn new(response_tx: mpsc::UnboundedSender<DapMessage>) -> Self {
        Self {
            debugger: None,
            source_map: HashMap::new(),
            breakpoints: HashMap::new(),
            response_tx,
        }
    }

    /// Initialize the debug adapter
    pub async fn initialize(&mut self, args: Value) -> Result<DapMessage, String> {
        tracing::info!("Initializing debug adapter with args: {:?}", args);

        // Build the source map from Windjammer files to Rust files
        self.build_source_map().await?;

        // Send capabilities response
        Ok(DapMessage {
            msg_type: "response".to_string(),
            seq: 1,
            command: Some("initialize".to_string()),
            arguments: None,
            body: Some(serde_json::json!({
                "supportsConfigurationDoneRequest": true,
                "supportsEvaluateForHovers": true,
                "supportsStepBack": false,
                "supportsSetVariable": true,
                "supportsRestartFrame": false,
                "supportsGotoTargetsRequest": false,
                "supportsStepInTargetsRequest": false,
                "supportsCompletionsRequest": false,
                "supportsModulesRequest": false,
                "supportsExceptionOptions": false,
                "supportsValueFormattingOptions": true,
                "supportsExceptionInfoRequest": false,
                "supportTerminateDebuggee": true,
                "supportSuspendDebuggee": false,
                "supportsDelayedStackTraceLoading": false,
                "supportsLoadedSourcesRequest": false,
                "supportsLogPoints": false,
                "supportsTerminateThreadsRequest": false,
                "supportsSetExpression": false,
                "supportsTerminateRequest": true,
                "supportsDataBreakpoints": false,
                "supportsReadMemoryRequest": false,
                "supportsWriteMemoryRequest": false,
                "supportsDisassembleRequest": false,
                "supportsCancelRequest": false,
                "supportsBreakpointLocationsRequest": false,
                "supportsClipboardContext": false,
                "supportsSteppingGranularity": false,
                "supportsInstructionBreakpoints": false,
                "supportsExceptionFilterOptions": false
            })),
            success: Some(true),
            message: None,
        })
    }

    /// Launch a debug session
    pub async fn launch(&mut self, args: Value) -> Result<DapMessage, String> {
        tracing::info!("Launching debug session with args: {:?}", args);

        // Extract program path from args
        let program = args
            .get("program")
            .and_then(|p| p.as_str())
            .ok_or("No program specified")?;

        // Start lldb
        let child = Command::new("lldb")
            .arg("--batch")
            .arg("-o")
            .arg(format!("file {}", program))
            .stdin(Stdio::piped())
            .stdout(Stdio::piped())
            .stderr(Stdio::piped())
            .spawn()
            .map_err(|e| format!("Failed to start lldb: {}", e))?;

        self.debugger = Some(child);

        Ok(DapMessage {
            msg_type: "response".to_string(),
            seq: 2,
            command: Some("launch".to_string()),
            arguments: None,
            body: None,
            success: Some(true),
            message: None,
        })
    }

    /// Set breakpoints
    pub async fn set_breakpoints(
        &mut self,
        source: String,
        lines: Vec<i32>,
    ) -> Result<DapMessage, String> {
        tracing::info!("Setting breakpoints in {} at lines: {:?}", source, lines);

        // Map Windjammer source to Rust source
        let rust_source = self
            .source_map
            .get(&source)
            .ok_or("Source file not found in source map")?;

        // Convert Windjammer line numbers to Rust line numbers
        // (In a real implementation, we'd use the source map for this)
        let breakpoints: Vec<Breakpoint> = lines
            .iter()
            .map(|&line| Breakpoint {
                line,
                verified: true,
                source: Some(Source {
                    path: rust_source.clone(),
                    name: Some(source.clone()),
                }),
            })
            .collect();

        // Store breakpoints
        self.breakpoints.insert(source.clone(), breakpoints.clone());

        // Send breakpoint commands to lldb
        if let Some(ref mut _debugger) = self.debugger {
            for bp in &breakpoints {
                let cmd = format!("breakpoint set --file {} --line {}\n", rust_source, bp.line);
                // TODO: Send command to lldb stdin
                tracing::debug!("Would send to lldb: {}", cmd.trim());
            }
        }

        Ok(DapMessage {
            msg_type: "response".to_string(),
            seq: 3,
            command: Some("setBreakpoints".to_string()),
            arguments: None,
            body: Some(serde_json::json!({
                "breakpoints": breakpoints
            })),
            success: Some(true),
            message: None,
        })
    }

    /// Continue execution
    pub async fn continue_exec(&mut self) -> Result<DapMessage, String> {
        tracing::info!("Continuing execution");

        // Send continue command to lldb
        if let Some(ref mut _debugger) = self.debugger {
            // TODO: Send "continue" to lldb stdin
            tracing::debug!("Would send to lldb: continue");
        }

        Ok(DapMessage {
            msg_type: "response".to_string(),
            seq: 4,
            command: Some("continue".to_string()),
            arguments: None,
            body: Some(serde_json::json!({
                "allThreadsContinued": true
            })),
            success: Some(true),
            message: None,
        })
    }

    /// Step over
    pub async fn next(&mut self) -> Result<DapMessage, String> {
        tracing::info!("Stepping over (next)");

        // Send next command to lldb
        if let Some(ref mut _debugger) = self.debugger {
            // TODO: Send "next" to lldb stdin
            tracing::debug!("Would send to lldb: next");
        }

        Ok(DapMessage {
            msg_type: "response".to_string(),
            seq: 5,
            command: Some("next".to_string()),
            arguments: None,
            body: None,
            success: Some(true),
            message: None,
        })
    }

    /// Step into
    pub async fn step_in(&mut self) -> Result<DapMessage, String> {
        tracing::info!("Stepping into (step)");

        // Send step command to lldb
        if let Some(ref mut _debugger) = self.debugger {
            // TODO: Send "step" to lldb stdin
            tracing::debug!("Would send to lldb: step");
        }

        Ok(DapMessage {
            msg_type: "response".to_string(),
            seq: 6,
            command: Some("stepIn".to_string()),
            arguments: None,
            body: None,
            success: Some(true),
            message: None,
        })
    }

    /// Step out
    pub async fn step_out(&mut self) -> Result<DapMessage, String> {
        tracing::info!("Stepping out (finish)");

        // Send finish command to lldb
        if let Some(ref mut _debugger) = self.debugger {
            // TODO: Send "finish" to lldb stdin
            tracing::debug!("Would send to lldb: finish");
        }

        Ok(DapMessage {
            msg_type: "response".to_string(),
            seq: 7,
            command: Some("stepOut".to_string()),
            arguments: None,
            body: None,
            success: Some(true),
            message: None,
        })
    }

    /// Get stack trace
    pub async fn stack_trace(&mut self) -> Result<DapMessage, String> {
        tracing::info!("Getting stack trace");

        // Get stack trace from lldb
        let frames = vec![
            StackFrame {
                id: 1,
                name: "main".to_string(),
                source: Source {
                    path: "src/main.wj".to_string(),
                    name: Some("main.wj".to_string()),
                },
                line: 10,
                column: 0,
            },
            // More frames would be parsed from lldb output
        ];

        Ok(DapMessage {
            msg_type: "response".to_string(),
            seq: 8,
            command: Some("stackTrace".to_string()),
            arguments: None,
            body: Some(serde_json::json!({
                "stackFrames": frames,
                "totalFrames": frames.len()
            })),
            success: Some(true),
            message: None,
        })
    }

    /// Get variables
    pub async fn variables(&mut self, variables_reference: i32) -> Result<DapMessage, String> {
        tracing::info!("Getting variables for reference: {}", variables_reference);

        // Get variables from lldb
        let vars = vec![
            Variable {
                name: "x".to_string(),
                value: "42".to_string(),
                var_type: Some("int".to_string()),
                variables_reference: 0,
            },
            Variable {
                name: "name".to_string(),
                value: "\"Windjammer\"".to_string(),
                var_type: Some("string".to_string()),
                variables_reference: 0,
            },
            // More variables would be parsed from lldb output
        ];

        Ok(DapMessage {
            msg_type: "response".to_string(),
            seq: 9,
            command: Some("variables".to_string()),
            arguments: None,
            body: Some(serde_json::json!({
                "variables": vars
            })),
            success: Some(true),
            message: None,
        })
    }

    /// Evaluate expression
    pub async fn evaluate(&mut self, expression: String) -> Result<DapMessage, String> {
        tracing::info!("Evaluating expression: {}", expression);

        // Evaluate expression in lldb
        // For now, return a placeholder result
        Ok(DapMessage {
            msg_type: "response".to_string(),
            seq: 10,
            command: Some("evaluate".to_string()),
            arguments: None,
            body: Some(serde_json::json!({
                "result": "<evaluation result>",
                "type": "unknown",
                "variablesReference": 0
            })),
            success: Some(true),
            message: None,
        })
    }

    /// Terminate the debug session
    pub async fn terminate(&mut self) -> Result<DapMessage, String> {
        tracing::info!("Terminating debug session");

        // Kill the debugger process
        if let Some(mut debugger) = self.debugger.take() {
            let _ = debugger.kill();
        }

        Ok(DapMessage {
            msg_type: "response".to_string(),
            seq: 11,
            command: Some("terminate".to_string()),
            arguments: None,
            body: None,
            success: Some(true),
            message: None,
        })
    }

    /// Build source map from Windjammer files to generated Rust files
    async fn build_source_map(&mut self) -> Result<(), String> {
        // In a real implementation, this would:
        // 1. Find all .wj files in the project
        // 2. Determine the corresponding generated .rs files
        // 3. Build a mapping between them
        //
        // For now, use placeholder mappings
        self.source_map.insert(
            "src/main.wj".to_string(),
            "build_output/src/main.rs".to_string(),
        );

        Ok(())
    }
}