@@ -16,9 +16,9 @@ import {
16
16
} from 'vscode' ;
17
17
import { Configuration , CreateChatCompletionRequest , OpenAIApi } from 'openai' ;
18
18
import { explainService , ExplainProblemPayload , SuggestRecomendationPayload } from '../services' ;
19
- import { CurrentQuestion , CurrentQuestionState , Session } from '../state' ;
19
+ import { CurrentQuestion , CurrentQuestionState , Session , Analyze , AnalyseMetaData } from '../state' ;
20
20
import { BackendService , GetChatGPTToken } from '../config' ;
21
- import { GenerateDecorations } from '../helpers' ;
21
+ import { GenerateDecorations , decorationType } from '../helpers' ;
22
22
import { DiscardCommandHandler , EndorseCommandHandler } from '../commands' ;
23
23
import CONSTANTS from '../constants' ;
24
24
import Util from '../utils' ;
@@ -31,6 +31,7 @@ export class RecommendationWebView implements WebviewViewProvider {
31
31
private readonly extensionURI : Uri ;
32
32
private readonly extensionContext : ExtensionContext ;
33
33
private extensionEventEmitter : EventEmitter < AnalysisEvents > ;
34
+ private eventEmitterQueue : Array < AnalysisEvents > = [ ] ;
34
35
35
36
constructor (
36
37
extensionPath : string ,
@@ -86,6 +87,29 @@ export class RecommendationWebView implements WebviewViewProvider {
86
87
return ;
87
88
}
88
89
90
+ let interval : NodeJS . Timeout | undefined = undefined ;
91
+
92
+ if ( ! this . _view . visible ) {
93
+ this . eventEmitterQueue . push ( event ) ;
94
+ interval = setInterval ( ( ) => {
95
+ if ( this ?. _view === null || this ?. _view === undefined || ! this ?. _view . webview ) {
96
+ return ;
97
+ }
98
+
99
+ if ( ! this . _view . visible ) {
100
+ return ;
101
+ }
102
+
103
+ const latestEvent = this . eventEmitterQueue . pop ( ) ;
104
+ if ( latestEvent ) {
105
+ this ?. _view ?. webview ?. postMessage ( latestEvent ) ;
106
+ }
107
+
108
+ this . eventEmitterQueue = [ ] ;
109
+ clearInterval ( interval ) ;
110
+ } , 500 ) ;
111
+ }
112
+
89
113
this . _view . webview . postMessage ( event ) ;
90
114
} ) ;
91
115
}
@@ -286,7 +310,7 @@ export class RecommendationWebView implements WebviewViewProvider {
286
310
const comment = `\t\t#${ input } ` ;
287
311
const position = new Position ( startLine - 1 , 0 ) ; // convert line number to position
288
312
editor . edit ( ( editBuilder : TextEditorEdit ) => {
289
- editBuilder . insert ( position , comment + '\n' ) ;
313
+ editBuilder . replace ( position , comment + '\n' ) ;
290
314
} ) ;
291
315
}
292
316
@@ -326,18 +350,56 @@ export class RecommendationWebView implements WebviewViewProvider {
326
350
throw new Error ( 'handleApplyRecommendation: Editor or Init Data is undefined' ) ;
327
351
}
328
352
329
- const startLine = initData . vuln . startLine ;
330
- const endLine = initData . vuln . endLine ;
331
- const comment = ` ${ input . replace ( '```' , '' ) } ` ;
353
+ debugChannel . appendLine ( 'here1' ) ;
354
+ const setAnalyzeState = new Analyze ( this . extensionContext ) ;
355
+ const getanalyzeState = new Analyze ( this . extensionContext ) . get ( ) ?. value ;
332
356
333
- const data = initData . vuln ;
334
- const start = new Position ( startLine - 1 , 0 ) ; // convert line number to position
335
- const end = new Position ( endLine , 0 ) ; // convert line number to position
336
- const range = new Range ( start , end ) ;
337
- editor . edit ( ( editBuilder : TextEditorEdit ) => {
338
- const decorations = GenerateDecorations ( [ { ...data } ] , editor ) ;
339
- editor . setDecorations ( decorations . decorationType , [ ] ) ;
340
- editBuilder . replace ( range , comment + '\n' ) ;
357
+ debugChannel . appendLine ( 'here2' ) ;
358
+
359
+ if ( ! getanalyzeState ) {
360
+ debugChannel . appendLine ( 'here3' ) ;
361
+ throw new Error ( 'Analze is undefined' ) ;
362
+ }
363
+ debugChannel . appendLine ( 'here4' ) ;
364
+
365
+ const copyAnalyzeValue = { ...getanalyzeState } ;
366
+
367
+ const key = `${ initData . path } @@${ initData . id } ` ;
368
+ copyAnalyzeValue [ key ] . isDiscarded = true ;
369
+
370
+ const results : AnalyseMetaData [ ] = [ ] ;
371
+
372
+ debugChannel . appendLine ( 'here5' ) ;
373
+
374
+ for ( const [ , value ] of Object . entries ( copyAnalyzeValue ) ) {
375
+ if ( ! value . isDiscarded ) {
376
+ results . push ( value ) ;
377
+ }
378
+ }
379
+
380
+ debugChannel . appendLine ( 'here6' ) ;
381
+
382
+ setAnalyzeState . set ( { ...copyAnalyzeValue } ) . then ( ( ) => {
383
+ debugChannel . appendLine ( 'here7' ) ;
384
+
385
+ const startLine = initData . vuln . startLine ;
386
+ const endLine = initData . vuln . endLine ;
387
+ const comment = `${ input . replace ( '```' , '' ) } ` ;
388
+
389
+ const start = new Position ( startLine - 1 , 0 ) ; // convert line number to position
390
+ const end = new Position ( endLine , 0 ) ; // convert line number to position
391
+ const range = new Range ( start , end ) ;
392
+
393
+ const { decorations } = GenerateDecorations ( results , editor ) ;
394
+
395
+ editor . setDecorations ( decorationType , [ ] ) ;
396
+ editor . setDecorations ( decorationType , decorations ) ;
397
+
398
+ debugChannel . appendLine ( 'here8' ) ;
399
+
400
+ editor . edit ( ( editBuilder : TextEditorEdit ) => {
401
+ editBuilder . replace ( range , comment + '\n' ) ;
402
+ } ) ;
341
403
} ) ;
342
404
}
343
405
@@ -466,7 +528,9 @@ export class RecommendationWebView implements WebviewViewProvider {
466
528
}
467
529
try {
468
530
this . handleApplyRecommendation ( input , initData ) ;
469
- } catch {
531
+ } catch ( error : any ) {
532
+ debugChannel . appendLine ( `Metabob: Apply Recommendation Error ${ JSON . stringify ( error ) } ` ) ;
533
+
470
534
this . _view . webview . postMessage ( {
471
535
type : 'applyRecommendation:Error' ,
472
536
data : { } ,
0 commit comments