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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
#![allow(dead_code)] // Refactoring implementation - some parts planned for future versions
//! Change Function Signature refactoring
//!
//! Allows reordering, adding, and removing function parameters while
//! automatically updating all call sites.

use super::ast_utils;
use crate::database::WindjammerDatabase;
use tower_lsp::lsp_types::*;

/// Change the signature of a function
pub struct ChangeSignature<'a> {
    db: &'a WindjammerDatabase,
    uri: Url,
    position: Position,
}

/// A parameter modification operation
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ParameterChange {
    /// Add a new parameter
    Add {
        name: String,
        type_hint: Option<String>,
        default_value: String,
        index: usize,
    },
    /// Remove an existing parameter
    Remove { index: usize },
    /// Reorder parameters (old_index -> new_index)
    Reorder { from: usize, to: usize },
    /// Rename a parameter
    Rename { index: usize, new_name: String },
}

/// Result of analyzing a function for signature change
#[derive(Debug, Clone)]
pub struct SignatureAnalysis {
    /// Function name
    pub function_name: String,
    /// Current parameters
    pub parameters: Vec<Parameter>,
    /// Range of the function signature
    pub signature_range: Range,
    /// Call sites that need updating
    pub call_sites: Vec<CallSite>,
    /// Whether it's safe to change
    pub is_safe: bool,
    /// Reason if not safe
    pub unsafe_reason: Option<String>,
}

/// Information about a function parameter
#[derive(Debug, Clone)]
pub struct Parameter {
    pub name: String,
    pub type_hint: Option<String>,
}

/// A call site that needs updating
#[derive(Debug, Clone)]
pub struct CallSite {
    pub range: Range,
    pub arguments: Vec<String>,
}

impl<'a> ChangeSignature<'a> {
    /// Create a new change signature refactoring
    pub fn new(db: &'a WindjammerDatabase, uri: Url, position: Position) -> Self {
        Self { db, uri, position }
    }

    /// Execute the refactoring
    pub fn execute(
        &self,
        changes: &[ParameterChange],
        source: &str,
    ) -> Result<WorkspaceEdit, String> {
        // Step 1: Analyze the function at the cursor
        let analysis = self.analyze_function(source)?;

        // Step 2: Safety checks
        if !analysis.is_safe {
            return Err(analysis
                .unsafe_reason
                .unwrap_or_else(|| "Cannot change signature: unsafe".to_string()));
        }

        // Step 3: Apply changes to get new signature and parameters
        let (new_signature, param_mapping) = self.apply_changes(&analysis, changes)?;

        // Step 4: Create text edits
        let mut edits = vec![];

        // Update function signature
        edits.push(TextEdit {
            range: analysis.signature_range,
            new_text: new_signature,
        });

        // Update all call sites
        for call_site in &analysis.call_sites {
            let new_args = self.reorder_arguments(&call_site.arguments, &param_mapping, changes)?;
            edits.push(TextEdit {
                range: call_site.range,
                new_text: format!("{}({})", analysis.function_name, new_args.join(", ")),
            });
        }

        // Step 5: Create workspace edit
        let mut changes_map = std::collections::HashMap::new();
        changes_map.insert(self.uri.clone(), edits);

        Ok(WorkspaceEdit {
            changes: Some(changes_map),
            document_changes: None,
            change_annotations: None,
        })
    }

    /// Analyze the function at the cursor position
    fn analyze_function(&self, source: &str) -> Result<SignatureAnalysis, String> {
        // Find the function definition at the cursor
        let (function_name, signature_range, parameters) = self.find_function_at_cursor(source)?;

        // Find all call sites
        let call_sites = self.find_call_sites(source, &function_name, signature_range)?;

        // Check if it's safe to change
        let (is_safe, unsafe_reason) = self.check_safety(&function_name, &call_sites);

        Ok(SignatureAnalysis {
            function_name,
            parameters,
            signature_range,
            call_sites,
            is_safe,
            unsafe_reason,
        })
    }

    /// Find the function definition at the cursor
    fn find_function_at_cursor(
        &self,
        source: &str,
    ) -> Result<(String, Range, Vec<Parameter>), String> {
        // Simple regex-based search for function definition
        // Pattern: fn name(param1: type1, param2: type2)
        let pattern = r"fn\s+(\w+)\s*\(([^)]*)\)";
        let re = regex::Regex::new(pattern).map_err(|e| e.to_string())?;

        let cursor_byte = ast_utils::position_to_byte_offset(source, self.position);

        // Find the function that contains the cursor
        for captures in re.captures_iter(source) {
            let full_match = captures.get(0).unwrap();
            let start = full_match.start();
            let end = full_match.end();

            // Check if cursor is within this function definition
            if cursor_byte >= start && cursor_byte <= end {
                let function_name = captures.get(1).unwrap().as_str().to_string();
                let params_str = captures.get(2).unwrap().as_str();

                let start_pos = ast_utils::byte_offset_to_position(source, start);
                let end_pos = ast_utils::byte_offset_to_position(source, end);

                let signature_range = Range {
                    start: start_pos,
                    end: end_pos,
                };

                let parameters = self.parse_parameters(params_str);

                return Ok((function_name, signature_range, parameters));
            }
        }

        Err("No function found at cursor".to_string())
    }

    /// Parse parameter list
    fn parse_parameters(&self, params_str: &str) -> Vec<Parameter> {
        if params_str.trim().is_empty() {
            return vec![];
        }

        params_str
            .split(',')
            .filter_map(|param| {
                let param = param.trim();
                if param.is_empty() {
                    return None;
                }

                // Parse "name: type" or just "name"
                if let Some(colon_pos) = param.find(':') {
                    let name = param[..colon_pos].trim().to_string();
                    let type_hint = Some(param[colon_pos + 1..].trim().to_string());
                    Some(Parameter { name, type_hint })
                } else {
                    Some(Parameter {
                        name: param.to_string(),
                        type_hint: None,
                    })
                }
            })
            .collect()
    }

    /// Find all call sites for the function
    fn find_call_sites(
        &self,
        source: &str,
        function_name: &str,
        signature_range: Range,
    ) -> Result<Vec<CallSite>, String> {
        let mut call_sites = vec![];

        // Pattern: function_name(...)
        let pattern = format!(r"{}\s*\(([^)]*)\)", regex::escape(function_name));
        let re = regex::Regex::new(&pattern).map_err(|e| e.to_string())?;

        let sig_start = ast_utils::position_to_byte_offset(source, signature_range.start);
        let sig_end = ast_utils::position_to_byte_offset(source, signature_range.end);

        for captures in re.captures_iter(source) {
            let full_match = captures.get(0).unwrap();
            let args_match = captures.get(1).unwrap();

            let start = full_match.start();
            let end = full_match.end();

            // Skip the function definition itself
            if start >= sig_start && end <= sig_end {
                continue;
            }

            let start_pos = ast_utils::byte_offset_to_position(source, start);
            let end_pos = ast_utils::byte_offset_to_position(source, end);

            let arguments = self.parse_arguments(args_match.as_str());

            call_sites.push(CallSite {
                range: Range {
                    start: start_pos,
                    end: end_pos,
                },
                arguments,
            });
        }

        Ok(call_sites)
    }

    /// Parse argument list
    fn parse_arguments(&self, args_str: &str) -> Vec<String> {
        if args_str.trim().is_empty() {
            return vec![];
        }

        args_str
            .split(',')
            .map(|arg| arg.trim().to_string())
            .filter(|arg| !arg.is_empty())
            .collect()
    }

    /// Apply parameter changes to generate new signature
    fn apply_changes(
        &self,
        analysis: &SignatureAnalysis,
        changes: &[ParameterChange],
    ) -> Result<(String, Vec<usize>), String> {
        let mut params = analysis.parameters.clone();
        let mut mapping: Vec<usize> = (0..params.len()).collect();

        // Apply changes in order
        for change in changes {
            match change {
                ParameterChange::Add {
                    name,
                    type_hint,
                    index,
                    ..
                } => {
                    if *index > params.len() {
                        return Err(format!("Invalid index: {}", index));
                    }
                    params.insert(
                        *index,
                        Parameter {
                            name: name.clone(),
                            type_hint: type_hint.clone(),
                        },
                    );
                    // Update mapping
                    for m in mapping.iter_mut() {
                        if *m >= *index {
                            *m += 1;
                        }
                    }
                }
                ParameterChange::Remove { index } => {
                    if *index >= params.len() {
                        return Err(format!("Invalid index: {}", index));
                    }
                    params.remove(*index);
                    // Update mapping
                    mapping.retain(|&m| m != *index);
                    for m in mapping.iter_mut() {
                        if *m > *index {
                            *m -= 1;
                        }
                    }
                }
                ParameterChange::Reorder { from, to } => {
                    if *from >= params.len() || *to >= params.len() {
                        return Err(format!("Invalid indices: {} -> {}", from, to));
                    }
                    let param = params.remove(*from);
                    params.insert(*to, param);
                    // Update mapping
                    let old_mapping = mapping[*from];
                    mapping.remove(*from);
                    mapping.insert(*to, old_mapping);
                }
                ParameterChange::Rename { index, new_name } => {
                    if *index >= params.len() {
                        return Err(format!("Invalid index: {}", index));
                    }
                    params[*index].name = new_name.clone();
                }
            }
        }

        // Generate new signature
        let params_str = params
            .iter()
            .map(|p| {
                if let Some(ref type_hint) = p.type_hint {
                    format!("{}: {}", p.name, type_hint)
                } else {
                    p.name.clone()
                }
            })
            .collect::<Vec<_>>()
            .join(", ");

        let new_signature = format!("fn {}({})", analysis.function_name, params_str);

        Ok((new_signature, mapping))
    }

    /// Reorder arguments according to parameter mapping
    fn reorder_arguments(
        &self,
        original_args: &[String],
        mapping: &[usize],
        changes: &[ParameterChange],
    ) -> Result<Vec<String>, String> {
        // Calculate new parameter count after all changes
        let mut param_count = original_args.len();
        for change in changes {
            match change {
                ParameterChange::Add { .. } => param_count += 1,
                ParameterChange::Remove { .. } => param_count -= 1,
                _ => {}
            }
        }

        let mut new_args = vec![String::new(); param_count];

        // First, map existing arguments
        for (new_idx, &old_idx) in mapping.iter().enumerate() {
            if old_idx < original_args.len() && new_idx < new_args.len() {
                new_args[new_idx] = original_args[old_idx].clone();
            }
        }

        // Then handle added parameters with default values
        for change in changes {
            if let ParameterChange::Add {
                index,
                default_value,
                ..
            } = change
            {
                if *index < new_args.len() {
                    new_args[*index] = default_value.clone();
                }
            }
        }

        Ok(new_args)
    }

    /// Check if it's safe to change the signature
    fn check_safety(
        &self,
        _function_name: &str,
        _call_sites: &[CallSite],
    ) -> (bool, Option<String>) {
        // For now, allow all changes
        // TODO: Add more sophisticated safety checks
        (true, None)
    }
}

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

    #[test]
    fn test_parse_parameters() {
        let db = WindjammerDatabase::new();
        let uri = Url::parse("file:///test.wj").unwrap();
        let position = Position {
            line: 0,
            character: 0,
        };
        let change_sig = ChangeSignature::new(&db, uri, position);

        let params = change_sig.parse_parameters("x: int, y: string");
        assert_eq!(params.len(), 2);
        assert_eq!(params[0].name, "x");
        assert_eq!(params[0].type_hint, Some("int".to_string()));
        assert_eq!(params[1].name, "y");
        assert_eq!(params[1].type_hint, Some("string".to_string()));
    }

    #[test]
    fn test_parse_arguments() {
        let db = WindjammerDatabase::new();
        let uri = Url::parse("file:///test.wj").unwrap();
        let position = Position {
            line: 0,
            character: 0,
        };
        let change_sig = ChangeSignature::new(&db, uri, position);

        let args = change_sig.parse_arguments("42, \"hello\"");
        assert_eq!(args.len(), 2);
        assert_eq!(args[0], "42");
        assert_eq!(args[1], "\"hello\"");
    }

    #[test]
    fn test_apply_add_parameter() {
        let db = WindjammerDatabase::new();
        let uri = Url::parse("file:///test.wj").unwrap();
        let position = Position {
            line: 0,
            character: 0,
        };
        let change_sig = ChangeSignature::new(&db, uri, position);

        let analysis = SignatureAnalysis {
            function_name: "test".to_string(),
            parameters: vec![Parameter {
                name: "x".to_string(),
                type_hint: Some("int".to_string()),
            }],
            signature_range: Range {
                start: Position {
                    line: 0,
                    character: 0,
                },
                end: Position {
                    line: 0,
                    character: 10,
                },
            },
            call_sites: vec![],
            is_safe: true,
            unsafe_reason: None,
        };

        let changes = vec![ParameterChange::Add {
            name: "y".to_string(),
            type_hint: Some("string".to_string()),
            default_value: "\"default\"".to_string(),
            index: 1,
        }];

        let (new_sig, _) = change_sig.apply_changes(&analysis, &changes).unwrap();
        assert!(new_sig.contains("x: int"));
        assert!(new_sig.contains("y: string"));
    }
}