codeLineDelete
Remove specified lines from a code file, ensuring minimal impact on the compiler. Use to cleanly delete code segments without manual adjustments for formatting.
Instructions
每次使用前、使用後必須先使用(codeFileRead)。刪除程式碼檔案指定範圍的行。(如果不影響編譯器的話,不需要針對縮排等小問題修改。提醒User就好了)
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| endLine | No | ||
| filePath | Yes | ||
| startLine | Yes |
Implementation Reference
- tools/csLineDeleter.ts:15-61 (handler)Core implementation of the line deletion logic in the myFileDelete class static method deleteLines, which reads the file, splits into lines, splices out the specified range, and writes back.static async deleteLines( filePath: string, startLine: number, endLine?: number ): Promise<string> { try { // 檢查檔案是否存在 if (!existsSync(filePath)) { return `錯誤: 檔案 ${filePath} 不存在`; } // 設置結束行,如果未提供則等於起始行 const effectiveEndLine = endLine || startLine; // 確保起始行小於等於結束行 if (startLine > effectiveEndLine) { return `錯誤: 起始行 ${startLine} 大於結束行 ${effectiveEndLine}`; } // 讀取檔案內容 const fileContent = await fs.readFile(filePath, 'utf8'); const lines = fileContent.split(/\r?\n/); // 檢查行號是否有效 if (startLine < 1 || startLine > lines.length) { return `錯誤: 起始行 ${startLine} 超出範圍,檔案共有 ${lines.length} 行`; } if (effectiveEndLine < 1 || effectiveEndLine > lines.length) { return `錯誤: 結束行 ${effectiveEndLine} 超出範圍,檔案共有 ${lines.length} 行`; } // 刪除指定範圍的行 lines.splice(startLine - 1, effectiveEndLine - startLine + 1); // 寫回檔案 await fs.writeFile(filePath, lines.join('\n'), 'utf8'); if (startLine === effectiveEndLine) { return `成功刪除第 ${startLine} 行`; } else { return `成功刪除第 ${startLine} 行到第 ${effectiveEndLine} 行`; } } catch (error) { console.error(`刪除內容時發生錯誤: ${error}`); return `刪除內容時發生錯誤: ${error instanceof Error ? error.message : '未知錯誤'}`; } }
- main.ts:109-128 (registration)Registration of the codeLineDelete tool in main.ts, including input schema with Zod and thin async handler wrapper that calls the core deleteLines function.server.tool("codeLineDelete", "每次使用前、使用後必須先使用(codeFileRead)。刪除程式碼檔案指定範圍的行。(如果不影響編譯器的話,不需要針對縮排等小問題修改。提醒User就好了)", { filePath: z.string(), startLine: z.number(), endLine: z.number().optional() }, async ({ filePath, startLine, endLine }) => { try { const result = await myFileDelete.deleteLines(filePath, startLine, endLine); return { content: [{ type: "text", text: result }] }; } catch (error) { return { content: [{ type: "text", text: `刪除內容失敗: ${error instanceof Error ? error.message : "未知錯誤"}` }] }; } } );
- main.ts:111-115 (schema)Zod schema defining input parameters for the codeLineDelete tool: filePath (string), startLine (number), endLine (number optional).{ filePath: z.string(), startLine: z.number(), endLine: z.number().optional() },
- main.ts:10-10 (helper)Import of the myFileDelete class from csLineDeleter.js which provides the deleteLines method.import { myFileDelete } from "./tools/csLineDeleter.js";