import * as vscode from 'vscode' ; import { ExtensionComponent } from '../extensionComponent' ; import { DependenciesTreeNode } from '../treeDataProviders/dependenciesTree/dependenciesTreeNode' ; import { TreesManager } from '../treeDataProviders/treesManager' ; import { Severity , SeverityUtils } from '../types/severity' ; import { DiagnosticsUtils } from './diagnosticsUtils' ; see DiagnosticsManager export abstract class AbstractCodeActionProvider implements vscode . CodeActionProvider , ExtensionComponent { static readonly XRAY_DIAGNOSTIC_SOURCE : string = 'JFrog Xray' ; constructor ( protected _documentSelector , protected _diagnosticCollection , protected _treesManager ) { } param document public abstract updateDiagnostics ( document ) ; param document protected abstract getDependenciesTree ( document ? ) ; public activate ( context ) { this . registerListeners ( context . subscriptions ) ; vscode . workspace . textDocuments . forEach ( this . updateDiagnostics , this ) ; context . subscriptions . push ( vscode . languages . registerCodeActionsProvider ( this . _documentSelector , this , { providedCodeActionKinds : [ vscode . CodeActionKind . Empty ] } ) ) ; } private registerListeners ( subscriptions ) { vscode . workspace . onDidOpenTextDocument ( this . updateDiagnostics , this , subscriptions ) ; subscriptions . push ( vscode . workspace . onDidSaveTextDocument ( this . updateDiagnostics , this ) ) ; vscode . workspace . onDidCloseTextDocument ( this . deleteDiagnostics , this , subscriptions ) ; } provideCodeActions ( document , range , context ) { let diagnostic = context . diagnostics . filter ( diagnostic => this . isJFrogSource ( diagnostic . source ) ) ; if ( diagnostic . length === 0 ) { return undefined ; } let dependenciesTree = this . getDependenciesTree ( document ) ; if ( ! dependenciesTree ) { return undefined ; } for ( let child of dependenciesTree . children ) { if ( child . componentId === diagnostic [ 0 ] . code ) { return [ this . createCommand ( child ) ] ; } for ( let grandchild of child . children ) { if ( grandchild . componentId === diagnostic [ 0 ] . code ) { return [ this . createCommand ( grandchild ) ] ; } } } return undefined ; } private createCommand ( node ) { return { command : 'jfrog.xray.codeAction' , title : 'Show in dependencies tree' , arguments : [ node ] } as vscode . Command ; } param diagnostics param node param dependencyPos addDiagnostic ( diagnostics , node , dependencyPos ) { for ( let i = 0 ; i < dependencyPos . length ; i += 2 ) { let diagnostic = new vscode . Diagnostic ( new vscode . Range ( dependencyPos [ i ] , dependencyPos [ i + 1 ] ) , node . topSeverity === Severity . Normal ? 'No issues found.' : 'Top issue severity: ' + SeverityUtils . getString ( node . topSeverity ) , DiagnosticsUtils . getDiagnosticSeverity ( node . topSeverity ) ) ; diagnostic . source = this . getSource ( ) ; diagnostic . code = node . componentId ; diagnostics . push ( diagnostic ) ; } } deleteDiagnostics ( document ) { if ( document . uri . scheme === 'file' ) { this . _diagnosticCollection . delete ( document . uri ) ; } } dispose ( ) { this . _diagnosticCollection . clear ( ) ; this . _diagnosticCollection . dispose ( ) ; } protected getSource ( ) { return AbstractCodeActionProvider . XRAY_DIAGNOSTIC_SOURCE ; } private isJFrogSource ( source ) { return source === AbstractCodeActionProvider . XRAY_DIAGNOSTIC_SOURCE ; } }