update apply

This commit is contained in:
duanfuxiang 2025-01-06 16:05:35 +08:00
parent dc7288e11b
commit eeacc67d70
6 changed files with 95 additions and 89 deletions

View File

@ -40,7 +40,7 @@ export class ChatView extends ItemView {
} }
getDisplayText() { getDisplayText() {
return 'Smart composer chat' return 'Infio chat'
} }
async onOpen() { async onOpen() {

View File

@ -5,7 +5,7 @@ import { useDarkModeContext } from '../../contexts/DarkModeContext'
import { MemoizedSyntaxHighlighterWrapper } from './SyntaxHighlighterWrapper' import { MemoizedSyntaxHighlighterWrapper } from './SyntaxHighlighterWrapper'
export default function MarkdownCodeComponent({ export default function MarkdownActionBlock({
onApply, onApply,
isApplying, isApplying,
language, language,

View File

@ -6,7 +6,7 @@ import {
parseinfioBlocks, parseinfioBlocks,
} from '../../utils/parse-infio-block' } from '../../utils/parse-infio-block'
import MarkdownCodeComponent from './MarkdownCodeComponent' import MarkdownActionBlock from './MarkdownActionBlock'
import MarkdownReferenceBlock from './MarkdownReferenceBlock' import MarkdownReferenceBlock from './MarkdownReferenceBlock'
function ReactMarkdown({ function ReactMarkdown({
@ -43,7 +43,7 @@ function ReactMarkdown({
endLine={block.endLine} endLine={block.endLine}
/> />
) : ( ) : (
<MarkdownCodeComponent <MarkdownActionBlock
key={index} key={index}
onApply={onApply} onApply={onApply}
isApplying={isApplying} isApplying={isApplying}
@ -54,7 +54,7 @@ function ReactMarkdown({
action={block.action} action={block.action}
> >
{block.content} {block.content}
</MarkdownCodeComponent> </MarkdownActionBlock>
), ),
)} )}
</> </>

View File

@ -38,14 +38,10 @@ export class DBManager {
dbManager.templateManager = new TemplateManager(app, dbManager) dbManager.templateManager = new TemplateManager(app, dbManager)
dbManager.conversationManager = new ConversationManager(app, dbManager) dbManager.conversationManager = new ConversationManager(app, dbManager)
console.log('Smart composer database initialized.') console.log('infio database initialized.')
return dbManager return dbManager
} }
// getDb() {
// return this.db
// }
getPgClient() { getPgClient() {
return this.db return this.db
} }

View File

@ -42,7 +42,7 @@ export default class InfioPlugin extends Plugin {
await this.loadSettings() await this.loadSettings()
// This creates an icon in the left ribbon. // This creates an icon in the left ribbon.
this.addRibbonIcon('wand-sparkles', 'Open smart composer', () => this.addRibbonIcon('wand-sparkles', 'Open infio copilot', () =>
this.openChatView(), this.openChatView(),
) )

View File

@ -1,13 +1,14 @@
import { TFile } from 'obsidian' import { TFile } from 'obsidian'
// 替换指定行范围的内容 /**
const replaceLines = (content: string, startLine: number, endLine: number, newContent: string): string => { * Applies changes to a file by replacing content within specified line range
const lines = content.split('\n') * @param content - The new content to insert
const beforeLines = lines.slice(0, startLine - 1) * @param currentFile - The file being modified
const afterLines = lines.slice(endLine) // 这里不需要 +1 因为 endLine 指向的是要替换的最后一行 * @param currentFileContent - The current content of the file
return [...beforeLines, newContent, ...afterLines].join('\n') * @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 ( export const manualApplyChangesToFile = async (
content: string, content: string,
currentFile: TFile, currentFile: TFile,
@ -15,27 +16,36 @@ export const manualApplyChangesToFile = async (
startLine?: number, startLine?: number,
endLine?: number, endLine?: number,
): Promise<string | null> => { ): Promise<string | null> => {
console.log('Manual apply changes to file:', currentFile.path)
console.log('Start line:', startLine)
console.log('End line:', endLine)
console.log('Content:', content)
try { try {
if (!startLine || !endLine) { // Input validation
console.error('Missing line numbers for edit') if (!content || !currentFileContent) {
return null throw new Error('Content cannot be empty')
} }
// 直接替换指定行范围的内容 const lines = currentFileContent.split('\n')
const newContent = replaceLines( const effectiveStartLine = Math.max(1, startLine ?? 1)
currentFileContent, const effectiveEndLine = Math.min(endLine ?? lines.length, lines.length)
startLine,
endLine,
content
)
return newContent // 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) { } catch (error) {
console.error('Error applying changes:', error) console.error('Error applying changes:', error instanceof Error ? error.message : 'Unknown error')
return null return null
} }
} }