codeLineDelete
Remove specific line ranges from code files to delete unwanted sections or clean up code. Specify file path and line numbers to execute deletions.
Instructions
每次使用前、使用後必須先使用(codeFileRead)。刪除程式碼檔案指定範圍的行。(如果不影響編譯器的話,不需要針對縮排等小問題修改。提醒User就好了)
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| filePath | Yes | ||
| startLine | Yes | ||
| endLine | No |
Implementation Reference
- tools/csLineDeleter.ts:15-61 (handler)Core handler function that deletes specified lines from a file by reading content, splitting into lines, removing the range with splice, and writing back. Handles validation and errors.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:111-115 (schema)Zod input schema defining parameters for the codeLineDelete tool: filePath, startLine (required), endLine (optional).{ filePath: z.string(), startLine: z.number(), endLine: z.number().optional() },
- main.ts:109-128 (registration)Registers the 'codeLineDelete' MCP tool with name, description, input schema, and an async wrapper handler that calls the core deleteLines function and formats the MCP response.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 : "未知錯誤"}` }] }; } } );