diff --git a/src/ApplyView.tsx b/src/ApplyView.tsx index 2576e50..a81f774 100644 --- a/src/ApplyView.tsx +++ b/src/ApplyView.tsx @@ -7,7 +7,7 @@ import { APPLY_VIEW_TYPE } from './constants' import { AppProvider } from './contexts/AppContext' export type ApplyViewState = { - file: TFile + file: string oldContent: string newContent: string onClose: (applied: boolean) => void diff --git a/src/components/apply-view/ApplyViewRoot.tsx b/src/components/apply-view/ApplyViewRoot.tsx index d941adb..e534d20 100644 --- a/src/components/apply-view/ApplyViewRoot.tsx +++ b/src/components/apply-view/ApplyViewRoot.tsx @@ -53,8 +53,11 @@ export default function ApplyViewRoot({ } return result; }, '') - - await app.vault.modify(state.file, newContent) + const file = app.vault.getFileByPath(state.file) + if (!file) { + throw new Error('File not found') + } + await app.vault.modify(file, newContent) if (state.onClose) { state.onClose(true) } diff --git a/src/components/chat-view/ChatView.tsx b/src/components/chat-view/ChatView.tsx index e8bc124..601eb4b 100644 --- a/src/components/chat-view/ChatView.tsx +++ b/src/components/chat-view/ChatView.tsx @@ -401,7 +401,7 @@ const Chat = forwardRef((props, ref) => { type: APPLY_VIEW_TYPE, active: true, state: { - file: opFile, + file: opFile.path, oldContent: '', newContent: toolArgs.content, onClose: (applied: boolean) => { @@ -452,7 +452,7 @@ const Chat = forwardRef((props, ref) => { type: APPLY_VIEW_TYPE, active: true, state: { - file: opFile, + file: opFile.path, oldContent: fileContent, newContent: appliedFileContent, onClose: (applied: boolean) => { @@ -494,7 +494,7 @@ const Chat = forwardRef((props, ref) => { type: APPLY_VIEW_TYPE, active: true, state: { - file: opFile, + file: opFile.path, oldContent: fileContent, newContent: appliedFileContent, onClose: (applied: boolean) => { @@ -536,7 +536,7 @@ const Chat = forwardRef((props, ref) => { type: APPLY_VIEW_TYPE, active: true, state: { - file: opFile, + file: opFile.path, oldContent: fileContent, newContent: appliedResult.content, onClose: (applied: boolean) => { diff --git a/src/components/chat-view/CustomModeView.tsx b/src/components/chat-view/CustomModeView.tsx index bb66226..6e3f37a 100644 --- a/src/components/chat-view/CustomModeView.tsx +++ b/src/components/chat-view/CustomModeView.tsx @@ -98,7 +98,7 @@ const CustomModeView = () => { setCustomInstructions(customMode.customInstructions || ''); setSelectedTools(customMode.groups); } else { - console.log("error, custom mode not found") + console.error("custom mode not found") } } }, [selectedMode, customModeList]); diff --git a/src/components/inline-edit/InlineEdit.tsx b/src/components/inline-edit/InlineEdit.tsx index beb1b03..b6530fa 100644 --- a/src/components/inline-edit/InlineEdit.tsx +++ b/src/components/inline-edit/InlineEdit.tsx @@ -11,7 +11,7 @@ import { removeAITags } from '../../utils/content-filter'; import { PromptGenerator } from '../../utils/prompt-generator'; type InlineEditProps = { - source: string; + source?: string; secStartLine: number; secEndLine: number; plugin: Plugin; @@ -173,20 +173,14 @@ export const InlineEdit: React.FC = ({ }; const parseSmartComposeBlock = (content: string) => { - const match = /]*>([\s\S]*?)<\/infio_block>/.exec(content); + const match = /([\s\S]*?)<\/response>/.exec(content); if (!match) { return null; } const blockContent = match[1].trim(); - const attributes = /startLine="(\d+)"/.exec(match[0]); - const startLine = attributes ? parseInt(attributes[1]) : undefined; - const endLineMatch = /endLine="(\d+)"/.exec(match[0]); - const endLine = endLineMatch ? parseInt(endLineMatch[1]) : undefined; return { - startLine, - endLine, content: blockContent, }; }; @@ -196,6 +190,7 @@ export const InlineEdit: React.FC = ({ try { const { activeFile, editor, selection } = await getActiveContext(); if (!activeFile || !editor || !selection) { + console.error("No active file, editor, or selection"); setIsSubmitting(false); return; } @@ -237,14 +232,26 @@ export const InlineEdit: React.FC = ({ response.choices[0].message.content ); const finalContent = parsedBlock?.content || response.choices[0].message.content; - const startLine = parsedBlock?.startLine || defaultStartLine; - const endLine = parsedBlock?.endLine || defaultEndLine; + + if (!activeFile || !(activeFile.path && typeof activeFile.path === 'string')) { + setIsSubmitting(false); + throw new Error("Invalid active file"); + } + + let fileContent: string; + try { + fileContent = await plugin.app.vault.cachedRead(activeFile); + } catch (error) { + console.error("Failed to read file:", error); + setIsSubmitting(false); + return; + } const updatedContent = await ApplyEditToFile( - await plugin.app.vault.cachedRead(activeFile), + fileContent, finalContent, - startLine, - endLine + defaultStartLine, + defaultEndLine ); if (!updatedContent) { @@ -258,7 +265,7 @@ export const InlineEdit: React.FC = ({ type: APPLY_VIEW_TYPE, active: true, state: { - file: activeFile, + file: activeFile.path, oldContent: removeAITags(oldContent), newContent: removeAITags(updatedContent), }, @@ -277,8 +284,8 @@ export const InlineEdit: React.FC = ({