From b0dad76383ab379f6ba5251b46bcba64ca1d1f58 Mon Sep 17 00:00:00 2001 From: "qingyi.xjh" Date: Mon, 19 Aug 2024 16:48:24 +0800 Subject: [PATCH] chore: use enableMultiLine --- .../inline-completions.handler.ts | 11 +------- .../model/inlineCompletionRequestTask.ts | 6 +---- .../intelligent-completions.handler.ts | 27 +++++++++++-------- .../intelligent-completions.ts | 22 +++++---------- .../ai-native/ai-native.contribution.ts | 12 +++++---- 5 files changed, 31 insertions(+), 47 deletions(-) diff --git a/packages/ai-native/src/browser/contrib/inline-completions/inline-completions.handler.ts b/packages/ai-native/src/browser/contrib/inline-completions/inline-completions.handler.ts index 372f61a872..63a081040c 100644 --- a/packages/ai-native/src/browser/contrib/inline-completions/inline-completions.handler.ts +++ b/packages/ai-native/src/browser/contrib/inline-completions/inline-completions.handler.ts @@ -17,10 +17,7 @@ import { monacoApi } from '@opensumi/ide-monaco/lib/browser/monaco-api'; import { InlineCompletionContextKeys } from '@opensumi/monaco-editor-core/esm/vs/editor/contrib/inlineCompletions/browser/inlineCompletionContextKeys'; import { IAIMonacoContribHandler } from '../base'; -import { - IIntelligentCompletionsResult, - isMultiLineCompletion, -} from '../intelligent-completions/intelligent-completions'; +import { IIntelligentCompletionsResult } from '../intelligent-completions/intelligent-completions'; import { IntelligentCompletionsHandler } from '../intelligent-completions/intelligent-completions.handler'; import { AIInlineCompletionsProvider } from './completeProvider'; @@ -153,12 +150,6 @@ export class InlineCompletionHandler extends IAIMonacoContribHandler { this.aiInlineCompletionsProvider.provideInlineCompletionItems(model, position, context, token), ); - if (completionsResult && completionsResult.items.some((i) => isMultiLineCompletion(i))) { - this.intelligentCompletionsHandler.applyInlineDecorations(completionsResult); - // 此时用 multi line 的 decoration 来渲染,而 inline completion 则不显示 - return { items: [] }; - } - return completionsResult; }, freeInlineCompletions() {}, diff --git a/packages/ai-native/src/browser/contrib/inline-completions/model/inlineCompletionRequestTask.ts b/packages/ai-native/src/browser/contrib/inline-completions/model/inlineCompletionRequestTask.ts index 9e139b20d1..bab63de0dd 100644 --- a/packages/ai-native/src/browser/contrib/inline-completions/model/inlineCompletionRequestTask.ts +++ b/packages/ai-native/src/browser/contrib/inline-completions/model/inlineCompletionRequestTask.ts @@ -7,17 +7,13 @@ import { IAICompletionResultModel } from '@opensumi/ide-core-common'; import { AISerivceType, IAIReporter } from '@opensumi/ide-core-common/lib/types/ai-native/reporter'; import * as monaco from '@opensumi/ide-monaco'; -import { - IIntelligentCompletionsResult, - isMultiLineCompletion, -} from '../../intelligent-completions/intelligent-completions'; +import { IIntelligentCompletionsResult } from '../../intelligent-completions/intelligent-completions'; import { InlineCompletionItem } from '../model/competionModel'; import { PromptCache } from '../promptCache'; import { lineBasedPromptProcessor } from '../provider'; import { AICompletionsService } from '../service/ai-completions.service'; import { ICompletionContext } from '../types'; - const makeHashSet = (str: string) => { const set = new Set(); for (const char of str) { diff --git a/packages/ai-native/src/browser/contrib/intelligent-completions/intelligent-completions.handler.ts b/packages/ai-native/src/browser/contrib/intelligent-completions/intelligent-completions.handler.ts index f4f5a81217..1596e5529f 100644 --- a/packages/ai-native/src/browser/contrib/intelligent-completions/intelligent-completions.handler.ts +++ b/packages/ai-native/src/browser/contrib/intelligent-completions/intelligent-completions.handler.ts @@ -51,25 +51,30 @@ export class IntelligentCompletionsHandler extends Disposable { const position = this.monacoEditor.getPosition()!; const intelligentCompletionModel = await provider(this.monacoEditor, position, bean, this.cancelIndicator.token); + if ( + intelligentCompletionModel && + intelligentCompletionModel.enableMultiLine && + intelligentCompletionModel.items.length > 0 + ) { + return this.applyInlineDecorations(intelligentCompletionModel); + } + return intelligentCompletionModel; } public applyInlineDecorations(completionModel: IIntelligentCompletionsResult) { const { items } = completionModel; - if (items.length === 0) { - return; - } const position = this.monacoEditor.getPosition()!; const model = this.monacoEditor.getModel(); - const { belowRadius, aboveRadius, insertText } = items[0]; - - const originalContent = model?.getValueInRange({ - startLineNumber: position.lineNumber - (aboveRadius || 0), - startColumn: 1, - endLineNumber: position.lineNumber + (belowRadius || 0), - endColumn: model.getLineMaxColumn(position.lineNumber + (belowRadius || 0)), - }); + const { range, insertText } = items[0]; + + // 如果只是开启了 enableMultiLine 而没有传递 range ,则不显示 multi line + if (!range) { + return completionModel; + } + + const originalContent = model?.getValueInRange(range); const diffComputerResult = this.multiLineDiffComputer.diff(originalContent!, insertText.toString()); diff --git a/packages/ai-native/src/browser/contrib/intelligent-completions/intelligent-completions.ts b/packages/ai-native/src/browser/contrib/intelligent-completions/intelligent-completions.ts index 5663fdcb65..594c3df4a6 100644 --- a/packages/ai-native/src/browser/contrib/intelligent-completions/intelligent-completions.ts +++ b/packages/ai-native/src/browser/contrib/intelligent-completions/intelligent-completions.ts @@ -1,25 +1,15 @@ -import { isUndefined } from '@opensumi/ide-core-common'; import { InlineCompletion } from '@opensumi/ide-monaco'; -export interface IIntelligentCompletionItem extends InlineCompletion { +export interface IIntelligentCompletionsResult { + readonly items: InlineCompletion[]; /** - * 表示当前行之前需要再补全几行 - * 最大值为 3 + * 是否开启多行补全 + * 开启后,items 中的 range 必填 + * 否则显示默认的 inline completion */ - aboveRadius?: number; - /** - * 表示当前行之后需要再补全几行 - * 最大值为 3 - */ - belowRadius?: number; -} - -export interface IIntelligentCompletionsResult { - readonly items: IIntelligentCompletionItem[]; + readonly enableMultiLine?: boolean | undefined; /** * 定义的额外信息 */ extra?: T; } - -export const isMultiLineCompletion = (item: IIntelligentCompletionItem) => !isUndefined(item.aboveRadius) || !isUndefined(item.belowRadius); diff --git a/packages/startup/entry/sample-modules/ai-native/ai-native.contribution.ts b/packages/startup/entry/sample-modules/ai-native/ai-native.contribution.ts index ae86151e40..853ff20dea 100644 --- a/packages/startup/entry/sample-modules/ai-native/ai-native.contribution.ts +++ b/packages/startup/entry/sample-modules/ai-native/ai-native.contribution.ts @@ -456,13 +456,15 @@ export class AINativeContribution implements AINativeCoreContribution { items: [ { insertText: insertRandomStrings(value), - belowRadius: 3, - aboveRadius: 0, - }, - { - insertText: insertRandomStrings(value), + range: { + startLineNumber: position.lineNumber, + startColumn: 1, + endLineNumber: position.lineNumber + 3, + endColumn: model?.getLineMaxColumn(position.lineNumber + 3), + }, }, ], + enableMultiLine: true, }; } catch (error) { if (error.name === 'AbortError') {