@@ -6,11 +6,17 @@ import { uriHandler, CodingServer } from 'src/codingServer';
6
6
import { Panel } from 'src/panel' ;
7
7
import { IFileNode , MRTreeDataProvider } from 'src/tree/mrTree' ;
8
8
import { ReleaseTreeDataProvider } from 'src/tree/releaseTree' ;
9
- import { IRepoInfo , IMRWebViewDetail , ISessionData } from 'src/typings/commonTypes' ;
9
+ import {
10
+ IRepoInfo ,
11
+ IMRWebViewDetail ,
12
+ ISessionData ,
13
+ ICachedCommentThreads ,
14
+ ICachedCommentController ,
15
+ } from 'src/typings/commonTypes' ;
10
16
import { GitService } from 'src/common/gitService' ;
11
17
import { MRUriScheme } from 'src/common/contants' ;
12
18
import { IDiffComment , IMRData , IFileDiffParam , IDiffFile } from 'src/typings/respResult' ;
13
- import { replyNote , ReviewComment } from '. /reviewCommentController' ;
19
+ import { replyNote , ReviewComment , makeCommentRangeProvider } from 'src /reviewCommentController' ;
14
20
import { getDiffLineNumber , isHunkLine } from 'src/common/utils' ;
15
21
16
22
export async function activate ( context : vscode . ExtensionContext ) {
@@ -48,59 +54,10 @@ export async function activate(context: vscode.ExtensionContext) {
48
54
} ) ;
49
55
50
56
const tdService = new TurndownService ( ) ;
51
- const commentController = vscode . comments . createCommentController (
52
- 'mrDiffComment' ,
53
- 'Merge request diff comments' ,
54
- ) ;
55
- context . subscriptions . push ( commentController ) ;
56
-
57
- const commentThreads : { [ key : string ] : vscode . CommentThread [ ] } = { } ;
58
57
const diffFileData : { [ key : string ] : IDiffFile } = { } ;
59
-
60
- commentController . commentingRangeProvider = {
61
- provideCommentingRanges : async (
62
- document : vscode . TextDocument ,
63
- token : vscode . CancellationToken ,
64
- ) => {
65
- if ( document . uri . scheme !== MRUriScheme ) {
66
- return [ ] ;
67
- }
68
-
69
- try {
70
- const params = new URLSearchParams ( decodeURIComponent ( document . uri . query ) ) ;
71
- const mrId = params . get ( 'id' ) || `` ;
72
- let param : IFileDiffParam = {
73
- path : params . get ( 'path' ) ?? `` ,
74
- base : params . get ( 'leftSha' ) ?? `` ,
75
- compare : params . get ( 'rightSha' ) ?? `` ,
76
- mergeRequestId : mrId ?? `` ,
77
- } ;
78
- const fileIdent = `${ params . get ( `mr` ) } /${ params . get ( `path` ) } ` ;
79
-
80
- const { data } = await codingSrv . fetchFileDiffs ( param ) ;
81
- diffFileData [ fileIdent ] = data ;
82
- const { diffLines } = data ;
83
-
84
- const ret = diffLines . reduce ( ( result , i ) => {
85
- const isHunk = isHunkLine ( i . text ) ;
86
- if ( ! isHunk ) {
87
- return result ;
88
- }
89
-
90
- const [ left , right ] = getDiffLineNumber ( i . text ) ;
91
- const [ start , end ] = params . get ( 'right' ) === `true` ? right : left ;
92
- if ( start > 0 ) {
93
- result . push ( new vscode . Range ( start - 1 , 0 , end , 0 ) ) ;
94
- }
95
- return result ;
96
- } , [ ] as vscode . Range [ ] ) ;
97
- return ret ;
98
- } catch ( e ) {
99
- console . error ( 'fetch diff lines failed.' ) ;
100
- return [ ] ;
101
- }
102
- } ,
103
- } ;
58
+ const cachedCommentThreads : ICachedCommentThreads = { } ;
59
+ const cachedCommentControllers : ICachedCommentController = { } ;
60
+ let selectedMrFile = `` ;
104
61
105
62
context . subscriptions . push ( vscode . window . registerUriHandler ( uriHandler ) ) ;
106
63
context . subscriptions . push (
@@ -247,6 +204,12 @@ export async function activate(context: vscode.ExtensionContext) {
247
204
vscode . commands . registerCommand (
248
205
`codingPlugin.showDiff` ,
249
206
async ( file : IFileNode , mr : IMRData ) => {
207
+ const curMrFile = `${ mr . iid } /${ file . path } ` ;
208
+ if ( selectedMrFile === curMrFile ) {
209
+ return ;
210
+ }
211
+ selectedMrFile = curMrFile ;
212
+
250
213
const headUri = vscode . Uri . parse ( file . path , false ) . with ( {
251
214
scheme : MRUriScheme ,
252
215
query : `leftSha=${ file . oldSha } &rightSha=${ file . newSha } &path=${ file . path } &right=true&mr=${ mr . iid } &id=${ mr . id } ` ,
@@ -262,9 +225,24 @@ export async function activate(context: vscode.ExtensionContext) {
262
225
{ preserveFocus : true } ,
263
226
) ;
264
227
265
- commentThreads [ mr . iid ] ?. forEach ( ( c ) => {
228
+ const cacheId = `${ mr . iid } /${ file . path } ` ;
229
+ // cachedCommentControllers[cacheId]?.dispose();
230
+ cachedCommentThreads [ cacheId ] ?. forEach ( ( c ) => {
266
231
c ?. dispose ( ) ;
267
232
} ) ;
233
+ cachedCommentThreads [ cacheId ] = [ ] ;
234
+
235
+ let commentController = cachedCommentControllers [ cacheId ] ;
236
+ if ( ! commentController ) {
237
+ commentController = vscode . comments . createCommentController (
238
+ `mr-${ mr . iid } ` ,
239
+ `mr-${ mr . iid } -comment-controller` ,
240
+ ) ;
241
+ commentController . commentingRangeProvider = {
242
+ provideCommentingRanges : makeCommentRangeProvider ( codingSrv , diffFileData ) ,
243
+ } ;
244
+ cachedCommentControllers [ cacheId ] = commentController ;
245
+ }
268
246
269
247
try {
270
248
const commentResp = await codingSrv . getMRComments ( mr . iid ) ;
@@ -284,11 +262,10 @@ export async function activate(context: vscode.ExtensionContext) {
284
262
285
263
Object . values ( validComments ) . forEach ( ( i ) => {
286
264
const root = i [ 0 ] ;
287
- const isLeft = root . change_type === 2 ;
288
265
const isRight = root . change_type === 1 ;
289
266
290
267
const rootLine = root . diffFile . diffLines [ root . diffFile . diffLines . length - 1 ] ;
291
- const lineNum = isLeft ? rootLine . leftNo - 1 : rootLine . rightNo - 1 ;
268
+ const lineNum = isRight ? rootLine . rightNo - 1 : rootLine . leftNo - 1 ;
292
269
const range = new vscode . Range ( lineNum , 0 , lineNum , 0 ) ;
293
270
294
271
const commentList : vscode . Comment [ ] = i
@@ -319,7 +296,9 @@ export async function activate(context: vscode.ExtensionContext) {
319
296
commentThread . comments = commentList ;
320
297
commentThread . collapsibleState = vscode . CommentThreadCollapsibleState . Expanded ;
321
298
322
- commentThreads [ mr . iid ] = ( commentThreads [ mr . iid ] ?? [ ] ) . concat ( commentThread ) ;
299
+ cachedCommentThreads [ cacheId ] = ( cachedCommentThreads [ mr . iid ] ?? [ ] ) . concat (
300
+ commentThread ,
301
+ ) ;
323
302
} ) ;
324
303
} finally {
325
304
}
@@ -331,15 +310,17 @@ export async function activate(context: vscode.ExtensionContext) {
331
310
vscode . commands . registerCommand (
332
311
`codingPlugin.diff.createComment` ,
333
312
async ( reply : vscode . CommentReply ) => {
334
- replyNote ( reply , context , codingSrv , diffFileData ) ;
313
+ const cachedThreadId = await replyNote ( reply , context , codingSrv , diffFileData ) ;
314
+ cachedCommentThreads [ cachedThreadId ] . push ( reply . thread ) ;
335
315
} ,
336
316
) ,
337
317
) ;
338
318
context . subscriptions . push (
339
319
vscode . commands . registerCommand (
340
320
`codingPlugin.diff.replyComment` ,
341
321
async ( reply : vscode . CommentReply ) => {
342
- replyNote ( reply , context , codingSrv , diffFileData ) ;
322
+ const cachedThreadId = await replyNote ( reply , context , codingSrv , diffFileData ) ;
323
+ cachedCommentThreads [ cachedThreadId ] . push ( reply . thread ) ;
343
324
} ,
344
325
) ,
345
326
) ;
0 commit comments