import { AbstractCancellationTokenSource , CancellationTokenSource } from 'vscode-jsonrpc' ; import { CancellationToken , Disposable , LSPErrorCodes , ResponseError } from 'vscode-languageserver' ; import { isDebugMode } from './core' ; export interface CancellationProvider { createCancellationTokenSource ( ) : AbstractCancellationTokenSource ; } let cancellationFolderName ; export function getCancellationFolderName ( ) { return cancellationFolderName ; } export function setCancellationFolderName ( folderName ? ) { cancellationFolderName = folderName ; } export class OperationCanceledException extends ResponseError < void > { constructor ( ) { super ( LSPErrorCodes . RequestCancelled , 'request cancelled' ) ; } static is ( e ) { return e . code === LSPErrorCodes . RequestCancelled ; } } export function throwIfCancellationRequested ( token ) { if ( ! isDebugMode ( ) && token . isCancellationRequested ) { throw new OperationCanceledException ( ) ; } } export function CancelAfter ( ... tokens ) { const source = new CancellationTokenSource ( ) ; const disposables = [ ] ; for ( const token of tokens ) { disposables . push ( token . onCancellationRequested ( ( _ ) => { source . cancel ( ) ; } ) ) ; } disposables . push ( source . token . onCancellationRequested ( ( _ ) => { disposables . forEach ( ( d ) => d . dispose ( ) ) ; } ) ) ; return source ; } export class DefaultCancellationProvider implements CancellationProvider { createCancellationTokenSource ( ) { return new CancellationTokenSource ( ) ; } }