mcpField
C
code-transfer

code-transfer

github.com/mhattingpete开发689171 次安装14 周前更新
代码仓库

关于 code-transfer

code-transfer 是用于在文件间移动、提取和插入代码的 Agent 技能,可精确控制行位置。少量文件使用原生工具,较大批量操作使用执行模式,并提示检查依赖和验证变更。

主要功能

  1. 01在文件间转移代码
  2. 02在指定行插入代码
  3. 03提取函数和类
  4. 04提供基础及批量执行模式
  5. 05指导更新导入并验证结果

适用场景

  1. 01将函数移至另一个文件
  2. 02把类提取到新模块
  3. 03跨多个文件重组代码

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/mhattingpete/claude-skills-marketplace --skill code-transfer

安装后在 Claude Code 中输入/code-transfer确认已加载。

附带文件scripts/line_insert.py

详细介绍

代码迁移

通过精确的行级控制,在文件之间迁移代码。双模式运行:原生工具(1-10 个文件)或执行模式(10 个以上文件,可节省 90% 的令牌)。

操作模式

基础模式(默认)

对 1-10 个文件的操作使用 Read、Edit、Bash 脚本。开箱即用,无需设置。

执行模式(10 个以上文件)
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)

适用场景

  • “将这段代码复制到 [文件]”
  • “将 [函数/类] 移动到 [文件]”
  • “将这段代码提取到新文件”
  • “在第 [数字] 行插入”
  • “重组为多个文件”

核心操作

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 function
2. 在指定行插入

使用 line_insert.py 脚本按行插入:

python3 skills/code-transfer/scripts/line_insert.py <file> <line_number> <code> [--backup]

示例:

# Insert function at line 50
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

# Insert at beginning
python3 skills/code-transfer/scripts/line_insert.py src/new.py 1 "import os"

适用场景:

  • 用户指定了确切的行号
  • 插入到新文件或空文件
  • 在开头或结尾插入,且没有上下文
3. 相对于内容插入

如果插入位置相对于现有代码,请使用 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"
)

工作流示例

在文件之间复制函数
  1. 查找:Grep(pattern="def validate_user", -n=true, -A=20)
  2. 提取:Read(file_path="auth.py", offset=45, limit=15)
  3. 检查目标文件:Read(file_path="validators.py")
  4. 插入:根据上下文使用 line_insert.py 或 Edit
将类提取到新文件
  1. 定位:Grep(pattern="class DatabaseConnection", -n=true, -A=50)
  2. 提取:Read(file_path="original.py", offset=100, limit=50)
  3. 创建:Write(file_path="database.py", content="<extracted>")
  4. 更新导入:在原文件中使用 Edit
  5. 移除旧类:使用 Edit 替换
在指定行插入
  1. 验证:Read(file_path="main.py", offset=20, limit=10)
  2. 插入:python3 skills/code-transfer/scripts/line_insert.py main.py 25 "logger.info('...')" --backup
  3. 验证:Read(file_path="main.py", offset=23, limit=5)
重组为模块
  1. 分析:Read(file_path="utils.py")
  2. 确定分组:Grep(pattern="^def |^class ", -n=true)
  3. 提取各个类别:使用 Write 创建新文件
  4. 更新原文件:重新导出或重定向

最佳实践

规划:

  • 理解依赖关系(导入、引用)
  • 确定代码块的确切起止位置
  • 检查目标文件结构
  • 确保包含必要的导入

保留完整性:

  • 包含文档字符串和注释
  • 一起迁移相关函数
  • 更新两个文件中的导入
  • 保持格式和缩进

验证:

  • 验证插入位置
  • 检查语法
  • 测试导入
  • 建议运行测试

备份:

  • 进行重大更改时使用 --backup
  • 操作关键文件
  • 大量删除

集成

  • code-refactor:迁移后进行重构
  • test-fixing:重组后运行测试
  • feature-planning:规划大型重组

评论

支持 Markdown