Skip to content

Commit

Permalink
fix: ai editor feature contribution duplicate registration (#3690)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ricbet committed May 17, 2024
1 parent 4d40f20 commit abed659
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 16 deletions.
10 changes: 5 additions & 5 deletions packages/ai-native/src/browser/ai-core.contribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import {
} from '@opensumi/ide-core-browser/lib/ai-native/command';
import { InlineChatIsVisible } from '@opensumi/ide-core-browser/lib/contextkey/ai-native';
import { DesignLayoutConfig } from '@opensumi/ide-core-browser/lib/layout/constants';
import { TerminalRegistryToken } from '@opensumi/ide-core-common';
import { Disposable, Schemes, TerminalRegistryToken } from '@opensumi/ide-core-common';
import {
AI_NATIVE_SETTING_GROUP_TITLE,
ChatFeatureRegistryToken,
Expand Down Expand Up @@ -154,6 +154,9 @@ export class AINativeBrowserContribution
@Autowired(ChatProxyServiceToken)
private readonly chatProxyService: ChatProxyService;

@Autowired(AIEditorContribution)
private readonly aiEditorFeatureContribution: AIEditorContribution;

constructor() {
this.registerFeature();
}
Expand Down Expand Up @@ -258,10 +261,7 @@ export class AINativeBrowserContribution

registerEditorFeature(registry: IEditorFeatureRegistry): void {
registry.registerEditorFeatureContribution({
contribute: (editor: IEditor) => {
const aiEditorContribution = this.injector.get(AIEditorContribution, [editor]);
return aiEditorContribution.contribute(editor);
},
contribute: (editor: IEditor) => this.aiEditorFeatureContribution.contribute(editor),
});
}

Expand Down
42 changes: 31 additions & 11 deletions packages/ai-native/src/browser/ai-editor.contribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ import {
runWhenIdle,
} from '@opensumi/ide-core-common';
import { DesignBrowserCtxMenuService } from '@opensumi/ide-design/lib/browser/override/menu.service';
import { WorkbenchEditorService } from '@opensumi/ide-editor';
import { EditorSelectionChangeEvent, IEditor, IEditorFeatureContribution } from '@opensumi/ide-editor/lib/browser';
import { BrowserCodeEditor } from '@opensumi/ide-editor/lib/browser/editor-collection.service';
import { WorkbenchEditorServiceImpl } from '@opensumi/ide-editor/lib/browser/workbench-editor.service';
import * as monaco from '@opensumi/ide-monaco';
import { monaco as monacoApi } from '@opensumi/ide-monaco/lib/browser/monaco-api';
import { languageFeaturesService } from '@opensumi/ide-monaco/lib/browser/monaco-api/languages';
Expand Down Expand Up @@ -88,13 +91,16 @@ export class AIEditorContribution extends Disposable implements IEditorFeatureCo
private aiCompletionsService: AICompletionsService;

@Autowired(IEventBus)
protected eventBus: IEventBus;
private eventBus: IEventBus;

@Autowired(LanguageParserService)
protected languageParserService: LanguageParserService;
private languageParserService: LanguageParserService;

@Autowired(WorkbenchEditorService)
private workbenchEditorService: WorkbenchEditorServiceImpl;

@Autowired()
monacoTelemetryService: MonacoTelemetryService;
private monacoTelemetryService: MonacoTelemetryService;

private latestMiddlewareCollector: IAIMiddleware;

Expand All @@ -112,6 +118,7 @@ export class AIEditorContribution extends Disposable implements IEditorFeatureCo
private aiInlineChatOperationDisposed: Disposable = new Disposable();

private modelSessionDisposable: Disposable;
private initialized: boolean = false;

private disposeAllWidget() {
[
Expand All @@ -128,18 +135,25 @@ export class AIEditorContribution extends Disposable implements IEditorFeatureCo
}

contribute(editor: IEditor): IDisposable {
if (!editor) {
if (!(editor instanceof BrowserCodeEditor) || this.initialized) {
return this;
}

const { monacoEditor, currentUri } = editor;
if (currentUri && currentUri.codeUri.scheme !== Schemes.file) {
return this;
}
this.disposables.push(
editor.onRefOpen((e) => {
const { uri } = e.instance;
if (uri.codeUri.scheme !== Schemes.file) {
return;
}

this.initialized = true;
this.contributeInlineCompletionFeature(editor);
this.contributeInlineChatFeature(editor);
this.registerLanguageFeatures(editor);
}),
);

this.contributeInlineCompletionFeature(editor);
this.contributeInlineChatFeature(editor);
this.registerLanguageFeatures(editor);
const { monacoEditor } = editor;

this.disposables.push(
monacoEditor.onDidScrollChange(() => {
Expand Down Expand Up @@ -223,6 +237,12 @@ export class AIEditorContribution extends Disposable implements IEditorFeatureCo
}),
// 通过 code actions 来透出我们 inline chat 的功能
this.inlineChatFeatureRegistry.onCodeActionRun(({ id, range }) => {
const currentEditor = this.workbenchEditorService.currentEditor;

if (currentEditor?.currentUri !== editor.currentUri) {
return;
}

monacoEditor.setSelection(range);
this.showInlineChat(editor);
if (this.aiInlineContentWidget) {
Expand Down

1 comment on commit abed659

@opensumi
Copy link
Contributor

@opensumi opensumi bot commented on abed659 May 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉 Next publish successful!

3.0.2-next-1715931121.0

Please sign in to comment.