async function constructDebugConfig()

in src/debugCodeLensProvider.ts [138:176]


async function constructDebugConfig(mainClass: string, projectName: string, workspace?: vscode.Uri): Promise<vscode.DebugConfiguration> {
    const launchConfigurations: vscode.WorkspaceConfiguration = vscode.workspace.getConfiguration("launch", workspace);
    const rawConfigs: vscode.DebugConfiguration[] = launchConfigurations.configurations;

    let debugConfig: vscode.DebugConfiguration | undefined = _.find(rawConfigs, (config) => {
        return config.mainClass === mainClass && _.toString(config.projectName) === _.toString(projectName);
    });

    if (!debugConfig) {
        debugConfig = _.find(rawConfigs, (config) => {
            return config.mainClass === mainClass && !config.projectName;
        });
    }

    if (!debugConfig) {
        debugConfig = {
            type: "java",
            name: `Launch ${mainClass.substr(mainClass.lastIndexOf(".") + 1)}`,
            request: "launch",
            mainClass,
            projectName,
        };

        // Persist the configuration into launch.json only if the launch.json file already exists in the workspace.
        if ((rawConfigs && rawConfigs.length) || await launchJsonExists(workspace)) {
            try {
                // Insert the default debug configuration to the beginning of launch.json.
                rawConfigs.splice(0, 0, debugConfig);
                await launchConfigurations.update("configurations", rawConfigs, vscode.ConfigurationTarget.WorkspaceFolder);
            } catch (error) {
                // When launch.json has unsaved changes before invoking the update api, it will throw the error below:
                // 'Unable to write into launch configuration file because the file is dirty. Please save it first and then try again.'
                // It's safe to ignore it because the only impact is the configuration is not saved, but you can continue to start the debugger.
            }
        }
    }

    return _.cloneDeep(debugConfig);
}