Skip to main content

rez_lsp_server/core/
traits.rs

1//! Core traits for the Rez LSP server.
2
3use async_trait::async_trait;
4use std::path::Path;
5
6use super::{
7    DependencyConflict, Package, Requirement, ResolvedContext, Result, Version, VersionConstraint,
8};
9
10/// Trait for Rez configuration management.
11#[async_trait]
12pub trait ConfigProvider: Send + Sync {
13    /// Get package search paths.
14    async fn get_package_paths(&self) -> Result<Vec<std::path::PathBuf>>;
15
16    /// Get local packages path.
17    async fn get_local_packages_path(&self) -> Result<Option<std::path::PathBuf>>;
18
19    /// Get release packages path.
20    async fn get_release_packages_path(&self) -> Result<Option<std::path::PathBuf>>;
21
22    /// Validate configuration.
23    async fn validate(&self) -> Result<()>;
24
25    /// Reload configuration from environment.
26    async fn reload(&mut self) -> Result<()>;
27}
28
29/// Trait for package discovery and caching.
30#[async_trait]
31pub trait PackageDiscovery: Send + Sync {
32    /// Scan all configured package repositories.
33    async fn scan_packages(&mut self) -> Result<()>;
34
35    /// Find packages by name pattern.
36    async fn find_packages(&self, pattern: &str) -> Result<Vec<Package>>;
37
38    /// Get all versions of a specific package.
39    async fn get_package_versions(&self, name: &str) -> Result<Vec<Package>>;
40
41    /// Get all package names.
42    async fn get_all_package_names(&self) -> Result<Vec<String>>;
43
44    /// Get package by exact name and version.
45    async fn get_package(&self, name: &str, version: &Version) -> Result<Option<Package>>;
46
47    /// Get discovery statistics.
48    async fn get_stats(&self) -> Result<(usize, usize)>; // (families, total_packages)
49
50    /// Clear package cache.
51    async fn clear_cache(&mut self) -> Result<()>;
52}
53
54/// Trait for parsing Rez package files.
55#[async_trait]
56pub trait PackageParser: Send + Sync {
57    /// Parse a package.py file.
58    async fn parse_package_file(&self, path: &Path) -> Result<Package>;
59
60    /// Parse package content from string.
61    async fn parse_package_content(&self, content: &str, base_path: &Path) -> Result<Package>;
62
63    /// Validate package syntax.
64    async fn validate_syntax(&self, content: &str) -> Result<Vec<SyntaxError>>;
65
66    /// Extract requirements from package content.
67    async fn extract_requirements(&self, content: &str) -> Result<Vec<Requirement>>;
68}
69
70/// Trait for dependency resolution.
71#[async_trait]
72pub trait DependencyResolver: Send + Sync {
73    /// Resolve a list of package requirements.
74    async fn resolve(&self, requirements: &[Requirement]) -> Result<ResolvedContext>;
75
76    /// Check if requirements can be satisfied.
77    async fn can_resolve(&self, requirements: &[Requirement]) -> Result<bool>;
78
79    /// Find conflicts in requirements.
80    async fn find_conflicts(&self, requirements: &[Requirement])
81        -> Result<Vec<DependencyConflict>>;
82
83    /// Get latest version of a package that satisfies constraints.
84    async fn get_latest_version(
85        &self,
86        name: &str,
87        constraint: &VersionConstraint,
88    ) -> Result<Option<Version>>;
89}
90
91/// Trait for completion providers.
92#[async_trait]
93pub trait CompletionProvider: Send + Sync {
94    /// Provide package name completions.
95    async fn complete_package_names(&self, prefix: &str) -> Result<Vec<CompletionItem>>;
96
97    /// Provide version completions for a package.
98    async fn complete_versions(
99        &self,
100        package_name: &str,
101        prefix: &str,
102    ) -> Result<Vec<CompletionItem>>;
103
104    /// Provide requirement completions.
105    async fn complete_requirements(&self, prefix: &str) -> Result<Vec<CompletionItem>>;
106
107    /// Provide tool completions.
108    async fn complete_tools(&self, prefix: &str) -> Result<Vec<CompletionItem>>;
109}
110
111/// Trait for hover information providers.
112#[async_trait]
113pub trait HoverProvider: Send + Sync {
114    /// Provide hover information for a package.
115    async fn hover_package(
116        &self,
117        name: &str,
118        version: Option<&Version>,
119    ) -> Result<Option<HoverInfo>>;
120
121    /// Provide hover information for a requirement.
122    async fn hover_requirement(&self, requirement: &str) -> Result<Option<HoverInfo>>;
123
124    /// Provide hover information for a tool.
125    async fn hover_tool(&self, tool: &str) -> Result<Option<HoverInfo>>;
126}
127
128/// Trait for diagnostic providers.
129#[async_trait]
130pub trait DiagnosticProvider: Send + Sync {
131    /// Analyze package file and return diagnostics.
132    async fn analyze_package(&self, content: &str, path: &Path) -> Result<Vec<Diagnostic>>;
133
134    /// Check for dependency conflicts.
135    async fn check_conflicts(&self, requirements: &[Requirement]) -> Result<Vec<Diagnostic>>;
136
137    /// Validate package syntax.
138    async fn validate_syntax(&self, content: &str) -> Result<Vec<Diagnostic>>;
139}
140
141/// Represents a completion item.
142#[derive(Debug, Clone)]
143pub struct CompletionItem {
144    /// The label of the completion item.
145    pub label: String,
146    /// The kind of completion item.
147    pub kind: CompletionItemKind,
148    /// Additional detail information.
149    pub detail: Option<String>,
150    /// Documentation for the item.
151    pub documentation: Option<String>,
152    /// Text to insert when this completion is selected.
153    pub insert_text: Option<String>,
154    /// Sort text for ordering completions.
155    pub sort_text: Option<String>,
156}
157
158/// The kind of completion item.
159#[derive(Debug, Clone, Copy, PartialEq, Eq)]
160pub enum CompletionItemKind {
161    /// A package
162    Package,
163    /// A version
164    Version,
165    /// A tool
166    Tool,
167    /// A requirement
168    Requirement,
169    /// A keyword
170    Keyword,
171    /// A variable
172    Variable,
173    /// A function
174    Function,
175}
176
177/// Represents hover information.
178#[derive(Debug, Clone)]
179pub struct HoverInfo {
180    /// The main content of the hover.
181    pub content: String,
182    /// Optional range that the hover applies to.
183    pub range: Option<Range>,
184}
185
186/// Represents a text range.
187#[derive(Debug, Clone, Copy, PartialEq, Eq)]
188pub struct Range {
189    /// Start position.
190    pub start: Position,
191    /// End position.
192    pub end: Position,
193}
194
195/// Represents a position in a text document.
196#[derive(Debug, Clone, Copy, PartialEq, Eq)]
197pub struct Position {
198    /// Line number (0-based).
199    pub line: u32,
200    /// Character offset (0-based).
201    pub character: u32,
202}
203
204/// Represents a diagnostic message.
205#[derive(Debug, Clone)]
206pub struct Diagnostic {
207    /// The range where the diagnostic applies.
208    pub range: Range,
209    /// The severity of the diagnostic.
210    pub severity: DiagnosticSeverity,
211    /// The diagnostic message.
212    pub message: String,
213    /// Optional source of the diagnostic.
214    pub source: Option<String>,
215    /// Optional diagnostic code.
216    pub code: Option<String>,
217}
218
219/// The severity of a diagnostic.
220#[derive(Debug, Clone, Copy, PartialEq, Eq)]
221pub enum DiagnosticSeverity {
222    /// An error.
223    Error,
224    /// A warning.
225    Warning,
226    /// An informational message.
227    Information,
228    /// A hint.
229    Hint,
230}
231
232/// Represents a syntax error.
233#[derive(Debug, Clone)]
234pub struct SyntaxError {
235    /// The range where the error occurs.
236    pub range: Range,
237    /// The error message.
238    pub message: String,
239    /// Optional suggestions for fixing the error.
240    pub suggestions: Vec<String>,
241}