import * as vscode from "vscode" ; import * as Net from "net" ; import * as path from "path" ; import { LuaDebugSession } from "./luaDebugSession" ; import { LaunchConfig , isCustomProgramConfig , LuaProgramConfig } from "./launchConfig" ; const enableServer = true ; const debuggerType = "lua-local" ; const interpreterSetting = `${ debuggerType }.interpreter` ; function abortLaunch ( message ) { void vscode . window . showErrorMessage ( message ) ; return null ; } const configurationProvider = { async resolveDebugConfiguration ( folder , config , token ? ) { const editor = vscode . window . activeTextEditor ; if ( typeof config . request === "undefined" || typeof config . type === "undefined" ) { if ( typeof editor === "undefined" || editor . document . languageId !== "lua" || editor . document . isUntitled ) { return abortLaunch ( "Nothing to debug" ) ; } config . request = "launch" ; config . type = debuggerType ; } if ( typeof config . program === "undefined" || ! isCustomProgramConfig ( config . program ) ) { const luaConfig = config . program ?? { } ; if ( typeof luaConfig . lua === "undefined" ) { const luaBin = vscode . workspace . getConfiguration ( ) . get ( interpreterSetting ) ; if ( typeof luaBin === "undefined" || luaBin . length === 0 ) { return abortLaunch ( `You must set "${ interpreterSetting }" in your settings, or "program.lua" ` + "in your launch.json, to debug with a lua interpreter." ) ; } luaConfig . lua = luaBin ; } if ( typeof luaConfig . file === "undefined" ) { if ( typeof editor === "undefined" || editor . document . languageId !== "lua" || editor . document . isUntitled ) { return abortLaunch ( "'program.file' not set in launch.json" ) ; } luaConfig . file = editor . document . uri . fsPath ; } config . program = luaConfig as LuaProgramConfig ; } if ( Array . isArray ( config . scriptFiles ) ) { const nonString = config . scriptFiles . find ( p => typeof p !== "string" ) ; if ( typeof nonString !== "undefined" ) { return abortLaunch ( `invalid value in scriptFiles: "${ nonString }"` ) ; } else { const uris = await Promise . all ( config . scriptFiles . map ( p => vscode . workspace . findFiles ( path . normalize ( p ) ) ) ) ; config . scriptFiles = uris . flatMap ( u => u . map ( uri => uri . fsPath ) ) ; } } if ( typeof folder !== "undefined" ) { config . workspacePath = folder . uri . fsPath ; } else if ( typeof vscode . window . activeTextEditor !== "undefined" ) { config . workspacePath = path . dirname ( vscode . window . activeTextEditor . document . uri . fsPath ) ; } else { return abortLaunch ( "No path for debugger" ) ; } const extension = vscode . extensions . getExtension ( "tomblind.local-lua-debugger-vscode" ) ; if ( typeof extension === "undefined" ) { return abortLaunch ( "Failed to find extension path" ) ; } config . extensionPath = extension . extensionPath ; if ( typeof config . cwd === "undefined" ) { config . cwd = config . workspacePath ; } return config ; } } ; let debugAdapaterDescriptorFactory ; if ( enableServer ) { let server = null ; debugAdapaterDescriptorFactory = { createDebugAdapterDescriptor ( session , executable ) { if ( server === null ) { server = Net . createServer ( socket => { const debugSession = new LuaDebugSession ( ) ; debugSession . setRunAsServer ( true ) ; debugSession . start ( socket as NodeJS . ReadableStream , socket ) ; } ) . listen ( 0 ) ; } return new vscode . DebugAdapterServer ( ( server . address ( ) as Net . AddressInfo ) . port ) ; } , dispose ( ) { if ( server !== null ) { server . close ( ) ; server = null ; } } } ; } export function activate ( context ) { context . subscriptions . push ( vscode . debug . registerDebugConfigurationProvider ( debuggerType , configurationProvider ) ) ; if ( typeof debugAdapaterDescriptorFactory !== "undefined" ) { context . subscriptions . push ( vscode . debug . registerDebugAdapterDescriptorFactory ( debuggerType , debugAdapaterDescriptorFactory ) ) ; context . subscriptions . push ( debugAdapaterDescriptorFactory ) ; } } export function deactivate ( ) { }