pub mod lsp_query_engine;
use crate::types::{CodeLocation, CodeRange, Layer3Result, QueryResult, QueryType};
use async_trait::async_trait;
use std::path::PathBuf;
pub use lsp_query_engine::{LspCodeAnalyzer, LspQueryEngine};
#[async_trait]
pub trait QueryEngine: Send + Sync {
async fn go_to_definition(&self, location: CodeLocation) -> Layer3Result<Option<QueryResult>>;
async fn find_references(&self, location: CodeLocation) -> Layer3Result<Vec<QueryResult>>;
async fn go_to_implementation(&self, location: CodeLocation) -> Layer3Result<Vec<QueryResult>>;
async fn go_to_type_definition(
&self,
location: CodeLocation,
) -> Layer3Result<Option<QueryResult>>;
async fn hover(&self, location: CodeLocation) -> Layer3Result<Option<String>>;
async fn document_symbols(&self, file: PathBuf) -> Layer3Result<Vec<SymbolInfo>>;
async fn workspace_symbols(&self, query: &str) -> Layer3Result<Vec<SymbolInfo>>;
async fn query(
&self,
query_type: QueryType,
location: CodeLocation,
) -> Layer3Result<Vec<QueryResult>>;
}
#[derive(Debug, Clone)]
pub struct SymbolInfo {
pub name: String,
pub kind: SymbolKind,
pub location: CodeLocation,
pub range: Option<CodeRange>,
pub container_name: Option<String>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SymbolKind {
File,
Module,
Namespace,
Package,
Class,
Method,
Property,
Field,
Constructor,
Enum,
Interface,
Function,
Variable,
Constant,
String,
Number,
Boolean,
Array,
Object,
Key,
Null,
EnumMember,
Struct,
Event,
Operator,
TypeParameter,
Macro,
Other,
}
#[async_trait]
pub trait CodeAnalyzer: Send + Sync {
async fn analyze_structure(&self, file: PathBuf) -> Layer3Result<CodeStructure>;
async fn find_similar(&self, snippet: &str, threshold: f32) -> Layer3Result<Vec<CodeMatch>>;
async fn detect_patterns(&self, file: PathBuf) -> Layer3Result<Vec<DetectedPattern>>;
}
#[derive(Debug, Clone)]
pub struct CodeStructure {
pub file: PathBuf,
pub imports: Vec<String>,
pub exports: Vec<String>,
pub symbols: Vec<SymbolInfo>,
pub lines: usize,
}
#[derive(Debug, Clone)]
pub struct CodeMatch {
pub location: CodeLocation,
pub content: String,
pub similarity: f32,
}
#[derive(Debug, Clone)]
pub struct DetectedPattern {
pub name: String,
pub pattern_type: PatternType,
pub locations: Vec<CodeLocation>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PatternType {
DesignPattern,
AntiPattern,
SecurityIssue,
PerformanceIssue,
CodeStyle,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_symbol_info_creation() {
let info = SymbolInfo {
name: "test_function".to_string(),
kind: SymbolKind::Function,
location: CodeLocation {
file: PathBuf::from("test.rs"),
line: 1,
column: 1,
},
range: None,
container_name: None,
};
assert_eq!(info.name, "test_function");
}
}