@@ -21,7 +21,8 @@ import { Autowired } from '@opensumi/di';
2121import { InlineChatController } from '@opensumi/ide-ai-native/lib/browser/widget/inline-chat/inline-chat-controller' ;
2222import { LiveInlineDiffPreviewer } from '@opensumi/ide-ai-native/lib/browser/widget/inline-diff/inline-diff-previewer' ;
2323import { InlineDiffHandler } from '@opensumi/ide-ai-native/lib/browser/widget/inline-diff/inline-diff.handler' ;
24- import { InlineStreamDiffHandler } from '@opensumi/ide-ai-native/lib/browser/widget/inline-stream-diff/inline-stream-diff.handler' ;
24+ import { IInlineStreamDiffSnapshotData , InlineStreamDiffHandler } from '@opensumi/ide-ai-native/lib/browser/widget/inline-stream-diff/inline-stream-diff.handler' ;
25+ import { AcceptPartialEditWidget } from '@opensumi/ide-ai-native/lib/browser/widget/inline-stream-diff/live-preview.component' ;
2526import { EResultKind } from '@opensumi/ide-ai-native/lib/common' ;
2627import { IMenuRegistry , MenuContribution , MenuId } from '@opensumi/ide-core-browser/lib/menu/next' ;
2728import { IEditor , IEditorDocumentModelService } from '@opensumi/ide-editor/lib/browser' ;
@@ -30,6 +31,15 @@ import path from 'path';
3031import { IDiffViewerProps , IDiffViewerTab , IExtendPartialEditEvent , ITabChangedEvent } from '../common' ;
3132import { removeStart } from '../utils' ;
3233
34+ export interface ITotalCodeInfo {
35+ totalAddedLinesCount : number ;
36+ totalDeletedLinesCount : number ;
37+ totalChangedLinesCount : number ;
38+ unresolvedAddedLinesCount : number ;
39+ unresolvedDeletedLinesCount : number ;
40+ unresolvedChangedLinesCount : number ;
41+ }
42+
3343@Domain ( ClientAppContribution , MenuContribution )
3444export class DiffViewerContribution implements ClientAppContribution , MenuContribution {
3545 private _disposables = new DisposableStore ( ) ;
@@ -231,11 +241,61 @@ export class DiffViewerContribution implements ClientAppContribution, MenuContri
231241 return this . getAllTabs ( ) . findIndex ( ( tab ) => tab . filePath === aPath ) ;
232242 } ;
233243
244+ /**
245+ * 获取当前编辑器的代码采纳状态
246+ * 1. 已经采纳的代码信息
247+ * 2. 还未处理的代码信息
248+ */
249+ getTotalCodeInfo ( snapshot : IInlineStreamDiffSnapshotData ) : ITotalCodeInfo {
250+ const partialEditWidgetList = snapshot . decorationSnapshotData . partialEditWidgetList ;
251+
252+ // 代码除了新增和删除行,还需要统计变更行
253+ // 1. 新增 N 行 => N
254+ // 2. 删除 N 行 => N
255+ // 3. 新增 M 行,删除 N 行 => max(M, N)
256+ // 综上所述,变更行数 = sum(list.map(item => max(新增行数, 删除行数)))
257+ const resolvedStatus = caculate ( partialEditWidgetList ) ;
258+ const unresolvedStatus = { added : 0 , deleted : 0 , changed : 0 } ;
259+
260+ partialEditWidgetList . forEach ( ( v , idx ) => {
261+ if ( v . status === 'pending' ) {
262+ const addedDec = snapshot . decorationSnapshotData . addedDecList [ idx ] ;
263+ const removedWidget = snapshot . decorationSnapshotData . removedWidgetList [ idx ] ;
264+ const addedLinesCount = addedDec ?. length || 0 ;
265+ const deletedLinesCount = removedWidget ?. height || 0 ;
266+ unresolvedStatus . added += addedLinesCount ;
267+ unresolvedStatus . deleted += deletedLinesCount ;
268+ unresolvedStatus . changed += Math . max ( addedLinesCount , deletedLinesCount ) ;
269+ }
270+ } ) ;
271+
272+ return {
273+ totalAddedLinesCount : resolvedStatus . added ,
274+ totalDeletedLinesCount : resolvedStatus . deleted ,
275+ totalChangedLinesCount : resolvedStatus . changed ,
276+ unresolvedAddedLinesCount : unresolvedStatus . added ,
277+ unresolvedDeletedLinesCount : unresolvedStatus . deleted ,
278+ unresolvedChangedLinesCount : unresolvedStatus . changed ,
279+ } ;
280+ function caculate ( list : AcceptPartialEditWidget [ ] ) {
281+ const result = { added : 0 , deleted : 0 , changed : 0 } ;
282+ list . forEach ( ( widget ) => {
283+ const addedLinesCount = widget . addedLinesCount ;
284+ const deletedLinesCount = widget . deletedLinesCount ;
285+ result . added += addedLinesCount ;
286+ result . deleted += deletedLinesCount ;
287+ result . changed += Math . max ( addedLinesCount , deletedLinesCount ) ;
288+ } ) ;
289+ return result ;
290+ }
291+ }
292+
234293 getDiffInfoForUri = ( uri : URI ) => {
235294 const result = {
236295 unresolved : 0 ,
237296 total : 0 ,
238297 toAddedLines : 0 ,
298+ toChangedLines : 0 ,
239299 } ;
240300 let resourceDiff = ( this . inlineDiffHandler as any ) . _previewerNodeStore . get ( uri . toString ( ) ) as
241301 | InlineStreamDiffHandler
@@ -254,12 +314,9 @@ export class DiffViewerContribution implements ClientAppContribution, MenuContri
254314 const unresolved = list . filter ( v => v . status === 'pending' ) ;
255315 result . total = list . length ;
256316 result . unresolved = unresolved . length ;
257- result . toAddedLines = snapshot . decorationSnapshotData . addedDecList . filter ( v => ! v . isHidden ) . reduce (
258- ( acc , item ) => {
259- return acc + item . length ;
260- } ,
261- 0 ,
262- ) ;
317+ const codeInfo = this . getTotalCodeInfo ( snapshot ) ;
318+ result . toAddedLines = codeInfo . unresolvedAddedLinesCount ;
319+ result . toChangedLines = codeInfo . unresolvedChangedLinesCount ;
263320 }
264321 return result ;
265322 } ;
@@ -297,6 +354,7 @@ export class DiffViewerContribution implements ClientAppContribution, MenuContri
297354 if ( e ?. uri ) {
298355 const diffInfo = this . getDiffInfoForUri ( e . uri ) ;
299356 event . diffNum = diffInfo . unresolved ;
357+ Object . assign ( event , diffInfo ) ;
300358 }
301359
302360 this . _onDidTabChange . fire ( event ) ;
0 commit comments