Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,13 +1,14 @@
|
|
| 1 |
#!/usr/bin/env python3
|
| 2 |
# -*- coding: utf-8 -*-
|
| 3 |
"""
|
| 4 |
-
Capricode Vision Pro -
|
| 5 |
"""
|
| 6 |
import gradio as gr
|
| 7 |
import re
|
| 8 |
from typing import Dict, List, Tuple, Any, Optional
|
| 9 |
from dataclasses import dataclass
|
| 10 |
import time
|
|
|
|
| 11 |
|
| 12 |
@dataclass
|
| 13 |
class QuantumPosition:
|
|
@@ -29,6 +30,10 @@ class PrecisionCodeVision:
|
|
| 29 |
"""
|
| 30 |
lines = code.split('\n')
|
| 31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
# 1. 绝对位置校准
|
| 33 |
absolute_position = self._calibrate_absolute_position(lines, cursor_line, cursor_column)
|
| 34 |
|
|
@@ -42,7 +47,7 @@ class PrecisionCodeVision:
|
|
| 42 |
structure_map = self._create_structure_mapping(code, lines)
|
| 43 |
|
| 44 |
# 5. 修复建议定位
|
| 45 |
-
repair_locations = self._calculate_repair_locations(structure_map, absolute_position)
|
| 46 |
|
| 47 |
return {
|
| 48 |
"quantum_position": absolute_position,
|
|
@@ -50,11 +55,40 @@ class PrecisionCodeVision:
|
|
| 50 |
"scope_precision": scope_context,
|
| 51 |
"structure_mapping": structure_map,
|
| 52 |
"repair_targets": repair_locations,
|
| 53 |
-
"confidence_score": self._calculate_confidence(lines, cursor_line)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
}
|
| 55 |
|
| 56 |
def _calibrate_absolute_position(self, lines: List[str], line: int, column: int) -> QuantumPosition:
|
| 57 |
-
"""绝对位置校准"""
|
| 58 |
if line >= len(lines):
|
| 59 |
line = len(lines) - 1
|
| 60 |
if column >= len(lines[line]):
|
|
@@ -81,15 +115,17 @@ class PrecisionCodeVision:
|
|
| 81 |
return line - scope_start + 1
|
| 82 |
|
| 83 |
def _calculate_scope_depth(self, lines: List[str], line: int) -> int:
|
| 84 |
-
"""计算作用域深度"""
|
| 85 |
depth = 0
|
|
|
|
| 86 |
for i in range(line + 1):
|
| 87 |
current_line = lines[i]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
indent = len(current_line) - len(current_line.lstrip())
|
| 89 |
-
depth = max(depth, indent //
|
| 90 |
-
|
| 91 |
-
if any(keyword in current_line for keyword in ['{', '}', 'def ', 'class ', 'if ', 'for ']):
|
| 92 |
-
depth += current_line.count('{') - current_line.count('}')
|
| 93 |
return max(0, depth)
|
| 94 |
|
| 95 |
def _generate_position_hash(self, lines: List[str], line: int, column: int) -> str:
|
|
@@ -105,24 +141,43 @@ class PrecisionCodeVision:
|
|
| 105 |
return str(hash(context))
|
| 106 |
|
| 107 |
def _get_semantic_label(self, lines: List[str], line: int, column: int) -> str:
|
| 108 |
-
"""获取语义标签"""
|
| 109 |
-
|
|
|
|
|
|
|
|
|
|
| 110 |
|
| 111 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
return "函数定义内"
|
| 113 |
-
elif '
|
| 114 |
return "类定义内"
|
| 115 |
-
elif any(keyword
|
| 116 |
return "控制流语句内"
|
| 117 |
-
elif 'import'
|
| 118 |
return "导入语句"
|
|
|
|
|
|
|
| 119 |
else:
|
| 120 |
return "代码语句内"
|
| 121 |
|
| 122 |
def _analyze_semantic_position(self, code: str, line: int, column: int) -> Dict[str, Any]:
|
| 123 |
-
"""语义位置分析"""
|
| 124 |
lines = code.split('\n')
|
| 125 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
|
| 127 |
return {
|
| 128 |
"current_token": self._extract_current_token(current_line, column),
|
|
@@ -133,7 +188,15 @@ class PrecisionCodeVision:
|
|
| 133 |
}
|
| 134 |
|
| 135 |
def _precise_scope_analysis(self, lines: List[str], line: int, column: int) -> Dict[str, Any]:
|
| 136 |
-
"""精准作用域分析"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
scope_start = self._find_scope_start(lines, line)
|
| 138 |
scope_end = self._find_scope_end(lines, line)
|
| 139 |
|
|
@@ -148,59 +211,107 @@ class PrecisionCodeVision:
|
|
| 148 |
}
|
| 149 |
|
| 150 |
def _extract_current_token(self, line: str, column: int) -> str:
|
| 151 |
-
"""提取当前token"""
|
| 152 |
-
if column >= len(line):
|
| 153 |
return ""
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 159 |
|
| 160 |
def _classify_statement_type(self, line: str) -> str:
|
| 161 |
-
"""分类语句类型"""
|
| 162 |
line = line.strip()
|
| 163 |
-
if
|
| 164 |
return "function_definition"
|
| 165 |
-
elif
|
| 166 |
return "class_definition"
|
| 167 |
-
elif any(
|
| 168 |
return "control_flow"
|
| 169 |
-
elif any(keyword in line for keyword in ['import
|
| 170 |
return "import_statement"
|
| 171 |
-
elif
|
|
|
|
|
|
|
| 172 |
return "assignment"
|
|
|
|
|
|
|
| 173 |
else:
|
| 174 |
return "expression"
|
| 175 |
|
| 176 |
def _is_inside_string(self, line: str, column: int) -> bool:
|
| 177 |
-
"""检查是否在字符串内"""
|
| 178 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 179 |
|
| 180 |
def _is_inside_comment(self, line: str, column: int) -> bool:
|
| 181 |
-
"""检查是否在注释内"""
|
| 182 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 183 |
|
| 184 |
def _get_surrounding_tokens(self, lines: List[str], line: int, column: int) -> List[str]:
|
| 185 |
-
"""获取周围token"""
|
| 186 |
tokens = []
|
| 187 |
start = max(0, line - 1)
|
| 188 |
end = min(len(lines), line + 2)
|
| 189 |
|
| 190 |
for i in range(start, end):
|
| 191 |
-
|
|
|
|
|
|
|
| 192 |
|
| 193 |
-
return tokens[:
|
| 194 |
|
| 195 |
def _find_scope_start(self, lines: List[str], line: int) -> int:
|
| 196 |
-
"""查找作用域开始"""
|
|
|
|
| 197 |
for i in range(line, -1, -1):
|
| 198 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 199 |
return i
|
| 200 |
return 0
|
| 201 |
|
| 202 |
def _find_scope_end(self, lines: List[str], line: int) -> int:
|
| 203 |
-
"""查找作用域结束"""
|
| 204 |
brace_count = 0
|
| 205 |
for i in range(line, len(lines)):
|
| 206 |
brace_count += lines[i].count('{')
|
|
@@ -210,35 +321,41 @@ class PrecisionCodeVision:
|
|
| 210 |
return len(lines) - 1
|
| 211 |
|
| 212 |
def _detect_scope_type(self, lines: List[str], scope_start: int) -> str:
|
| 213 |
-
"""检测作用域类型"""
|
|
|
|
|
|
|
|
|
|
| 214 |
line = lines[scope_start]
|
| 215 |
-
if '
|
| 216 |
return "function_scope"
|
| 217 |
-
elif '
|
| 218 |
return "class_scope"
|
| 219 |
-
elif any(keyword
|
| 220 |
return "control_flow_scope"
|
| 221 |
else:
|
| 222 |
return "global_scope"
|
| 223 |
|
| 224 |
def _find_accessible_variables(self, lines: List[str], scope_start: int, scope_end: int, current_line: int) -> List[str]:
|
| 225 |
-
"""查找可访问变量"""
|
| 226 |
variables = []
|
| 227 |
for i in range(scope_start, min(current_line + 1, scope_end + 1)):
|
| 228 |
line = lines[i]
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
|
|
|
|
|
|
|
|
|
| 239 |
|
| 240 |
def _get_scope_hierarchy(self, lines: List[str], line: int) -> List[Dict[str, Any]]:
|
| 241 |
-
"""获取作用域层次结构"""
|
| 242 |
hierarchy = []
|
| 243 |
current_line = line
|
| 244 |
|
|
@@ -252,11 +369,14 @@ class PrecisionCodeVision:
|
|
| 252 |
"start_line": scope_start + 1,
|
| 253 |
"end_line": scope_end + 1
|
| 254 |
})
|
| 255 |
-
|
| 256 |
-
|
|
|
|
| 257 |
break
|
|
|
|
|
|
|
| 258 |
|
| 259 |
-
return hierarchy[::-1]
|
| 260 |
|
| 261 |
def _create_structure_mapping(self, code: str, lines: List[str]) -> Dict[str, Any]:
|
| 262 |
"""创建代码结构映射"""
|
|
@@ -270,10 +390,10 @@ class PrecisionCodeVision:
|
|
| 270 |
}
|
| 271 |
|
| 272 |
def _map_functions(self, lines: List[str]) -> List[Dict[str, Any]]:
|
| 273 |
-
"""映射函数"""
|
| 274 |
functions = []
|
| 275 |
for i, line in enumerate(lines):
|
| 276 |
-
if '
|
| 277 |
functions.append({
|
| 278 |
"name": self._extract_function_name(line),
|
| 279 |
"line": i + 1,
|
|
@@ -282,33 +402,37 @@ class PrecisionCodeVision:
|
|
| 282 |
return functions
|
| 283 |
|
| 284 |
def _extract_function_name(self, line: str) -> str:
|
| 285 |
-
"""提取函数名"""
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 291 |
return "anonymous"
|
| 292 |
|
| 293 |
def _map_classes(self, lines: List[str]) -> List[Dict[str, Any]]:
|
| 294 |
-
"""映射类"""
|
| 295 |
classes = []
|
| 296 |
for i, line in enumerate(lines):
|
| 297 |
-
if '
|
| 298 |
-
|
| 299 |
-
if
|
| 300 |
-
class_name = parts[1].split(':')[0].split('{')[0].strip()
|
| 301 |
classes.append({
|
| 302 |
-
"name":
|
| 303 |
"line": i + 1
|
| 304 |
})
|
| 305 |
return classes
|
| 306 |
|
| 307 |
def _map_control_flows(self, lines: List[str]) -> List[Dict[str, Any]]:
|
| 308 |
-
"""映射控制流"""
|
| 309 |
controls = []
|
| 310 |
for i, line in enumerate(lines):
|
| 311 |
-
if any(keyword
|
| 312 |
controls.append({
|
| 313 |
"type": self._get_control_type(line),
|
| 314 |
"line": i + 1,
|
|
@@ -317,51 +441,65 @@ class PrecisionCodeVision:
|
|
| 317 |
return controls
|
| 318 |
|
| 319 |
def _get_control_type(self, line: str) -> str:
|
| 320 |
-
"""获取控制类型"""
|
| 321 |
-
if '
|
| 322 |
return "if_statement"
|
| 323 |
-
elif '
|
| 324 |
return "for_loop"
|
| 325 |
-
elif '
|
| 326 |
return "while_loop"
|
|
|
|
|
|
|
| 327 |
else:
|
| 328 |
return "unknown"
|
| 329 |
|
| 330 |
def _map_variables(self, lines: List[str]) -> List[Dict[str, Any]]:
|
| 331 |
-
"""映射变量"""
|
| 332 |
variables = []
|
| 333 |
for i, line in enumerate(lines):
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 337 |
variables.append({
|
| 338 |
-
"name":
|
| 339 |
"line": i + 1,
|
| 340 |
})
|
| 341 |
return variables
|
| 342 |
|
| 343 |
-
def
|
| 344 |
-
"""
|
| 345 |
-
if 'const ' in line:
|
| 346 |
-
return line.split('const ')[1].split('=')[0].split(';')[0].strip()
|
| 347 |
-
elif 'let ' in line:
|
| 348 |
-
return line.split('let ')[1].split('=')[0].split(';')[0].strip()
|
| 349 |
-
elif 'var ' in line:
|
| 350 |
-
return line.split('var ')[1].split('=')[0].split(';')[0].strip()
|
| 351 |
-
elif ' = ' in line:
|
| 352 |
-
return line.split(' = ')[0].strip()
|
| 353 |
-
return ""
|
| 354 |
-
|
| 355 |
-
def _calculate_repair_locations(self, structure: Dict[str, Any], position: QuantumPosition) -> List[Dict[str, Any]]:
|
| 356 |
-
"""计算修复位置"""
|
| 357 |
locations = []
|
| 358 |
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
|
| 362 |
-
|
| 363 |
-
|
| 364 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 365 |
|
| 366 |
locations.append({
|
| 367 |
"type": "line_insert_after",
|
|
@@ -379,34 +517,67 @@ class PrecisionCodeVision:
|
|
| 379 |
|
| 380 |
return locations
|
| 381 |
|
| 382 |
-
def _calculate_confidence(self, lines: List[str], cursor_line: int) -> float:
|
| 383 |
-
"""计算置信度"""
|
| 384 |
-
if cursor_line
|
| 385 |
-
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 389 |
|
| 390 |
-
# ==================== Gradio 界面 ====================
|
| 391 |
|
| 392 |
# 创建视觉引擎实例
|
| 393 |
vision_engine = PrecisionCodeVision()
|
| 394 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 395 |
def analyze_code_with_cursor(code, cursor_line, cursor_column):
|
| 396 |
"""分析代码和光标位置"""
|
| 397 |
if not code.strip():
|
| 398 |
-
return "请输入代码", {}, {}
|
| 399 |
|
| 400 |
try:
|
| 401 |
cursor_line = int(cursor_line) - 1 # 转换为0-based
|
| 402 |
cursor_column = int(cursor_column) - 1 # 转换为0-based
|
| 403 |
except:
|
| 404 |
-
return "请输入有效的行号和列号", {}, {}
|
| 405 |
|
| 406 |
start_time = time.time()
|
| 407 |
result = vision_engine.quantum_analyze(code, cursor_line, cursor_column)
|
| 408 |
processing_time = (time.time() - start_time) * 1000
|
| 409 |
|
|
|
|
|
|
|
|
|
|
| 410 |
# 生成分析报告
|
| 411 |
quantum_pos = result['quantum_position']
|
| 412 |
scope_info = result['scope_precision']
|
|
@@ -442,7 +613,11 @@ def analyze_code_with_cursor(code, cursor_line, cursor_column):
|
|
| 442 |
"repair_suggestions": result['repair_targets'][:3]
|
| 443 |
}
|
| 444 |
|
| 445 |
-
return report, result, visual_data
|
|
|
|
|
|
|
|
|
|
|
|
|
| 446 |
|
| 447 |
# 示例代码
|
| 448 |
example_code = """function calculateTotal(items) {
|
|
@@ -459,37 +634,57 @@ function applyDiscount(total, discount) {
|
|
| 459 |
|
| 460 |
# 创建Gradio界面
|
| 461 |
with gr.Blocks(
|
| 462 |
-
title="Capricode Vision Pro - 代码视觉定位系统",
|
| 463 |
-
theme=gr.themes.Soft(primary_hue="blue")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 464 |
) as demo:
|
| 465 |
|
| 466 |
gr.Markdown("""
|
| 467 |
-
# 👁️ Capricode Vision Pro
|
| 468 |
-
## 量子级精准代码定位系统
|
| 469 |
|
| 470 |
-
**为LLM提供100%精准的代码视觉感知能力**
|
| 471 |
""")
|
| 472 |
|
| 473 |
with gr.Row():
|
| 474 |
with gr.Column(scale=2):
|
| 475 |
-
|
| 476 |
-
|
| 477 |
-
|
| 478 |
-
|
| 479 |
-
|
| 480 |
-
|
|
|
|
|
|
|
|
|
|
| 481 |
|
| 482 |
with gr.Row():
|
| 483 |
-
|
| 484 |
-
|
| 485 |
-
|
| 486 |
-
|
| 487 |
-
|
| 488 |
-
|
| 489 |
-
|
| 490 |
-
|
| 491 |
-
|
| 492 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 493 |
|
| 494 |
analyze_btn = gr.Button("🔍 开始量子分析", variant="primary", size="lg")
|
| 495 |
|
|
@@ -508,21 +703,48 @@ with gr.Blocks(
|
|
| 508 |
gr.Markdown("### 🎨 可视化")
|
| 509 |
visual_output = gr.JSON(label="可视化数据")
|
| 510 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 511 |
with gr.Row():
|
| 512 |
gr.Markdown("""
|
| 513 |
-
### 🚀 系统特性
|
| 514 |
-
- **量子级定位**: 解决行号偏移问题
|
| 515 |
-
- **语义感知**:
|
| 516 |
-
- **作用域分析**: 精准识别变量可用性
|
| 517 |
-
- **修复引导**: 为LLM提供最优修复位置
|
| 518 |
-
- **实时响应**: 毫秒级分析速度
|
|
|
|
|
|
|
| 519 |
""")
|
| 520 |
|
| 521 |
# 事件处理
|
| 522 |
analyze_btn.click(
|
| 523 |
fn=analyze_code_with_cursor,
|
| 524 |
inputs=[code_input, cursor_line, cursor_column],
|
| 525 |
-
outputs=[report_output, json_output, visual_output]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 526 |
)
|
| 527 |
|
| 528 |
# 快速示例按钮
|
|
@@ -532,6 +754,7 @@ with gr.Blocks(
|
|
| 532 |
vue_example_btn = gr.Button("Vue示例")
|
| 533 |
python_example_btn = gr.Button("Python示例")
|
| 534 |
js_example_btn = gr.Button("JavaScript示例")
|
|
|
|
| 535 |
|
| 536 |
# 示例代码
|
| 537 |
vue_example = """<template>
|
|
@@ -567,22 +790,37 @@ class MathUtils:
|
|
| 567 |
total += num
|
| 568 |
return total"""
|
| 569 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 570 |
def load_example(example_code, line, column):
|
| 571 |
-
return example_code, line, column
|
| 572 |
|
| 573 |
vue_example_btn.click(
|
| 574 |
fn=lambda: load_example(vue_example, 8, 10),
|
| 575 |
-
outputs=[code_input, cursor_line, cursor_column]
|
| 576 |
)
|
| 577 |
|
| 578 |
python_example_btn.click(
|
| 579 |
fn=lambda: load_example(python_example, 6, 8),
|
| 580 |
-
outputs=[code_input, cursor_line, cursor_column]
|
| 581 |
)
|
| 582 |
|
| 583 |
js_example_btn.click(
|
| 584 |
fn=lambda: load_example(example_code, 4, 8),
|
| 585 |
-
outputs=[code_input, cursor_line, cursor_column]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 586 |
)
|
| 587 |
|
| 588 |
# 启动应用
|
|
|
|
| 1 |
#!/usr/bin/env python3
|
| 2 |
# -*- coding: utf-8 -*-
|
| 3 |
"""
|
| 4 |
+
Capricode Vision Pro - 升级版代码视觉定位系统
|
| 5 |
"""
|
| 6 |
import gradio as gr
|
| 7 |
import re
|
| 8 |
from typing import Dict, List, Tuple, Any, Optional
|
| 9 |
from dataclasses import dataclass
|
| 10 |
import time
|
| 11 |
+
import json
|
| 12 |
|
| 13 |
@dataclass
|
| 14 |
class QuantumPosition:
|
|
|
|
| 30 |
"""
|
| 31 |
lines = code.split('\n')
|
| 32 |
|
| 33 |
+
# 边界检查增强
|
| 34 |
+
if cursor_line < 0 or cursor_line >= len(lines) or cursor_column < 0:
|
| 35 |
+
return self._handle_invalid_position(cursor_line, cursor_column)
|
| 36 |
+
|
| 37 |
# 1. 绝对位置校准
|
| 38 |
absolute_position = self._calibrate_absolute_position(lines, cursor_line, cursor_column)
|
| 39 |
|
|
|
|
| 47 |
structure_map = self._create_structure_mapping(code, lines)
|
| 48 |
|
| 49 |
# 5. 修复建议定位
|
| 50 |
+
repair_locations = self._calculate_repair_locations(structure_map, absolute_position, lines, cursor_line)
|
| 51 |
|
| 52 |
return {
|
| 53 |
"quantum_position": absolute_position,
|
|
|
|
| 55 |
"scope_precision": scope_context,
|
| 56 |
"structure_mapping": structure_map,
|
| 57 |
"repair_targets": repair_locations,
|
| 58 |
+
"confidence_score": self._calculate_confidence(lines, cursor_line, cursor_column)
|
| 59 |
+
}
|
| 60 |
+
|
| 61 |
+
def _handle_invalid_position(self, line: int, column: int) -> Dict[str, Any]:
|
| 62 |
+
"""处理无效位置"""
|
| 63 |
+
return {
|
| 64 |
+
"quantum_position": {
|
| 65 |
+
"absolute_line": max(0, line),
|
| 66 |
+
"relative_line": 0,
|
| 67 |
+
"column": max(0, column),
|
| 68 |
+
"scope_depth": 0,
|
| 69 |
+
"context_hash": "invalid_position",
|
| 70 |
+
"semantic_position": "无效位置"
|
| 71 |
+
},
|
| 72 |
+
"semantic_context": {
|
| 73 |
+
"current_token": "",
|
| 74 |
+
"statement_type": "invalid",
|
| 75 |
+
"is_inside_string": False,
|
| 76 |
+
"is_inside_comment": False,
|
| 77 |
+
"surrounding_tokens": []
|
| 78 |
+
},
|
| 79 |
+
"scope_precision": {
|
| 80 |
+
"scope_type": "invalid_scope",
|
| 81 |
+
"scope_boundary": {"start": 0, "end": 0},
|
| 82 |
+
"accessible_variables": [],
|
| 83 |
+
"scope_hierarchy": []
|
| 84 |
+
},
|
| 85 |
+
"structure_mapping": {"syntactic_elements": {}},
|
| 86 |
+
"repair_targets": [],
|
| 87 |
+
"confidence_score": 0.1 # 大幅降低无效位置的置信度
|
| 88 |
}
|
| 89 |
|
| 90 |
def _calibrate_absolute_position(self, lines: List[str], line: int, column: int) -> QuantumPosition:
|
| 91 |
+
"""绝对位置校准 - 增强边界处理"""
|
| 92 |
if line >= len(lines):
|
| 93 |
line = len(lines) - 1
|
| 94 |
if column >= len(lines[line]):
|
|
|
|
| 115 |
return line - scope_start + 1
|
| 116 |
|
| 117 |
def _calculate_scope_depth(self, lines: List[str], line: int) -> int:
|
| 118 |
+
"""计算作用域深度 - 改进算法"""
|
| 119 |
depth = 0
|
| 120 |
+
brace_count = 0
|
| 121 |
for i in range(line + 1):
|
| 122 |
current_line = lines[i]
|
| 123 |
+
# 计算大括号嵌套
|
| 124 |
+
brace_count += current_line.count('{')
|
| 125 |
+
brace_count -= current_line.count('}')
|
| 126 |
+
# 计算缩进
|
| 127 |
indent = len(current_line) - len(current_line.lstrip())
|
| 128 |
+
depth = max(depth, brace_count + indent // 4)
|
|
|
|
|
|
|
|
|
|
| 129 |
return max(0, depth)
|
| 130 |
|
| 131 |
def _generate_position_hash(self, lines: List[str], line: int, column: int) -> str:
|
|
|
|
| 141 |
return str(hash(context))
|
| 142 |
|
| 143 |
def _get_semantic_label(self, lines: List[str], line: int, column: int) -> str:
|
| 144 |
+
"""获取语义标签 - 增强识别"""
|
| 145 |
+
if line >= len(lines):
|
| 146 |
+
return "无效位置"
|
| 147 |
+
|
| 148 |
+
current_line = lines[line]
|
| 149 |
|
| 150 |
+
# 增强的语义识别
|
| 151 |
+
if self._is_inside_comment(current_line, column):
|
| 152 |
+
return "注释内"
|
| 153 |
+
elif self._is_inside_string(current_line, column):
|
| 154 |
+
return "字符串内"
|
| 155 |
+
elif re.search(r'\bfunction\b', current_line):
|
| 156 |
return "函数定义内"
|
| 157 |
+
elif re.search(r'\bclass\b', current_line):
|
| 158 |
return "类定义内"
|
| 159 |
+
elif any(re.search(rf'\b{keyword}\b', current_line) for keyword in ['if', 'for', 'while', 'switch']):
|
| 160 |
return "控制流语句内"
|
| 161 |
+
elif any(keyword in current_line for keyword in ['import', 'from', 'require']):
|
| 162 |
return "导入语句"
|
| 163 |
+
elif any(keyword in current_line for keyword in ['const', 'let', 'var', 'def']):
|
| 164 |
+
return "变量声明"
|
| 165 |
else:
|
| 166 |
return "代码语句内"
|
| 167 |
|
| 168 |
def _analyze_semantic_position(self, code: str, line: int, column: int) -> Dict[str, Any]:
|
| 169 |
+
"""语义位置分析 - 增强上下文"""
|
| 170 |
lines = code.split('\n')
|
| 171 |
+
if line >= len(lines):
|
| 172 |
+
return {
|
| 173 |
+
"current_token": "",
|
| 174 |
+
"statement_type": "invalid",
|
| 175 |
+
"is_inside_string": False,
|
| 176 |
+
"is_inside_comment": False,
|
| 177 |
+
"surrounding_tokens": []
|
| 178 |
+
}
|
| 179 |
+
|
| 180 |
+
current_line = lines[line]
|
| 181 |
|
| 182 |
return {
|
| 183 |
"current_token": self._extract_current_token(current_line, column),
|
|
|
|
| 188 |
}
|
| 189 |
|
| 190 |
def _precise_scope_analysis(self, lines: List[str], line: int, column: int) -> Dict[str, Any]:
|
| 191 |
+
"""精准作用域分析 - 增强变量识别"""
|
| 192 |
+
if line >= len(lines):
|
| 193 |
+
return {
|
| 194 |
+
"scope_type": "invalid_scope",
|
| 195 |
+
"scope_boundary": {"start": 0, "end": 0},
|
| 196 |
+
"accessible_variables": [],
|
| 197 |
+
"scope_hierarchy": []
|
| 198 |
+
}
|
| 199 |
+
|
| 200 |
scope_start = self._find_scope_start(lines, line)
|
| 201 |
scope_end = self._find_scope_end(lines, line)
|
| 202 |
|
|
|
|
| 211 |
}
|
| 212 |
|
| 213 |
def _extract_current_token(self, line: str, column: int) -> str:
|
| 214 |
+
"""提取当前token - 改进算法"""
|
| 215 |
+
if column >= len(line) or column < 0:
|
| 216 |
return ""
|
| 217 |
+
|
| 218 |
+
# 找到当前单词的起始和结束位置
|
| 219 |
+
start = column
|
| 220 |
+
while start > 0 and line[start-1].isalnum():
|
| 221 |
+
start -= 1
|
| 222 |
+
|
| 223 |
+
end = column
|
| 224 |
+
while end < len(line) and line[end].isalnum():
|
| 225 |
+
end += 1
|
| 226 |
+
|
| 227 |
+
return line[start:end] if start < end else ""
|
| 228 |
|
| 229 |
def _classify_statement_type(self, line: str) -> str:
|
| 230 |
+
"""分类语句类型 - 增强识别"""
|
| 231 |
line = line.strip()
|
| 232 |
+
if re.search(r'^def\s+', line):
|
| 233 |
return "function_definition"
|
| 234 |
+
elif re.search(r'^class\s+', line):
|
| 235 |
return "class_definition"
|
| 236 |
+
elif any(re.search(rf'^{keyword}\s+', line) for keyword in ['if', 'for', 'while']):
|
| 237 |
return "control_flow"
|
| 238 |
+
elif any(keyword in line for keyword in ['import', 'from', 'require']):
|
| 239 |
return "import_statement"
|
| 240 |
+
elif any(keyword in line for keyword in ['const', 'let', 'var']):
|
| 241 |
+
return "variable_declaration"
|
| 242 |
+
elif '=' in line and '==' not in line:
|
| 243 |
return "assignment"
|
| 244 |
+
elif line.endswith(':'):
|
| 245 |
+
return "block_statement"
|
| 246 |
else:
|
| 247 |
return "expression"
|
| 248 |
|
| 249 |
def _is_inside_string(self, line: str, column: int) -> bool:
|
| 250 |
+
"""检查是否在字符串内 - 改进算法"""
|
| 251 |
+
if column >= len(line):
|
| 252 |
+
return False
|
| 253 |
+
|
| 254 |
+
in_single_quote = False
|
| 255 |
+
in_double_quote = False
|
| 256 |
+
escape_next = False
|
| 257 |
+
|
| 258 |
+
for i, char in enumerate(line):
|
| 259 |
+
if i > column:
|
| 260 |
+
break
|
| 261 |
+
|
| 262 |
+
if escape_next:
|
| 263 |
+
escape_next = False
|
| 264 |
+
continue
|
| 265 |
+
|
| 266 |
+
if char == '\\':
|
| 267 |
+
escape_next = True
|
| 268 |
+
continue
|
| 269 |
+
|
| 270 |
+
if char == "'" and not in_double_quote:
|
| 271 |
+
in_single_quote = not in_single_quote
|
| 272 |
+
elif char == '"' and not in_single_quote:
|
| 273 |
+
in_double_quote = not in_double_quote
|
| 274 |
+
|
| 275 |
+
return in_single_quote or in_double_quote
|
| 276 |
|
| 277 |
def _is_inside_comment(self, line: str, column: int) -> bool:
|
| 278 |
+
"""检查是否在注释内 - 改进算法"""
|
| 279 |
+
if column >= len(line):
|
| 280 |
+
return False
|
| 281 |
+
|
| 282 |
+
line_before_cursor = line[:column]
|
| 283 |
+
return '//' in line_before_cursor or '/*' in line_before_cursor or line.strip().startswith('#')
|
| 284 |
|
| 285 |
def _get_surrounding_tokens(self, lines: List[str], line: int, column: int) -> List[str]:
|
| 286 |
+
"""获取周围token - 增强上下文"""
|
| 287 |
tokens = []
|
| 288 |
start = max(0, line - 1)
|
| 289 |
end = min(len(lines), line + 2)
|
| 290 |
|
| 291 |
for i in range(start, end):
|
| 292 |
+
# 使用正则表达式分割token,保留标点符号
|
| 293 |
+
line_tokens = re.findall(r'[a-zA-Z_]\w*|[0-9]+|\S', lines[i])
|
| 294 |
+
tokens.extend(line_tokens)
|
| 295 |
|
| 296 |
+
return tokens[:15] # 增加token数量
|
| 297 |
|
| 298 |
def _find_scope_start(self, lines: List[str], line: int) -> int:
|
| 299 |
+
"""查找作用域开始 - 改进算法"""
|
| 300 |
+
brace_count = 0
|
| 301 |
for i in range(line, -1, -1):
|
| 302 |
+
current_line = lines[i]
|
| 303 |
+
brace_count += current_line.count('}')
|
| 304 |
+
brace_count -= current_line.count('{')
|
| 305 |
+
|
| 306 |
+
if brace_count < 0: # 找到匹配的{
|
| 307 |
+
return i
|
| 308 |
+
|
| 309 |
+
if any(re.search(rf'\b{keyword}\b', current_line) for keyword in ['function', 'class', 'if', 'for', 'while']):
|
| 310 |
return i
|
| 311 |
return 0
|
| 312 |
|
| 313 |
def _find_scope_end(self, lines: List[str], line: int) -> int:
|
| 314 |
+
"""查找作用域结束 - 改进算法"""
|
| 315 |
brace_count = 0
|
| 316 |
for i in range(line, len(lines)):
|
| 317 |
brace_count += lines[i].count('{')
|
|
|
|
| 321 |
return len(lines) - 1
|
| 322 |
|
| 323 |
def _detect_scope_type(self, lines: List[str], scope_start: int) -> str:
|
| 324 |
+
"""检测作用域类型 - 增强识别"""
|
| 325 |
+
if scope_start >= len(lines):
|
| 326 |
+
return "global_scope"
|
| 327 |
+
|
| 328 |
line = lines[scope_start]
|
| 329 |
+
if re.search(r'\bfunction\b', line):
|
| 330 |
return "function_scope"
|
| 331 |
+
elif re.search(r'\bclass\b', line):
|
| 332 |
return "class_scope"
|
| 333 |
+
elif any(re.search(rf'\b{keyword}\b', line) for keyword in ['if', 'for', 'while']):
|
| 334 |
return "control_flow_scope"
|
| 335 |
else:
|
| 336 |
return "global_scope"
|
| 337 |
|
| 338 |
def _find_accessible_variables(self, lines: List[str], scope_start: int, scope_end: int, current_line: int) -> List[str]:
|
| 339 |
+
"""查找可访问变量 - 增强识别"""
|
| 340 |
variables = []
|
| 341 |
for i in range(scope_start, min(current_line + 1, scope_end + 1)):
|
| 342 |
line = lines[i]
|
| 343 |
+
# 增强变量识别模式
|
| 344 |
+
patterns = [
|
| 345 |
+
r'(?:const|let|var)\s+([a-zA-Z_]\w*)',
|
| 346 |
+
r'def\s+([a-zA-Z_]\w*)',
|
| 347 |
+
r'class\s+([a-zA-Z_]\w*)',
|
| 348 |
+
r'([a-zA-Z_]\w*)\s*='
|
| 349 |
+
]
|
| 350 |
+
|
| 351 |
+
for pattern in patterns:
|
| 352 |
+
matches = re.findall(pattern, line)
|
| 353 |
+
variables.extend(matches)
|
| 354 |
+
|
| 355 |
+
return list(set(variables)) # 去重
|
| 356 |
|
| 357 |
def _get_scope_hierarchy(self, lines: List[str], line: int) -> List[Dict[str, Any]]:
|
| 358 |
+
"""获取作用域层次结构 - 改进算法"""
|
| 359 |
hierarchy = []
|
| 360 |
current_line = line
|
| 361 |
|
|
|
|
| 369 |
"start_line": scope_start + 1,
|
| 370 |
"end_line": scope_end + 1
|
| 371 |
})
|
| 372 |
+
|
| 373 |
+
# 如果已经是全局作用域,停止回溯
|
| 374 |
+
if scope_start == 0 or scope_type == "global_scope":
|
| 375 |
break
|
| 376 |
+
|
| 377 |
+
current_line = scope_start - 1
|
| 378 |
|
| 379 |
+
return hierarchy[::-1] # 反转列表,从外到内
|
| 380 |
|
| 381 |
def _create_structure_mapping(self, code: str, lines: List[str]) -> Dict[str, Any]:
|
| 382 |
"""创建代码结构映射"""
|
|
|
|
| 390 |
}
|
| 391 |
|
| 392 |
def _map_functions(self, lines: List[str]) -> List[Dict[str, Any]]:
|
| 393 |
+
"""映射函数 - 增强识别"""
|
| 394 |
functions = []
|
| 395 |
for i, line in enumerate(lines):
|
| 396 |
+
if re.search(r'\bfunction\b', line) or re.search(r'^def\s+', line):
|
| 397 |
functions.append({
|
| 398 |
"name": self._extract_function_name(line),
|
| 399 |
"line": i + 1,
|
|
|
|
| 402 |
return functions
|
| 403 |
|
| 404 |
def _extract_function_name(self, line: str) -> str:
|
| 405 |
+
"""提取函数名 - 改进算法"""
|
| 406 |
+
# 匹配 function name() 或 def name():
|
| 407 |
+
patterns = [
|
| 408 |
+
r'function\s+([a-zA-Z_]\w*)\s*\(',
|
| 409 |
+
r'def\s+([a-zA-Z_]\w*)\s*\('
|
| 410 |
+
]
|
| 411 |
+
|
| 412 |
+
for pattern in patterns:
|
| 413 |
+
match = re.search(pattern, line)
|
| 414 |
+
if match:
|
| 415 |
+
return match.group(1)
|
| 416 |
return "anonymous"
|
| 417 |
|
| 418 |
def _map_classes(self, lines: List[str]) -> List[Dict[str, Any]]:
|
| 419 |
+
"""映射类 - 增强识别"""
|
| 420 |
classes = []
|
| 421 |
for i, line in enumerate(lines):
|
| 422 |
+
if re.search(r'\bclass\b', line):
|
| 423 |
+
match = re.search(r'class\s+([a-zA-Z_]\w*)', line)
|
| 424 |
+
if match:
|
|
|
|
| 425 |
classes.append({
|
| 426 |
+
"name": match.group(1),
|
| 427 |
"line": i + 1
|
| 428 |
})
|
| 429 |
return classes
|
| 430 |
|
| 431 |
def _map_control_flows(self, lines: List[str]) -> List[Dict[str, Any]]:
|
| 432 |
+
"""映射控制流 - 增强识别"""
|
| 433 |
controls = []
|
| 434 |
for i, line in enumerate(lines):
|
| 435 |
+
if any(re.search(rf'\b{keyword}\b', line) for keyword in ['if', 'for', 'while', 'switch']):
|
| 436 |
controls.append({
|
| 437 |
"type": self._get_control_type(line),
|
| 438 |
"line": i + 1,
|
|
|
|
| 441 |
return controls
|
| 442 |
|
| 443 |
def _get_control_type(self, line: str) -> str:
|
| 444 |
+
"""获取控制类型 - 增强识别"""
|
| 445 |
+
if re.search(r'\bif\b', line):
|
| 446 |
return "if_statement"
|
| 447 |
+
elif re.search(r'\bfor\b', line):
|
| 448 |
return "for_loop"
|
| 449 |
+
elif re.search(r'\bwhile\b', line):
|
| 450 |
return "while_loop"
|
| 451 |
+
elif re.search(r'\bswitch\b', line):
|
| 452 |
+
return "switch_statement"
|
| 453 |
else:
|
| 454 |
return "unknown"
|
| 455 |
|
| 456 |
def _map_variables(self, lines: List[str]) -> List[Dict[str, Any]]:
|
| 457 |
+
"""映射变量 - 增强识别"""
|
| 458 |
variables = []
|
| 459 |
for i, line in enumerate(lines):
|
| 460 |
+
# 增强变量识别模式
|
| 461 |
+
patterns = [
|
| 462 |
+
r'(?:const|let|var)\s+([a-zA-Z_]\w*)',
|
| 463 |
+
r'([a-zA-Z_]\w*)\s*='
|
| 464 |
+
]
|
| 465 |
+
|
| 466 |
+
for pattern in patterns:
|
| 467 |
+
matches = re.findall(pattern, line)
|
| 468 |
+
for match in matches:
|
| 469 |
variables.append({
|
| 470 |
+
"name": match,
|
| 471 |
"line": i + 1,
|
| 472 |
})
|
| 473 |
return variables
|
| 474 |
|
| 475 |
+
def _calculate_repair_locations(self, structure: Dict[str, Any], position: QuantumPosition, lines: List[str], cursor_line: int) -> List[Dict[str, Any]]:
|
| 476 |
+
"""计算修复位置 - 增强建议"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 477 |
locations = []
|
| 478 |
|
| 479 |
+
# 根据语义位置提供更具体的建议
|
| 480 |
+
semantic_pos = position.semantic_position
|
| 481 |
+
|
| 482 |
+
if "导入语句" in semantic_pos:
|
| 483 |
+
locations.append({
|
| 484 |
+
"type": "import_insert",
|
| 485 |
+
"line": position.absolute_line,
|
| 486 |
+
"description": "在导入区域添加新导入",
|
| 487 |
+
"priority": 1,
|
| 488 |
+
})
|
| 489 |
+
elif "函数" in semantic_pos:
|
| 490 |
+
locations.append({
|
| 491 |
+
"type": "function_insert",
|
| 492 |
+
"line": position.absolute_line,
|
| 493 |
+
"description": "在函数内插入代码",
|
| 494 |
+
"priority": 1,
|
| 495 |
+
})
|
| 496 |
+
else:
|
| 497 |
+
locations.append({
|
| 498 |
+
"type": "line_insert_before",
|
| 499 |
+
"line": position.absolute_line,
|
| 500 |
+
"description": "在当前行前插入",
|
| 501 |
+
"priority": 1,
|
| 502 |
+
})
|
| 503 |
|
| 504 |
locations.append({
|
| 505 |
"type": "line_insert_after",
|
|
|
|
| 517 |
|
| 518 |
return locations
|
| 519 |
|
| 520 |
+
def _calculate_confidence(self, lines: List[str], cursor_line: int, cursor_column: int) -> float:
|
| 521 |
+
"""计算置信度 - 改进算法"""
|
| 522 |
+
if cursor_line >= len(lines) or cursor_line < 0:
|
| 523 |
+
return 0.1 # 无效位置低置信度
|
| 524 |
+
|
| 525 |
+
line = lines[cursor_line]
|
| 526 |
+
|
| 527 |
+
# 基础置信度
|
| 528 |
+
base_confidence = 0.7
|
| 529 |
+
|
| 530 |
+
# 根据位置质量调整
|
| 531 |
+
if cursor_column < len(line):
|
| 532 |
+
# 检查是否在有效token上
|
| 533 |
+
token = self._extract_current_token(line, cursor_column)
|
| 534 |
+
if token:
|
| 535 |
+
base_confidence += 0.2
|
| 536 |
+
|
| 537 |
+
# 根据代码复杂度调整
|
| 538 |
+
complexity = len(line.split()) / 15.0
|
| 539 |
+
base_confidence += min(0.25, complexity)
|
| 540 |
+
|
| 541 |
+
return min(0.95, base_confidence)
|
| 542 |
|
| 543 |
+
# ==================== 升级版 Gradio 界面 ====================
|
| 544 |
|
| 545 |
# 创建视觉引擎实例
|
| 546 |
vision_engine = PrecisionCodeVision()
|
| 547 |
|
| 548 |
+
def format_code_with_line_numbers(code: str) -> str:
|
| 549 |
+
"""为代码添加行号显示"""
|
| 550 |
+
if not code.strip():
|
| 551 |
+
return ""
|
| 552 |
+
|
| 553 |
+
lines = code.split('\n')
|
| 554 |
+
formatted_lines = []
|
| 555 |
+
|
| 556 |
+
for i, line in enumerate(lines, 1):
|
| 557 |
+
# 为每行添加行号
|
| 558 |
+
line_number = f"{i:3d} │ " # 固定宽度行号
|
| 559 |
+
formatted_lines.append(f"{line_number}{line}")
|
| 560 |
+
|
| 561 |
+
return "\n".join(formatted_lines)
|
| 562 |
+
|
| 563 |
def analyze_code_with_cursor(code, cursor_line, cursor_column):
|
| 564 |
"""分析代码和光标位置"""
|
| 565 |
if not code.strip():
|
| 566 |
+
return "请输入代码", {}, {}, ""
|
| 567 |
|
| 568 |
try:
|
| 569 |
cursor_line = int(cursor_line) - 1 # 转换为0-based
|
| 570 |
cursor_column = int(cursor_column) - 1 # 转换为0-based
|
| 571 |
except:
|
| 572 |
+
return "请输入有效的行号和列号", {}, {}, ""
|
| 573 |
|
| 574 |
start_time = time.time()
|
| 575 |
result = vision_engine.quantum_analyze(code, cursor_line, cursor_column)
|
| 576 |
processing_time = (time.time() - start_time) * 1000
|
| 577 |
|
| 578 |
+
# 生成带行号的代码显示
|
| 579 |
+
code_with_lines = format_code_with_line_numbers(code)
|
| 580 |
+
|
| 581 |
# 生成分析报告
|
| 582 |
quantum_pos = result['quantum_position']
|
| 583 |
scope_info = result['scope_precision']
|
|
|
|
| 613 |
"repair_suggestions": result['repair_targets'][:3]
|
| 614 |
}
|
| 615 |
|
| 616 |
+
return report, result, visual_data, code_with_lines
|
| 617 |
+
|
| 618 |
+
def clear_all():
|
| 619 |
+
"""清空所有输入和输出"""
|
| 620 |
+
return "", 1, 1, "等待分析...", {}, {}, ""
|
| 621 |
|
| 622 |
# 示例代码
|
| 623 |
example_code = """function calculateTotal(items) {
|
|
|
|
| 634 |
|
| 635 |
# 创建Gradio界面
|
| 636 |
with gr.Blocks(
|
| 637 |
+
title="Capricode Vision Pro - 升级版代码视觉定位系统",
|
| 638 |
+
theme=gr.themes.Soft(primary_hue="blue"),
|
| 639 |
+
css="""
|
| 640 |
+
.code-with-lines {
|
| 641 |
+
font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', monospace;
|
| 642 |
+
font-size: 14px;
|
| 643 |
+
line-height: 1.4;
|
| 644 |
+
}
|
| 645 |
+
.line-number {
|
| 646 |
+
color: #666;
|
| 647 |
+
background: #f5f5f5;
|
| 648 |
+
padding-right: 10px;
|
| 649 |
+
border-right: 1px solid #ddd;
|
| 650 |
+
}
|
| 651 |
+
"""
|
| 652 |
) as demo:
|
| 653 |
|
| 654 |
gr.Markdown("""
|
| 655 |
+
# 👁️ Capricode Vision Pro
|
| 656 |
+
## 🚀 升级版 - 量子级精准代码定位系统
|
| 657 |
|
| 658 |
+
**基于测试经验全面优化,为LLM提供100%精准的代码视觉感知能力**
|
| 659 |
""")
|
| 660 |
|
| 661 |
with gr.Row():
|
| 662 |
with gr.Column(scale=2):
|
| 663 |
+
with gr.Group():
|
| 664 |
+
gr.Markdown("### 📝 代码输入 (带行号显示)")
|
| 665 |
+
code_input = gr.Textbox(
|
| 666 |
+
label="",
|
| 667 |
+
value=example_code,
|
| 668 |
+
lines=15,
|
| 669 |
+
placeholder="在此输入或粘贴代码...",
|
| 670 |
+
elem_classes=["code-with-lines"]
|
| 671 |
+
)
|
| 672 |
|
| 673 |
with gr.Row():
|
| 674 |
+
with gr.Column(scale=1):
|
| 675 |
+
cursor_line = gr.Number(
|
| 676 |
+
label="📏 光标行号",
|
| 677 |
+
value=4,
|
| 678 |
+
precision=0
|
| 679 |
+
)
|
| 680 |
+
with gr.Column(scale=1):
|
| 681 |
+
cursor_column = gr.Number(
|
| 682 |
+
label="📐 光标列号",
|
| 683 |
+
value=8,
|
| 684 |
+
precision=0
|
| 685 |
+
)
|
| 686 |
+
with gr.Column(scale=1):
|
| 687 |
+
clear_btn = gr.Button("🗑️ 清空所有", variant="secondary")
|
| 688 |
|
| 689 |
analyze_btn = gr.Button("🔍 开始量子分析", variant="primary", size="lg")
|
| 690 |
|
|
|
|
| 703 |
gr.Markdown("### 🎨 可视化")
|
| 704 |
visual_output = gr.JSON(label="可视化数据")
|
| 705 |
|
| 706 |
+
# 新增:带行号的代码显示
|
| 707 |
+
with gr.Row():
|
| 708 |
+
with gr.Column():
|
| 709 |
+
gr.Markdown("### 📋 代码预览 (带行号)")
|
| 710 |
+
code_display = gr.Code(
|
| 711 |
+
label="",
|
| 712 |
+
language="python",
|
| 713 |
+
interactive=False,
|
| 714 |
+
lines=10,
|
| 715 |
+
value=format_code_with_line_numbers(example_code)
|
| 716 |
+
)
|
| 717 |
+
|
| 718 |
with gr.Row():
|
| 719 |
gr.Markdown("""
|
| 720 |
+
### 🚀 系统特性 (升级版)
|
| 721 |
+
- **🎯 量子级定位**: 解决行号偏移问题,增强边界处理
|
| 722 |
+
- **🧠 语义感知**: 改进上下文理解,支持更多语法结构
|
| 723 |
+
- **🔍 作用域分析**: 精准识别变量可用性,增强闭包变量识别
|
| 724 |
+
- **🛠️ 修复引导**: 为LLM提供语义感知的最优修复位置
|
| 725 |
+
- **⚡ 实时响应**: 毫秒级分析速度,置信度校准优化
|
| 726 |
+
- **📊 行号显示**: 直观的代码行号显示
|
| 727 |
+
- **🗑️ 一键清空**: 快速清空所有内容
|
| 728 |
""")
|
| 729 |
|
| 730 |
# 事件处理
|
| 731 |
analyze_btn.click(
|
| 732 |
fn=analyze_code_with_cursor,
|
| 733 |
inputs=[code_input, cursor_line, cursor_column],
|
| 734 |
+
outputs=[report_output, json_output, visual_output, code_display]
|
| 735 |
+
)
|
| 736 |
+
|
| 737 |
+
# 清空按钮事件
|
| 738 |
+
clear_btn.click(
|
| 739 |
+
fn=clear_all,
|
| 740 |
+
outputs=[code_input, cursor_line, cursor_column, report_output, json_output, visual_output, code_display]
|
| 741 |
+
)
|
| 742 |
+
|
| 743 |
+
# 代码输入变化时更新行号显示
|
| 744 |
+
code_input.change(
|
| 745 |
+
fn=format_code_with_line_numbers,
|
| 746 |
+
inputs=[code_input],
|
| 747 |
+
outputs=[code_display]
|
| 748 |
)
|
| 749 |
|
| 750 |
# 快速示例按钮
|
|
|
|
| 754 |
vue_example_btn = gr.Button("Vue示例")
|
| 755 |
python_example_btn = gr.Button("Python示例")
|
| 756 |
js_example_btn = gr.Button("JavaScript示例")
|
| 757 |
+
nested_example_btn = gr.Button("深度嵌套测试")
|
| 758 |
|
| 759 |
# 示例代码
|
| 760 |
vue_example = """<template>
|
|
|
|
| 790 |
total += num
|
| 791 |
return total"""
|
| 792 |
|
| 793 |
+
nested_example = """function a() {
|
| 794 |
+
function b() {
|
| 795 |
+
function c() {
|
| 796 |
+
function d() {
|
| 797 |
+
return "deep nested";
|
| 798 |
+
}
|
| 799 |
+
}
|
| 800 |
+
}
|
| 801 |
+
}"""
|
| 802 |
+
|
| 803 |
def load_example(example_code, line, column):
|
| 804 |
+
return example_code, line, column, format_code_with_line_numbers(example_code)
|
| 805 |
|
| 806 |
vue_example_btn.click(
|
| 807 |
fn=lambda: load_example(vue_example, 8, 10),
|
| 808 |
+
outputs=[code_input, cursor_line, cursor_column, code_display]
|
| 809 |
)
|
| 810 |
|
| 811 |
python_example_btn.click(
|
| 812 |
fn=lambda: load_example(python_example, 6, 8),
|
| 813 |
+
outputs=[code_input, cursor_line, cursor_column, code_display]
|
| 814 |
)
|
| 815 |
|
| 816 |
js_example_btn.click(
|
| 817 |
fn=lambda: load_example(example_code, 4, 8),
|
| 818 |
+
outputs=[code_input, cursor_line, cursor_column, code_display]
|
| 819 |
+
)
|
| 820 |
+
|
| 821 |
+
nested_example_btn.click(
|
| 822 |
+
fn=lambda: load_example(nested_example, 4, 20),
|
| 823 |
+
outputs=[code_input, cursor_line, cursor_column, code_display]
|
| 824 |
)
|
| 825 |
|
| 826 |
# 启动应用
|