-
Notifications
You must be signed in to change notification settings - Fork 448
feat: line-change of code edits adds change parameter #4329
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
103 changes: 79 additions & 24 deletions
103
packages/ai-native/src/browser/contrib/intelligent-completions/source/line-change.source.ts
This file contains hidden or 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 |
|---|---|---|
| @@ -1,47 +1,102 @@ | ||
| import { Injectable } from '@opensumi/di'; | ||
| import { AINativeSettingSectionsId, ECodeEditsSourceTyping, IDisposable } from '@opensumi/ide-core-common'; | ||
| import { ICursorPositionChangedEvent, Position } from '@opensumi/ide-monaco'; | ||
| import { ICursorPositionChangedEvent, IModelContentChangedEvent } from '@opensumi/ide-monaco'; | ||
| import { | ||
| autorunDelta, | ||
| derivedHandleChanges, | ||
| observableFromEvent, | ||
| recomputeInitiallyAndOnChange, | ||
| } from '@opensumi/ide-monaco/lib/common/observable'; | ||
|
|
||
| import { BaseCodeEditsSource } from './base'; | ||
|
|
||
| export interface ILineChangeData { | ||
| currentLineNumber: number; | ||
| preLineNumber?: number; | ||
| change?: IModelContentChangedEvent; | ||
| } | ||
|
|
||
| @Injectable({ multiple: true }) | ||
| export class LineChangeCodeEditsSource extends BaseCodeEditsSource { | ||
| public priority = 2; | ||
|
|
||
| private prePosition = this.monacoEditor.getPosition(); | ||
|
|
||
| public mount(): IDisposable { | ||
| const modelContentChangeObs = observableFromEvent<IModelContentChangedEvent>( | ||
| this, | ||
| this.monacoEditor.onDidChangeModelContent, | ||
| (event: IModelContentChangedEvent) => event, | ||
| ); | ||
| const positionChangeObs = observableFromEvent<ICursorPositionChangedEvent>( | ||
| this, | ||
| this.monacoEditor.onDidChangeCursorPosition, | ||
| (event: ICursorPositionChangedEvent) => event, | ||
| ); | ||
|
|
||
| const latestModelContentChangeObs = derivedHandleChanges( | ||
| { | ||
| owner: this, | ||
| createEmptyChangeSummary: () => ({ change: undefined }), | ||
| handleChange: (ctx, changeSummary: { change: IModelContentChangedEvent | undefined }) => { | ||
| // 如果只是改了光标则设置 change 为空,避免获取到缓存的 change | ||
| if (ctx.didChange(positionChangeObs)) { | ||
| changeSummary.change = undefined; | ||
| } else { | ||
| changeSummary.change = modelContentChangeObs.get(); | ||
| } | ||
| return true; | ||
| }, | ||
| }, | ||
| (reader, changeSummary) => { | ||
| positionChangeObs.read(reader); | ||
| modelContentChangeObs.read(reader); | ||
| return changeSummary.change; | ||
| }, | ||
| ); | ||
|
Ricbet marked this conversation as resolved.
|
||
|
|
||
| this.addDispose(recomputeInitiallyAndOnChange(latestModelContentChangeObs)); | ||
|
|
||
| let lastModelContent: IModelContentChangedEvent | undefined; | ||
| this.addDispose( | ||
| this.monacoEditor.onDidChangeCursorPosition((event: ICursorPositionChangedEvent) => { | ||
| const currentPosition = event.position; | ||
| if (this.prePosition && this.prePosition.lineNumber !== currentPosition.lineNumber) { | ||
| this.doTrigger(currentPosition); | ||
| this.prePosition = currentPosition; | ||
| } | ||
| /** | ||
| * 由于 monaco 的 changeModelContent 事件比 changeCursorPosition 事件先触发,所以这里需要拿上一次的值进行消费 | ||
| * 否则永远返回 undefined | ||
| */ | ||
| autorunDelta(latestModelContentChangeObs, ({ lastValue }) => { | ||
| lastModelContent = lastValue; | ||
| }), | ||
| ); | ||
| return this; | ||
| } | ||
|
|
||
| protected doTrigger(position: Position) { | ||
| const isLineChangeEnabled = this.preferenceService.getValid(AINativeSettingSectionsId.CodeEditsLineChange, false); | ||
| this.addDispose( | ||
| autorunDelta(positionChangeObs, ({ lastValue, newValue }) => { | ||
| const contentChange = lastModelContent; | ||
|
|
||
| if (!isLineChangeEnabled || !position) { | ||
| return; | ||
| } | ||
| const isLineChangeEnabled = this.preferenceService.getValid( | ||
| AINativeSettingSectionsId.CodeEditsLineChange, | ||
| false, | ||
| ); | ||
| if (!isLineChangeEnabled) { | ||
| return false; | ||
| } | ||
|
|
||
| this.setBean({ | ||
| typing: ECodeEditsSourceTyping.LineChange, | ||
| position, | ||
| data: { | ||
| preLineNumber: this.prePosition?.lineNumber, | ||
| currentLineNumber: position.lineNumber, | ||
| }, | ||
| }); | ||
| const prePosition = lastValue?.position; | ||
| const currentPosition = newValue?.position; | ||
| if (prePosition && prePosition.lineNumber !== currentPosition?.lineNumber) { | ||
| this.setBean({ | ||
| typing: ECodeEditsSourceTyping.LineChange, | ||
| position: currentPosition, | ||
| data: { | ||
| preLineNumber: prePosition.lineNumber, | ||
| currentLineNumber: currentPosition.lineNumber, | ||
| change: contentChange, | ||
| }, | ||
| }); | ||
| } | ||
|
|
||
| // 消费完之后设置为 undefined,避免下次获取到缓存的值 | ||
| lastModelContent = undefined; | ||
| }), | ||
| ); | ||
|
|
||
| return this; | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.