Skip to content

Commit

Permalink
feat: support typing code edits source
Browse files Browse the repository at this point in the history
  • Loading branch information
Ricbet committed Jan 13, 2025
1 parent b367234 commit 29d1355
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 7 deletions.
4 changes: 4 additions & 0 deletions packages/ai-native/src/browser/ai-core.contribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,10 @@ export class AINativeBrowserContribution
id: AINativeSettingSectionsId.CodeEditsLineChange,
localized: 'preference.ai.native.codeEdits.lineChange',
},
{
id: AINativeSettingSectionsId.CodeEditsTyping,
localized: 'preference.ai.native.codeEdits.typing',
},
],
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Disposable, ECodeEditsSourceTyping } from '@opensumi/ide-core-common';
import { IPosition, IRange, InlineCompletion } from '@opensumi/ide-monaco';
import { IModelContentChangedEvent, IPosition, IRange, InlineCompletion } from '@opensumi/ide-monaco';

import type { ILineChangeData } from './source/line-change.source';
import type { ILinterErrorData } from './source/lint-error.source';
Expand All @@ -14,7 +14,8 @@ export interface IIntelligentCompletionsResult<T = any> {

export type ICodeEditsContextBean =
| { typing: ECodeEditsSourceTyping.LinterErrors; position: IPosition; data: ILinterErrorData }
| { typing: ECodeEditsSourceTyping.LineChange; position: IPosition; data: ILineChangeData };
| { typing: ECodeEditsSourceTyping.LineChange; position: IPosition; data: ILineChangeData }
| { typing: ECodeEditsSourceTyping.Typing; position: IPosition; data: IModelContentChangedEvent };

export interface ICodeEdit {
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import { IntelligentCompletionsRegistry } from './intelligent-completions.featur
import { CodeEditsSourceCollection } from './source/base';
import { LineChangeCodeEditsSource } from './source/line-change.source';
import { LintErrorCodeEditsSource } from './source/lint-error.source';
import { TypingCodeEditsSource } from './source/typing.source';

import { CodeEditsResultValue } from './index';

Expand Down Expand Up @@ -95,7 +96,7 @@ export class IntelligentCompletionsController extends BaseAIMonacoEditorControll
this.additionsDeletionsDecorationModel = new AdditionsDeletionsDecorationModel(this.monacoEditor);
this.aiNativeContextKey = this.injector.get(AINativeContextKey, [this.monacoEditor.contextKeyService]);
this.codeEditsSourceCollection = this.injector.get(CodeEditsSourceCollection, [
[LintErrorCodeEditsSource, LineChangeCodeEditsSource],
[LintErrorCodeEditsSource, LineChangeCodeEditsSource, TypingCodeEditsSource],
this.monacoEditor,
]);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { Injectable } from '@opensumi/di';
import { AINativeSettingSectionsId, ECodeEditsSourceTyping, IDisposable } from '@opensumi/ide-core-common';
import { IModelContentChangedEvent, Position } from '@opensumi/ide-monaco';

import { BaseCodeEditsSource } from './base';

@Injectable({ multiple: true })
export class TypingCodeEditsSource extends BaseCodeEditsSource {
public priority = 0;

public mount(): IDisposable {
this.addDispose(
this.monacoEditor.onDidChangeModelContent((event: IModelContentChangedEvent) => {
const position = this.monacoEditor.getPosition();
if (position) {
this.doTrigger(position, event);
}
}),
);
return this;
}

protected async doTrigger(position: Position, data: IModelContentChangedEvent) {
const isTypingEnabled = this.preferenceService.getValid(AINativeSettingSectionsId.CodeEditsTyping, false);

if (!isTypingEnabled || !this.model) {
return;
}

this.setBean({
typing: ECodeEditsSourceTyping.Typing,
position,
data,
});
}
}
4 changes: 4 additions & 0 deletions packages/ai-native/src/browser/preferences/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,9 @@ export const aiNativePreferenceSchema: PreferenceSchema = {
type: 'boolean',
default: false,
},
[AINativeSettingSectionsId.CodeEditsTyping]: {
type: 'boolean',
default: false,
},
},
};
1 change: 1 addition & 0 deletions packages/core-common/src/settings/ai-native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export enum AINativeSettingSectionsId {
*/
CodeEditsLintErrors = 'ai.native.codeEdits.lintErrors',
CodeEditsLineChange = 'ai.native.codeEdits.lineChange',
CodeEditsTyping = 'ai.native.codeEdits.typing',
}
export const AI_NATIVE_SETTING_GROUP_ID = 'AI-Native';
export const AI_NATIVE_SETTING_GROUP_TITLE = 'AI Native';
1 change: 1 addition & 0 deletions packages/core-common/src/types/ai-native/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -354,5 +354,6 @@ export interface IHistoryChatMessage extends IChatMessage {
export enum ECodeEditsSourceTyping {
LinterErrors = 'lint_errors',
LineChange = 'line_change',
Typing = 'typing',
}
// ## Code Edits ends ##
6 changes: 4 additions & 2 deletions packages/i18n/src/common/en-US.lang.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1504,8 +1504,10 @@ export const localizationBundle = {
'preference.ai.native.intelligentCompletions.alwaysVisible': 'Whether to always show intelligent completions',

'preference.ai.native.codeEdits.title': 'Code Edits',
'preference.ai.native.codeEdits.lintErrors': 'Whether to enable intelligent rewriting of Lint Errors',
'preference.ai.native.codeEdits.lineChange': 'Whether to enable intelligent rewriting of Line Change',
'preference.ai.native.codeEdits.lintErrors': 'Whether to trigger intelligent rewriting when Lint Error occurs',
'preference.ai.native.codeEdits.lineChange':
'Whether to trigger intelligent rewriting when the cursor line number changes',
'preference.ai.native.codeEdits.typing': 'Whether to trigger intelligent rewriting when the content changes',
// #endregion AI Native

// #endregion merge editor
Expand Down
5 changes: 3 additions & 2 deletions packages/i18n/src/common/zh-CN.lang.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1271,8 +1271,9 @@ export const localizationBundle = {
'preference.ai.native.intelligentCompletions.alwaysVisible': '是否总是展示智能补全',

'preference.ai.native.codeEdits.title': '智能改写',
'preference.ai.native.codeEdits.lintErrors': '是否开启对 Lint Error 类型的智能改写',
'preference.ai.native.codeEdits.lineChange': '是否开启对 Line Change 类型的智能改写',
'preference.ai.native.codeEdits.lintErrors': '是否在发生 Lint Error 时触发智能改写',
'preference.ai.native.codeEdits.lineChange': '是否在光标行号发生变化时触发智能改写',
'preference.ai.native.codeEdits.typing': '是否在内容发生变化时触发智能改写',
// #endregion AI Native

'webview.webviewTagUnavailable': '非 Electron 环境不支持 webview 标签,请使用 iframe 标签',
Expand Down

0 comments on commit 29d1355

Please sign in to comment.