All files / src AntlrDebugConfigurationProvider.ts

0% Statements 0/22
0% Branches 0/5
0% Functions 0/5
0% Lines 0/21

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65                                                                                                                                 
/*
 * This file is released under the MIT license.
 * Copyright (c) 2022, Mike Lischke
 *
 * See LICENSE file for more info.
 */
 
import * as Net from "net";
 
import {
    CancellationToken, DebugConfiguration, DebugConfigurationProvider, ProviderResult, workspace, WorkspaceFolder,
    window,
} from "vscode";
 
import { AntlrFacade } from "./backend/facade";
import { AntlrDebugSession } from "./frontend/AntlrDebugAdapter";
import { ParseTreeProvider } from "./frontend/webviews/ParseTreeProvider";
 
/**
 * Validates launch configuration for grammar debugging.
 */
export class AntlrDebugConfigurationProvider implements DebugConfigurationProvider {
    private server?: Net.Server;
 
    public constructor(private backend: AntlrFacade, private parseTreeProvider: ParseTreeProvider) { }
 
    public resolveDebugConfiguration(folder: WorkspaceFolder | undefined, config: DebugConfiguration,
        _token?: CancellationToken): ProviderResult<DebugConfiguration> {
 
        Iif (workspace.getConfiguration("antlr4.generation").mode === "none") {
            void window.showErrorMessage("Interpreter data generation is disabled in the preferences (see " +
                "'antlr4.generation'). Set this at least to 'internal' to enable debugging.");
 
            return null;
        }
 
        Iif (!this.server) {
            this.server = Net.createServer((socket) => {
                socket.on("end", () => {
                    //console.error('>> ANTLR debugging client connection closed\n');
                });
 
                const session = new AntlrDebugSession(folder, this.backend, [this.parseTreeProvider]);
                session.setRunAsServer(true);
                session.start(socket, socket);
            }).listen(0);
        }
 
        const info = this.server.address() as Net.AddressInfo;
        if (info) {
            config.debugServer = info.port;
        } else {
            config.debugServer = 0;
        }
 
        return config;
    }
 
    public dispose(): void {
        Iif (this.server) {
            this.server.close();
        }
    }
}