Skip to content

Commit 62ce856

Browse files
author
guqiankun
committed
fix: 修复acr内代码折叠问题
1 parent bbaa8ba commit 62ce856

9 files changed

Lines changed: 183 additions & 50 deletions

File tree

packages/acr/src/modules/client-modules.ts

Lines changed: 0 additions & 38 deletions
This file was deleted.

packages/acr/src/modules/comments/annotation.service.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { toGitUri } from '../merge-request/changes-tree/util';
55
import { URI } from '@opensumi/ide-core-browser';
66
import { THREAD_TYPE } from './index';
77

8+
// 代码扫描数据
89
@Injectable()
910
export class AnnotationService {
1011
@Autowired(IAntcodeService)
@@ -67,4 +68,8 @@ export class AnnotationService {
6768
(thread) => thread.data?.type === THREAD_TYPE.ANNOTATION && thread.uri.isEqual(annotationUri)
6869
).length;
6970
}
71+
72+
public getAllAnnotations() {
73+
return this._annotations;
74+
}
7075
}

packages/acr/src/modules/comments/comment-filter.view.tsx

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,20 @@ export const CommentFilter = () => {
2626
],
2727
[]
2828
);
29-
const [type, setType] = React.useState(options[0].label);
3029
const antCodeCommentsService = useInjectable<AntcodeCommentsService>(AntcodeCommentsService);
31-
30+
const commentsType = options.find(
31+
(item) => item.value === antCodeCommentsService.commentFilterType
32+
);
33+
const [type, setType] = React.useState(commentsType?.value);
3234
const onChange = React.useCallback((value) => {
3335
setType(value);
3436
antCodeCommentsService.commentFilterType = value;
37+
if (value === COMMENT_FILTER_TYPE.ALL) {
38+
antCodeCommentsService.showAll();
39+
}
3540
}, []);
3641

37-
return <Select options={options} value={type} onChange={onChange} />;
42+
return (
43+
<Select options={options} value={type} defaultValue={commentsType?.value} onChange={onChange} />
44+
);
3845
};

packages/acr/src/modules/comments/comment.interface.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,13 @@ export interface IAnnotationThreadData extends IAnnotationData {
1515
export interface IAntcodeCommentThread extends ICommentsThread {
1616
data: ICommentsThreadData | IAnnotationThreadData;
1717
}
18+
19+
export interface FoldingRegion {
20+
index: number;
21+
ranges: any;
22+
endLineNumber: number;
23+
isCollapsed: boolean;
24+
parentIndex: number;
25+
regionIndex: number;
26+
startLineNumber: number;
27+
}

packages/acr/src/modules/comments/comments.contribution.tsx

Lines changed: 114 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,15 @@ import {
1616
getIcon,
1717
Disposable,
1818
toDisposable,
19+
WithEventBus,
20+
OnEvent,
1921
} from '@opensumi/ide-core-browser';
2022
import {
2123
IEditorDocumentModel,
2224
DidApplyEditorDecorationFromProvider,
2325
IEditor,
2426
} from '@opensumi/ide-editor/lib/browser';
27+
import { WorkbenchEditorService } from '@opensumi/ide-editor';
2528
import { AntcodeCommentsService } from './comments.service';
2629
import { IAntcodeService, IPullRequestChangeDiff } from '../antcode-service/base';
2730
import { COMMENT_FILTER_TYPE, THREAD_TYPE } from '.';
@@ -32,11 +35,14 @@ import { MouseWheelBlock } from './components/MouseWheelBlock';
3235
import { AnnotationService } from './annotation.service';
3336
import * as styles from './index.module.less';
3437
import { Portal } from '../../portal';
35-
import { IAntcodeCommentThread, ICommentsThreadData } from './comment.interface';
38+
import { IAntcodeCommentThread, ICommentsThreadData, FoldingRegion } from './comment.interface';
3639
import { 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
}

packages/acr/src/modules/comments/comments.service.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,17 @@ export class AntcodeCommentsService {
3939
return this._comments.get(id);
4040
}
4141

42+
public getAllComments() {
43+
return this._comments;
44+
}
45+
4246
/**
4347
* 创建一个评论
4448
* @param commentId noteId
4549
* @param data note data
4650
*/
4751
public createComment(comment: IComment) {
4852
const [, leftLineNumberStr, rightLineNumberStr] = comment.lineCode.split('_');
49-
5053
const [leftLineNumber, rightLineNumber] = [
5154
parseInt(leftLineNumberStr, 10),
5255
parseInt(rightLineNumberStr, 10),
@@ -108,4 +111,9 @@ export class AntcodeCommentsService {
108111
this._comments.delete(commentId);
109112
}
110113
}
114+
public showAll() {
115+
for (let [, thread] of this._comments) {
116+
thread.show();
117+
}
118+
}
111119
}

packages/acr/src/modules/comments/index.module.less

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,21 @@
2323
background: rgba(#687687, 45%) !important;
2424
}
2525
}
26+
:global {
27+
// 覆盖评论样式 展开收起按钮样式
28+
.comments-decoration.comments-add {
29+
width: 20px !important;
30+
}
31+
.codicon.codicon-folding-expanded {
32+
margin-left: 20px !important;
33+
width: 20px !important;
34+
35+
}
36+
.codicon.codicon-folding-collapsed {
37+
margin-left: 20px !important;
38+
width: 20px !important;
39+
}
40+
}
2641

2742
.comment_avatar {
2843
background-size: cover !important;

packages/acr/src/modules/edtior-empty/editor-empty.view.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export const EditorEmptyComponent: React.FC = () => {
3737
<div className={styles.description}>{localize('misc.ide-mode.description')}</div>
3838
<div className={styles.keybindings}>
3939
{keyList.map((item) => (
40-
<div key={item.value} className={styles.keybinding}>
40+
<div key={item.name} className={styles.keybinding}>
4141
<div className={styles.desc}>{item.name}</div>
4242
<div className={styles.key}>
4343
{item.value &&

yarn.lock

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6357,6 +6357,11 @@ image-size@~0.5.0:
63576357
resolved "https://registry.npm.alibaba-inc.com/image-size/download/image-size-0.5.5.tgz#09dfd4ab9d20e29eb1c3e80b8990378df9e3cb9c"
63586358
integrity sha1-Cd/Uq50g4p6xw+gLiZA3jfnjy5w=
63596359

6360+
immediate@~3.0.5:
6361+
version "3.0.6"
6362+
resolved "https://registry.npm.alibaba-inc.com/immediate/download/immediate-3.0.6.tgz#9db1dbd0faf8de6fbe0f5dd5e56bb606280de69b"
6363+
integrity sha1-nbHb0Pr43m++D13V5Wu2BigN5ps=
6364+
63606365
immutable@^3.7.4:
63616366
version "3.8.2"
63626367
resolved "https://registry.npm.alibaba-inc.com/immutable/download/immutable-3.8.2.tgz#c2439951455bb39913daf281376f1530e104adf3"
@@ -7727,6 +7732,13 @@ levn@~0.3.0:
77277732
prelude-ls "~1.1.2"
77287733
type-check "~0.3.2"
77297734

7735+
lie@3.1.1:
7736+
version "3.1.1"
7737+
resolved "https://registry.npm.alibaba-inc.com/lie/download/lie-3.1.1.tgz#9a436b2cc7746ca59de7a41fa469b3efb76bd87e"
7738+
integrity sha1-mkNrLMd0bKWd56QfpGmz77dr2H4=
7739+
dependencies:
7740+
immediate "~3.0.5"
7741+
77307742
liftoff@^3.1.0:
77317743
version "3.1.0"
77327744
resolved "https://registry.npm.alibaba-inc.com/liftoff/download/liftoff-3.1.0.tgz#c9ba6081f908670607ee79062d700df062c52ed3"
@@ -7828,6 +7840,13 @@ loader-utils@^2.0.0:
78287840
emojis-list "^3.0.0"
78297841
json5 "^2.1.2"
78307842

7843+
localforage@^1.10.0:
7844+
version "1.10.0"
7845+
resolved "https://registry.npm.alibaba-inc.com/localforage/download/localforage-1.10.0.tgz#5c465dc5f62b2807c3a84c0c6a1b1b3212781dd4"
7846+
integrity sha1-XEZdxfYrKAfDqEwMahsbMhJ4HdQ=
7847+
dependencies:
7848+
lie "3.1.1"
7849+
78317850
locate-path@^2.0.0:
78327851
version "2.0.0"
78337852
resolved "https://registry.npm.alibaba-inc.com/locate-path/download/locate-path-2.0.0.tgz#2b568b265eec944c6d9c0de9c3dbbbca0354cd8e"

0 commit comments

Comments
 (0)