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() {
return 'Smart composer chat'
return 'Infio chat'
}
async onOpen() {

View File

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

View File

@ -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' ? (
<Markdown key={index} className="infio-markdown">
{block.content}
</Markdown>
) : block.startLine && block.endLine && block.filename && block.action === 'reference' ? (
<MarkdownReferenceBlock
key={index}
filename={block.filename}
startLine={block.startLine}
endLine={block.endLine}
/>
) : (
<MarkdownCodeComponent
key={index}
onApply={onApply}
isApplying={isApplying}
language={block.language}
filename={block.filename}
startLine={block.startLine}
endLine={block.endLine}
action={block.action}
>
{block.content}
</MarkdownCodeComponent>
),
)}
</>
)
return (
<>
{blocks.map((block, index) =>
block.type === 'string' ? (
<Markdown key={index} className="infio-markdown">
{block.content}
</Markdown>
) : block.startLine && block.endLine && block.filename && block.action === 'reference' ? (
<MarkdownReferenceBlock
key={index}
filename={block.filename}
startLine={block.startLine}
endLine={block.endLine}
/>
) : (
<MarkdownActionBlock
key={index}
onApply={onApply}
isApplying={isApplying}
language={block.language}
filename={block.filename}
startLine={block.startLine}
endLine={block.endLine}
action={block.action}
>
{block.content}
</MarkdownActionBlock>
),
)}
</>
)
}
export default React.memo(ReactMarkdown)

View File

@ -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
}

View File

@ -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(),
)

View File

@ -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<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 {
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
}
}