update edit aply file content

This commit is contained in:
duanfuxiang 2025-04-01 11:41:29 +08:00
parent c415175de8
commit 4fb0bb22ac
5 changed files with 54 additions and 50 deletions

View File

@ -7,7 +7,7 @@ import { AppProvider } from './contexts/AppContext'
export type ApplyViewState = {
file: TFile
originalContent: string
oldContent: string
newContent: string
onClose: (applied: boolean) => void
}

View File

@ -28,7 +28,7 @@ export default function ApplyViewRoot({
const app = useApp()
const [diff, setDiff] = useState<Change[]>(
diffLines(state.originalContent, state.newContent),
diffLines(state.oldContent, state.newContent),
)
const handleAccept = async () => {

View File

@ -48,7 +48,7 @@ import { PromptGenerator, addLineNumbers } from '../../utils/prompt-generator'
import { fetchUrlsContent, webSearch } from '../../utils/web-search'
// Simple file reading function that returns a placeholder content for testing
const readFileContent = async (app: App, filePath: string): Promise<string> => {
const readFileContentByPath = async (app: App, filePath: string): Promise<string> => {
const file = app.vault.getFileByPath(filePath)
if (!file) {
throw new Error(`File not found: ${filePath}`)
@ -367,24 +367,23 @@ const Chat = forwardRef<ChatRef, ChatProps>((props, ref) => {
>({
mutationFn: async ({ applyMsgId, toolArgs }) => {
try {
const activeFile = app.workspace.getActiveFile()
if (!activeFile) {
throw new Error(
'No file is currently open to apply changes. Please open a file and try again.',
)
let opFile = app.workspace.getActiveFile()
if ('filepath' in toolArgs && toolArgs.filepath) {
opFile = app.vault.getFileByPath(toolArgs.filepath)
}
const activeFileContent = await readTFileContent(activeFile, app.vault)
if (toolArgs.type === 'write_to_file' || toolArgs.type === 'insert_content') {
const applyRes = await ApplyEditToFile(
activeFile,
activeFileContent,
if (!opFile) {
throw new Error(`File not found: ${toolArgs.filepath}`)
}
const fileContent = await readTFileContent(opFile, app.vault)
const appliedFileContent = await ApplyEditToFile(
fileContent,
toolArgs.content,
toolArgs.startLine,
toolArgs.endLine
)
if (!applyRes) {
if (!appliedFileContent) {
throw new Error('Failed to apply edit changes')
}
// return a Promise, which will be resolved after user makes a choice
@ -393,9 +392,9 @@ const Chat = forwardRef<ChatRef, ChatProps>((props, ref) => {
type: APPLY_VIEW_TYPE,
active: true,
state: {
file: activeFile,
originalContent: activeFileContent,
newContent: applyRes,
file: opFile,
oldContent: fileContent,
newContent: appliedFileContent,
onClose: (applied: boolean) => {
const applyStatus = applied ? ApplyStatus.Applied : ApplyStatus.Rejected
const applyEditContent = applied ? 'Changes successfully applied'
@ -418,13 +417,15 @@ const Chat = forwardRef<ChatRef, ChatProps>((props, ref) => {
})
})
} else if (toolArgs.type === 'search_and_replace') {
const fileContent = activeFile.path === toolArgs.filepath ? activeFileContent : await readFileContent(app, toolArgs.filepath)
const applyRes = await SearchAndReplace(
activeFile,
if (!opFile) {
throw new Error(`File not found: ${toolArgs.filepath}`)
}
const fileContent = await readTFileContent(opFile, app.vault)
const appliedFileContent = await SearchAndReplace(
fileContent,
toolArgs.operations
)
if (!applyRes) {
if (!appliedFileContent) {
throw new Error('Failed to search_and_replace')
}
// return a Promise, which will be resolved after user makes a choice
@ -433,9 +434,9 @@ const Chat = forwardRef<ChatRef, ChatProps>((props, ref) => {
type: APPLY_VIEW_TYPE,
active: true,
state: {
file: activeFile,
originalContent: activeFileContent,
newContent: applyRes,
file: opFile,
oldContent: fileContent,
newContent: appliedFileContent,
onClose: (applied: boolean) => {
const applyStatus = applied ? ApplyStatus.Applied : ApplyStatus.Rejected
const applyEditContent = applied ? 'Changes successfully applied'
@ -458,12 +459,15 @@ const Chat = forwardRef<ChatRef, ChatProps>((props, ref) => {
})
})
} else if (toolArgs.type === 'apply_diff') {
const diffResult = await diffStrategy.applyDiff(
activeFileContent,
if (!opFile) {
throw new Error(`File not found: ${toolArgs.filepath}`)
}
const fileContent = await readTFileContent(opFile, app.vault)
const appliedResult = await diffStrategy.applyDiff(
fileContent,
toolArgs.diff
)
if (!diffResult.success) {
console.log(diffResult)
if (!appliedResult || !appliedResult.success) {
throw new Error(`Failed to apply_diff`)
}
// return a Promise, which will be resolved after user makes a choice
@ -472,9 +476,9 @@ const Chat = forwardRef<ChatRef, ChatProps>((props, ref) => {
type: APPLY_VIEW_TYPE,
active: true,
state: {
file: activeFile,
originalContent: activeFileContent,
newContent: diffResult.content,
file: opFile,
oldContent: fileContent,
newContent: appliedResult.content,
onClose: (applied: boolean) => {
const applyStatus = applied ? ApplyStatus.Applied : ApplyStatus.Rejected
const applyEditContent = applied ? 'Changes successfully applied'
@ -497,7 +501,10 @@ const Chat = forwardRef<ChatRef, ChatProps>((props, ref) => {
})
})
} else if (toolArgs.type === 'read_file') {
const fileContent = await readFileContent(app, toolArgs.filepath)
if (!opFile) {
throw new Error(`File not found: ${toolArgs.filepath}`)
}
const fileContent = await readTFileContent(opFile, app.vault)
const formattedContent = `[read_file for '${toolArgs.filepath}'] Result:\n${addLineNumbers(fileContent)}\n`;
return {
type: 'read_file',

View File

@ -241,8 +241,7 @@ export const InlineEdit: React.FC<InlineEditProps> = ({
const endLine = parsedBlock?.endLine || defaultEndLine;
const updatedContent = await ApplyEditToFile(
activeFile,
await plugin.app.vault.read(activeFile),
await plugin.app.vault.cachedRead(activeFile),
finalContent,
startLine,
endLine

View File

@ -4,32 +4,31 @@ import { SearchAndReplaceToolArgs } from '../types/apply';
/**
* Applies changes to a file by replacing content within specified line range
* @param content - The new content to insert
* @param updateContent - The new content to insert
* @param currentFile - The file being modified
* @param currentFileContent - The current content of the file
* @param rawContent - The current content of the file
* @param startLine - Starting line number (1-based indexing, optional)
* @param endLine - Ending line number (1-based indexing, optional)
* @returns Promise resolving to the modified content or null if operation fails
*/
export const ApplyEditToFile = async (
currentFile: TFile,
currentFileContent: string,
content: string,
rawContent: string,
updateContent: string,
startLine?: number,
endLine?: number,
): Promise<string | null> => {
try {
// 如果文件为空,直接返回新内容
if (!currentFileContent || currentFileContent.trim() === '') {
return content;
// if file is empty, return new content
if (!rawContent || rawContent.trim() === '') {
return updateContent;
}
// 如果要清空文件,直接返回空字符串
if (content === '') {
// if content is empty, return empty string
if (updateContent === '') {
return '';
}
const lines = currentFileContent.split('\n')
const lines = rawContent.split('\n')
const effectiveStartLine = Math.max(1, startLine ?? 1)
const effectiveEndLine = Math.min(endLine ?? lines.length, lines.length)
@ -41,7 +40,7 @@ export const ApplyEditToFile = async (
// Construct new content
return [
...lines.slice(0, effectiveStartLine - 1),
content,
updateContent,
...lines.slice(effectiveEndLine)
].join('\n')
} catch (error) {
@ -58,16 +57,15 @@ function escapeRegExp(string: string): string {
/**
*
* @param currentFile -
* @param currentFileContent -
* @param rawContent -
* @param search -
* @param replace -
*/
export const SearchAndReplace = async (
currentFile: TFile,
currentFileContent: string,
rawContent: string,
operations: SearchAndReplaceToolArgs['operations']
) => {
let lines = currentFileContent.split("\n")
let lines = rawContent.split("\n")
for (const op of operations) {
const flags = op.regexFlags ?? (op.ignoreCase ? "gi" : "g")