delete_from_file
Remove specific text or lines from files using path and selector options. Supports exact text, line ranges, or mark tags. Preview changes with dry run before applying.
Instructions
從檔案中刪除特定內容
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| dryRun | No | 預覽變更而不實際修改檔案 | |
| path | Yes | ||
| selector | Yes |
Implementation Reference
- tools/fileEditorTool.ts:130-194 (handler)The core handler function implementing the delete_from_file tool logic. Handles deletion by text match, line range, or markers between start/end strings. Supports dry-run previews.static async deleteFromFile( filePath: string, selector: string | {startLine: number, endLine?: number} | {start: string, end: string}, dryRun: boolean = false ): Promise<string> { try { // 檢查檔案是否存在 if (!existsSync(filePath)) { return `錯誤: 檔案 ${filePath} 不存在`; } // 讀取檔案內容 let fileContent = await fs.readFile(filePath, 'utf8'); let originalContent = fileContent; // 根據不同的選擇器類型處理刪除操作 if (typeof selector === 'string') { // 刪除特定文字 if (!fileContent.includes(selector)) { return `錯誤: 在檔案中找不到文字「${selector}」`; } fileContent = fileContent.replace(selector, ''); } else if ('startLine' in selector) { // 刪除行範圍 const lines = fileContent.split(/\r?\n/); const startLine = selector.startLine; const endLine = selector.endLine || startLine; if (startLine < 1 || startLine > lines.length || endLine < startLine || endLine > lines.length) { return `錯誤: 行範圍 ${startLine}-${endLine} 超出檔案範圍 (1-${lines.length})`; } // 刪除指定行範圍 lines.splice(startLine - 1, endLine - startLine + 1); fileContent = lines.join('\n'); } else if ('start' in selector && 'end' in selector) { // 刪除兩個標記之間的內容 const startPos = fileContent.indexOf(selector.start); const endPos = fileContent.indexOf(selector.end, startPos + selector.start.length); if (startPos === -1 || endPos === -1) { return `錯誤: 在檔案中找不到指定的標記範圍`; } // 刪除開始標記到結束標記之間的內容(包括標記) const beforeStart = fileContent.substring(0, startPos); const afterEnd = fileContent.substring(endPos + selector.end.length); fileContent = beforeStart + afterEnd; } // 如果是預覽模式,生成差異報告 if (dryRun) { return this.generateDiff(originalContent, fileContent, filePath); } // 寫入檔案 await fs.writeFile(filePath, fileContent, 'utf8'); return `內容已成功從檔案刪除: ${filePath}`; } catch (error) { console.error(`刪除內容時發生錯誤: ${error}`); return `刪除內容時發生錯誤: ${error instanceof Error ? error.message : '未知錯誤'}`; } }
- main.ts:274-287 (schema)Zod schema defining the input parameters for the delete_from_file tool: path, selector (union of string, line range, or markers), and dryRun flag.path: z.string(), selector: z.union([ z.string().describe("要刪除的確切文字"), z.object({ startLine: z.number().describe("開始行號 (從1開始)"), endLine: z.number().optional().describe("結束行號,如果省略則只刪除單行") }), z.object({ start: z.string().describe("開始標記文字"), end: z.string().describe("結束標記文字") }) ]), dryRun: z.boolean().default(false).describe("預覽變更而不實際修改檔案") },
- main.ts:271-300 (registration)Registers the delete_from_file tool with the MCP server, providing name, description, input schema, and async handler that delegates to FileEditorTool.server.tool("delete_from_file", "從檔案中刪除特定內容", { path: z.string(), selector: z.union([ z.string().describe("要刪除的確切文字"), z.object({ startLine: z.number().describe("開始行號 (從1開始)"), endLine: z.number().optional().describe("結束行號,如果省略則只刪除單行") }), z.object({ start: z.string().describe("開始標記文字"), end: z.string().describe("結束標記文字") }) ]), dryRun: z.boolean().default(false).describe("預覽變更而不實際修改檔案") }, async ({ path, selector, dryRun = false }) => { try { const result = await FileEditorTool.deleteFromFile(path, selector, dryRun); return { content: [{ type: "text", text: result }] }; } catch (error) { return { content: [{ type: "text", text: `刪除內容失敗: ${error instanceof Error ? error.message : "未知錯誤"}` }] }; } } );