From eeacc67d70d8503cf765d8f588f257bc67c00dd0 Mon Sep 17 00:00:00 2001 From: duanfuxiang Date: Mon, 6 Jan 2025 16:05:35 +0800 Subject: [PATCH] update apply --- src/ChatView.tsx | 2 +- ...eComponent.tsx => MarkdownActionBlock.tsx} | 2 +- src/components/chat-view/ReactMarkdown.tsx | 94 +++++++++---------- src/database/database-manager.ts | 6 +- src/main.ts | 2 +- src/utils/apply.ts | 78 ++++++++------- 6 files changed, 95 insertions(+), 89 deletions(-) rename src/components/chat-view/{MarkdownCodeComponent.tsx => MarkdownActionBlock.tsx} (98%) diff --git a/src/ChatView.tsx b/src/ChatView.tsx index d455b44..65f9da1 100644 --- a/src/ChatView.tsx +++ b/src/ChatView.tsx @@ -40,7 +40,7 @@ export class ChatView extends ItemView { } getDisplayText() { - return 'Smart composer chat' + return 'Infio chat' } async onOpen() { diff --git a/src/components/chat-view/MarkdownCodeComponent.tsx b/src/components/chat-view/MarkdownActionBlock.tsx similarity index 98% rename from src/components/chat-view/MarkdownCodeComponent.tsx rename to src/components/chat-view/MarkdownActionBlock.tsx index 2699735..ed9fa8d 100644 --- a/src/components/chat-view/MarkdownCodeComponent.tsx +++ b/src/components/chat-view/MarkdownActionBlock.tsx @@ -5,7 +5,7 @@ import { useDarkModeContext } from '../../contexts/DarkModeContext' import { MemoizedSyntaxHighlighterWrapper } from './SyntaxHighlighterWrapper' -export default function MarkdownCodeComponent({ +export default function MarkdownActionBlock({ onApply, isApplying, language, diff --git a/src/components/chat-view/ReactMarkdown.tsx b/src/components/chat-view/ReactMarkdown.tsx index a56fa50..d5c787e 100644 --- a/src/components/chat-view/ReactMarkdown.tsx +++ b/src/components/chat-view/ReactMarkdown.tsx @@ -6,59 +6,59 @@ import { parseinfioBlocks, } from '../../utils/parse-infio-block' -import MarkdownCodeComponent from './MarkdownCodeComponent' +import MarkdownActionBlock from './MarkdownActionBlock' import MarkdownReferenceBlock from './MarkdownReferenceBlock' function ReactMarkdown({ - onApply, - isApplying, - children, + onApply, + isApplying, + children, }: { - onApply: (blockInfo: { - content: string - filename?: string - startLine?: number - endLine?: number - }) => void - children: string - isApplying: boolean + onApply: (blockInfo: { + content: string + filename?: string + startLine?: number + endLine?: number + }) => void + children: string + isApplying: boolean }) { - const blocks: ParsedinfioBlock[] = useMemo( - () => parseinfioBlocks(children), - [children], - ) + const blocks: ParsedinfioBlock[] = useMemo( + () => parseinfioBlocks(children), + [children], + ) - return ( - <> - {blocks.map((block, index) => - block.type === 'string' ? ( - - {block.content} - - ) : block.startLine && block.endLine && block.filename && block.action === 'reference' ? ( - - ) : ( - - {block.content} - - ), - )} - - ) + return ( + <> + {blocks.map((block, index) => + block.type === 'string' ? ( + + {block.content} + + ) : block.startLine && block.endLine && block.filename && block.action === 'reference' ? ( + + ) : ( + + {block.content} + + ), + )} + + ) } export default React.memo(ReactMarkdown) diff --git a/src/database/database-manager.ts b/src/database/database-manager.ts index 3eeefb7..753b307 100644 --- a/src/database/database-manager.ts +++ b/src/database/database-manager.ts @@ -38,14 +38,10 @@ export class DBManager { dbManager.templateManager = new TemplateManager(app, dbManager) dbManager.conversationManager = new ConversationManager(app, dbManager) - console.log('Smart composer database initialized.') + console.log('infio database initialized.') return dbManager } - // getDb() { - // return this.db - // } - getPgClient() { return this.db } diff --git a/src/main.ts b/src/main.ts index 9ae2b20..e109202 100644 --- a/src/main.ts +++ b/src/main.ts @@ -42,7 +42,7 @@ export default class InfioPlugin extends Plugin { await this.loadSettings() // This creates an icon in the left ribbon. - this.addRibbonIcon('wand-sparkles', 'Open smart composer', () => + this.addRibbonIcon('wand-sparkles', 'Open infio copilot', () => this.openChatView(), ) diff --git a/src/utils/apply.ts b/src/utils/apply.ts index 13faf9f..0c85006 100644 --- a/src/utils/apply.ts +++ b/src/utils/apply.ts @@ -1,41 +1,51 @@ import { TFile } from 'obsidian' -// 替换指定行范围的内容 -const replaceLines = (content: string, startLine: number, endLine: number, newContent: string): string => { - const lines = content.split('\n') - const beforeLines = lines.slice(0, startLine - 1) - const afterLines = lines.slice(endLine) // 这里不需要 +1 因为 endLine 指向的是要替换的最后一行 - return [...beforeLines, newContent, ...afterLines].join('\n') -} - +/** + * Applies changes to a file by replacing content within specified line range + * @param content - The new content to insert + * @param currentFile - The file being modified + * @param currentFileContent - 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 manualApplyChangesToFile = async ( - content: string, - currentFile: TFile, - currentFileContent: string, - startLine?: number, - endLine?: number, + content: string, + currentFile: TFile, + currentFileContent: string, + startLine?: number, + endLine?: number, ): Promise => { - console.log('Manual apply changes to file:', currentFile.path) - console.log('Start line:', startLine) - console.log('End line:', endLine) - console.log('Content:', content) - try { - if (!startLine || !endLine) { - console.error('Missing line numbers for edit') - return null - } + try { + // Input validation + if (!content || !currentFileContent) { + throw new Error('Content cannot be empty') + } - // 直接替换指定行范围的内容 - const newContent = replaceLines( - currentFileContent, - startLine, - endLine, - content - ) + const lines = currentFileContent.split('\n') + const effectiveStartLine = Math.max(1, startLine ?? 1) + const effectiveEndLine = Math.min(endLine ?? lines.length, lines.length) - return newContent - } catch (error) { - console.error('Error applying changes:', error) - return null - } + // Validate line numbers + if (effectiveStartLine > effectiveEndLine) { + throw new Error('Start line cannot be greater than end line') + } + + console.log('Applying changes to file:', { + path: currentFile.path, + startLine: effectiveStartLine, + endLine: effectiveEndLine, + contentLength: content.length + }) + + // Construct new content + return [ + ...lines.slice(0, effectiveStartLine - 1), + content, + ...lines.slice(effectiveEndLine) + ].join('\n') + } catch (error) { + console.error('Error applying changes:', error instanceof Error ? error.message : 'Unknown error') + return null + } }