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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
#![allow(dead_code)] // Refactoring implementation - some parts planned for future versions
//! Move Item refactoring
//!
//! Move functions, structs, and other items between files while
//! automatically updating imports.

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

/// Move an item (function, struct, etc.) to another file
pub struct MoveItem<'a> {
    db: &'a WindjammerDatabase,
    source_uri: Url,
    target_uri: Url,
    position: Position,
}

/// Type of item being moved
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ItemType {
    Function,
    Struct,
    Enum,
    Trait,
    Const,
    Static,
}

/// Result of analyzing an item for moving
#[derive(Debug, Clone)]
pub struct MoveAnalysis {
    /// Type of item
    pub item_type: ItemType,
    /// Name of the item
    pub item_name: String,
    /// Full text of the item
    pub item_text: String,
    /// Range of the item in source file
    pub item_range: Range,
    /// Items that depend on this item (in source file)
    pub dependencies: Vec<String>,
    /// Whether it's safe to move
    pub is_safe: bool,
    /// Reason if not safe
    pub unsafe_reason: Option<String>,
}

impl<'a> MoveItem<'a> {
    /// Create a new move item refactoring
    pub fn new(
        db: &'a WindjammerDatabase,
        source_uri: Url,
        target_uri: Url,
        position: Position,
    ) -> Self {
        Self {
            db,
            source_uri,
            target_uri,
            position,
        }
    }

    /// Execute the refactoring
    pub fn execute(
        &self,
        source_content: &str,
        target_content: &str,
    ) -> Result<WorkspaceEdit, String> {
        // Step 1: Analyze the item at the cursor
        let analysis = self.analyze_item(source_content)?;

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

        // Step 3: Check for circular dependencies
        let target_module = self.extract_module_name(&self.target_uri);
        if self.would_create_cycle(&analysis, source_content, &target_module) {
            return Err(format!(
                "Cannot move {}: would create circular dependency",
                analysis.item_name
            ));
        }

        // Step 4: Create text edits
        let mut changes = std::collections::HashMap::new();

        // Prepare source file edits
        let mut source_edits = vec![];

        // Remove item from source file
        source_edits.push(TextEdit {
            range: analysis.item_range,
            new_text: String::new(), // Delete the item
        });

        // Add import in source file if there are usages
        let usages = self.find_item_usages(source_content, &analysis.item_name);
        if !usages.is_empty() {
            let import_edit =
                self.create_import_edit(source_content, &analysis.item_name, &target_module);
            if let Some(edit) = import_edit {
                source_edits.push(edit);
            }
        }

        // Prepare target file edits
        let mut target_edits = vec![];

        // Add to target file (append at end)
        let target_position = self.find_insert_position(target_content);
        target_edits.push(TextEdit {
            range: Range {
                start: target_position,
                end: target_position,
            },
            new_text: format!("\n{}\n", analysis.item_text),
        });

        changes.insert(self.source_uri.clone(), source_edits);
        changes.insert(self.target_uri.clone(), target_edits);

        // Step 5: Create workspace edit
        Ok(WorkspaceEdit {
            changes: Some(changes),
            document_changes: None,
            change_annotations: None,
        })
    }

    /// Analyze the item at the cursor position
    fn analyze_item(&self, source: &str) -> Result<MoveAnalysis, String> {
        // Find the item definition at the cursor
        let (item_type, item_name, item_range, item_text) = self.find_item_at_cursor(source)?;

        // Track dependencies
        let dependencies = self.track_dependencies(source, &item_text);

        // Check if it's safe to move
        let (is_safe, unsafe_reason) = self.check_safety(&item_name, &dependencies);

        Ok(MoveAnalysis {
            item_type,
            item_name,
            item_text,
            item_range,
            dependencies,
            is_safe,
            unsafe_reason,
        })
    }

    /// Find the item definition at the cursor
    fn find_item_at_cursor(
        &self,
        source: &str,
    ) -> Result<(ItemType, String, Range, String), String> {
        let cursor_byte = ast_utils::position_to_byte_offset(source, self.position);

        // Try to find different types of items
        // Pattern for functions: fn name(...) { ... }
        if let Ok(result) = self.find_function(source, cursor_byte) {
            return Ok(result);
        }

        // Pattern for structs: struct Name { ... }
        if let Ok(result) = self.find_struct(source, cursor_byte) {
            return Ok(result);
        }

        // Pattern for enums: enum Name { ... }
        if let Ok(result) = self.find_enum(source, cursor_byte) {
            return Ok(result);
        }

        Err("No movable item found at cursor".to_string())
    }

    /// Find a function at the cursor
    fn find_function(
        &self,
        source: &str,
        cursor_byte: usize,
    ) -> Result<(ItemType, String, Range, String), String> {
        // Pattern: fn name(...) { ... }
        // Simplified: just find fn keyword and capture until the end of the block
        let lines: Vec<&str> = source.lines().collect();
        let cursor_line = ast_utils::byte_offset_to_position(source, cursor_byte).line as usize;

        if cursor_line >= lines.len() {
            return Err("Cursor out of bounds".to_string());
        }

        // Find the function definition line
        let mut start_line = cursor_line;
        while start_line > 0 && !lines[start_line].trim_start().starts_with("fn ") {
            start_line -= 1;
        }

        if !lines[start_line].trim_start().starts_with("fn ") {
            return Err("No function found".to_string());
        }

        // Extract function name
        let fn_line = lines[start_line];
        let name_start = fn_line.find("fn ").ok_or("No fn keyword")? + 3;
        let name_end = fn_line[name_start..]
            .find(|c: char| !c.is_alphanumeric() && c != '_')
            .map(|i| name_start + i)
            .unwrap_or(fn_line.len());
        let function_name = fn_line[name_start..name_end].to_string();

        // Find the end of the function (closing brace)
        let mut end_line = start_line;
        let mut brace_count = 0;
        let mut found_opening = false;

        for (i, line) in lines.iter().enumerate().skip(start_line) {
            for ch in line.chars() {
                if ch == '{' {
                    brace_count += 1;
                    found_opening = true;
                } else if ch == '}' {
                    brace_count -= 1;
                }
            }

            if found_opening && brace_count == 0 {
                end_line = i;
                break;
            }
        }

        // Extract the full function text
        let item_text = lines[start_line..=end_line].join("\n");

        let start_pos = Position {
            line: start_line as u32,
            character: 0,
        };
        let end_pos = Position {
            line: (end_line + 1) as u32,
            character: 0,
        };

        Ok((
            ItemType::Function,
            function_name,
            Range {
                start: start_pos,
                end: end_pos,
            },
            item_text,
        ))
    }

    /// Find a struct at the cursor
    fn find_struct(
        &self,
        source: &str,
        cursor_byte: usize,
    ) -> Result<(ItemType, String, Range, String), String> {
        let lines: Vec<&str> = source.lines().collect();
        let cursor_line = ast_utils::byte_offset_to_position(source, cursor_byte).line as usize;

        if cursor_line >= lines.len() {
            return Err("Cursor out of bounds".to_string());
        }

        // Find the struct definition line
        let mut start_line = cursor_line;
        while start_line > 0 && !lines[start_line].trim_start().starts_with("struct ") {
            start_line -= 1;
        }

        if !lines[start_line].trim_start().starts_with("struct ") {
            return Err("No struct found".to_string());
        }

        // Extract struct name
        let struct_line = lines[start_line];
        let name_start = struct_line.find("struct ").ok_or("No struct keyword")? + 7;
        let name_end = struct_line[name_start..]
            .find(|c: char| !c.is_alphanumeric() && c != '_')
            .map(|i| name_start + i)
            .unwrap_or(struct_line.len());
        let struct_name = struct_line[name_start..name_end].to_string();

        // Find the end (closing brace or semicolon)
        let mut end_line = start_line;
        if struct_line.contains('{') {
            // Struct with fields
            let mut brace_count = 0;
            let mut found_opening = false;

            for (i, line) in lines.iter().enumerate().skip(start_line) {
                for ch in line.chars() {
                    if ch == '{' {
                        brace_count += 1;
                        found_opening = true;
                    } else if ch == '}' {
                        brace_count -= 1;
                    }
                }

                if found_opening && brace_count == 0 {
                    end_line = i;
                    break;
                }
            }
        } else {
            // Tuple struct or unit struct (ends with semicolon)
            end_line = start_line;
        }

        let item_text = lines[start_line..=end_line].join("\n");

        let start_pos = Position {
            line: start_line as u32,
            character: 0,
        };
        let end_pos = Position {
            line: (end_line + 1) as u32,
            character: 0,
        };

        Ok((
            ItemType::Struct,
            struct_name,
            Range {
                start: start_pos,
                end: end_pos,
            },
            item_text,
        ))
    }

    /// Find an enum at the cursor
    fn find_enum(
        &self,
        _source: &str,
        _cursor_byte: usize,
    ) -> Result<(ItemType, String, Range, String), String> {
        // Simplified version - similar to struct
        Err("Enum finding not implemented yet".to_string())
    }

    /// Find where to insert the item in the target file
    fn find_insert_position(&self, target_content: &str) -> Position {
        // Insert at the end of the file
        let lines = target_content.lines().count();
        Position {
            line: lines as u32,
            character: 0,
        }
    }

    /// Check if it's safe to move the item
    fn check_safety(&self, item_name: &str, dependencies: &[String]) -> (bool, Option<String>) {
        // Check if item has too many dependencies
        if dependencies.len() > 10 {
            return (
                false,
                Some(format!(
                    "{} has {} dependencies - consider refactoring first",
                    item_name,
                    dependencies.len()
                )),
            );
        }

        (true, None)
    }

    /// Extract module name from URI
    fn extract_module_name(&self, uri: &Url) -> String {
        uri.path()
            .split('/')
            .next_back()
            .unwrap_or("unknown")
            .trim_end_matches(".wj")
            .to_string()
    }

    /// Find usages of an item in source code
    fn find_item_usages(&self, source: &str, item_name: &str) -> Vec<Range> {
        let mut usages = vec![];
        let pattern = format!(r"\b{}\b", regex::escape(item_name));
        let re = regex::Regex::new(&pattern).unwrap();

        for (line_num, line) in source.lines().enumerate() {
            for cap in re.find_iter(line) {
                let start_pos = Position {
                    line: line_num as u32,
                    character: cap.start() as u32,
                };
                let end_pos = Position {
                    line: line_num as u32,
                    character: cap.end() as u32,
                };
                usages.push(Range {
                    start: start_pos,
                    end: end_pos,
                });
            }
        }

        usages
    }

    /// Create an import edit for the moved item
    fn create_import_edit(
        &self,
        source: &str,
        item_name: &str,
        target_module: &str,
    ) -> Option<TextEdit> {
        // Find if there's already a use statement section
        let lines: Vec<&str> = source.lines().collect();
        let mut insert_line = 0;

        // Find the last use statement
        for (i, line) in lines.iter().enumerate() {
            if line.trim().starts_with("use ") {
                insert_line = i + 1;
            }
        }

        // If no use statements, insert after any initial comments
        if insert_line == 0 {
            for (i, line) in lines.iter().enumerate() {
                if !line.trim().is_empty() && !line.trim().starts_with("//") {
                    insert_line = i;
                    break;
                }
            }
        }

        let import_statement = format!("use {}.{}\n", target_module, item_name);

        Some(TextEdit {
            range: Range {
                start: Position {
                    line: insert_line as u32,
                    character: 0,
                },
                end: Position {
                    line: insert_line as u32,
                    character: 0,
                },
            },
            new_text: import_statement,
        })
    }

    /// Check if moving would create a circular dependency
    fn would_create_cycle(
        &self,
        analysis: &MoveAnalysis,
        source_content: &str,
        _target_module: &str,
    ) -> bool {
        // Check if target module imports from source module
        let source_module = self.extract_module_name(&self.source_uri);

        // Look for use statements in target that reference source module
        let pattern = format!(r"use\s+{}\.(\w+)", regex::escape(&source_module));
        if let Ok(re) = regex::Regex::new(&pattern) {
            if re.is_match(source_content) {
                // Target imports from source, check if moved item is in those imports
                for cap in re.captures_iter(source_content) {
                    if let Some(imported_item) = cap.get(1) {
                        // Check if the moved item depends on this imported item
                        if analysis
                            .dependencies
                            .contains(&imported_item.as_str().to_string())
                        {
                            return true;
                        }
                    }
                }
            }
        }

        false
    }

    /// Track dependencies of an item
    fn track_dependencies(&self, source: &str, item_text: &str) -> Vec<String> {
        let mut dependencies = vec![];

        // Find all identifiers in the item that might be dependencies
        // Pattern: word boundaries, excluding keywords
        let keywords = vec![
            "fn", "struct", "enum", "let", "mut", "if", "else", "for", "while", "return",
        ];

        let re = regex::Regex::new(r"\b[a-zA-Z_][a-zA-Z0-9_]*\b").unwrap();

        for cap in re.find_iter(item_text) {
            let word = cap.as_str();

            // Skip keywords
            if keywords.contains(&word) {
                continue;
            }

            // Check if this identifier is defined elsewhere in source
            if self.is_external_dependency(source, item_text, word)
                && !dependencies.contains(&word.to_string())
            {
                dependencies.push(word.to_string());
            }
        }

        dependencies
    }

    /// Check if an identifier is an external dependency
    fn is_external_dependency(&self, source: &str, item_text: &str, identifier: &str) -> bool {
        // Check if identifier is defined in source but outside the item
        let pattern = format!(
            r"\b(fn|struct|enum|const|static)\s+{}\b",
            regex::escape(identifier)
        );

        if let Ok(re) = regex::Regex::new(&pattern) {
            for cap in re.find_iter(source) {
                let match_text = cap.as_str();
                // If found in source but not in item text, it's an external dependency
                if !item_text.contains(match_text) {
                    return true;
                }
            }
        }

        false
    }
}

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

    #[test]
    fn test_find_function() {
        let db = WindjammerDatabase::new();
        let source_uri = Url::parse("file:///source.wj").unwrap();
        let target_uri = Url::parse("file:///target.wj").unwrap();
        let position = Position {
            line: 0,
            character: 3,
        };

        let mover = MoveItem::new(&db, source_uri, target_uri, position);

        let source = r#"fn calculate(x: int) -> int {
    x * 2
}
"#;

        let cursor_byte = ast_utils::position_to_byte_offset(source, position);
        let result = mover.find_function(source, cursor_byte);

        assert!(result.is_ok(), "Should find function");
        let (item_type, name, _, _) = result.unwrap();
        assert_eq!(item_type, ItemType::Function);
        assert_eq!(name, "calculate");
    }

    #[test]
    fn test_find_struct() {
        let db = WindjammerDatabase::new();
        let source_uri = Url::parse("file:///source.wj").unwrap();
        let target_uri = Url::parse("file:///target.wj").unwrap();
        let position = Position {
            line: 0,
            character: 7,
        };

        let mover = MoveItem::new(&db, source_uri, target_uri, position);

        let source = r#"struct User {
    name: string,
    age: int,
}
"#;

        let cursor_byte = ast_utils::position_to_byte_offset(source, position);
        let result = mover.find_struct(source, cursor_byte);

        assert!(result.is_ok(), "Should find struct");
        let (item_type, name, _, _) = result.unwrap();
        assert_eq!(item_type, ItemType::Struct);
        assert_eq!(name, "User");
    }
}