From d7142f95998d3eb677c5593ea9a195c8986da51f Mon Sep 17 00:00:00 2001 From: John Date: Thu, 7 Nov 2024 15:49:54 +0800 Subject: [PATCH] feat: support code edits perferences (#4155) --- .../ai-native/src/browser/ai-core.contribution.ts | 13 +++++++++++++ .../contrib/intelligent-completions/source/base.ts | 4 ++++ .../source/line-change.source.ts | 6 ++++-- .../source/lint-error.source.ts | 12 ++++++++++-- .../ai-native/src/browser/preferences/schema.ts | 8 ++++++++ packages/core-common/src/settings/ai-native.ts | 6 ++++++ packages/i18n/src/common/en-US.lang.ts | 4 ++++ packages/i18n/src/common/zh-CN.lang.ts | 4 ++++ 8 files changed, 53 insertions(+), 4 deletions(-) diff --git a/packages/ai-native/src/browser/ai-core.contribution.ts b/packages/ai-native/src/browser/ai-core.contribution.ts index 5b85a47c7a..cc1dfa4c4e 100644 --- a/packages/ai-native/src/browser/ai-core.contribution.ts +++ b/packages/ai-native/src/browser/ai-core.contribution.ts @@ -354,6 +354,19 @@ export class AINativeBrowserContribution }, ], }); + registry.registerSettingSection(AI_NATIVE_SETTING_GROUP_ID, { + title: localize('preference.ai.native.codeEdits.title'), + preferences: [ + { + id: AINativeSettingSectionsId.CodeEditsLintErrors, + localized: 'preference.ai.native.codeEdits.lintErrors', + }, + { + id: AINativeSettingSectionsId.CodeEditsLineChange, + localized: 'preference.ai.native.codeEdits.lineChange', + }, + ], + }); } if (this.aiNativeConfigService.capabilities.supportsInlineChat) { diff --git a/packages/ai-native/src/browser/contrib/intelligent-completions/source/base.ts b/packages/ai-native/src/browser/contrib/intelligent-completions/source/base.ts index 17db91ff2b..8045fb641e 100644 --- a/packages/ai-native/src/browser/contrib/intelligent-completions/source/base.ts +++ b/packages/ai-native/src/browser/contrib/intelligent-completions/source/base.ts @@ -1,4 +1,5 @@ import { Autowired, INJECTOR_TOKEN, Injectable, Injector, Optional } from '@opensumi/di'; +import { PreferenceService } from '@opensumi/ide-core-browser'; import { AIServiceType, CodeEditsRT, @@ -63,6 +64,9 @@ export abstract class BaseCodeEditsSource extends Disposable { @Autowired(IAIReporter) private aiReporter: IAIReporter; + @Autowired(PreferenceService) + protected preferenceService: PreferenceService; + private cancellationTokenSource = new CancellationTokenSource(); private readonly relationID = observableValue(this, undefined); diff --git a/packages/ai-native/src/browser/contrib/intelligent-completions/source/line-change.source.ts b/packages/ai-native/src/browser/contrib/intelligent-completions/source/line-change.source.ts index a997710982..c5ed4de04c 100644 --- a/packages/ai-native/src/browser/contrib/intelligent-completions/source/line-change.source.ts +++ b/packages/ai-native/src/browser/contrib/intelligent-completions/source/line-change.source.ts @@ -1,5 +1,5 @@ import { Injectable } from '@opensumi/di'; -import { ECodeEditsSourceTyping, IDisposable } from '@opensumi/ide-core-common'; +import { AINativeSettingSectionsId, ECodeEditsSourceTyping, IDisposable } from '@opensumi/ide-core-common'; import { ICursorPositionChangedEvent, Position } from '@opensumi/ide-monaco'; import { BaseCodeEditsSource } from './base'; @@ -30,7 +30,9 @@ export class LineChangeCodeEditsSource extends BaseCodeEditsSource { private lastEditTime: number | null = null; protected doTrigger(position: Position) { - if (!position) { + const isLineChangeEnabled = this.preferenceService.getValid(AINativeSettingSectionsId.CodeEditsLineChange, false); + + if (!isLineChangeEnabled || !position) { return; } diff --git a/packages/ai-native/src/browser/contrib/intelligent-completions/source/lint-error.source.ts b/packages/ai-native/src/browser/contrib/intelligent-completions/source/lint-error.source.ts index 71cfa347f8..4e2d12f6c5 100644 --- a/packages/ai-native/src/browser/contrib/intelligent-completions/source/lint-error.source.ts +++ b/packages/ai-native/src/browser/contrib/intelligent-completions/source/lint-error.source.ts @@ -1,5 +1,11 @@ import { Autowired, Injectable } from '@opensumi/di'; -import { ECodeEditsSourceTyping, Event, FRAME_THREE, IDisposable } from '@opensumi/ide-core-common'; +import { + AINativeSettingSectionsId, + ECodeEditsSourceTyping, + Event, + FRAME_THREE, + IDisposable, +} from '@opensumi/ide-core-common'; import { ICursorPositionChangedEvent, IPosition, Position } from '@opensumi/ide-monaco'; import { URI } from '@opensumi/ide-monaco/lib/browser/monaco-api'; import { IWorkspaceService } from '@opensumi/ide-workspace'; @@ -85,7 +91,9 @@ export class LintErrorCodeEditsSource extends BaseCodeEditsSource { } protected async doTrigger(position: Position) { - if (!this.model) { + const isLintErrorsEnabled = this.preferenceService.getValid(AINativeSettingSectionsId.CodeEditsLintErrors, false); + + if (!isLintErrorsEnabled || !this.model) { return; } diff --git a/packages/ai-native/src/browser/preferences/schema.ts b/packages/ai-native/src/browser/preferences/schema.ts index 9cd6161b5d..d41812f9ee 100644 --- a/packages/ai-native/src/browser/preferences/schema.ts +++ b/packages/ai-native/src/browser/preferences/schema.ts @@ -50,5 +50,13 @@ export const aiNativePreferenceSchema: PreferenceSchema = { type: 'boolean', default: false, }, + [AINativeSettingSectionsId.CodeEditsLintErrors]: { + type: 'boolean', + default: false, + }, + [AINativeSettingSectionsId.CodeEditsLineChange]: { + type: 'boolean', + default: false, + }, }, }; diff --git a/packages/core-common/src/settings/ai-native.ts b/packages/core-common/src/settings/ai-native.ts index e6d2c5f32b..a034cd9f52 100644 --- a/packages/core-common/src/settings/ai-native.ts +++ b/packages/core-common/src/settings/ai-native.ts @@ -16,6 +16,12 @@ export enum AINativeSettingSectionsId { IntelligentCompletionsDebounceTime = 'ai.native.intelligentCompletions.debounceTime', IntelligentCompletionsCacheEnabled = 'ai.native.intelligentCompletions.cache.enabled', IntelligentCompletionsAlwaysVisible = 'ai.native.intelligentCompletions.alwaysVisible', + + /** + * Code edits settings + */ + CodeEditsLintErrors = 'ai.native.codeEdits.lintErrors', + CodeEditsLineChange = 'ai.native.codeEdits.lineChange', } export const AI_NATIVE_SETTING_GROUP_ID = 'AI-Native'; export const AI_NATIVE_SETTING_GROUP_TITLE = 'AI Native'; diff --git a/packages/i18n/src/common/en-US.lang.ts b/packages/i18n/src/common/en-US.lang.ts index 4662905d96..4c53ad1568 100644 --- a/packages/i18n/src/common/en-US.lang.ts +++ b/packages/i18n/src/common/en-US.lang.ts @@ -1495,6 +1495,10 @@ export const localizationBundle = { 'preference.ai.native.intelligentCompletions.debounceTime': 'Debounce time for intelligent completions', 'preference.ai.native.intelligentCompletions.cache.enabled': 'Whether to enable cache for intelligent completions', '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', // #endregion AI Native // #endregion merge editor diff --git a/packages/i18n/src/common/zh-CN.lang.ts b/packages/i18n/src/common/zh-CN.lang.ts index 00c6e363b0..17fe721a7b 100644 --- a/packages/i18n/src/common/zh-CN.lang.ts +++ b/packages/i18n/src/common/zh-CN.lang.ts @@ -1260,6 +1260,10 @@ export const localizationBundle = { 'preference.ai.native.intelligentCompletions.debounceTime': '智能补全的延迟时间(毫秒)', 'preference.ai.native.intelligentCompletions.cache.enabled': '是否启用智能补全的缓存', 'preference.ai.native.intelligentCompletions.alwaysVisible': '是否总是展示智能补全', + + 'preference.ai.native.codeEdits.title': '智能改写', + 'preference.ai.native.codeEdits.lintErrors': '是否开启对 Lint Error 类型的智能改写', + 'preference.ai.native.codeEdits.lineChange': '是否开启对 Line Change 类型的智能改写', // #endregion AI Native 'webview.webviewTagUnavailable': '非 Electron 环境不支持 webview 标签,请使用 iframe 标签',