11import * as vscode from 'vscode' ;
2+ import * as path from 'path' ;
23import { DebugConfigurationBase } from "../base/DebugConfigurationBase" ;
34import { DebuggerProvider } from "../base/DebuggerProvider" ;
45
@@ -12,10 +13,57 @@ export interface EmmyLaunchDebugConfiguration extends DebugConfigurationBase {
1213
1314
1415export class EmmyLaunchDebuggerProvider extends DebuggerProvider {
15- async resolveDebugConfiguration ( folder : vscode . WorkspaceFolder | undefined , configuration : EmmyLaunchDebugConfiguration , token ?: vscode . CancellationToken ) : Promise < vscode . DebugConfiguration > {
16+ async resolveDebugConfiguration ( folder : vscode . WorkspaceFolder | undefined , configuration : EmmyLaunchDebugConfiguration , token ?: vscode . CancellationToken ) : Promise < vscode . DebugConfiguration | undefined > {
17+ const resolvedProgram = this . resolveProgramPath ( configuration . program ) ;
18+ const resolvedWorkingDir = this . resolveWorkingDirectory ( folder , configuration . workingDir , resolvedProgram ) ;
19+
20+ if ( this . isNullOrEmpty ( resolvedProgram ) ) {
21+ vscode . window . showErrorMessage ( 'EmmyLua launch debug requires a Lua entry file. Open a Lua file or set the program field in launch.json.' ) ;
22+ return undefined ;
23+ }
24+
1625 configuration . extensionPath = this . context . extensionPath ;
1726 configuration . sourcePaths = this . getSourceRoots ( ) ;
1827 configuration . ext = this . getExt ( ) ;
28+ configuration . request = 'launch' ;
29+ configuration . type = 'emmylua_launch' ;
30+ configuration . program = resolvedProgram ;
31+ configuration . workingDir = resolvedWorkingDir ;
32+ configuration . arguments = configuration . arguments ?? [ ] ;
1933 return configuration ;
2034 }
35+
36+ private resolveProgramPath ( configuredProgram ?: string ) : string {
37+ if ( ! this . isNullOrEmpty ( configuredProgram ) ) {
38+ return configuredProgram ! . trim ( ) ;
39+ }
40+
41+ const activeDocument = vscode . window . activeTextEditor ?. document ;
42+ if ( activeDocument ?. languageId === 'lua' && activeDocument . uri . scheme === 'file' ) {
43+ return activeDocument . uri . fsPath ;
44+ }
45+
46+ return '' ;
47+ }
48+
49+ private resolveWorkingDirectory (
50+ folder : vscode . WorkspaceFolder | undefined ,
51+ configuredWorkingDir : string | undefined ,
52+ resolvedProgram : string
53+ ) : string {
54+ if ( ! this . isNullOrEmpty ( configuredWorkingDir ) ) {
55+ return configuredWorkingDir ! . trim ( ) ;
56+ }
57+
58+ if ( folder ) {
59+ return folder . uri . fsPath ;
60+ }
61+
62+ if ( ! this . isNullOrEmpty ( resolvedProgram ) ) {
63+ return path . dirname ( resolvedProgram ) ;
64+ }
65+
66+ const firstWorkspaceFolder = vscode . workspace . workspaceFolders ?. [ 0 ] ;
67+ return firstWorkspaceFolder ?. uri . fsPath ?? '' ;
68+ }
2169}
0 commit comments