code-transfer
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseCode Transfer
代码传输
Transfer code between files with precise line-based control. Dual-mode operation: native tools (1-10 files) or execution mode (10+ files, 90% token savings).
以行级精度在文件间传输代码。双模式运行:原生工具(处理1-10个文件)或执行模式(处理10个以上文件,可节省90%的token消耗)。
Operation Modes
操作模式
Basic Mode (Default)
基础模式(默认)
Use Read, Edit, Bash scripts for 1-10 file operations. Works immediately, no setup required.
使用Read、Edit、Bash脚本处理1-10个文件的操作。无需配置,即可立即使用。
Execution Mode (10+ files)
执行模式(10个以上文件)
python
from api.filesystem import batch_copy
from api.code_analysis import find_functions
functions = find_functions('app.py', pattern='handle_.*')
operations = [{
'source_file': 'app.py',
'start_line': f['start_line'],
'end_line': f['end_line'],
'target_file': 'handlers.py',
'target_line': -1
} for f in functions]
batch_copy(operations)python
from api.filesystem import batch_copy
from api.code_analysis import find_functions
functions = find_functions('app.py', pattern='handle_.*')
operations = [{
'source_file': 'app.py',
'start_line': f['start_line'],
'end_line': f['end_line'],
'target_file': 'handlers.py',
'target_line': -1
} for f in functions]
batch_copy(operations)When to Use
使用场景
- "copy this code to [file]"
- "move [function/class] to [file]"
- "extract this to a new file"
- "insert at line [number]"
- "reorganize into separate files"
- "将这段代码复制到[文件]"
- "将[函数/类]移动到[文件]"
- "将这段代码提取到新文件"
- "插入到第[数字]行"
- "重新整理到不同文件中"
Core Operations
核心操作
1. Extract Source Code
1. 提取源代码
Read(file_path="src/auth.py") # Full file
Read(file_path="src/auth.py", offset=10, limit=20) # Line range
Grep(pattern="def authenticate", -n=true, -A=10) # Find functionRead(file_path="src/auth.py") # 读取整个文件
Read(file_path="src/auth.py", offset=10, limit=20) # 读取行范围
Grep(pattern="def authenticate", -n=true, -A=10) # 查找函数2. Insert at Specific Line
2. 在特定行插入
Use script for line-based insertion:
line_insert.pybash
python3 skills/code-transfer/scripts/line_insert.py <file> <line_number> <code> [--backup]Examples:
bash
undefined使用脚本实现基于行的插入:
line_insert.pybash
python3 skills/code-transfer/scripts/line_insert.py <file> <line_number> <code> [--backup]示例:
bash
undefinedInsert function at line 50
在第50行插入函数
python3 skills/code-transfer/scripts/line_insert.py src/utils.py 50 "def helper():\n pass"
python3 skills/code-transfer/scripts/line_insert.py src/utils.py 50 "def helper():\n pass"
Insert with backup
插入并创建备份
python3 skills/code-transfer/scripts/line_insert.py src/utils.py 50 "code" --backup
python3 skills/code-transfer/scripts/line_insert.py src/utils.py 50 "code" --backup
Insert at beginning
在文件开头插入
python3 skills/code-transfer/scripts/line_insert.py src/new.py 1 "import os"
**When to use:**
- User specifies exact line number
- Inserting into new/empty files
- Inserting at beginning/end without contextpython3 skills/code-transfer/scripts/line_insert.py src/new.py 1 "import os"
**适用场景:**
- 用户指定了精确的行号
- 插入到新文件/空文件中
- 在文件开头/结尾插入且无需上下文3. Insert Relative to Content
3. 相对于现有内容插入
Use Edit when insertion point is relative to existing code:
Edit(
file_path="src/utils.py",
old_string="def existing():\n pass",
new_string="def existing():\n pass\n\ndef new():\n return True"
)当插入位置与现有代码相关时,使用Edit操作:
Edit(
file_path="src/utils.py",
old_string="def existing():\n pass",
new_string="def existing():\n pass\n\ndef new():\n return True"
)Workflow Examples
工作流示例
Copy Function Between Files
在文件间复制函数
- Find:
Grep(pattern="def validate_user", -n=true, -A=20) - Extract:
Read(file_path="auth.py", offset=45, limit=15) - Check target:
Read(file_path="validators.py") - Insert: Use or Edit based on context
line_insert.py
- 查找:
Grep(pattern="def validate_user", -n=true, -A=20) - 提取:
Read(file_path="auth.py", offset=45, limit=15) - 检查目标文件:
Read(file_path="validators.py") - 插入:根据上下文使用或Edit操作
line_insert.py
Extract Class to New File
将类提取到新文件
- Locate:
Grep(pattern="class DatabaseConnection", -n=true, -A=50) - Extract:
Read(file_path="original.py", offset=100, limit=50) - Create:
Write(file_path="database.py", content="<extracted>") - Update imports: in original file
Edit - Remove old class: with replacement
Edit
- 定位:
Grep(pattern="class DatabaseConnection", -n=true, -A=50) - 提取:
Read(file_path="original.py", offset=100, limit=50) - 创建新文件:
Write(file_path="database.py", content="<extracted>") - 更新导入:在原文件中使用Edit操作
- 删除旧类:使用Edit操作替换内容
Insert at Specific Line
在特定行插入
- Validate:
Read(file_path="main.py", offset=20, limit=10) - Insert:
python3 skills/code-transfer/scripts/line_insert.py main.py 25 "logger.info('...')" --backup - Verify:
Read(file_path="main.py", offset=23, limit=5)
- 验证:
Read(file_path="main.py", offset=20, limit=10) - 插入:
python3 skills/code-transfer/scripts/line_insert.py main.py 25 "logger.info('...')" --backup - 验证:
Read(file_path="main.py", offset=23, limit=5)
Reorganize Into Modules
重新整理为模块
- Analyze:
Read(file_path="utils.py") - Identify groups:
Grep(pattern="^def |^class ", -n=true) - Extract each category: new files
Write - Update original: Re-export or redirect
- 分析:
Read(file_path="utils.py") - 识别代码组:
Grep(pattern="^def |^class ", -n=true) - 提取各类代码:写入新文件
- 更新原文件:重新导出或重定向
Best Practices
最佳实践
Planning:
- Understand dependencies (imports, references)
- Identify exact start/end of code block
- Check target file structure
- Ensure necessary imports included
Preservation:
- Include docstrings and comments
- Transfer related functions together
- Update imports in both files
- Maintain formatting/indentation
Validation:
- Verify insertion placement
- Check syntax
- Test imports
- Suggest running tests
Backups:
- Use for significant changes
--backup - Critical file operations
- Large deletions
规划阶段:
- 了解代码依赖(导入、引用关系)
- 确定代码块的精确起始和结束位置
- 检查目标文件结构
- 确保包含必要的导入语句
代码保留:
- 保留文档字符串和注释
- 相关函数一起迁移
- 更新两个文件中的导入语句
- 保持格式/缩进一致
验证阶段:
- 验证插入位置是否正确
- 检查语法
- 测试导入是否正常
- 建议运行测试
备份:
- 进行重大修改时使用参数
--backup - 关键文件操作
- 大量删除操作时
Integration
集成
- code-refactor: Refactor after transferring
- test-fixing: Run tests after reorganizing
- feature-planning: Plan large reorganizations
- code-refactor:代码传输后进行重构
- test-fixing:重新整理后运行测试
- feature-planning:规划大规模代码重组织