perl-lsp-code-actions 0.12.2

LSP code actions provider for Perl
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
//! Enhanced code actions with additional refactorings
//!
//! This module extends the base code actions with more sophisticated refactorings,
//! including extract variable, extract subroutine, loop conversion, and import management.
//!
//! # Architecture
//!
//! Enhanced actions are organized into focused submodules:
//!
//! - **extract_variable**: Extract selected expression into a named variable
//! - **extract_subroutine**: Extract code block into a new subroutine
//! - **loop_conversion**: Convert between loop styles (for/foreach/while)
//! - **import_management**: Organize and add/remove use statements
//! - **postfix**: Postfix completion-style actions (e.g., `.if`, `.unless`)
//! - **error_checking**: Add error handling around expressions
//! - **helpers**: Shared utilities for text manipulation and position mapping
//!
//! # Refactoring Categories
//!
//! Actions are categorized following LSP CodeActionKind:
//!
//! - **refactor.extract**: Extract variable, extract subroutine
//! - **refactor.rewrite**: Loop conversion, error wrapping
//! - **source.organizeImports**: Import management
//!
//! # Performance Characteristics
//!
//! - **Action generation**: <50ms for typical refactoring suggestions
//! - **Edit computation**: <100ms for complex multi-location edits
//! - **Incremental analysis**: Leverages parsed AST for efficient analysis

use crate::types::CodeAction;
use perl_parser_core::ast::{Node, NodeKind};
use std::collections::HashSet;

mod error_checking;
mod extract_subroutine;
mod extract_variable;
mod helpers;
mod import_management;
mod loop_conversion;
mod postfix;

use helpers::Helpers;

/// Enhanced code actions provider with additional refactorings
pub struct EnhancedCodeActionsProvider {
    source: String,
    lines: Vec<String>,
}

impl EnhancedCodeActionsProvider {
    /// Create a new enhanced code actions provider
    pub fn new(source: String) -> Self {
        let lines = source.lines().map(|s| s.to_string()).collect();
        Self { source, lines }
    }

    /// Get additional refactoring actions
    pub fn get_enhanced_refactoring_actions(
        &self,
        ast: &Node,
        range: (usize, usize),
    ) -> Vec<CodeAction> {
        let mut actions = Vec::new();
        // Track (stmt_start, var_name) pairs already emitted to prevent duplicate
        // extract-variable actions when both a parent and child node overlap the range.
        let mut extract_var_seen: HashSet<(usize, String)> = HashSet::new();

        // Find all nodes that overlap the range and collect actions
        self.collect_actions_for_range(ast, range, false, &mut actions, &mut extract_var_seen);

        // Global actions (not node-specific)
        actions.extend(self.get_global_refactorings(ast));

        actions
    }

    /// Recursively collect actions for all nodes in range.
    ///
    /// `is_control_body` is `true` when the current node is the body block of a
    /// control-flow construct (`If`, `While`, `For`, `Foreach`, `Subroutine`).
    /// In that case the node is not offered as "Extract to subroutine" — only
    /// standalone bare blocks are extractable.
    fn collect_actions_for_range(
        &self,
        node: &Node,
        range: (usize, usize),
        is_control_body: bool,
        actions: &mut Vec<CodeAction>,
        extract_var_seen: &mut HashSet<(usize, String)>,
    ) {
        // Check if this node overlaps the range
        if node.location.start <= range.1 && node.location.end >= range.0 {
            let helpers = Helpers::new(&self.source, &self.lines);

            // Extract variable — only emit when the node's end reaches or exceeds the
            // selection's end. This prevents duplicate actions for nested expressions:
            // when both a Binary(8..25) and its inner FunctionCall(8..20) overlap a
            // selection (8..25), the FunctionCall's end (20) is before the selection's
            // end (25) and is skipped; only the outermost matching node emits an action.
            // Partial-left overlap (cursor inside expression) is still supported.
            let node_reaches_selection_end = node.location.end >= range.1;
            if node_reaches_selection_end && self.is_extractable_expression(node) {
                let action =
                    extract_variable::create_extract_variable_action(node, &self.source, &helpers);
                if let Some(decl) = action.edit.changes.first() {
                    let key = (decl.location.start, decl.new_text.clone());
                    if extract_var_seen.insert(key) {
                        actions.push(action);
                    }
                } else {
                    actions.push(action);
                }
            }

            // Convert old-style loops
            if let Some(action) = loop_conversion::convert_loop_style(node, &self.source) {
                actions.push(action);
            }

            // Add error checking
            if let Some(action) = error_checking::add_error_checking(node, &self.source) {
                actions.push(action);
            }

            // Convert to postfix
            if let Some(action) = postfix::convert_to_postfix(node, &self.source) {
                actions.push(action);
            }

            // Extract subroutine — only for standalone blocks, not control-flow bodies
            if !is_control_body && self.is_extractable_block(node) {
                actions.push(extract_subroutine::create_extract_subroutine_action(
                    node,
                    &self.source,
                    &helpers,
                ));
            }
        }

        // Recursively check children, flagging control-flow body blocks
        match &node.kind {
            NodeKind::Program { statements } => {
                for stmt in statements {
                    self.collect_actions_for_range(stmt, range, false, actions, extract_var_seen);
                }
            }
            NodeKind::Block { statements } => {
                for stmt in statements {
                    self.collect_actions_for_range(stmt, range, false, actions, extract_var_seen);
                }
            }
            NodeKind::ExpressionStatement { expression } => {
                self.collect_actions_for_range(expression, range, false, actions, extract_var_seen);
            }
            NodeKind::If { condition, then_branch, elsif_branches, else_branch } => {
                self.collect_actions_for_range(condition, range, false, actions, extract_var_seen);
                self.collect_actions_for_range(
                    then_branch,
                    range,
                    true, // then-body is a control-flow block
                    actions,
                    extract_var_seen,
                );
                for (cond, branch) in elsif_branches {
                    self.collect_actions_for_range(cond, range, false, actions, extract_var_seen);
                    self.collect_actions_for_range(branch, range, true, actions, extract_var_seen);
                }
                if let Some(branch) = else_branch {
                    self.collect_actions_for_range(branch, range, true, actions, extract_var_seen);
                }
            }
            NodeKind::FunctionCall { args, .. } => {
                for arg in args {
                    self.collect_actions_for_range(arg, range, false, actions, extract_var_seen);
                }
            }
            NodeKind::Binary { left, right, .. } => {
                self.collect_actions_for_range(left, range, false, actions, extract_var_seen);
                self.collect_actions_for_range(right, range, false, actions, extract_var_seen);
            }
            NodeKind::Assignment { lhs, rhs, .. } => {
                self.collect_actions_for_range(lhs, range, false, actions, extract_var_seen);
                self.collect_actions_for_range(rhs, range, false, actions, extract_var_seen);
            }
            NodeKind::VariableDeclaration { variable, initializer, .. } => {
                self.collect_actions_for_range(variable, range, false, actions, extract_var_seen);
                if let Some(init) = initializer {
                    self.collect_actions_for_range(init, range, false, actions, extract_var_seen);
                }
            }
            NodeKind::For { init, condition, update, body, .. } => {
                if let Some(init) = init {
                    self.collect_actions_for_range(init, range, false, actions, extract_var_seen);
                }
                if let Some(condition) = condition {
                    self.collect_actions_for_range(
                        condition,
                        range,
                        false,
                        actions,
                        extract_var_seen,
                    );
                }
                if let Some(update) = update {
                    self.collect_actions_for_range(update, range, false, actions, extract_var_seen);
                }
                self.collect_actions_for_range(
                    body,
                    range,
                    true, // loop body is a control-flow block
                    actions,
                    extract_var_seen,
                );
            }
            NodeKind::Foreach { variable, list, body, continue_block } => {
                self.collect_actions_for_range(variable, range, false, actions, extract_var_seen);
                self.collect_actions_for_range(list, range, false, actions, extract_var_seen);
                self.collect_actions_for_range(body, range, true, actions, extract_var_seen);
                if let Some(cb) = continue_block {
                    self.collect_actions_for_range(cb, range, false, actions, extract_var_seen);
                }
            }
            NodeKind::While { condition, body, .. } => {
                self.collect_actions_for_range(condition, range, false, actions, extract_var_seen);
                self.collect_actions_for_range(
                    body,
                    range,
                    true, // loop body is a control-flow block
                    actions,
                    extract_var_seen,
                );
            }
            NodeKind::MethodCall { object, args, .. } => {
                self.collect_actions_for_range(object, range, false, actions, extract_var_seen);
                for arg in args {
                    self.collect_actions_for_range(arg, range, false, actions, extract_var_seen);
                }
            }
            NodeKind::Subroutine { body, prototype, signature, .. } => {
                self.collect_actions_for_range(
                    body,
                    range,
                    true, // subroutine body block is not a standalone block
                    actions,
                    extract_var_seen,
                );
                if let Some(proto) = prototype {
                    self.collect_actions_for_range(proto, range, false, actions, extract_var_seen);
                }
                if let Some(sig) = signature {
                    self.collect_actions_for_range(sig, range, false, actions, extract_var_seen);
                }
            }
            _ => {}
        }
    }

    /// Check if expression is extractable
    fn is_extractable_expression(&self, node: &Node) -> bool {
        matches!(
            &node.kind,
            NodeKind::FunctionCall { .. }
                | NodeKind::Binary { .. }
                | NodeKind::Unary { .. }
                | NodeKind::MethodCall { .. }
                | NodeKind::Ternary { .. }
        )
    }

    /// Check if block is extractable
    fn is_extractable_block(&self, node: &Node) -> bool {
        matches!(&node.kind, NodeKind::Block { .. })
    }

    /// Get global refactoring actions
    fn get_global_refactorings(&self, ast: &Node) -> Vec<CodeAction> {
        let mut actions = Vec::new();
        let helpers = Helpers::new(&self.source, &self.lines);

        // Add missing imports
        if let Some(action) = import_management::add_missing_imports(ast, &self.source, &helpers) {
            actions.push(action);
        }

        // Organize imports
        if let Some(action) = import_management::organize_imports(ast, &self.source, &helpers) {
            actions.push(action);
        }

        // Add pragmas
        actions.extend(self.add_recommended_pragmas(&helpers));

        actions
    }

    /// Add recommended pragmas
    fn add_recommended_pragmas(&self, helpers: &Helpers<'_>) -> Vec<CodeAction> {
        use crate::types::{CodeAction, CodeActionEdit, CodeActionKind};
        use perl_lsp_rename::TextEdit;
        use perl_parser_core::ast::SourceLocation;

        let mut actions = Vec::new();

        // Check for missing strict and warnings
        let has_strict = self.source.contains("use strict");
        let has_warnings = self.source.contains("use warnings");

        if !has_strict || !has_warnings {
            let mut pragmas = Vec::new();
            if !has_strict {
                pragmas.push("use strict;");
            }
            if !has_warnings {
                pragmas.push("use warnings;");
            }

            let insert_pos = helpers.find_pragma_insert_position();

            actions.push(CodeAction {
                title: format!("Add missing pragmas ({})", pragmas.join(", ")),
                kind: CodeActionKind::QuickFix,
                diagnostics: Vec::new(),
                edit: CodeActionEdit {
                    changes: vec![TextEdit {
                        location: SourceLocation { start: insert_pos, end: insert_pos },
                        new_text: format!("{}\n", pragmas.join("\n")),
                    }],
                },
                is_preferred: true,
            });
        }

        // Add utf8 support if missing
        if !self.source.contains("use utf8") && helpers.has_non_ascii_content() {
            let insert_pos = helpers.find_pragma_insert_position();

            actions.push(CodeAction {
                title: "Add UTF-8 support".to_string(),
                kind: CodeActionKind::QuickFix,
                diagnostics: Vec::new(),
                edit: CodeActionEdit {
                    changes: vec![TextEdit {
                        location: SourceLocation { start: insert_pos, end: insert_pos },
                        new_text: "use utf8;\nuse open qw(:std :utf8);\n".to_string(),
                    }],
                },
                is_preferred: false,
            });
        }

        actions
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use perl_parser_core::Parser;
    use perl_tdd_support::must;

    #[test]
    fn test_extract_variable() {
        let source = "my $x = length($string) + 10;";
        let mut parser = Parser::new(source);
        let ast = must(parser.parse());

        let provider = EnhancedCodeActionsProvider::new(source.to_string());
        let actions = provider.get_enhanced_refactoring_actions(&ast, (8, 23)); // Select "length($string)"

        // Debug: print all actions
        for action in &actions {
            eprintln!("Action: {}", action.title);
        }

        assert!(!actions.is_empty(), "Expected at least one action");
        assert!(
            actions.iter().any(|a| a.title.contains("Extract")),
            "Expected an Extract action, got: {:?}",
            actions.iter().map(|a| &a.title).collect::<Vec<_>>()
        );
    }

    #[test]
    fn test_add_error_checking() {
        let source = "open my $fh, '<', 'file.txt';";
        let mut parser = Parser::new(source);
        let ast = must(parser.parse());

        let provider = EnhancedCodeActionsProvider::new(source.to_string());
        let actions = provider.get_enhanced_refactoring_actions(&ast, (0, 30));

        assert!(actions.iter().any(|a| a.title.contains("error checking")));
    }

    #[test]
    fn test_convert_to_postfix() {
        let source = "if ($debug) { print \"Debug\\n\"; }";
        let mut parser = Parser::new(source);
        let ast = must(parser.parse());

        let provider = EnhancedCodeActionsProvider::new(source.to_string());
        let actions = provider.get_enhanced_refactoring_actions(&ast, (0, source.len()));

        assert!(actions.iter().any(|a| a.title.contains("postfix")));
    }
}

#[cfg(test)]
mod extract_variable_tests {
    use super::*;
    use perl_parser_core::Parser;
    use perl_tdd_support::must;

    #[test]
    fn test_extract_hash_access_to_variable() {
        // Use assignment so hash access is in the RHS, not a print argument
        let source = "my $x = $hash{$key};";
        let mut parser = Parser::new(source);
        let ast = must(parser.parse());

        let provider = EnhancedCodeActionsProvider::new(source.to_string());
        // Select the range covering $hash{$key} (bytes 8..19)
        let actions = provider.get_enhanced_refactoring_actions(&ast, (8, 19));

        let extract_actions: Vec<_> =
            actions.iter().filter(|a| a.title.contains("Extract")).collect();

        assert!(
            !extract_actions.is_empty(),
            "Expected an Extract action for hash access, got: {:?}",
            actions.iter().map(|a| &a.title).collect::<Vec<_>>()
        );

        // Verify the action produces a declaration with `my $val`
        let action = &extract_actions[0];
        let decl_edit = &action.edit.changes[0];
        assert!(
            decl_edit.new_text.contains("my $val"),
            "Expected variable name '$val' for hash access, got: {}",
            decl_edit.new_text
        );
    }

    #[test]
    fn test_extract_method_call_to_variable() {
        let source = "print $obj->method();";
        let mut parser = Parser::new(source);
        let ast = must(parser.parse());

        let provider = EnhancedCodeActionsProvider::new(source.to_string());
        // Select the range covering $obj->method()
        let actions = provider.get_enhanced_refactoring_actions(&ast, (6, 20));

        let extract_actions: Vec<_> =
            actions.iter().filter(|a| a.title.contains("Extract")).collect();

        assert!(
            !extract_actions.is_empty(),
            "Expected an Extract action for method call, got: {:?}",
            actions.iter().map(|a| &a.title).collect::<Vec<_>>()
        );

        // Verify the action produces a declaration with `my $result`
        let action = &extract_actions[0];
        let decl_edit = &action.edit.changes[0];
        assert!(
            decl_edit.new_text.contains("my $result"),
            "Expected variable name '$result' for method call, got: {}",
            decl_edit.new_text
        );

        // Verify the replacement edit uses $result
        let replace_edit = &action.edit.changes[1];
        assert!(
            replace_edit.new_text.contains("$result"),
            "Expected replacement with '$result', got: {}",
            replace_edit.new_text
        );
    }

    #[test]
    fn test_extract_method_call_new_suggests_instance() {
        let source = "my $x = Foo->new();";
        let mut parser = Parser::new(source);
        let ast = must(parser.parse());

        let provider = EnhancedCodeActionsProvider::new(source.to_string());
        let actions = provider.get_enhanced_refactoring_actions(&ast, (8, 18));

        let extract_actions: Vec<_> =
            actions.iter().filter(|a| a.title.contains("Extract")).collect();

        assert!(
            !extract_actions.is_empty(),
            "Expected an Extract action for constructor call, got: {:?}",
            actions.iter().map(|a| &a.title).collect::<Vec<_>>()
        );

        // Constructor call ->new() should suggest $instance
        let action = &extract_actions[0];
        let decl_edit = &action.edit.changes[0];
        assert!(
            decl_edit.new_text.contains("my $instance"),
            "Expected variable name '$instance' for ->new(), got: {}",
            decl_edit.new_text
        );
    }

    #[test]
    fn test_extract_variable_edit_structure() {
        let source = "my $x = $obj->get();";
        let mut parser = Parser::new(source);
        let ast = must(parser.parse());

        let provider = EnhancedCodeActionsProvider::new(source.to_string());
        let actions = provider.get_enhanced_refactoring_actions(&ast, (8, 19));

        let extract_actions: Vec<_> =
            actions.iter().filter(|a| a.title.contains("Extract")).collect();

        assert!(!extract_actions.is_empty(), "Expected at least one extract action");

        let action = &extract_actions[0];
        assert_eq!(action.edit.changes.len(), 2, "Expected exactly 2 edits (insert + replace)");

        // First edit: insertion of variable declaration
        let insert_edit = &action.edit.changes[0];
        assert!(
            insert_edit.new_text.starts_with("my $"),
            "First edit should be a variable declaration"
        );
        assert!(insert_edit.new_text.ends_with(";\n"), "Declaration should end with semicolon");

        // Second edit: replacement of expression with variable reference
        let replace_edit = &action.edit.changes[1];
        assert!(
            replace_edit.new_text.starts_with('$'),
            "Second edit should be a variable reference"
        );
    }
}