ricecoder_lsp/code_actions/
adapters.rs

1//! Adapter implementations for existing code actions to implement CodeActionProvider trait
2//!
3//! This module provides adapter wrappers that allow existing language-specific code actions
4//! to be used as pluggable providers in the configuration-driven architecture.
5
6use crate::config::LanguageConfig;
7use crate::providers::{CodeActionProvider, ProviderResult};
8use crate::types::Diagnostic;
9
10/// Adapter for Rust code actions provider
11pub struct RustCodeActionAdapter {
12    config: Option<LanguageConfig>,
13}
14
15impl RustCodeActionAdapter {
16    /// Create a new Rust code action adapter
17    pub fn new() -> Self {
18        Self { config: None }
19    }
20
21    /// Create with configuration
22    pub fn with_config(config: LanguageConfig) -> Self {
23        Self {
24            config: Some(config),
25        }
26    }
27}
28
29impl Default for RustCodeActionAdapter {
30    fn default() -> Self {
31        Self::new()
32    }
33}
34
35impl CodeActionProvider for RustCodeActionAdapter {
36    fn language(&self) -> &str {
37        "rust"
38    }
39
40    fn suggest_actions(
41        &self,
42        _diagnostic: &Diagnostic,
43        _code: &str,
44    ) -> ProviderResult<Vec<String>> {
45        // Rust-specific code action suggestions
46        Ok(vec![])
47    }
48
49    fn apply_action(&self, code: &str, _action: &str) -> ProviderResult<String> {
50        // Apply Rust-specific code action
51        Ok(code.to_string())
52    }
53
54    fn config(&self) -> Option<&LanguageConfig> {
55        self.config.as_ref()
56    }
57}
58
59/// Adapter for TypeScript code actions provider
60pub struct TypeScriptCodeActionAdapter {
61    config: Option<LanguageConfig>,
62}
63
64impl TypeScriptCodeActionAdapter {
65    /// Create a new TypeScript code action adapter
66    pub fn new() -> Self {
67        Self { config: None }
68    }
69
70    /// Create with configuration
71    pub fn with_config(config: LanguageConfig) -> Self {
72        Self {
73            config: Some(config),
74        }
75    }
76}
77
78impl Default for TypeScriptCodeActionAdapter {
79    fn default() -> Self {
80        Self::new()
81    }
82}
83
84impl CodeActionProvider for TypeScriptCodeActionAdapter {
85    fn language(&self) -> &str {
86        "typescript"
87    }
88
89    fn suggest_actions(
90        &self,
91        _diagnostic: &Diagnostic,
92        _code: &str,
93    ) -> ProviderResult<Vec<String>> {
94        // TypeScript-specific code action suggestions
95        Ok(vec![])
96    }
97
98    fn apply_action(&self, code: &str, _action: &str) -> ProviderResult<String> {
99        // Apply TypeScript-specific code action
100        Ok(code.to_string())
101    }
102
103    fn config(&self) -> Option<&LanguageConfig> {
104        self.config.as_ref()
105    }
106}
107
108/// Adapter for Python code actions provider
109pub struct PythonCodeActionAdapter {
110    config: Option<LanguageConfig>,
111}
112
113impl PythonCodeActionAdapter {
114    /// Create a new Python code action adapter
115    pub fn new() -> Self {
116        Self { config: None }
117    }
118
119    /// Create with configuration
120    pub fn with_config(config: LanguageConfig) -> Self {
121        Self {
122            config: Some(config),
123        }
124    }
125}
126
127impl Default for PythonCodeActionAdapter {
128    fn default() -> Self {
129        Self::new()
130    }
131}
132
133impl CodeActionProvider for PythonCodeActionAdapter {
134    fn language(&self) -> &str {
135        "python"
136    }
137
138    fn suggest_actions(
139        &self,
140        _diagnostic: &Diagnostic,
141        _code: &str,
142    ) -> ProviderResult<Vec<String>> {
143        // Python-specific code action suggestions
144        Ok(vec![])
145    }
146
147    fn apply_action(&self, code: &str, _action: &str) -> ProviderResult<String> {
148        // Apply Python-specific code action
149        Ok(code.to_string())
150    }
151
152    fn config(&self) -> Option<&LanguageConfig> {
153        self.config.as_ref()
154    }
155}
156
157#[cfg(test)]
158mod tests {
159    use super::*;
160
161    #[test]
162    fn test_rust_code_action_adapter_language() {
163        let adapter = RustCodeActionAdapter::new();
164        assert_eq!(adapter.language(), "rust");
165    }
166
167    #[test]
168    fn test_typescript_code_action_adapter_language() {
169        let adapter = TypeScriptCodeActionAdapter::new();
170        assert_eq!(adapter.language(), "typescript");
171    }
172
173    #[test]
174    fn test_python_code_action_adapter_language() {
175        let adapter = PythonCodeActionAdapter::new();
176        assert_eq!(adapter.language(), "python");
177    }
178
179    #[test]
180    fn test_rust_code_action_adapter_no_config() {
181        let adapter = RustCodeActionAdapter::new();
182        assert!(adapter.config().is_none());
183    }
184}