-
Notifications
You must be signed in to change notification settings - Fork 384
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support problem fix api (#3999)
* feat: support problem fix components * chore: implement hover fix api * feat: run hover fix action * fix: improve code
- Loading branch information
Showing
16 changed files
with
371 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
packages/ai-native/src/browser/contrib/problem-fix/problem-fix.component.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import React from 'react'; | ||
import ReactDOM from 'react-dom/client'; | ||
|
||
import { Button } from '@opensumi/ide-components'; | ||
import { AppConfig, ConfigProvider, useInjectable } from '@opensumi/ide-core-browser'; | ||
import { localize } from '@opensumi/ide-core-common'; | ||
import { MarkerHover } from '@opensumi/monaco-editor-core/esm/vs/editor/contrib/hover/browser/markerHoverParticipant'; | ||
|
||
import styles from './problem-fix.module.less'; | ||
import { ProblemFixService } from './problem-fix.service'; | ||
|
||
interface IProblemFixComponentProps { | ||
part: MarkerHover; | ||
} | ||
|
||
export const ProblemFixComponent = ({ part }: IProblemFixComponentProps) => { | ||
const problemFixService = useInjectable(ProblemFixService); | ||
|
||
const handleClick = React.useCallback(() => { | ||
problemFixService.triggerHoverFix(part); | ||
}, [problemFixService, part]); | ||
|
||
return ( | ||
<Button size='small' onClick={handleClick}> | ||
{localize('aiNative.inline.problem.fix.title')} | ||
</Button> | ||
); | ||
}; | ||
|
||
export const MarkerHoverParticipantComponent = { | ||
mount(container: DocumentFragment, hoverParts: MarkerHover[], configContext: AppConfig) { | ||
container.childNodes.forEach((node, index) => { | ||
const dom = document.createElement('div'); | ||
dom.className = styles.problem_fix_btn_container; | ||
|
||
const part = hoverParts[index]; | ||
|
||
ReactDOM.createRoot(dom).render( | ||
<ConfigProvider value={configContext}> | ||
<ProblemFixComponent part={part} /> | ||
</ConfigProvider>, | ||
); | ||
node.appendChild(dom); | ||
}); | ||
}, | ||
}; |
16 changes: 16 additions & 0 deletions
16
packages/ai-native/src/browser/contrib/problem-fix/problem-fix.feature.registry.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { Injectable } from '@opensumi/di'; | ||
|
||
import { IHoverFixHandler, IProblemFixProviderRegistry } from '../../types'; | ||
|
||
@Injectable() | ||
export class ProblemFixProviderRegistry implements IProblemFixProviderRegistry { | ||
private hoverFixProvider: IHoverFixHandler | undefined; | ||
|
||
registerHoverFixProvider(provider: IHoverFixHandler): void { | ||
this.hoverFixProvider = provider; | ||
} | ||
|
||
getHoverFixProvider(): IHoverFixHandler | undefined { | ||
return this.hoverFixProvider; | ||
} | ||
} |
161 changes: 161 additions & 0 deletions
161
packages/ai-native/src/browser/contrib/problem-fix/problem-fix.handler.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
import { Autowired, INJECTOR_TOKEN, Injectable, Injector } from '@opensumi/di'; | ||
import { AppConfig } from '@opensumi/ide-core-browser'; | ||
import { InlineChatIsVisible } from '@opensumi/ide-core-browser/lib/contextkey/ai-native'; | ||
import { | ||
AISerivceType, | ||
ActionSourceEnum, | ||
ActionTypeEnum, | ||
Disposable, | ||
IAIReporter, | ||
ProblemFixRegistryToken, | ||
} from '@opensumi/ide-core-common'; | ||
import { IEditor } from '@opensumi/ide-editor'; | ||
import { Range } from '@opensumi/ide-monaco'; | ||
import { HoverController } from '@opensumi/monaco-editor-core/esm/vs/editor/contrib/hover/browser/hover'; | ||
import { | ||
HoverParticipantRegistry, | ||
IEditorHoverRenderContext, | ||
} from '@opensumi/monaco-editor-core/esm/vs/editor/contrib/hover/browser/hoverTypes'; | ||
import { | ||
MarkerHover, | ||
MarkerHoverParticipant, | ||
} from '@opensumi/monaco-editor-core/esm/vs/editor/contrib/hover/browser/markerHoverParticipant'; | ||
|
||
import { AINativeContextKey } from '../../contextkey/ai-native.contextkey.service'; | ||
import { IHoverFixHandler } from '../../types'; | ||
import { InlineChatHandler } from '../../widget/inline-chat/inline-chat.handler'; | ||
import { IAIMonacoContribHandler } from '../base'; | ||
|
||
import { MarkerHoverParticipantComponent } from './problem-fix.component'; | ||
import { ProblemFixProviderRegistry } from './problem-fix.feature.registry'; | ||
import { ProblemFixService } from './problem-fix.service'; | ||
|
||
class AIMonacoHoverParticipant extends MarkerHoverParticipant { | ||
static injector: Injector; | ||
|
||
override renderHoverParts(context: IEditorHoverRenderContext, hoverParts: MarkerHover[]) { | ||
const disposable = super.renderHoverParts(context, hoverParts); | ||
|
||
const { fragment } = context; | ||
MarkerHoverParticipantComponent.mount(fragment, hoverParts, AIMonacoHoverParticipant.injector.get(AppConfig)); | ||
|
||
return disposable; | ||
} | ||
} | ||
|
||
@Injectable() | ||
export class ProblemFixHandler extends IAIMonacoContribHandler { | ||
@Autowired(InlineChatHandler) | ||
private readonly inlineChatHandler: InlineChatHandler; | ||
|
||
@Autowired(INJECTOR_TOKEN) | ||
private readonly injector: Injector; | ||
|
||
@Autowired(ProblemFixService) | ||
private readonly problemFixService: ProblemFixService; | ||
|
||
@Autowired(ProblemFixRegistryToken) | ||
private readonly problemFixProviderRegistry: ProblemFixProviderRegistry; | ||
|
||
@Autowired(IAIReporter) | ||
private readonly aiReporter: IAIReporter; | ||
|
||
private aiNativeContextKey: AINativeContextKey; | ||
|
||
mountEditor(editor: IEditor) { | ||
this.aiNativeContextKey = this.injector.get(AINativeContextKey, [editor.monacoEditor.contextKeyService]); | ||
return super.mountEditor(editor); | ||
} | ||
|
||
doContribute() { | ||
const disposable = new Disposable(); | ||
|
||
const provider = this.problemFixProviderRegistry.getHoverFixProvider(); | ||
if (!provider) { | ||
return disposable; | ||
} | ||
|
||
// 先去掉 monaco 默认的 MarkerHoverParticipant | ||
HoverParticipantRegistry._participants = HoverParticipantRegistry._participants.filter( | ||
(participant) => participant !== MarkerHoverParticipant, | ||
); | ||
|
||
AIMonacoHoverParticipant.injector = this.injector; | ||
HoverParticipantRegistry.register(AIMonacoHoverParticipant); | ||
|
||
disposable.addDispose( | ||
this.problemFixService.onHoverFixTrigger((part) => { | ||
const hoverController = this.editor?.monacoEditor.getContribution<HoverController>(HoverController.ID); | ||
if (hoverController) { | ||
hoverController.hideContentHover(); | ||
} | ||
|
||
this.handleHoverFix(part, provider); | ||
}), | ||
); | ||
|
||
return disposable; | ||
} | ||
|
||
private async handleHoverFix(part: MarkerHover, provider: IHoverFixHandler) { | ||
const monacoEditor = this.editor?.monacoEditor; | ||
|
||
if (!monacoEditor) { | ||
return; | ||
} | ||
|
||
const model = monacoEditor.getModel(); | ||
|
||
// 以 marker 的 range 为中心,向上取 2 行,向下取 3 行 | ||
const editRange = new Range( | ||
Math.max(part.range.startLineNumber - 2, 0), | ||
1, | ||
Math.min(part.range.endLineNumber + 3, model!.getLineCount() ?? 0), | ||
model!.getLineMaxColumn(part.range.endLineNumber + 3) ?? 0, | ||
); | ||
|
||
const context = { | ||
marker: part.marker, | ||
editRange, | ||
}; | ||
|
||
monacoEditor.setSelection(editRange); | ||
|
||
const contextKeyDisposed = new Disposable(); | ||
const inlineChatIsVisible = new Set([InlineChatIsVisible.raw]); | ||
|
||
contextKeyDisposed.addDispose( | ||
this.aiNativeContextKey.contextKeyService!.onDidChangeContext((e) => { | ||
if (e.payload.affectsSome(inlineChatIsVisible)) { | ||
const isVisible = this.aiNativeContextKey.inlineChatIsVisible.get(); | ||
if (isVisible) { | ||
this.inlineChatHandler.runAction({ | ||
monacoEditor, | ||
reporterFn: (): string => { | ||
const relationId = this.aiReporter.start(AISerivceType.ProblemFix, { | ||
message: ActionTypeEnum.HoverFix, | ||
type: AISerivceType.InlineChat, | ||
source: ActionTypeEnum.HoverFix, | ||
actionSource: ActionSourceEnum.Hover, | ||
actionType: ActionTypeEnum.HoverFix, | ||
}); | ||
return relationId; | ||
}, | ||
crossSelection: monacoEditor.getSelection()!, | ||
providerPreview: () => | ||
provider.provideFix(monacoEditor, context, this.inlineChatHandler.cancelIndicator.token), | ||
extraData: { | ||
actionSource: ActionSourceEnum.Hover, | ||
actionType: ActionTypeEnum.HoverFix, | ||
}, | ||
}); | ||
} | ||
|
||
contextKeyDisposed.dispose(); | ||
} | ||
}), | ||
); | ||
|
||
this.addDispose(contextKeyDisposed); | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
packages/ai-native/src/browser/contrib/problem-fix/problem-fix.module.less
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
.problem_fix_btn_container { | ||
margin-left: 8px; | ||
margin-bottom: 8px; | ||
height: 22px; | ||
} |
13 changes: 13 additions & 0 deletions
13
packages/ai-native/src/browser/contrib/problem-fix/problem-fix.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { Injectable } from '@opensumi/di'; | ||
import { Emitter, Event } from '@opensumi/ide-core-common'; | ||
import { MarkerHover } from '@opensumi/monaco-editor-core/esm/vs/editor/contrib/hover/browser/markerHoverParticipant'; | ||
|
||
@Injectable() | ||
export class ProblemFixService { | ||
private readonly _onHoverFixTrigger = new Emitter<MarkerHover>(); | ||
public readonly onHoverFixTrigger: Event<MarkerHover> = this._onHoverFixTrigger.event; | ||
|
||
public triggerHoverFix(isTrigger: MarkerHover) { | ||
this._onHoverFixTrigger.fire(isTrigger); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.