Skip to content

Commit 43a6a7a

Browse files
committed
Refactor code structure for improved readability and maintainability
1 parent c8edaba commit 43a6a7a

23 files changed

+6184
-2300
lines changed
Lines changed: 284 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,284 @@
1+
# SCIP 關係圖實施計畫
2+
3+
**版本**: 1.0
4+
**日期**: 2025-01-14
5+
**狀態**: 規劃階段
6+
7+
## 📋 問題分析
8+
9+
### 當前狀況
10+
- ✅ SCIP Protocol Buffer 結構完整實現
11+
- ✅ 符號定義和出現位置正確處理
12+
-**關鍵缺失**: SCIP Relationship 功能完全未實現
13+
- ❌ 內部 `CallRelationships` 與標準 SCIP `Relationship` 完全分離
14+
15+
### 影響評估
16+
- **合規性**: 目前僅 60-70% 符合 SCIP 標準
17+
- **功能性**: 關係圖和跨符號導航功能不可用
18+
- **兼容性**: 無法與標準 SCIP 工具鏈集成
19+
20+
## 🎯 目標
21+
22+
### 主要目標
23+
1. **100% SCIP 標準合規性**: 完整實現 `scip_pb2.Relationship` 支援
24+
2. **關係圖功能**: 啟用函數調用、繼承、實現等關係追蹤
25+
3. **多語言支援**: 6 種程式語言的關係提取
26+
4. **向後兼容**: 不破壞現有功能
27+
28+
### 成功指標
29+
- ✅ 所有符號包含正確的 SCIP Relationship 信息
30+
- ✅ 通過官方 SCIP 驗證工具檢查
31+
- ✅ 關係查詢 API 正常運作
32+
- ✅ 性能影響 < 20%
33+
34+
## 🏗️ 技術架構
35+
36+
### 當前架構問題
37+
```
38+
[CallRelationships (內部格式)] ❌ 斷層 ❌ [SCIP Relationship (標準格式)]
39+
```
40+
41+
### 目標架構
42+
```
43+
[程式碼分析] → [關係提取] → [關係管理器] → [SCIP Relationship] → [SymbolInformation]
44+
```
45+
46+
### 核心組件
47+
48+
#### 1. 關係管理器 (`relationship_manager.py`)
49+
```python
50+
class SCIPRelationshipManager:
51+
"""SCIP 關係轉換和管理核心"""
52+
53+
def create_relationship(self, target_symbol: str, relationship_type: RelationshipType) -> scip_pb2.Relationship
54+
def add_relationships_to_symbol(self, symbol_info: scip_pb2.SymbolInformation, relationships: List[Relationship])
55+
def convert_call_relationships(self, call_rels: CallRelationships) -> List[scip_pb2.Relationship]
56+
```
57+
58+
#### 2. 關係類型定義 (`relationship_types.py`)
59+
```python
60+
class RelationshipType(Enum):
61+
CALLS = "calls" # 函數調用關係
62+
INHERITS = "inherits" # 繼承關係
63+
IMPLEMENTS = "implements" # 實現關係
64+
REFERENCES = "references" # 引用關係
65+
TYPE_DEFINITION = "type_definition" # 類型定義關係
66+
```
67+
68+
## 📁 檔案修改計畫
69+
70+
### 🆕 新增檔案 (4個)
71+
72+
#### 核心組件
73+
```
74+
src/code_index_mcp/scip/core/
75+
├── relationship_manager.py # 關係轉換核心 (優先級 1)
76+
└── relationship_types.py # 關係類型定義 (優先級 1)
77+
```
78+
79+
#### 測試檔案
80+
```
81+
tests/
82+
├── scip/test_relationship_manager.py # 單元測試 (優先級 3)
83+
└── integration/test_scip_relationships.py # 整合測試 (優先級 3)
84+
```
85+
86+
### 🔄 修改現有檔案 (9個)
87+
88+
#### 核心系統
89+
```
90+
src/code_index_mcp/scip/core/
91+
└── local_reference_resolver.py # 關係存儲和查詢 (優先級 1)
92+
```
93+
94+
#### 策略層
95+
```
96+
src/code_index_mcp/scip/strategies/
97+
├── base_strategy.py # 基礎關係處理 (優先級 1)
98+
├── python_strategy.py # Python 關係提取 (優先級 2)
99+
├── javascript_strategy.py # JavaScript 關係提取 (優先級 2)
100+
├── java_strategy.py # Java 關係提取 (優先級 2)
101+
├── objective_c_strategy.py # Objective-C 關係提取 (優先級 2)
102+
├── zig_strategy.py # Zig 關係提取 (優先級 2)
103+
└── fallback_strategy.py # 後備關係處理 (優先級 2)
104+
```
105+
106+
#### 分析工具
107+
```
108+
src/code_index_mcp/tools/scip/
109+
├── symbol_definitions.py # 關係數據結構增強 (優先級 2)
110+
└── scip_symbol_analyzer.py # 關係分析整合 (優先級 2)
111+
```
112+
113+
## 🗓️ 實施時程
114+
115+
### 階段 1:核心基礎 (第1-2週) - 優先級 1
116+
- [ ] **Week 1.1**: 創建 `relationship_manager.py`
117+
- SCIP Relationship 創建和轉換邏輯
118+
- 關係類型映射功能
119+
- 基礎 API 設計
120+
121+
- [ ] **Week 1.2**: 創建 `relationship_types.py`
122+
- 內部關係類型枚舉定義
123+
- SCIP 標準關係映射
124+
- 關係驗證邏輯
125+
126+
- [ ] **Week 2.1**: 修改 `base_strategy.py`
127+
- 新增 `_create_scip_relationships` 方法
128+
- 修改 `_create_scip_symbol_information` 加入關係處理
129+
- 新增抽象方法 `_build_symbol_relationships`
130+
131+
- [ ] **Week 2.2**: 更新 `local_reference_resolver.py`
132+
- 新增關係存儲功能
133+
- 實現 `add_symbol_relationship` 方法
134+
- 實現 `get_symbol_relationships` 方法
135+
136+
### 階段 2:語言實現 (第3-4週) - 優先級 2
137+
138+
#### Week 3: 主要語言策略
139+
- [ ] **Week 3.1**: Python 策略 (`python_strategy.py`)
140+
- 函數調用關係提取
141+
- 類繼承關係檢測
142+
- 方法重寫關係處理
143+
144+
- [ ] **Week 3.2**: JavaScript 策略 (`javascript_strategy.py`)
145+
- 函數調用和原型鏈關係
146+
- ES6 類繼承關係
147+
- 模組導入關係
148+
149+
#### Week 4: 其他語言策略
150+
- [ ] **Week 4.1**: Java 策略 (`java_strategy.py`)
151+
- 類繼承和介面實現關係
152+
- 方法調用關係
153+
- 包導入關係
154+
155+
- [ ] **Week 4.2**: Objective-C 和 Zig 策略
156+
- Objective-C 協議和繼承關係
157+
- Zig 結構體和函數關係
158+
- 後備策略更新
159+
160+
- [ ] **Week 4.3**: 工具層更新
161+
- 更新 `symbol_definitions.py`
162+
- 整合 `scip_symbol_analyzer.py`
163+
164+
### 階段 3:測試驗證 (第5週) - 優先級 3
165+
- [ ] **Week 5.1**: 單元測試
166+
- 關係管理器測試
167+
- 關係類型轉換測試
168+
- 各語言策略關係提取測試
169+
170+
- [ ] **Week 5.2**: 整合測試
171+
- 端到端關係功能測試
172+
- 多語言項目關係測試
173+
- 性能回歸測試
174+
175+
- [ ] **Week 5.3**: SCIP 合規性驗證
176+
- 使用官方 SCIP 工具驗證
177+
- 關係格式正確性檢查
178+
- 兼容性測試
179+
180+
### 階段 4:優化完善 (第6週) - 優先級 4
181+
- [ ] **Week 6.1**: 性能優化
182+
- 關係查詢 API 優化
183+
- 記憶體使用優化
184+
- 大型項目支援測試
185+
186+
- [ ] **Week 6.2**: 文檔和工具
187+
- 更新 ARCHITECTURE.md
188+
- 更新 API 文檔
189+
- 使用範例和指南
190+
191+
- [ ] **Week 6.3**: 發布準備
192+
- 版本號更新
193+
- 變更日誌準備
194+
- 向後兼容性最終檢查
195+
196+
## 🧪 測試策略
197+
198+
### 單元測試範圍
199+
```python
200+
# test_relationship_manager.py
201+
def test_create_scip_relationship()
202+
def test_convert_call_relationships()
203+
def test_relationship_type_mapping()
204+
205+
# test_python_relationships.py
206+
def test_function_call_extraction()
207+
def test_class_inheritance_detection()
208+
def test_method_override_relationships()
209+
```
210+
211+
### 整合測試範圍
212+
```python
213+
# test_scip_relationships.py
214+
def test_end_to_end_relationship_flow()
215+
def test_multi_language_relationship_support()
216+
def test_cross_file_relationship_resolution()
217+
def test_scip_compliance_validation()
218+
```
219+
220+
### 測試數據
221+
- 使用現有 `test/sample-projects/` 中的範例項目
222+
- 新增特定關係測試案例
223+
- 包含邊界情況和錯誤處理測試
224+
225+
## 📊 風險評估與緩解
226+
227+
### 高風險項目
228+
1. **性能影響**: 關係處理可能影響索引速度
229+
- **緩解**: 增量關係更新、並行處理
230+
231+
2. **複雜度增加**: 多語言關係邏輯複雜
232+
- **緩解**: 分階段實施、詳細測試
233+
234+
3. **向後兼容**: 現有 API 可能受影響
235+
- **緩解**: 保持現有接口、漸進式更新
236+
237+
### 中風險項目
238+
1. **SCIP 標準理解**: 關係映射可能不精確
239+
- **緩解**: 參考官方實現、社群驗證
240+
241+
2. **語言特性差異**: 不同語言關係模型差異大
242+
- **緩解**: 分語言設計、彈性架構
243+
244+
## 🚀 預期成果
245+
246+
### 功能改進
247+
- ✅ 完整的 SCIP 關係圖支援
248+
- ✅ 跨文件符號導航功能
249+
- ✅ 與標準 SCIP 工具鏈兼容
250+
- ✅ 6 種程式語言的關係分析
251+
252+
### 合規性提升
253+
- **當前**: 60-70% SCIP 標準合規
254+
- **目標**: 95%+ SCIP 標準合規
255+
- **關鍵**: 100% Relationship 功能合規
256+
257+
### 性能目標
258+
- 索引速度影響 < 15%
259+
- 記憶體使用增長 < 20%
260+
- 大型項目 (1000+ 檔案) 支援良好
261+
262+
## 📝 變更管理
263+
264+
### 版本控制策略
265+
- 功能分支開發 (`feature/scip-relationships`)
266+
- 增量 PR 提交,便於審查
267+
- 完整功能後合併到主分支
268+
269+
### 文檔更新
270+
- [ ] 更新 `ARCHITECTURE.md` 包含關係架構
271+
- [ ] 更新 `README.md` 功能描述
272+
- [ ] 新增關係 API 使用指南
273+
- [ ] 更新 `SCIP_OFFICIAL_STANDARDS.md` 實現狀態
274+
275+
### 發布策略
276+
- 作為主要版本發布 (v3.0.0)
277+
- 提供升級指南和遷移文檔
278+
- 社群通知和反饋收集
279+
280+
---
281+
282+
**負責人**: Claude Code
283+
**審查者**: 項目維護者
284+
**最後更新**: 2025-01-14

diff.txt

539 KB
Binary file not shown.

parse_scip_index.py

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#!/usr/bin/env python3
2+
"""解析 SCIP 索引文件"""
3+
4+
import sys
5+
import os
6+
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'src'))
7+
8+
def parse_scip_index():
9+
"""解析 SCIP 索引文件"""
10+
11+
scip_file_path = r"C:\Users\P10362~1\AppData\Local\Temp\code_indexer\22bf459212636f4b8ae327f69d901283\index.scip"
12+
13+
try:
14+
from code_index_mcp.scip.proto import scip_pb2
15+
16+
print(f"🔍 解析 SCIP 文件: {scip_file_path}")
17+
18+
# 檢查文件是否存在
19+
if not os.path.exists(scip_file_path):
20+
print("❌ SCIP 文件不存在")
21+
return
22+
23+
# 獲取文件大小
24+
file_size = os.path.getsize(scip_file_path)
25+
print(f"📊 文件大小: {file_size} bytes")
26+
27+
# 讀取並解析 SCIP 文件
28+
with open(scip_file_path, 'rb') as f:
29+
scip_data = f.read()
30+
31+
print(f"✅ 讀取了 {len(scip_data)} bytes 的數據")
32+
33+
# 解析 protobuf
34+
scip_index = scip_pb2.Index()
35+
scip_index.ParseFromString(scip_data)
36+
37+
print(f"✅ SCIP 索引解析成功")
38+
print(f"📄 文檔數量: {len(scip_index.documents)}")
39+
40+
# 檢查元數據
41+
if scip_index.metadata:
42+
print(f"📋 元數據:")
43+
print(f" 版本: {scip_index.metadata.version}")
44+
print(f" 項目根目錄: {scip_index.metadata.project_root}")
45+
print(f" 工具信息: {scip_index.metadata.tool_info}")
46+
47+
# 檢查前幾個文檔
48+
for i, doc in enumerate(scip_index.documents[:5]):
49+
print(f"\n📄 文檔 {i+1}: {doc.relative_path}")
50+
print(f" 語言: {doc.language}")
51+
print(f" 符號數量: {len(doc.symbols)}")
52+
print(f" 出現次數: {len(doc.occurrences)}")
53+
54+
# 檢查符號
55+
for j, symbol in enumerate(doc.symbols[:3]):
56+
print(f" 🔍 符號 {j+1}: {symbol.display_name}")
57+
print(f" 符號 ID: {symbol.symbol}")
58+
print(f" 類型: {symbol.kind}")
59+
print(f" 關係數量: {len(symbol.relationships)}")
60+
61+
# 檢查關係
62+
if symbol.relationships:
63+
for k, rel in enumerate(symbol.relationships[:2]):
64+
print(f" 🔗 關係 {k+1}: -> {rel.symbol}")
65+
print(f" is_reference: {rel.is_reference}")
66+
print(f" is_implementation: {rel.is_implementation}")
67+
print(f" is_type_definition: {rel.is_type_definition}")
68+
69+
# 統計信息
70+
total_symbols = sum(len(doc.symbols) for doc in scip_index.documents)
71+
total_occurrences = sum(len(doc.occurrences) for doc in scip_index.documents)
72+
total_relationships = sum(len(symbol.relationships) for doc in scip_index.documents for symbol in doc.symbols)
73+
74+
print(f"\n📊 統計信息:")
75+
print(f" 總文檔數: {len(scip_index.documents)}")
76+
print(f" 總符號數: {total_symbols}")
77+
print(f" 總出現次數: {total_occurrences}")
78+
print(f" 總關係數: {total_relationships}")
79+
80+
return True
81+
82+
except Exception as e:
83+
print(f"❌ 解析失敗: {e}")
84+
import traceback
85+
traceback.print_exc()
86+
return False
87+
88+
if __name__ == "__main__":
89+
print("🚀 開始解析 SCIP 索引文件...")
90+
success = parse_scip_index()
91+
92+
if success:
93+
print("\n✅ SCIP 索引解析完成!")
94+
else:
95+
print("\n❌ SCIP 索引解析失敗")

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ dependencies = [
2121
"tree-sitter-typescript>=0.20.0",
2222
"tree-sitter-java>=0.20.0",
2323
"tree-sitter-c>=0.20.0",
24+
"tree-sitter-zig>=0.20.0",
2425
"pathspec>=0.12.1",
2526
]
2627

0 commit comments

Comments
 (0)