ricecoder_completion/
types.rs

1/// Core data types for the completion engine
2use serde::{Deserialize, Serialize};
3
4/// Represents a position in a document (line and character)
5#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
6pub struct Position {
7    /// Zero-based line number
8    pub line: u32,
9    /// Zero-based character offset within the line
10    pub character: u32,
11}
12
13impl Position {
14    pub fn new(line: u32, character: u32) -> Self {
15        Self { line, character }
16    }
17}
18
19/// Represents a range in a document (start and end positions)
20#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
21pub struct Range {
22    pub start: Position,
23    pub end: Position,
24}
25
26impl Range {
27    pub fn new(start: Position, end: Position) -> Self {
28        Self { start, end }
29    }
30}
31
32/// Represents a symbol (variable, function, type, etc.)
33#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
34pub struct Symbol {
35    pub name: String,
36    pub kind: SymbolKind,
37    pub scope: Scope,
38    pub type_info: Option<String>,
39    pub documentation: Option<String>,
40}
41
42/// Kind of symbol
43#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
44pub enum SymbolKind {
45    Variable,
46    Function,
47    Type,
48    Constant,
49    Module,
50    Class,
51    Struct,
52    Enum,
53    Interface,
54    Trait,
55    Method,
56    Property,
57    Field,
58    Parameter,
59    Keyword,
60}
61
62/// Represents a scope in code (function, class, module, etc.)
63#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
64pub struct Scope {
65    pub kind: ScopeKind,
66    pub name: Option<String>,
67    pub range: Range,
68}
69
70/// Kind of scope
71#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
72pub enum ScopeKind {
73    Global,
74    Module,
75    Function,
76    Class,
77    Struct,
78    Impl,
79    Block,
80}
81
82/// Type information for expected types
83#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
84pub struct Type {
85    pub name: String,
86    pub is_optional: bool,
87    pub is_array: bool,
88    pub generic_params: Vec<Type>,
89}
90
91impl Type {
92    pub fn new(name: String) -> Self {
93        Self {
94            name,
95            is_optional: false,
96            is_array: false,
97            generic_params: Vec::new(),
98        }
99    }
100
101    pub fn optional(mut self) -> Self {
102        self.is_optional = true;
103        self
104    }
105
106    pub fn array(mut self) -> Self {
107        self.is_array = true;
108        self
109    }
110}
111
112/// Context information for completion
113#[derive(Debug, Clone, Serialize, Deserialize)]
114pub struct CompletionContext {
115    pub scope: Scope,
116    pub available_symbols: Vec<Symbol>,
117    pub expected_type: Option<Type>,
118    pub prefix: String,
119    pub language: String,
120    pub position: Position,
121}
122
123impl CompletionContext {
124    pub fn new(language: String, position: Position, prefix: String) -> Self {
125        Self {
126            scope: Scope {
127                kind: ScopeKind::Global,
128                name: None,
129                range: Range::new(Position::new(0, 0), Position::new(0, 0)),
130            },
131            available_symbols: Vec::new(),
132            expected_type: None,
133            prefix,
134            language,
135            position,
136        }
137    }
138}
139
140/// Kind of completion item
141#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
142pub enum CompletionItemKind {
143    Text,
144    Method,
145    Function,
146    Constructor,
147    Field,
148    Variable,
149    Class,
150    Interface,
151    Module,
152    Property,
153    Unit,
154    Value,
155    Enum,
156    Keyword,
157    Snippet,
158    Color,
159    File,
160    Reference,
161    Folder,
162    EnumMember,
163    Constant,
164    Struct,
165    EventListener,
166    Operator,
167    TypeParameter,
168    Trait,
169}
170
171/// Represents a single completion suggestion
172#[derive(Debug, Clone, Serialize, Deserialize)]
173pub struct CompletionItem {
174    pub label: String,
175    pub kind: CompletionItemKind,
176    pub detail: Option<String>,
177    pub documentation: Option<String>,
178    pub sort_text: Option<String>,
179    pub filter_text: Option<String>,
180    pub insert_text: String,
181    pub score: f32,
182    pub additional_edits: Vec<TextEdit>,
183}
184
185impl CompletionItem {
186    pub fn new(label: String, kind: CompletionItemKind, insert_text: String) -> Self {
187        Self {
188            label,
189            kind,
190            detail: None,
191            documentation: None,
192            sort_text: None,
193            filter_text: None,
194            insert_text,
195            score: 0.0,
196            additional_edits: Vec::new(),
197        }
198    }
199
200    pub fn with_detail(mut self, detail: String) -> Self {
201        self.detail = Some(detail);
202        self
203    }
204
205    pub fn with_documentation(mut self, documentation: String) -> Self {
206        self.documentation = Some(documentation);
207        self
208    }
209
210    pub fn with_score(mut self, score: f32) -> Self {
211        self.score = score;
212        self
213    }
214}
215
216/// Represents a text edit (insertion, deletion, or replacement)
217#[derive(Debug, Clone, Serialize, Deserialize)]
218pub struct TextEdit {
219    pub range: Range,
220    pub new_text: String,
221}
222
223impl TextEdit {
224    pub fn new(range: Range, new_text: String) -> Self {
225        Self { range, new_text }
226    }
227}
228
229/// Ghost text suggestion (inline suggestion shown in lighter color)
230#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
231pub struct GhostText {
232    pub text: String,
233    pub range: Range,
234}
235
236impl GhostText {
237    pub fn new(text: String, range: Range) -> Self {
238        Self { text, range }
239    }
240}
241
242/// Ranking weights for completion scoring
243#[derive(Debug, Clone, Serialize, Deserialize)]
244pub struct RankingWeights {
245    pub relevance: f32,
246    pub frequency: f32,
247    pub recency: f32,
248}
249
250impl Default for RankingWeights {
251    fn default() -> Self {
252        Self {
253            relevance: 0.5,
254            frequency: 0.3,
255            recency: 0.2,
256        }
257    }
258}
259
260/// Completion configuration for a language
261#[derive(Debug, Clone, Serialize, Deserialize)]
262pub struct CompletionConfig {
263    pub language: String,
264    pub keywords: Vec<String>,
265    pub snippets: Vec<CompletionSnippet>,
266    pub ranking_weights: RankingWeights,
267    pub provider: Option<String>,
268}
269
270impl CompletionConfig {
271    pub fn new(language: String) -> Self {
272        Self {
273            language,
274            keywords: Vec::new(),
275            snippets: Vec::new(),
276            ranking_weights: RankingWeights::default(),
277            provider: None,
278        }
279    }
280}
281
282/// Snippet template for code completion
283#[derive(Debug, Clone, Serialize, Deserialize)]
284pub struct CompletionSnippet {
285    pub label: String,
286    pub template: String,
287    pub description: Option<String>,
288}
289
290impl CompletionSnippet {
291    pub fn new(label: String, template: String) -> Self {
292        Self {
293            label,
294            template,
295            description: None,
296        }
297    }
298
299    pub fn with_description(mut self, description: String) -> Self {
300        self.description = Some(description);
301        self
302    }
303}
304
305/// Error type for completion operations
306#[derive(Debug, thiserror::Error)]
307pub enum CompletionError {
308    #[error("Configuration error: {0}")]
309    ConfigError(String),
310
311    #[error("Context analysis failed: {0}")]
312    ContextAnalysisError(String),
313
314    #[error("Completion generation failed: {0}")]
315    GenerationError(String),
316
317    #[error("Ranking failed: {0}")]
318    RankingError(String),
319
320    #[error("Invalid position: {0}")]
321    InvalidPosition(String),
322
323    #[error("Language not supported: {0}")]
324    UnsupportedLanguage(String),
325
326    #[error("IO error: {0}")]
327    IoError(#[from] std::io::Error),
328
329    #[error("Serialization error: {0}")]
330    SerializationError(#[from] serde_json::Error),
331
332    #[error("YAML error: {0}")]
333    YamlError(#[from] serde_yaml::Error),
334
335    #[error("Storage error: {0}")]
336    StorageError(#[from] ricecoder_storage::StorageError),
337
338    #[error("Internal error: {0}")]
339    InternalError(String),
340}
341
342pub type CompletionResult<T> = Result<T, CompletionError>;