windjammer-mcp 0.37.1

Model Context Protocol (MCP) server for Windjammer - AI-powered development
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
//! Extract function refactoring tool
//!
//! Transforms selected code into a new reusable function.

use crate::error::{McpError, McpResult};
use crate::protocol::{Range, ToolCallResult};
use crate::tools::text_response;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::collections::HashSet;
use std::sync::Arc;
use tokio::sync::Mutex;
use windjammer::lexer::Lexer;
use windjammer::parser::{Expression, Parser, Statement, Type};
use windjammer_lsp::database::WindjammerDatabase;

#[derive(Debug, Serialize, Deserialize)]
pub struct ExtractFunctionRequest {
    /// Source code to refactor
    pub code: String,

    /// Selection range to extract
    pub range: Range,

    /// Name for the new function
    pub function_name: String,

    /// Optional: Make function public
    #[serde(default)]
    pub make_public: bool,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct ExtractFunctionResponse {
    pub success: bool,

    /// Refactored code with new function
    pub refactored_code: Option<String>,

    /// New function signature
    pub function_signature: Option<String>,

    /// Variables captured from outer scope
    pub captured_variables: Option<Vec<String>>,

    pub error: Option<String>,
}

/// Extract function refactoring tool
pub async fn handle(
    _db: Arc<Mutex<WindjammerDatabase>>,
    arguments: Value,
) -> McpResult<ToolCallResult> {
    let request: ExtractFunctionRequest =
        serde_json::from_value(arguments).map_err(|e| McpError::ValidationError {
            field: "arguments".to_string(),
            message: e.to_string(),
        })?;

    // Parse the source code
    let mut lexer = Lexer::new(&request.code);
    let tokens = lexer.tokenize_with_locations();
    let mut parser = Parser::new(tokens);
    let parse_result = parser.parse();

    let program = match parse_result {
        Ok(prog) => prog,
        Err(e) => {
            let response = ExtractFunctionResponse {
                success: false,
                refactored_code: None,
                function_signature: None,
                captured_variables: None,
                error: Some(format!("Parse error: {}", e)),
            };
            return Ok(text_response(&serde_json::to_string(&response)?));
        }
    };

    // Extract statements in the selected range
    let extracted = extract_statements_in_range(&program, &request.range);

    if extracted.is_empty() {
        let response = ExtractFunctionResponse {
            success: false,
            refactored_code: None,
            function_signature: None,
            captured_variables: None,
            error: Some("No statements found in selection".to_string()),
        };
        return Ok(text_response(&serde_json::to_string(&response)?));
    }

    // Analyze variable usage to determine parameters and return value
    let analysis = analyze_variable_usage(&extracted);

    // Generate new function
    let new_function = generate_function(
        &request.function_name,
        &extracted,
        &analysis,
        request.make_public,
    );

    // Generate function call to replace extracted code
    let function_call = generate_function_call(&request.function_name, &analysis);

    // Build refactored code (simplified - would need proper AST manipulation)
    let refactored_code = format!(
        "// TODO: Implement full AST manipulation\n// New function:\n{}\n\n// Replace selection with:\n{}",
        new_function, function_call
    );

    let response = ExtractFunctionResponse {
        success: true,
        refactored_code: Some(refactored_code),
        function_signature: Some(format!(
            "fn {}({}) -> {}",
            request.function_name,
            analysis
                .parameters
                .iter()
                .map(|(name, ty)| format!("{}: {:?}", name, ty))
                .collect::<Vec<_>>()
                .join(", "),
            if let Some(ret) = &analysis.return_type {
                format!("{:?}", ret)
            } else {
                "()".to_string()
            }
        )),
        captured_variables: Some(analysis.parameters.iter().map(|(n, _)| n.clone()).collect()),
        error: None,
    };

    Ok(text_response(&serde_json::to_string(&response)?))
}

/// Extract statements within a given range
fn extract_statements_in_range(
    _program: &windjammer::parser::Program,
    _range: &Range,
) -> Vec<Statement> {
    // TODO: Implement actual statement extraction based on line/column range
    // For now, return empty vec
    vec![]
}

/// Analysis result for variable usage
#[derive(Debug)]
struct VariableAnalysis {
    parameters: Vec<(String, Type)>,
    return_type: Option<Type>,
    #[allow(dead_code)]
    used_variables: HashSet<String>,
    #[allow(dead_code)]
    defined_variables: HashSet<String>,
}

/// Analyze variable usage in extracted statements
fn analyze_variable_usage(statements: &[Statement]) -> VariableAnalysis {
    let mut used = HashSet::new();
    let mut defined = HashSet::new();

    for stmt in statements {
        collect_variable_usage(stmt, &mut used, &mut defined);
    }

    // Parameters are variables used but not defined in selection
    let parameters: Vec<(String, Type)> = used
        .difference(&defined)
        .map(|name| (name.clone(), Type::String)) // Placeholder type
        .collect();

    // Return type is inferred from return statements or last expression
    let return_type = infer_return_type(statements);

    VariableAnalysis {
        parameters,
        return_type,
        used_variables: used,
        defined_variables: defined,
    }
}

/// Collect variable usage from a statement
fn collect_variable_usage(
    stmt: &Statement,
    used: &mut HashSet<String>,
    defined: &mut HashSet<String>,
) {
    match stmt {
        Statement::Let { pattern, value, .. } => {
            collect_expr_variables(value, used);
            // Only handle simple identifier patterns
            if let windjammer::parser::Pattern::Identifier(name) = pattern {
                defined.insert(name.clone());
            }
        }
        Statement::Assignment {
            target,
            value,
            location: _,
        } => {
            collect_expr_variables(target, used);
            collect_expr_variables(value, used);
        }
        Statement::Return {
            value: Some(expr),
            location: _,
        } => {
            collect_expr_variables(expr, used);
        }
        Statement::Expression { expr, location: _ } => {
            collect_expr_variables(expr, used);
        }
        Statement::If {
            condition,
            then_block,
            else_block,
            location: _,
        } => {
            collect_expr_variables(condition, used);
            for s in then_block {
                collect_variable_usage(s, used, defined);
            }
            if let Some(else_stmts) = else_block {
                for s in else_stmts {
                    collect_variable_usage(s, used, defined);
                }
            }
        }
        Statement::For {
            pattern,
            iterable,
            body,
            location: _,
        } => {
            // Extract identifier from pattern
            if let windjammer::parser::Pattern::Identifier(var) = pattern {
                defined.insert(var.clone());
            }
            collect_expr_variables(iterable, used);
            for s in body {
                collect_variable_usage(s, used, defined);
            }
        }
        Statement::While {
            condition,
            body,
            location: _,
        } => {
            collect_expr_variables(condition, used);
            for s in body {
                collect_variable_usage(s, used, defined);
            }
        }
        Statement::Loop { body, location: _ } => {
            for s in body {
                collect_variable_usage(s, used, defined);
            }
        }
        _ => {}
    }
}

/// Collect variables from an expression
fn collect_expr_variables(expr: &Expression, used: &mut HashSet<String>) {
    match expr {
        Expression::Identifier { name, location: _ } => {
            used.insert(name.clone());
        }
        Expression::Binary { left, right, .. } => {
            collect_expr_variables(left, used);
            collect_expr_variables(right, used);
        }
        Expression::Unary { operand, .. } => {
            collect_expr_variables(operand, used);
        }
        Expression::Call {
            function,
            arguments,
            location: _,
        } => {
            collect_expr_variables(function, used);
            for (_, arg) in arguments {
                collect_expr_variables(arg, used);
            }
        }
        Expression::MethodCall {
            object, arguments, ..
        } => {
            collect_expr_variables(object, used);
            for (_, arg) in arguments {
                collect_expr_variables(arg, used);
            }
        }
        Expression::FieldAccess { object, .. } => {
            collect_expr_variables(object, used);
        }
        Expression::Index {
            object,
            index,
            location: _,
        } => {
            collect_expr_variables(object, used);
            collect_expr_variables(index, used);
        }
        Expression::Block {
            statements: stmts,
            location: _,
        } => {
            let mut defined = HashSet::new();
            for stmt in stmts {
                collect_variable_usage(stmt, used, &mut defined);
            }
        }
        _ => {}
    }
}

/// Infer return type from statements
fn infer_return_type(statements: &[Statement]) -> Option<Type> {
    // Look for explicit return statements
    for stmt in statements {
        if let Statement::Return {
            value: Some(_expr),
            location: _,
        } = stmt
        {
            // TODO: Infer type from expression
            return Some(Type::String); // Placeholder type
        }
    }

    // Check if last statement is an expression (implicit return)
    if let Some(Statement::Expression {
        expr: _expr,
        location: _,
    }) = statements.last()
    {
        // TODO: Infer type from expression
        return Some(Type::String); // Placeholder type
    }

    None
}

/// Generate new function declaration
fn generate_function(
    name: &str,
    _statements: &[Statement],
    analysis: &VariableAnalysis,
    _make_public: bool,
) -> String {
    let params = analysis
        .parameters
        .iter()
        .map(|(name, _ty)| format!("{}: Type", name))
        .collect::<Vec<_>>()
        .join(", ");

    let return_type = if analysis.return_type.is_some() {
        " -> Type"
    } else {
        ""
    };

    format!(
        "fn {}({}){}  {{\n    // Extracted code here\n}}",
        name, params, return_type
    )
}

/// Generate function call to replace extracted code
fn generate_function_call(name: &str, analysis: &VariableAnalysis) -> String {
    let args = analysis
        .parameters
        .iter()
        .map(|(name, _)| name.clone())
        .collect::<Vec<_>>()
        .join(", ");

    format!("{}({})", name, args)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[tokio::test]
    async fn test_extract_function_basic() {
        let db = Arc::new(Mutex::new(WindjammerDatabase::new()));
        let args = serde_json::json!({
            "code": "fn main() {\n    let x = 1;\n    let y = 2;\n    println!(\"{}\", x + y);\n}",
            "range": {
                "start": { "line": 1, "column": 4 },
                "end": { "line": 2, "column": 17 }
            },
            "function_name": "calculate_sum"
        });

        let result = handle(db, args).await;
        assert!(result.is_ok());
    }
}