1pub mod lsp_query_engine;
6
7use crate::types::{CodeLocation, CodeRange, Layer3Result, QueryResult, QueryType};
8use async_trait::async_trait;
9use std::path::PathBuf;
10
11pub use lsp_query_engine::{LspCodeAnalyzer, LspQueryEngine};
13
14#[async_trait]
18pub trait QueryEngine: Send + Sync {
19 async fn go_to_definition(&self, location: CodeLocation) -> Layer3Result<Option<QueryResult>>;
21
22 async fn find_references(&self, location: CodeLocation) -> Layer3Result<Vec<QueryResult>>;
24
25 async fn go_to_implementation(&self, location: CodeLocation) -> Layer3Result<Vec<QueryResult>>;
27
28 async fn go_to_type_definition(
30 &self,
31 location: CodeLocation,
32 ) -> Layer3Result<Option<QueryResult>>;
33
34 async fn hover(&self, location: CodeLocation) -> Layer3Result<Option<String>>;
36
37 async fn document_symbols(&self, file: PathBuf) -> Layer3Result<Vec<SymbolInfo>>;
39
40 async fn workspace_symbols(&self, query: &str) -> Layer3Result<Vec<SymbolInfo>>;
42
43 async fn query(
45 &self,
46 query_type: QueryType,
47 location: CodeLocation,
48 ) -> Layer3Result<Vec<QueryResult>>;
49}
50
51#[derive(Debug, Clone)]
53pub struct SymbolInfo {
54 pub name: String,
56 pub kind: SymbolKind,
58 pub location: CodeLocation,
60 pub range: Option<CodeRange>,
62 pub container_name: Option<String>,
64}
65
66#[derive(Debug, Clone, Copy, PartialEq, Eq)]
68pub enum SymbolKind {
69 File,
70 Module,
71 Namespace,
72 Package,
73 Class,
74 Method,
75 Property,
76 Field,
77 Constructor,
78 Enum,
79 Interface,
80 Function,
81 Variable,
82 Constant,
83 String,
84 Number,
85 Boolean,
86 Array,
87 Object,
88 Key,
89 Null,
90 EnumMember,
91 Struct,
92 Event,
93 Operator,
94 TypeParameter,
95 Macro,
96 Other,
97}
98
99#[async_trait]
103pub trait CodeAnalyzer: Send + Sync {
104 async fn analyze_structure(&self, file: PathBuf) -> Layer3Result<CodeStructure>;
106
107 async fn find_similar(&self, snippet: &str, threshold: f32) -> Layer3Result<Vec<CodeMatch>>;
109
110 async fn detect_patterns(&self, file: PathBuf) -> Layer3Result<Vec<DetectedPattern>>;
112}
113
114#[derive(Debug, Clone)]
116pub struct CodeStructure {
117 pub file: PathBuf,
119 pub imports: Vec<String>,
121 pub exports: Vec<String>,
123 pub symbols: Vec<SymbolInfo>,
125 pub lines: usize,
127}
128
129#[derive(Debug, Clone)]
131pub struct CodeMatch {
132 pub location: CodeLocation,
134 pub content: String,
136 pub similarity: f32,
138}
139
140#[derive(Debug, Clone)]
142pub struct DetectedPattern {
143 pub name: String,
145 pub pattern_type: PatternType,
147 pub locations: Vec<CodeLocation>,
149}
150
151#[derive(Debug, Clone, Copy, PartialEq, Eq)]
153pub enum PatternType {
154 DesignPattern,
156 AntiPattern,
158 SecurityIssue,
160 PerformanceIssue,
162 CodeStyle,
164}
165
166#[cfg(test)]
167mod tests {
168 use super::*;
169
170 #[test]
171 fn test_symbol_info_creation() {
172 let info = SymbolInfo {
173 name: "test_function".to_string(),
174 kind: SymbolKind::Function,
175 location: CodeLocation {
176 file: PathBuf::from("test.rs"),
177 line: 1,
178 column: 1,
179 },
180 range: None,
181 container_name: None,
182 };
183 assert_eq!(info.name, "test_function");
184 }
185}