-
Notifications
You must be signed in to change notification settings - Fork 400
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into feat/add-polling-watcher
- Loading branch information
Showing
10 changed files
with
397 additions
and
45 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
103 changes: 79 additions & 24 deletions
103
packages/ai-native/src/browser/contrib/intelligent-completions/source/line-change.source.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 |
---|---|---|
@@ -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; | ||
}, | ||
); | ||
|
||
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; | ||
} | ||
} |
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
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
75 changes: 75 additions & 0 deletions
75
packages/terminal-next/__tests__/browser/links/word-link-provider.test.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,75 @@ | ||
import { ILink, Terminal } from '@xterm/xterm'; | ||
|
||
import { URI } from '@opensumi/ide-core-common'; | ||
import { createBrowserInjector } from '@opensumi/ide-dev-tool/src/injector-helper'; | ||
|
||
import { TerminalWordLinkProvider } from '../../../src/browser/links/word-link-provider'; | ||
|
||
describe('Workbench - TerminalWordLinkProvider', () => { | ||
const injector = createBrowserInjector([]); | ||
|
||
async function assertLink(text: string, isWindows: boolean, expected: { text: string; range: [number, number][] }[]) { | ||
const xterm = new Terminal({ allowProposedApi: true }); | ||
const provider = injector.get(TerminalWordLinkProvider, [ | ||
xterm, | ||
(link: string, callback: (result: { uri: URI; isDirectory: boolean } | undefined) => void) => {}, | ||
(_: string, cb: (result: { uri: URI; isDirectory: boolean } | undefined) => void) => { | ||
cb({ uri: URI.file('/'), isDirectory: false }); | ||
}, | ||
]); | ||
|
||
// Write the text and wait for the parser to finish | ||
await new Promise<void>((r) => xterm.write(text, r)); | ||
|
||
// Ensure all links are provided | ||
const links = (await new Promise<ILink[] | undefined>((r) => provider.provideLinks(1, r)))!; | ||
expect(links.length).toStrictEqual(expected.length); | ||
const actual = links.map((e) => ({ | ||
text: e.text, | ||
range: e.range, | ||
})); | ||
const expectedVerbose = expected.map((e) => ({ | ||
text: e.text, | ||
range: { | ||
start: { x: e.range[0][0], y: e.range[0][1] }, | ||
end: { x: e.range[1][0], y: e.range[1][1] }, | ||
}, | ||
})); | ||
|
||
expect(actual).toEqual(expectedVerbose); | ||
} | ||
|
||
test('parse word link', async () => { | ||
const text = '000000.mp4 dist dsada dsadas'; | ||
await assertLink(text, false, [ | ||
{ | ||
text: '000000.mp4', | ||
range: [ | ||
[1, 1], | ||
[10, 1], | ||
], | ||
}, | ||
{ | ||
text: 'dist', | ||
range: [ | ||
[18, 1], | ||
[21, 1], | ||
], | ||
}, | ||
{ | ||
text: 'dsada', | ||
range: [ | ||
[35, 1], | ||
[39, 1], | ||
], | ||
}, | ||
{ | ||
text: 'dsadas', | ||
range: [ | ||
[52, 1], | ||
[57, 1], | ||
], | ||
}, | ||
]); | ||
}); | ||
}); |
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.