import { Disposable , DocumentFilter , languages , TextDocument , workspace } from 'vscode' ; import { Server } from './server' ; export class LeanSyncService implements Disposable { private subscriptions : Disposable [ ] = [ ] ; private didAutoStartServer = false ; constructor ( private server , private documentFilter ) { this . subscriptions . push ( workspace . onDidChangeTextDocument ( ( ev ) => this . syncDoc ( ev . document ) ) ) ; this . subscriptions . push ( workspace . onDidOpenTextDocument ( ( ev ) => this . syncDoc ( ev ) ) ) ; this . syncAll ( ) ; this . subscriptions . push ( this . server . restarted . on ( ( ) => this . syncAll ( ) ) ) ; } private syncAll ( ) { workspace . textDocuments . forEach ( ( doc ) => this . syncDoc ( doc ) ) ; } private async syncDoc ( doc ) { if ( ! languages . match ( this . documentFilter , doc ) ) { return ; } if ( ! this . didAutoStartServer && ! this . server . alive ( ) ) { this . didAutoStartServer = true ; this . server . connect ( ) ; } await this . server . sync ( doc . fileName , doc . getText ( ) ) ; } dispose ( ) { for ( const s of this . subscriptions ) { s . dispose ( ) ; } } }