perl-parser 0.13.3

Native Perl parser (v3) — recursive descent with Tree-sitter-compatible AST, semantic analysis, and LSP provider engine
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
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
//! Code actions provider for LSP textDocument/codeAction
//!
//! This module provides intelligent code actions for Perl source code,
//! including refactoring, quick fixes, and productivity enhancements.
//!
//! # LSP Workflow Integration
//!
//! Core component in the Parse → Index → Navigate → Complete → Analyze pipeline:
//! 1. **Parse**: AST generation with pattern detection
//! 2. **Index**: Workspace symbol table for cross-file analysis
//! 3. **Navigate**: Go-to-definition and reference finding
//! 4. **Complete**: Context-aware completion
//! 5. **Analyze**: Code actions and refactoring with this module
//!
//! # Protocol and Client Capabilities
//!
//! - **Client capabilities**: Adapts returned actions to advertised client
//!   code-action and resolve capabilities.
//! - **Protocol compliance**: Implements the `textDocument/codeAction`
//!   request/response flow from the LSP 3.17 specification.
//!
//! # Performance Characteristics
//!
//! - **Action detection**: O(n) where n is AST nodes
//! - **Code analysis**: <5ms for typical files
//! - **Memory usage**: ~200KB for 100 code actions
//! - **Workspace actions**: <50μs for cross-file operations
//!
//! # See Also
//!
//! - [`crate::ide::lsp_compat::diagnostics::DiagnosticProvider`]
//! - [`crate::ide::lsp_compat::references::ReferenceProvider`]
//!
//! # Usage Examples
//!
//! ```rust
//! use perl_parser::ide::lsp_compat::code_actions::CodeActionProvider;
//! use lsp_types::{CodeActionParams, Range, Position};
//! use url::Url;
//!
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let provider = CodeActionProvider::new();
//!
//! let params = CodeActionParams {
//!     text_document: lsp_types::TextDocumentIdentifier { 
//!         uri: Url::parse("file:///example.pl")? 
//!     },
//!     range: Range::new(Position::new(0, 0), Position::new(0, 10)),
//!     context: lsp_types::CodeActionContext::default(),
//!     work_done_progress_params: Default::default(),
//! };
//!
//! let actions = provider.provide_code_actions(params)?;
//! # Ok(())
//! # }
//! ```

use crate::ast::{Node, NodeKind};
use crate::position::{Position, Range};
use lsp_types::*;
use std::collections::HashMap;
use url::Url;

/// Provides code actions for Perl source code
///
/// This struct implements LSP code action functionality, offering
/// intelligent suggestions for refactoring, quick fixes, and
/// productivity enhancements.
///
/// # Performance
///
/// - Action detection: O(n) where n is AST nodes
/// - Code analysis: <5ms for typical files
/// - Memory footprint: ~200KB for 100 actions
/// - Workspace actions: <50μs for cross-file operations
#[derive(Debug, Clone)]
pub struct CodeActionProvider {
    /// Configuration for code action behavior
    config: CodeActionConfig,
    /// Workspace index for cross-file analysis
    workspace_index: Option<crate::workspace::workspace_index::WorkspaceIndex>,
}

/// Configuration for code action generation
#[derive(Debug, Clone)]
pub struct CodeActionConfig {
    /// Enable refactoring actions
    pub enable_refactoring: bool,
    /// Enable quick fix actions
    pub enable_quick_fixes: bool,
    /// Enable productivity actions
    pub enable_productivity: bool,
    /// Enable import optimization actions
    pub enable_import_optimization: bool,
    /// Maximum number of actions to return
    pub max_actions: usize,
}

impl Default for CodeActionConfig {
    fn default() -> Self {
        Self {
            enable_refactoring: true,
            enable_quick_fixes: true,
            enable_productivity: true,
            enable_import_optimization: true,
            max_actions: 50,
        }
    }
}

/// Categories of code actions for better organization
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CodeActionCategory {
    /// Quick fixes for diagnostics
    QuickFix,
    /// Refactoring operations
    Refactor,
    /// Productivity enhancements
    Productivity,
    /// Import management
    ImportOptimization,
    /// Code style improvements
    StyleImprovement,
}

impl CodeActionProvider {
    /// Creates a new code action provider with default configuration
    ///
    /// # Returns
    ///
    /// A new `CodeActionProvider` instance with default settings
    ///
    /// # Examples
    ///
    /// ```rust
    /// use perl_parser::ide::lsp_compat::code_actions::CodeActionProvider;
    ///
    /// let provider = CodeActionProvider::new();
    /// assert!(provider.config.enable_refactoring);
    /// ```
    pub fn new() -> Self {
        Self {
            config: CodeActionConfig::default(),
            workspace_index: None,
        }
    }

    /// Creates a code action provider with custom configuration
    ///
    /// # Arguments
    ///
    /// * `config` - Custom code action configuration
    ///
    /// # Returns
    ///
    /// A new `CodeActionProvider` with the specified configuration
    ///
    /// # Examples
    ///
    /// ```rust
    /// use perl_parser::ide::lsp_compat::code_actions::{CodeActionProvider, CodeActionConfig};
    ///
    /// let config = CodeActionConfig {
    ///     enable_refactoring: false,
    ///     enable_quick_fixes: true,
    ///     enable_productivity: true,
    ///     enable_import_optimization: false,
    ///     max_actions: 25,
    /// };
    ///
    /// let provider = CodeActionProvider::with_config(config);
    /// assert!(!provider.config.enable_refactoring);
    /// ```
    pub fn with_config(config: CodeActionConfig) -> Self {
        Self {
            config,
            workspace_index: None,
        }
    }

    /// Creates a code action provider with workspace index
    ///
    /// # Arguments
    ///
    /// * `workspace_index` - Pre-populated workspace index
    ///
    /// # Returns
    ///
    /// A new `CodeActionProvider` using the provided index
    ///
    /// # Examples
    ///
    /// ```rust
    /// use perl_parser::ide::lsp_compat::code_actions::CodeActionProvider;
    /// use perl_parser::workspace::workspace_index::WorkspaceIndex;
    ///
    /// let index = WorkspaceIndex::new();
    /// let provider = CodeActionProvider::with_index(index);
    /// ```
    pub fn with_index(workspace_index: crate::workspace::workspace_index::WorkspaceIndex) -> Self {
        Self {
            config: CodeActionConfig::default(),
            workspace_index: Some(workspace_index),
        }
    }

    /// Provides code actions for the given range and context
    ///
    /// # Arguments
    ///
    /// * `params` - LSP code action parameters
    ///
    /// # Returns
    ///
    /// A vector of code actions applicable to the current context
    ///
    /// # Performance
    ///
    /// - O(n) where n is AST nodes in the range
    /// - <5ms for typical files
    /// - Includes workspace-aware actions when index is available
    pub fn provide_code_actions(&self, params: CodeActionParams) -> Option<Vec<CodeActionOrCommand>> {
        let mut actions = Vec::new();
        
        // Analyze the range and context
        let uri = params.text_document.uri.clone();
        let range = params.range;
        let context = &params.context;
        
        // Quick fixes for diagnostics
        if self.config.enable_quick_fixes {
            actions.extend(self.provide_quick_fixes(&uri, range, context));
        }
        
        // Refactoring actions
        if self.config.enable_refactoring {
            actions.extend(self.provide_refactoring_actions(&uri, range));
        }
        
        // Productivity actions
        if self.config.enable_productivity {
            actions.extend(self.provide_productivity_actions(&uri, range));
        }
        
        // Import optimization
        if self.config.enable_import_optimization {
            actions.extend(self.provide_import_actions(&uri, range));
        }
        
        // Sort by priority and limit
        actions.sort_by(|a, b| {
            let a_priority = self.get_action_priority(a);
            let b_priority = self.get_action_priority(b);
            b_priority.cmp(&a_priority)
        });
        
        actions.truncate(self.config.max_actions);
        
        Some(actions)
    }

    /// Provides quick fix actions for diagnostics
    ///
    /// # Arguments
    ///
    /// * `uri` - Document URI
    /// * `range` - Selected range
    /// * `context` - Code action context with diagnostics
    ///
    /// # Returns
    ///
    /// Vector of quick fix code actions
    fn provide_quick_fixes(
        &self,
        uri: &Url,
        range: Range,
        context: &CodeActionContext,
    ) -> Vec<CodeActionOrCommand> {
        let mut actions = Vec::new();
        
        for diagnostic in &context.diagnostics {
            if let Some(code) = &diagnostic.code {
                match code {
                    NumberOrString::String(code_str) => {
                        match code_str.as_str() {
                            "syntax-error" => {
                                actions.push(self.create_syntax_error_fix(uri, diagnostic));
                            }
                            "style" => {
                                actions.push(self.create_style_fix(uri, diagnostic));
                            }
                            "security" => {
                                actions.push(self.create_security_fix(uri, diagnostic));
                            }
                            _ => {}
                        }
                    }
                    _ => {}
                }
            }
        }
        
        actions
    }

    /// Provides refactoring actions
    ///
    /// # Arguments
    ///
    /// * `uri` - Document URI
    /// * `range` - Selected range
    ///
    /// # Returns
    ///
    /// Vector of refactoring code actions
    fn provide_refactoring_actions(&self, uri: &Url, range: Range) -> Vec<CodeActionOrCommand> {
        let mut actions = Vec::new();
        
        // Extract variable
        actions.push(self.create_extract_variable_action(uri, range));
        
        // Extract subroutine
        actions.push(self.create_extract_subroutine_action(uri, range));
        
        // Convert to modern Perl patterns
        actions.push(self.create_modernize_action(uri, range));
        
        // Add pragmas
        actions.push(self.create_add_pragmas_action(uri, range));
        
        actions
    }

    /// Provides productivity actions
    ///
    /// # Arguments
    ///
    /// * `uri` - Document URI
    /// * `range` - Selected range
    ///
    /// # Returns
    ///
    /// Vector of productivity code actions
    fn provide_productivity_actions(&self, uri: &Url, range: Range) -> Vec<CodeActionOrCommand> {
        let mut actions = Vec::new();
        
        // Generate documentation
        actions.push(self.create_generate_docs_action(uri, range));
        
        // Add tests
        actions.push(self.create_add_tests_action(uri, range));
        
        // Optimize code
        actions.push(self.create_optimize_action(uri, range));
        
        actions
    }

    /// Provides import optimization actions
    ///
    /// # Arguments
    ///
    /// * `uri` - Document URI
    /// * `range` - Selected range
    ///
    /// # Returns
    ///
    /// Vector of import optimization code actions
    fn provide_import_actions(&self, uri: &Url, range: Range) -> Vec<CodeActionOrCommand> {
        let mut actions = Vec::new();
        
        // Remove unused imports
        actions.push(self.create_remove_unused_imports_action(uri, range));
        
        // Add missing imports
        actions.push(self.create_add_missing_imports_action(uri, range));
        
        // Sort imports
        actions.push(self.create_sort_imports_action(uri, range));
        
        actions
    }

    /// Creates a syntax error quick fix
    ///
    /// # Arguments
    ///
    /// * `uri` - Document URI
    /// * `diagnostic` - Diagnostic to fix
    ///
    /// # Returns
    ///
    /// Code action for fixing the syntax error
    fn create_syntax_error_fix(&self, uri: &Url, diagnostic: &Diagnostic) -> CodeActionOrCommand {
        let title = "Fix syntax error".to_string();
        let action = CodeAction {
            title: title.clone(),
            kind: Some(CodeActionKind::QUICK_FIX),
            diagnostics: Some(vec![diagnostic.clone()]),
            edit: Some(WorkspaceEdit {
                changes: Some({
                    let mut changes = HashMap::new();
                    changes.insert(
                        uri.clone(),
                        vec![TextEdit {
                            range: diagnostic.range,
                            new_text: ";".to_string(), // Simple fix example
                        }],
                    );
                    changes
                }),
                document_changes: None,
                change_annotations: None,
            }),
            command: None,
            is_preferred: Some(true),
            disabled: None,
            data: None,
        };
        
        CodeActionOrCommand::CodeAction(action)
    }

    /// Creates a style improvement fix
    ///
    /// # Arguments
    ///
    /// * `uri` - Document URI
    /// * `diagnostic` - Diagnostic to fix
    ///
    /// # Returns
    ///
    /// Code action for style improvement
    fn create_style_fix(&self, uri: &Url, diagnostic: &Diagnostic) -> CodeActionOrCommand {
        let title = "Improve style".to_string();
        let action = CodeAction {
            title: title.clone(),
            kind: Some(CodeActionKind::QUICK_FIX),
            diagnostics: Some(vec![diagnostic.clone()]),
            edit: Some(WorkspaceEdit::default()),
            command: None,
            is_preferred: Some(false),
            disabled: None,
            data: None,
        };
        
        CodeActionOrCommand::CodeAction(action)
    }

    /// Creates a security improvement fix
    ///
    /// # Arguments
    ///
    /// * `uri` - Document URI
    /// * `diagnostic` - Diagnostic to fix
    ///
    /// # Returns
    ///
    /// Code action for security improvement
    fn create_security_fix(&self, uri: &Url, diagnostic: &Diagnostic) -> CodeActionOrCommand {
        let title = "Improve security".to_string();
        let action = CodeAction {
            title: title.clone(),
            kind: Some(CodeActionKind::QUICK_FIX),
            diagnostics: Some(vec![diagnostic.clone()]),
            edit: Some(WorkspaceEdit::default()),
            command: None,
            is_preferred: Some(true),
            disabled: None,
            data: None,
        };
        
        CodeActionOrCommand::CodeAction(action)
    }

    /// Creates an extract variable refactoring action
    ///
    /// # Arguments
    ///
    /// * `uri` - Document URI
    /// * `range` - Selected range
    ///
    /// # Returns
    ///
    /// Code action for extracting a variable
    fn create_extract_variable_action(&self, uri: &Url, range: Range) -> CodeActionOrCommand {
        let title = "Extract variable".to_string();
        let action = CodeAction {
            title: title.clone(),
            kind: Some(CodeActionKind::REFACTOR),
            diagnostics: None,
            edit: Some(WorkspaceEdit::default()),
            command: None,
            is_preferred: Some(false),
            disabled: None,
            data: None,
        };
        
        CodeActionOrCommand::CodeAction(action)
    }

    /// Creates an extract subroutine refactoring action
    ///
    /// # Arguments
    ///
    /// * `uri` - Document URI
    /// * `range` - Selected range
    ///
    /// # Returns
    ///
    /// Code action for extracting a subroutine
    fn create_extract_subroutine_action(&self, uri: &Url, range: Range) -> CodeActionOrCommand {
        let title = "Extract subroutine".to_string();
        let action = CodeAction {
            title: title.clone(),
            kind: Some(CodeActionKind::REFACTOR),
            diagnostics: None,
            edit: Some(WorkspaceEdit::default()),
            command: None,
            is_preferred: Some(false),
            disabled: None,
            data: None,
        };
        
        CodeActionOrCommand::CodeAction(action)
    }

    /// Creates a modernize code action
    ///
    /// # Arguments
    ///
    /// * `uri` - Document URI
    /// * `range` - Selected range
    ///
    /// # Returns
    ///
    /// Code action for modernizing code
    fn create_modernize_action(&self, uri: &Url, range: Range) -> CodeActionOrCommand {
        let title = "Modernize code".to_string();
        let action = CodeAction {
            title: title.clone(),
            kind: Some(CodeActionKind::REFACTOR),
            diagnostics: None,
            edit: Some(WorkspaceEdit::default()),
            command: None,
            is_preferred: Some(false),
            disabled: None,
            data: None,
        };
        
        CodeActionOrCommand::CodeAction(action)
    }

    /// Creates an add pragmas action
    ///
    /// # Arguments
    ///
    /// * `uri` - Document URI
    /// * `range` - Selected range
    ///
    /// # Returns
    ///
    /// Code action for adding pragmas
    fn create_add_pragmas_action(&self, uri: &Url, range: Range) -> CodeActionOrCommand {
        let title = "Add pragmas".to_string();
        let action = CodeAction {
            title: title.clone(),
            kind: Some(CodeActionKind::REFACTOR),
            diagnostics: None,
            edit: Some(WorkspaceEdit::default()),
            command: None,
            is_preferred: Some(false),
            disabled: None,
            data: None,
        };
        
        CodeActionOrCommand::CodeAction(action)
    }

    /// Creates a generate documentation action
    ///
    /// # Arguments
    ///
    /// * `uri` - Document URI
    /// * `range` - Selected range
    ///
    /// # Returns
    ///
    /// Code action for generating documentation
    fn create_generate_docs_action(&self, uri: &Url, range: Range) -> CodeActionOrCommand {
        let title = "Generate documentation".to_string();
        let action = CodeAction {
            title: title.clone(),
            kind: Some(CodeActionKind::SOURCE),
            diagnostics: None,
            edit: Some(WorkspaceEdit::default()),
            command: None,
            is_preferred: Some(false),
            disabled: None,
            data: None,
        };
        
        CodeActionOrCommand::CodeAction(action)
    }

    /// Creates an add tests action
    ///
    /// # Arguments
    ///
    /// * `uri` - Document URI
    /// * `range` - Selected range
    ///
    /// # Returns
    ///
    /// Code action for adding tests
    fn create_add_tests_action(&self, uri: &Url, range: Range) -> CodeActionOrCommand {
        let title = "Add tests".to_string();
        let action = CodeAction {
            title: title.clone(),
            kind: Some(CodeActionKind::SOURCE),
            diagnostics: None,
            edit: Some(WorkspaceEdit::default()),
            command: None,
            is_preferred: Some(false),
            disabled: None,
            data: None,
        };
        
        CodeActionOrCommand::CodeAction(action)
    }

    /// Creates an optimize code action
    ///
    /// # Arguments
    ///
    /// * `uri` - Document URI
    /// * `range` - Selected range
    ///
    /// # Returns
    ///
    /// Code action for optimizing code
    fn create_optimize_action(&self, uri: &Url, range: Range) -> CodeActionOrCommand {
        let title = "Optimize code".to_string();
        let action = CodeAction {
            title: title.clone(),
            kind: Some(CodeActionKind::REFACTOR),
            diagnostics: None,
            edit: Some(WorkspaceEdit::default()),
            command: None,
            is_preferred: Some(false),
            disabled: None,
            data: None,
        };
        
        CodeActionOrCommand::CodeAction(action)
    }

    /// Creates a remove unused imports action
    ///
    /// # Arguments
    ///
    /// * `uri` - Document URI
    /// * `range` - Selected range
    ///
    /// # Returns
    ///
    /// Code action for removing unused imports
    fn create_remove_unused_imports_action(&self, uri: &Url, range: Range) -> CodeActionOrCommand {
        let title = "Remove unused imports".to_string();
        let action = CodeAction {
            title: title.clone(),
            kind: Some(CodeActionKind::SOURCE_ORGANIZE_IMPORTS),
            diagnostics: None,
            edit: Some(WorkspaceEdit::default()),
            command: None,
            is_preferred: Some(true),
            disabled: None,
            data: None,
        };
        
        CodeActionOrCommand::CodeAction(action)
    }

    /// Creates an add missing imports action
    ///
    /// # Arguments
    ///
    /// * `uri` - Document URI
    /// * `range` - Selected range
    ///
    /// # Returns
    ///
    /// Code action for adding missing imports
    fn create_add_missing_imports_action(&self, uri: &Url, range: Range) -> CodeActionOrCommand {
        let title = "Add missing imports".to_string();
        let action = CodeAction {
            title: title.clone(),
            kind: Some(CodeActionKind::SOURCE_ORGANIZE_IMPORTS),
            diagnostics: None,
            edit: Some(WorkspaceEdit::default()),
            command: None,
            is_preferred: Some(true),
            disabled: None,
            data: None,
        };
        
        CodeActionOrCommand::CodeAction(action)
    }

    /// Creates a sort imports action
    ///
    /// # Arguments
    ///
    /// * `uri` - Document URI
    /// * `range` - Selected range
    ///
    /// # Returns
    ///
    /// Code action for sorting imports
    fn create_sort_imports_action(&self, uri: &Url, range: Range) -> CodeActionOrCommand {
        let title = "Sort imports".to_string();
        let action = CodeAction {
            title: title.clone(),
            kind: Some(CodeActionKind::SOURCE_ORGANIZE_IMPORTS),
            diagnostics: None,
            edit: Some(WorkspaceEdit::default()),
            command: None,
            is_preferred: Some(false),
            disabled: None,
            data: None,
        };
        
        CodeActionOrCommand::CodeAction(action)
    }

    /// Gets the priority for a code action
    ///
    /// # Arguments
    ///
    /// * `action` - Code action to prioritize
    ///
    /// # Returns
    ///
    /// Priority value (higher = more important)
    fn get_action_priority(&self, action: &CodeActionOrCommand) -> u8 {
        match action {
            CodeActionOrCommand::CodeAction(code_action) => {
                match code_action.kind {
                    Some(CodeActionKind::QUICK_FIX) => 100,
                    Some(CodeActionKind::REFACTOR) => 80,
                    Some(CodeActionKind::SOURCE_ORGANIZE_IMPORTS) => 70,
                    Some(CodeActionKind::SOURCE) => 60,
                    _ => 50,
                }
            }
            CodeActionOrCommand::Command(_) => 40,
        }
    }

    /// Updates the workspace index
    ///
    /// # Arguments
    ///
    /// * `workspace_index` - New workspace index
    pub fn update_workspace_index(&mut self, workspace_index: crate::workspace::workspace_index::WorkspaceIndex) {
        self.workspace_index = Some(workspace_index);
    }
}

impl Default for CodeActionProvider {
    fn default() -> Self {
        Self::new()
    }
}

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

    #[test]
    fn test_code_action_provider_creation() {
        let provider = CodeActionProvider::new();
        assert!(provider.config.enable_refactoring);
        assert!(provider.config.enable_quick_fixes);
        assert!(provider.config.enable_productivity);
        assert!(provider.config.enable_import_optimization);
        assert_eq!(provider.config.max_actions, 50);
    }

    #[test]
    fn test_custom_config() {
        let config = CodeActionConfig {
            enable_refactoring: false,
            enable_quick_fixes: true,
            enable_productivity: false,
            enable_import_optimization: true,
            max_actions: 25,
        };

        let provider = CodeActionProvider::with_config(config);
        assert!(!provider.config.enable_refactoring);
        assert!(provider.config.enable_quick_fixes);
        assert!(!provider.config.enable_productivity);
        assert!(provider.config.enable_import_optimization);
        assert_eq!(provider.config.max_actions, 25);
    }

    #[test]
    fn test_action_priority() {
        let provider = CodeActionProvider::new();
        
        let quick_fix = CodeActionOrCommand::CodeAction(CodeAction {
            title: "Fix".to_string(),
            kind: Some(CodeActionKind::QUICK_FIX),
            ..Default::default()
        });
        
        let refactor = CodeActionOrCommand::CodeAction(CodeAction {
            title: "Refactor".to_string(),
            kind: Some(CodeActionKind::REFACTOR),
            ..Default::default()
        });
        
        assert!(provider.get_action_priority(&quick_fix) > provider.get_action_priority(&refactor));
    }

    #[test]
    fn test_workspace_index_update() {
        let mut provider = CodeActionProvider::new();
        let new_index = crate::workspace::workspace_index::WorkspaceIndex::new();
        
        // Should not panic
        provider.update_workspace_index(new_index);
        assert!(provider.workspace_index.is_some());
    }
}