@@ -13,20 +13,25 @@ import { minimatch } from "minimatch";
1313export class OssScannerService extends BaseScannerService {
1414 private decorationTypes = {
1515 malicious : vscode . window . createTextEditorDecorationType ( {
16- overviewRulerColor : 'red' ,
17- overviewRulerLane : vscode . OverviewRulerLane . Left ,
1816 gutterIconPath : vscode . Uri . file ( path . join ( __dirname , '..' , '..' , '..' , '..' , 'media' , 'icons' , 'malicious.svg' ) ) ,
17+ rangeBehavior : vscode . DecorationRangeBehavior . ClosedClosed
1918 } ) ,
2019 ok : vscode . window . createTextEditorDecorationType ( {
2120 gutterIconPath : path . join ( __dirname , '..' , '..' , '..' , '..' , 'media' , 'icons' , 'circle-check.svg' ) ,
21+ rangeBehavior : vscode . DecorationRangeBehavior . ClosedClosed
2222 } ) ,
2323 unknown : vscode . window . createTextEditorDecorationType ( {
2424 gutterIconPath : vscode . Uri . file ( path . join ( __dirname , '..' , '..' , '..' , '..' , 'media' , 'icons' , 'question-mark.svg' ) ) ,
25- gutterIconSize : 'contain'
25+ rangeBehavior : vscode . DecorationRangeBehavior . ClosedClosed
2626 } )
2727 } ;
2828
2929 private diagnosticsMap : Map < string , vscode . Diagnostic [ ] > = new Map ( ) ;
30+ private maliciousDecorationsMap : Map < string , vscode . DecorationOptions [ ] > = new Map ( ) ;
31+ private okDecorationsMap : Map < string , vscode . DecorationOptions [ ] > = new Map ( ) ;
32+ private unknownDecorationsMap : Map < string , vscode . DecorationOptions [ ] > = new Map ( ) ;
33+ private documentOpenListener : vscode . Disposable | undefined ;
34+ private editorChangeListener : vscode . Disposable | undefined ;
3035
3136 constructor ( ) {
3237 const config : IScannerConfig = {
@@ -40,7 +45,45 @@ export class OssScannerService extends BaseScannerService {
4045
4146 super ( config ) ;
4247 }
48+
49+ public async initializeScanner ( ) : Promise < void > {
50+ this . documentOpenListener = vscode . workspace . onDidOpenTextDocument ( this . onDocumentOpen . bind ( this ) ) ;
51+ this . editorChangeListener = vscode . window . onDidChangeActiveTextEditor ( this . onEditorChange . bind ( this ) ) ;
52+ }
53+ private onDocumentOpen ( document : vscode . TextDocument ) : void {
54+ if ( this . matchesManifestPattern ( document . uri . fsPath ) ) {
55+ this . applyDecorations ( document . uri ) ;
56+ }
57+ }
4358
59+ private onEditorChange ( editor : vscode . TextEditor | undefined ) : void {
60+ if ( editor && this . matchesManifestPattern ( editor . document . uri . fsPath ) ) {
61+ this . applyDecorations ( editor . document . uri ) ;
62+ }
63+ }
64+
65+ private applyDecorations ( uri : vscode . Uri ) : void {
66+ const filePath = uri . fsPath ;
67+ const editor = vscode . window . visibleTextEditors . find ( e => e . document . uri . toString ( ) === uri . toString ( ) ) ;
68+
69+ if ( editor ) {
70+ const maliciousDecorations = this . maliciousDecorationsMap . get ( filePath ) || [ ] ;
71+ const okDecorations = this . okDecorationsMap . get ( filePath ) || [ ] ;
72+ const unknownDecorations = this . unknownDecorationsMap . get ( filePath ) || [ ] ;
73+
74+ editor . setDecorations ( this . decorationTypes . malicious , maliciousDecorations ) ;
75+ editor . setDecorations ( this . decorationTypes . ok , okDecorations ) ;
76+ editor . setDecorations ( this . decorationTypes . unknown , unknownDecorations ) ;
77+ }
78+ }
79+
80+ private applyDiagnostics ( ) : void {
81+ this . diagnosticsMap . forEach ( ( diagnostics , filePath ) => {
82+ const vscodeUri = vscode . Uri . file ( filePath ) ;
83+ this . diagnosticCollection . set ( vscodeUri , diagnostics ) ;
84+ } ) ;
85+ }
86+
4487 matchesManifestPattern ( filePath : string ) : boolean {
4588 const normalizedPath = filePath . replace ( / \\ / g, '/' ) ;
4689 return constants . supportedManifestFilePatterns . some ( pattern => minimatch ( normalizedPath , pattern ) ) ;
@@ -58,13 +101,13 @@ export class OssScannerService extends BaseScannerService {
58101 if ( ! this . shouldScanFile ( document ) ) {
59102 return ;
60103 }
61-
104+
62105 const originalFilePath = document . uri . fsPath ;
106+
63107 const tempSubFolder = this . getTempSubFolderPath ( document , constants . ossRealtimeScannerDirectory ) ;
64108
65109 try {
66110 this . createTempFolder ( tempSubFolder ) ;
67-
68111 const mainTempPath = this . saveMainManifestFile ( tempSubFolder , originalFilePath , document . getText ( ) ) ;
69112 this . saveCompanionFile ( tempSubFolder , originalFilePath ) ;
70113
@@ -74,13 +117,24 @@ export class OssScannerService extends BaseScannerService {
74117 this . updateProblems < CxOssResult [ ] > ( scanResults , document . uri ) ;
75118
76119 } catch ( error ) {
120+ this . storeAndApplyResults ( originalFilePath , document . uri , [ ] , [ ] , [ ] , [ ] ) ;
77121 console . error ( error ) ;
78122 logs . error ( this . config . errorMessage ) ;
79123 } finally {
80124 this . deleteTempFolder ( tempSubFolder ) ;
81125 }
82126 }
127+
128+ private storeAndApplyResults ( filePath : string , uri : vscode . Uri , diagnostics : vscode . Diagnostic [ ] , maliciousDecorations : vscode . DecorationOptions [ ] , okDecorations : vscode . DecorationOptions [ ] , unknownDecorations : vscode . DecorationOptions [ ] ) : void {
129+ this . diagnosticsMap . set ( filePath , diagnostics ) ;
130+ this . maliciousDecorationsMap . set ( filePath , maliciousDecorations ) ;
131+ this . okDecorationsMap . set ( filePath , okDecorations ) ;
132+ this . unknownDecorationsMap . set ( filePath , unknownDecorations ) ;
83133
134+ this . applyDiagnostics ( ) ;
135+ this . applyDecorations ( uri ) ;
136+ }
137+
84138 private saveMainManifestFile ( tempFolder : string , originalFilePath : string , content : string ) : string {
85139 const fileName = path . basename ( originalFilePath ) ;
86140 const tempFilePath = path . join ( tempFolder , fileName ) ;
@@ -143,31 +197,26 @@ export class OssScannerService extends BaseScannerService {
143197 default :
144198 continue ;
145199 }
146-
147- }
148- this . diagnosticsMap . set ( filePath , diagnostics ) ;
149-
150- this . applyDiagnostics ( ) ;
151-
152- const editor = vscode . window . visibleTextEditors . find ( e => e . document . uri . toString ( ) === uri . toString ( ) ) ;
153- if ( editor ) {
154- editor . setDecorations ( this . decorationTypes . malicious , maliciousDecorations ) ;
155- editor . setDecorations ( this . decorationTypes . ok , okDecorations ) ;
156- editor . setDecorations ( this . decorationTypes . unknown , unknownDecorations ) ;
157200 }
158- }
159-
160- private applyDiagnostics ( ) : void {
161- console . log ( "applyDiagnostics" , this . diagnosticsMap ) ;
162201
163- this . diagnosticsMap . forEach ( ( diagnostics , filePath ) => {
164- const vscodeUri = vscode . Uri . file ( filePath ) ;
165- this . diagnosticCollection . set ( vscodeUri , diagnostics ) ;
166- } ) ;
202+ this . storeAndApplyResults ( filePath , uri , diagnostics , maliciousDecorations , okDecorations , unknownDecorations ) ;
167203 }
168204
169- public async clearProblems ( ) : Promise < void > {
205+ public async clearProblems ( ) : Promise < void > {
170206 await super . clearProblems ( ) ;
171207 this . diagnosticsMap . clear ( ) ;
208+ this . maliciousDecorationsMap . clear ( ) ;
209+ this . okDecorationsMap . clear ( ) ;
210+ this . unknownDecorationsMap . clear ( ) ;
211+ }
212+
213+ public dispose ( ) : void {
214+ if ( this . documentOpenListener ) {
215+ this . documentOpenListener . dispose ( ) ;
216+ }
217+
218+ if ( this . editorChangeListener ) {
219+ this . editorChangeListener . dispose ( ) ;
220+ }
172221 }
173222}
0 commit comments