planFileMove
Plan TypeScript file moves and renames with automatic import path updates. Generates edit plans and file move suggestions without modifying the filesystem.
Instructions
Plan file move/rename with import path updates. Returns edit plans and file move suggestions without modifying the filesystem.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| projectRoot | Yes | Absolute or relative path to the project root | |
| oldPath | Yes | Absolute path or path relative to projectRoot of the file to move | |
| newPath | Yes | Absolute path or path relative to projectRoot of the destination |
Implementation Reference
- src/tools/planFileMove.ts:22-117 (handler)The main handler function implementing the planFileMove tool logic using TypeScript Language Service to compute edits for file moves/renames.export function planFileMove(params: PlanFileMoveParams): PlanFileMoveResult { // 1. projectRoot を絶対パスに正規化 const absProjectRoot = path.resolve(params.projectRoot); // 2. oldPath / newPath を projectRoot 基準で絶対パスに正規化 const oldAbs = path.isAbsolute(params.oldPath) ? params.oldPath : path.resolve(absProjectRoot, params.oldPath); const newAbs = path.isAbsolute(params.newPath) ? params.newPath : path.resolve(absProjectRoot, params.newPath); // 3. createTsService から service を取得 const { service } = createTsService(absProjectRoot); // 4. service.getEditsForFileRename を呼び出し const fileTextChanges = service.getEditsForFileRename( oldAbs, newAbs, /* formatOptions */ {}, /* preferences */ {} ); // 5. FileTextChanges[] を FileTextEdits[] に変換 const edits: FileTextEdits[] = fileTextChanges.map((change) => { const fileName = change.fileName; const fileText = ts.sys.readFile(fileName); const textEdits: TextEdit[] = change.textChanges.map((textChange) => { // ファイルテキストがない場合はデフォルトの位置を使用 if (!fileText) { return { range: { start: { line: 0, character: textChange.span.start }, end: { line: 0, character: textChange.span.start + textChange.span.length, }, }, newText: textChange.newText, }; } // span.start / span.length を Range に変換 const sourceFile = ts.createSourceFile( fileName, fileText, ts.ScriptTarget.Latest, true ); const start = ts.getLineAndCharacterOfPosition( sourceFile, textChange.span.start ); const end = ts.getLineAndCharacterOfPosition( sourceFile, textChange.span.start + textChange.span.length ); return { range: { start: { line: start.line, character: start.character, }, end: { line: end.line, character: end.character, }, }, newText: textChange.newText, }; }); return { filePath: fileName, textEdits, }; }); // 6. fsMoves として 1 件追加 const fsMoves = [ { from: oldAbs, to: newAbs, }, ]; // 7. 結果を返す return { edits, fsMoves, }; }
- src/index.ts:69-93 (registration)The tool registration in the TOOLS array, defining name, description, and inputSchema for planFileMove.{ name: "planFileMove", description: "Plan file move/rename with import path updates. Returns edit plans and file move suggestions without modifying the filesystem.", inputSchema: { type: "object", properties: { projectRoot: { type: "string", description: "Absolute or relative path to the project root", }, oldPath: { type: "string", description: "Absolute path or path relative to projectRoot of the file to move", }, newPath: { type: "string", description: "Absolute path or path relative to projectRoot of the destination", }, }, required: ["projectRoot", "oldPath", "newPath"], }, },
- src/types.ts:73-87 (schema)Type definitions for PlanFileMoveParams (input) and PlanFileMoveResult (output).* planFileMove の入力パラメータ */ export type PlanFileMoveParams = { projectRoot: string; // 絶対 or 相対 oldPath: string; // 元ファイルパス newPath: string; // 移動先ファイルパス }; /** * planFileMove の出力結果 */ export type PlanFileMoveResult = { edits: FileTextEdits[]; fsMoves: FsMove[]; // 通常は 1 件だけ };
- src/index.ts:164-175 (registration)The dispatch handler in the switch statement that calls the planFileMove function when the tool is invoked.case "planFileMove": { const params = args as unknown as PlanFileMoveParams; const result = planFileMove(params); return { content: [ { type: "text", text: JSON.stringify(result, null, 2), }, ], }; }