import * as vscode from 'vscode' ; import { CheckPointObject , CheckPointTreeItem } from './Interfaces/CheckPointInterfaces' ; import { logger , initLogger } from './logger' ; import { CheckPointProvider , CheckPointObjectImpl } from './checkPointProvider' ; /**
 * Class to generate and set the `TreeDataProvider` & `TreeView` for displaying CheckPoints.
 */ export class CheckPointExplorer { /**
     *`TreeView` object to utlize the Tree's data.
     */ private checkPointTreeView : vscode . TreeView < CheckPointTreeItem > | undefined = undefined ; /**
     *`ExtensionContext` object to store globalState.
     */ private checkPointExplorerContext : vscode . ExtensionContext ; /**
     *`CheckPointProvider` object to store the Tree's data in order to set the `checkPointTreeView` object.
     */ private treeDataProvider : CheckPointProvider | undefined = undefined ; private currentFileCheckPointObject : CheckPointObject = { } as CheckPointObject ; param context constructor ( context ) { this . checkPointExplorerContext = context ; if ( ! logger ) { initLogger ( context . logPath ) ; } this . createDataProvider ( ) ; logger . info ( "Registering commands" ) ; context . subscriptions . push ( vscode . commands . registerCommand ( 'checkPointExplorer.commenceTracking' , ( ) => this . commenceTracking ( ) ) ) ; context . subscriptions . push ( vscode . commands . registerCommand ( 'checkPointExplorer.openCheckPoint' , index => this . treeDataProvider ?. openCheckPoint ( index ) . catch ( error => logger . warn ( "Open checkpoint failed for index: " + index ) ) ) ) ; context . subscriptions . push ( vscode . commands . registerCommand ( 'checkPointExplorer.deleteAllCheckPoints' , ( ) => this . deleteCheckPoints ( ) ) ) ; context . subscriptions . push ( vscode . commands . registerCommand ( 'checkPointExplorer.deleteCheckPoint' , element => this . treeDataProvider ?. deleteSingleCheckPoint ( element . index ) . then ( ( checkPointObject ) => this . currentFileCheckPointObject = checkPointObject ) . catch ( error => logger . warn ( "Delete checkpoint failed for index: " + element . index ) ) ) ) ; context . subscriptions . push ( vscode . commands . registerCommand ( 'checkPointExplorer.setActiveCheckPoint' , element => this . treeDataProvider ?. setActiveCheckPoint ( element . index ) . catch ( error => logger . warn ( "Set active checkpoint failed for index: " + element . index ) ) ) ) ; vscode . workspace . onDidSaveTextDocument ( ( document ) => { if ( this . treeDataProvider && Object . keys ( this . currentFileCheckPointObject ) . length ) { this . treeDataProvider . saveCheckPoint ( document ) ; } else { logger . warn ( "Commence tracking not initialised" ) ; } } ) ; vscode . window . onDidChangeActiveTextEditor ( ( documentEditor ) => { logger . info ( "Active editor switched to: " + documentEditor ?. document . fileName ) ; if ( documentEditor ) { this . currentFileCheckPointObject = this . checkPointExplorerContext . globalState . get ( documentEditor . document . fileName || "" ) || { } as CheckPointObject ; this . treeDataProvider ?. updateLastSavedFile ( documentEditor . document . getText ( ) ) ; this . treeDataProvider ?. updateCheckPointObject ( this . currentFileCheckPointObject ) . catch ( error => logger . warn ( "Document is undefined " ) ) ; } else { logger . warn ( "Active editor is undefined" ) ; this . treeDataProvider ?. updateCheckPointObject ( { } as CheckPointObject ) . catch ( error => logger . warn ( "Document is undefined " ) ) ; ; } } ) ; } returns private deleteCheckPoints ( ) { logger . info ( "Deleting all checkpoints" ) ; this . treeDataProvider ?. updateCheckPointObject ( { } as CheckPointObject ) . catch ( error => logger . warn ( "Document is undefined " ) ) ; ; this . currentFileCheckPointObject = { } as CheckPointObject ; } returns private commenceTracking ( ) { const currentDocument = vscode . window . activeTextEditor ?. document ; if ( currentDocument ) { logger . info ( "Starting file tracking for: " + currentDocument . fileName ) ; const currentDocumentText = currentDocument . getText ( ) ; this . currentFileCheckPointObject = new CheckPointObjectImpl ( [ currentDocumentText as string ] , [ new Date ( Date . now ( ) ) ] , currentDocumentText as string , 0 ) ; this . treeDataProvider ?. updateCheckPointObject ( this . currentFileCheckPointObject as CheckPointObject ) . catch ( error => logger . warn ( "Document is undefined " ) ) ; ; } else { logger . warn ( "Document is undefined" ) ; } } returns private createDataProvider ( ) { logger . info ( "Creating tree data provider" ) ; let current_document = vscode . window . activeTextEditor ?. document ; this . currentFileCheckPointObject = this . checkPointExplorerContext . globalState . get ( current_document ?. fileName || "" ) || { } as CheckPointObject ; this . treeDataProvider = new CheckPointProvider ( this . checkPointExplorerContext , this . currentFileCheckPointObject as CheckPointObject ) ; this . checkPointTreeView = vscode . window . createTreeView ( 'checkPointExplorer' , { treeDataProvider : this . treeDataProvider } ) ; } }