Skip to main content

sh_layer3/query_engine/
mod.rs

1//! # Query Engine
2//!
3//! 代码查询引擎:基于 LSP 的代码分析能力。
4
5pub mod lsp_query_engine;
6
7use crate::types::{CodeLocation, CodeRange, Layer3Result, QueryResult, QueryType};
8use async_trait::async_trait;
9use std::path::PathBuf;
10
11// Re-export the LSP implementation
12pub use lsp_query_engine::{LspCodeAnalyzer, LspQueryEngine};
13
14/// 查询引擎 trait
15///
16/// 提供代码符号查询能力。
17#[async_trait]
18pub trait QueryEngine: Send + Sync {
19    /// 查询符号定义
20    async fn go_to_definition(&self, location: CodeLocation) -> Layer3Result<Option<QueryResult>>;
21
22    /// 查询符号引用
23    async fn find_references(&self, location: CodeLocation) -> Layer3Result<Vec<QueryResult>>;
24
25    /// 查询接口实现
26    async fn go_to_implementation(&self, location: CodeLocation) -> Layer3Result<Vec<QueryResult>>;
27
28    /// 查询类型定义
29    async fn go_to_type_definition(
30        &self,
31        location: CodeLocation,
32    ) -> Layer3Result<Option<QueryResult>>;
33
34    /// 获取悬停信息
35    async fn hover(&self, location: CodeLocation) -> Layer3Result<Option<String>>;
36
37    /// 列出文档符号
38    async fn document_symbols(&self, file: PathBuf) -> Layer3Result<Vec<SymbolInfo>>;
39
40    /// 工作区符号搜索
41    async fn workspace_symbols(&self, query: &str) -> Layer3Result<Vec<SymbolInfo>>;
42
43    /// 通用查询方法
44    async fn query(
45        &self,
46        query_type: QueryType,
47        location: CodeLocation,
48    ) -> Layer3Result<Vec<QueryResult>>;
49}
50
51/// 符号信息
52#[derive(Debug, Clone)]
53pub struct SymbolInfo {
54    /// 符号名称
55    pub name: String,
56    /// 符号类型
57    pub kind: SymbolKind,
58    /// 定义位置
59    pub location: CodeLocation,
60    /// 符号范围
61    pub range: Option<CodeRange>,
62    /// 所属容器(如类名)
63    pub container_name: Option<String>,
64}
65
66/// 符号类型
67#[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/// 代码分析器 trait
100///
101/// 提供更高级的代码分析能力。
102#[async_trait]
103pub trait CodeAnalyzer: Send + Sync {
104    /// 分析代码结构
105    async fn analyze_structure(&self, file: PathBuf) -> Layer3Result<CodeStructure>;
106
107    /// 查找相似代码
108    async fn find_similar(&self, snippet: &str, threshold: f32) -> Layer3Result<Vec<CodeMatch>>;
109
110    /// 检测代码模式
111    async fn detect_patterns(&self, file: PathBuf) -> Layer3Result<Vec<DetectedPattern>>;
112}
113
114/// 代码结构分析结果
115#[derive(Debug, Clone)]
116pub struct CodeStructure {
117    /// 文件路径
118    pub file: PathBuf,
119    /// 导入/依赖
120    pub imports: Vec<String>,
121    /// 导出符号
122    pub exports: Vec<String>,
123    /// 定义的所有符号
124    pub symbols: Vec<SymbolInfo>,
125    /// 代码行数
126    pub lines: usize,
127}
128
129/// 代码匹配结果
130#[derive(Debug, Clone)]
131pub struct CodeMatch {
132    /// 匹配位置
133    pub location: CodeLocation,
134    /// 匹配内容
135    pub content: String,
136    /// 相似度分数
137    pub similarity: f32,
138}
139
140/// 检测到的代码模式
141#[derive(Debug, Clone)]
142pub struct DetectedPattern {
143    /// 模式名称
144    pub name: String,
145    /// 模式类型
146    pub pattern_type: PatternType,
147    /// 出现位置
148    pub locations: Vec<CodeLocation>,
149}
150
151/// 代码模式类型
152#[derive(Debug, Clone, Copy, PartialEq, Eq)]
153pub enum PatternType {
154    /// 设计模式
155    DesignPattern,
156    /// 反模式
157    AntiPattern,
158    /// 安全漏洞
159    SecurityIssue,
160    /// 性能问题
161    PerformanceIssue,
162    /// 代码风格
163    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}