Skip to content

Commit

Permalink
chore: use enableMultiLine
Browse files Browse the repository at this point in the history
  • Loading branch information
Ricbet committed Aug 19, 2024
1 parent 6b35cae commit b0dad76
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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() {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>();
for (const char of str) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down
Original file line number Diff line number Diff line change
@@ -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<T = any> {
readonly items: InlineCompletion[];
/**
* 表示当前行之前需要再补全几行
* 最大值为 3
* 是否开启多行补全
* 开启后,items 中的 range 必填
* 否则显示默认的 inline completion
*/
aboveRadius?: number;
/**
* 表示当前行之后需要再补全几行
* 最大值为 3
*/
belowRadius?: number;
}

export interface IIntelligentCompletionsResult<T = any> {
readonly items: IIntelligentCompletionItem[];
readonly enableMultiLine?: boolean | undefined;
/**
* 定义的额外信息
*/
extra?: T;
}

export const isMultiLineCompletion = (item: IIntelligentCompletionItem) => !isUndefined(item.aboveRadius) || !isUndefined(item.belowRadius);
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down

0 comments on commit b0dad76

Please sign in to comment.