mirror of
https://github.com/EthanMarti/infio-copilot.git
synced 2026-01-16 16:31:56 +00:00
update apply
This commit is contained in:
parent
dc7288e11b
commit
eeacc67d70
@ -40,7 +40,7 @@ export class ChatView extends ItemView {
|
|||||||
}
|
}
|
||||||
|
|
||||||
getDisplayText() {
|
getDisplayText() {
|
||||||
return 'Smart composer chat'
|
return 'Infio chat'
|
||||||
}
|
}
|
||||||
|
|
||||||
async onOpen() {
|
async onOpen() {
|
||||||
|
|||||||
@ -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,
|
||||||
@ -6,59 +6,59 @@ 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({
|
||||||
onApply,
|
onApply,
|
||||||
isApplying,
|
isApplying,
|
||||||
children,
|
children,
|
||||||
}: {
|
}: {
|
||||||
onApply: (blockInfo: {
|
onApply: (blockInfo: {
|
||||||
content: string
|
content: string
|
||||||
filename?: string
|
filename?: string
|
||||||
startLine?: number
|
startLine?: number
|
||||||
endLine?: number
|
endLine?: number
|
||||||
}) => void
|
}) => void
|
||||||
children: string
|
children: string
|
||||||
isApplying: boolean
|
isApplying: boolean
|
||||||
}) {
|
}) {
|
||||||
const blocks: ParsedinfioBlock[] = useMemo(
|
const blocks: ParsedinfioBlock[] = useMemo(
|
||||||
() => parseinfioBlocks(children),
|
() => parseinfioBlocks(children),
|
||||||
[children],
|
[children],
|
||||||
)
|
)
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
{blocks.map((block, index) =>
|
{blocks.map((block, index) =>
|
||||||
block.type === 'string' ? (
|
block.type === 'string' ? (
|
||||||
<Markdown key={index} className="infio-markdown">
|
<Markdown key={index} className="infio-markdown">
|
||||||
{block.content}
|
{block.content}
|
||||||
</Markdown>
|
</Markdown>
|
||||||
) : block.startLine && block.endLine && block.filename && block.action === 'reference' ? (
|
) : block.startLine && block.endLine && block.filename && block.action === 'reference' ? (
|
||||||
<MarkdownReferenceBlock
|
<MarkdownReferenceBlock
|
||||||
key={index}
|
key={index}
|
||||||
filename={block.filename}
|
filename={block.filename}
|
||||||
startLine={block.startLine}
|
startLine={block.startLine}
|
||||||
endLine={block.endLine}
|
endLine={block.endLine}
|
||||||
/>
|
/>
|
||||||
) : (
|
) : (
|
||||||
<MarkdownCodeComponent
|
<MarkdownActionBlock
|
||||||
key={index}
|
key={index}
|
||||||
onApply={onApply}
|
onApply={onApply}
|
||||||
isApplying={isApplying}
|
isApplying={isApplying}
|
||||||
language={block.language}
|
language={block.language}
|
||||||
filename={block.filename}
|
filename={block.filename}
|
||||||
startLine={block.startLine}
|
startLine={block.startLine}
|
||||||
endLine={block.endLine}
|
endLine={block.endLine}
|
||||||
action={block.action}
|
action={block.action}
|
||||||
>
|
>
|
||||||
{block.content}
|
{block.content}
|
||||||
</MarkdownCodeComponent>
|
</MarkdownActionBlock>
|
||||||
),
|
),
|
||||||
)}
|
)}
|
||||||
</>
|
</>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
export default React.memo(ReactMarkdown)
|
export default React.memo(ReactMarkdown)
|
||||||
|
|||||||
@ -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
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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(),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@ -1,41 +1,51 @@
|
|||||||
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,
|
||||||
currentFileContent: string,
|
currentFileContent: string,
|
||||||
startLine?: number,
|
startLine?: number,
|
||||||
endLine?: number,
|
endLine?: number,
|
||||||
): Promise<string | null> => {
|
): Promise<string | null> => {
|
||||||
console.log('Manual apply changes to file:', currentFile.path)
|
try {
|
||||||
console.log('Start line:', startLine)
|
// Input validation
|
||||||
console.log('End line:', endLine)
|
if (!content || !currentFileContent) {
|
||||||
console.log('Content:', content)
|
throw new Error('Content cannot be empty')
|
||||||
try {
|
}
|
||||||
if (!startLine || !endLine) {
|
|
||||||
console.error('Missing line numbers for edit')
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
|
|
||||||
// 直接替换指定行范围的内容
|
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
|
||||||
} catch (error) {
|
if (effectiveStartLine > effectiveEndLine) {
|
||||||
console.error('Error applying changes:', error)
|
throw new Error('Start line cannot be greater than end line')
|
||||||
return null
|
}
|
||||||
}
|
|
||||||
|
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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user