'use strict' ; import * as Net from 'net' ; import * as vscode from 'vscode' ; import { CancellationToken , DebugConfiguration , ProviderResult , WorkspaceFolder } from 'vscode' ; import { ApimDebugSession } from './apimDebug' ; export function activate ( context ) { const provider = new ApimPolicyConfigurationProvider ( ) ; context . subscriptions . push ( vscode . debug . registerDebugConfigurationProvider ( 'apim-policy' , provider ) ) ; const factory = new ApimPolicyDebugAdapterDescriptorFactory ( ) ; context . subscriptions . push ( vscode . debug . registerDebugAdapterDescriptorFactory ( 'apim-policy' , factory ) ) ; context . subscriptions . push ( factory ) ; } export function deactivate ( ) { } class ApimPolicyConfigurationProvider implements vscode . DebugConfigurationProvider { public resolveDebugConfiguration ( _folder , config , _token ? ) { if ( ! config . type && ! config . request && ! config . name ) { const editor = vscode . window . activeTextEditor ; if ( editor && editor . document . languageId === 'apim-policy' ) { config . type = 'apim-policy' ; config . name = 'Debug open APIM policy' ; config . request = 'launch' ; config . stopOnEntry = true ; } } return config ; } } class ApimPolicyDebugAdapterDescriptorFactory implements vscode . DebugAdapterDescriptorFactory { private server ? : Net . Server ; public createDebugAdapterDescriptor ( _session , _executable ) { if ( ! this . server ) { this . server = Net . createServer ( socket => { const session = new ApimDebugSession ( ) ; session . setRunAsServer ( true ) ; session . start ( < NodeJS . ReadableStream > socket , socket ) ; } ) . listen ( 0 ) ; } return new vscode . DebugAdapterServer ( this . server . address ( ) . port ) ; } public dispose ( ) { if ( this . server ) { this . server . close ( ) ; } } }