@@ -16,12 +16,15 @@ import {
1616 getIcon ,
1717 Disposable ,
1818 toDisposable ,
19+ WithEventBus ,
20+ OnEvent ,
1921} from '@opensumi/ide-core-browser' ;
2022import {
2123 IEditorDocumentModel ,
2224 DidApplyEditorDecorationFromProvider ,
2325 IEditor ,
2426} from '@opensumi/ide-editor/lib/browser' ;
27+ import { WorkbenchEditorService } from '@opensumi/ide-editor' ;
2528import { AntcodeCommentsService } from './comments.service' ;
2629import { IAntcodeService , IPullRequestChangeDiff } from '../antcode-service/base' ;
2730import { COMMENT_FILTER_TYPE , THREAD_TYPE } from '.' ;
@@ -32,11 +35,14 @@ import { MouseWheelBlock } from './components/MouseWheelBlock';
3235import { AnnotationService } from './annotation.service' ;
3336import * as styles from './index.module.less' ;
3437import { Portal } from '../../portal' ;
35- import { IAntcodeCommentThread , ICommentsThreadData } from './comment.interface' ;
38+ import { IAntcodeCommentThread , ICommentsThreadData , FoldingRegion } from './comment.interface' ;
3639import { getChangeRangeByDiff } from './utils' ;
37-
40+ import { OpenDiffEditorEvent } from '../../common/events' ;
3841@Domain ( ClientAppContribution , CoreCommentsContribution )
39- export class CommentsContribution implements ClientAppContribution , CoreCommentsContribution {
42+ export class CommentsContribution
43+ extends WithEventBus
44+ implements ClientAppContribution , CoreCommentsContribution
45+ {
4046 @Autowired ( AnnotationService )
4147 private readonly annotationService : AnnotationService ;
4248
@@ -52,8 +58,34 @@ export class CommentsContribution implements ClientAppContribution, CoreComments
5258 @Autowired ( IIconService )
5359 private readonly iconService : IIconService ;
5460
61+ @Autowired ( WorkbenchEditorService )
62+ private readonly workbenchEditorService : WorkbenchEditorService ;
63+
5564 @Autowired ( IEventBus )
56- private readonly eventBus : IEventBus ;
65+ readonly eventBus : IEventBus ;
66+
67+ @OnEvent ( OpenDiffEditorEvent )
68+ onEditorGroupOpenEvent ( e : OpenDiffEditorEvent ) {
69+ const openType = this . workbenchEditorService . currentEditorGroup . currentOpenType ?. type ;
70+ this . registerOnDidChangeFolding ( ) ;
71+ // TODO 目前重新打开 会导致展开全部代码 如果 annotation 已经折叠那就再也打不开了
72+ // 展开全部annotation
73+ let currentUri : URI | null | undefined ;
74+ if ( openType === 'code' ) {
75+ currentUri = this . workbenchEditorService . currentEditor ?. currentUri ;
76+ } else if ( openType === 'diff' ) {
77+ currentUri =
78+ this . workbenchEditorService . currentEditorGroup . diffEditor . modifiedEditor . currentUri ;
79+ }
80+ if ( currentUri ) {
81+ for ( let [ , thread ] of this . annotationService . getAllAnnotations ( ) ) {
82+ thread . show ( ) ;
83+ if ( thread . uri . isEqual ( currentUri ) ) {
84+ thread . show ( ) ;
85+ }
86+ }
87+ }
88+ }
5789
5890 async onStart ( ) {
5991 // 初始化评论数据
@@ -252,19 +284,94 @@ export class CommentsContribution implements ClientAppContribution, CoreComments
252284 const onlyShowChangeLineRelatedComment =
253285 commentFilterType === COMMENT_FILTER_TYPE . CHANGE_LINE_RELATED ;
254286
287+ const showAll = commentFilterType === COMMENT_FILTER_TYPE . ALL ;
255288 // 隐藏全部评论 | 仅展示问题评论
256289 if (
257290 isHideAll ||
258291 ( onlyShowProblem && ! treadData ?. isProblem ) ||
259292 ( onlyShowChangeLineRelatedComment && ! treadData ?. isChangeLineRelated )
260293 ) {
261- thread . hideAll ( ) ;
262- } else {
263- thread . show ( ) ;
294+ thread . hide ( ) ;
264295 }
265296 // 只有在已有评论才出现用户头像
266297 return ! ! treadData ?. noteId ;
267298 } ,
268299 } ) ;
269300 }
301+
302+ private registerOnDidChangeFolding ( ) {
303+ const openType = this . workbenchEditorService . currentEditorGroup . currentOpenType ?. type ;
304+ if ( openType === 'code' ) {
305+ const foldingContrib =
306+ this . workbenchEditorService . currentEditor ?. monacoEditor . getContribution (
307+ 'editor.contrib.folding'
308+ ) ;
309+ if ( foldingContrib ) {
310+ const currentUri = this . workbenchEditorService . currentEditor ! . currentUri as URI ;
311+ this . hideOrShowThread ( foldingContrib , currentUri ) ;
312+ }
313+ } else if ( openType === 'diff' ) {
314+ const { diffEditor } = this . workbenchEditorService . currentEditorGroup ;
315+ const { originalEditor, modifiedEditor } = diffEditor ;
316+ const originalFoldingContrib =
317+ originalEditor . monacoEditor . getContribution ( 'editor.contrib.folding' ) ;
318+ const modifyFoldingContrib =
319+ modifiedEditor . monacoEditor . getContribution ( 'editor.contrib.folding' ) ;
320+ if ( originalFoldingContrib ) {
321+ const currentUri = originalEditor . currentUri as URI ;
322+ this . hideOrShowThread ( originalFoldingContrib , currentUri ) ;
323+ }
324+ if ( modifyFoldingContrib ) {
325+ const currentUri = modifiedEditor . currentUri as URI ;
326+ this . hideOrShowThread ( modifyFoldingContrib , currentUri ) ;
327+ }
328+ }
329+ }
330+
331+ private hideOrShowThread ( foldingContrib , currentUri : URI ) {
332+ foldingContrib . getFoldingModel ( ) ?. then ( ( foldingModel ) => {
333+ foldingModel . onDidChange ( ( e ) => {
334+ if ( e . collapseStateChanged ) {
335+ const foldingRegin = e . collapseStateChanged [ 0 ] as FoldingRegion ;
336+ // 折叠隐藏折叠区域评论块
337+ const { startLineNumber, endLineNumber } = foldingRegin ;
338+ if ( foldingRegin . isCollapsed ) {
339+ for ( let [ , commentThread ] of this . antcodeCommentsService . getAllComments ( ) ) {
340+ if ( commentThread . uri . isEqual ( currentUri ) && ! commentThread . isCollapsed ) {
341+ if (
342+ commentThread . range . startLineNumber > startLineNumber &&
343+ commentThread . range . endLineNumber <= endLineNumber
344+ ) {
345+ commentThread . hide ( ) ;
346+ }
347+ }
348+ }
349+
350+ for ( let [ , commentThread ] of this . annotationService . getAllAnnotations ( ) ) {
351+ if ( commentThread . uri . isEqual ( currentUri ) && ! commentThread . isCollapsed ) {
352+ if (
353+ commentThread . range . startLineNumber > startLineNumber &&
354+ commentThread . range . endLineNumber <= endLineNumber
355+ ) {
356+ commentThread . hide ( ) ;
357+ }
358+ }
359+ }
360+ } else {
361+ // annotation 没有展开按钮所以折叠后需展开
362+ for ( let [ , commentThread ] of this . annotationService . getAllAnnotations ( ) ) {
363+ if ( commentThread . uri . isEqual ( currentUri ) ) {
364+ if (
365+ commentThread . range . startLineNumber > startLineNumber &&
366+ commentThread . range . endLineNumber <= endLineNumber
367+ ) {
368+ commentThread . show ( ) ;
369+ }
370+ }
371+ }
372+ }
373+ }
374+ } ) ;
375+ } ) ;
376+ }
270377}
0 commit comments