<commit_before>/*---------------------------------------------------------
 * Copyright (C) Microsoft Corporation. All rights reserved.
 *--------------------------------------------------------*/

import vscode = require('vscode');
import utils = require('../utils');
import Settings = require('../settings');
import { dirname } from 'path';
import { IFeature } from '../feature';
import { SessionManager } from '../session';
import { OperatingSystem, PlatformDetails, getPlatformDetails } from '../platform';

import { LanguageClient, RequestType, NotificationType } from 'vscode-languageclient';
import { CancellationToken, DebugConfiguration, DebugConfigurationProvider,
    ExtensionContext, ProviderResult, WorkspaceFolder } from 'vscode';

export class DebugSessionFeature implements IFeature, DebugConfigurationProvider {

    private sessionCount: number = 1;
    private command: vscode.Disposable;
    private examplesPath: string;

    constructor(context: ExtensionContext, private sessionManager: SessionManager) {
        // Register a debug configuration provider
        context.subscriptions.push(vscode.debug.registerDebugConfigurationProvider('PowerShell', this));
    }

    public setLanguageClient(languageClient: LanguageClient) {
    }

    public dispose() {
        this.command.dispose();
    }

    // DebugConfigurationProvider method
    resolveDebugConfiguration(
        folder: WorkspaceFolder | undefined,
        config: DebugConfiguration,
        token?: CancellationToken): ProviderResult<DebugConfiguration> {

        let currentDocument = vscode.window.activeTextEditor.document;
        let debugCurrentScript = (config.script === "${file}") || !config.request;
        let generateLaunchConfig = !config.request;

        var settings = Settings.load();
        let createNewIntegratedConsole = settings.debugging.createTemporaryIntegratedConsole;

        if (config.request === "attach") {
            let platformDetails = getPlatformDetails();
            let versionDetails = this.sessionManager.getPowerShellVersionDetails();

            if (versionDetails.edition.toLowerCase() === "core" &&
                platformDetails.operatingSystem !== OperatingSystem.Windows) {

                let msg = "PowerShell Core only supports attaching to a host process on Windows.";
                return vscode.window.showErrorMessage(msg).then(_ => {
                    return undefined;
                });
            }
        }

        if (generateLaunchConfig) {
            // No launch.json, create the default configuration for both unsaved (Untitled) and saved documents.
            config.type = 'PowerShell';
            config.name = 'PowerShell Launch Current File';
            config.request = 'launch';
            config.args = [];

            config.script =
                currentDocument.isUntitled
                    ? currentDocument.uri.toString()
                    : currentDocument.fileName;

            // For a folder-less workspace, vscode.workspace.rootPath will be undefined.
            // PSES will convert that undefined to a reasonable working dir.
            config.cwd =
                currentDocument.isUntitled
                    ? vscode.workspace.rootPath
                    : currentDocument.fileName;
        }

        if (config.request === 'launch') {

            // For debug launch of "current script" (saved or unsaved), warn before starting the debugger if either
            // A) the unsaved document's language type is not PowerShell or
            // B) the saved document's extension is a type that PowerShell can't debug.
            if (debugCurrentScript) {

                if (currentDocument.isUntitled) {
                    if (currentDocument.languageId === 'powershell') {
                        if (!generateLaunchConfig) {
                            // Cover the case of existing launch.json but unsaved (Untitled) document.
                            // In this case, vscode.workspace.rootPath will not be undefined.
                            config.script = currentDocument.uri.toString();
                            config.cwd = vscode.workspace.rootPath
                        }
                    }
                    else {
                        let msg = "To debug '" + currentDocument.fileName +
                                  "', change the document's language mode to PowerShell or save the file with a PowerShell extension.";
                        vscode.window.showErrorMessage(msg);
                        return;
                    }
                }
                else {
                    let isValidExtension = false;
                    let extIndex = currentDocument.fileName.lastIndexOf('.');
                    if (extIndex !== -1) {
                        let ext = currentDocument.fileName.substr(extIndex + 1).toUpperCase();
                        isValidExtension = (ext === "PS1" || ext === "PSM1");
                    }

                    if ((currentDocument.languageId !== 'powershell') || !isValidExtension) {
                        let path = currentDocument.fileName;
                        let workspaceRootPath = vscode.workspace.rootPath;
                        if (currentDocument.fileName.startsWith(workspaceRootPath)) {
                            path = currentDocument.fileName.substring(vscode.workspace.rootPath.length + 1);
                        }

                        let msg = "'" + path + "' is a file type that cannot be debugged by the PowerShell debugger.";
                        vscode.window.showErrorMessage(msg);
                        return;
                    }
                }
            }

            if (config.createTemporaryIntegratedConsole !== undefined) {
                createNewIntegratedConsole = config.createTemporaryIntegratedConsole;
            }
        }

        // Prevent the Debug Console from opening
        config.internalConsoleOptions = "neverOpen";

        // Create or show the interactive console
        vscode.commands.executeCommand('PowerShell.ShowSessionConsole', true);

        var sessionFilePath = utils.getDebugSessionFilePath();

        if (createNewIntegratedConsole) {
            var debugProcess =
                this.sessionManager.createDebugSessionProcess(
                    sessionFilePath,
                    settings);

            debugProcess
                .start(`DebugSession-${this.sessionCount++}`)
                .then(
                    sessionDetails => {<commit_msg>Workaround VSCode bug where ${script} var not expanded (#1095)<commit_after>/*---------------------------------------------------------
 * Copyright (C) Microsoft Corporation. All rights reserved.
 *--------------------------------------------------------*/

import vscode = require('vscode');
import utils = require('../utils');
import Settings = require('../settings');
import { dirname } from 'path';
import { IFeature } from '../feature';
import { SessionManager } from '../session';
import { OperatingSystem, PlatformDetails, getPlatformDetails } from '../platform';

import { LanguageClient, RequestType, NotificationType } from 'vscode-languageclient';
import { CancellationToken, DebugConfiguration, DebugConfigurationProvider,
    ExtensionContext, ProviderResult, WorkspaceFolder } from 'vscode';

export class DebugSessionFeature implements IFeature, DebugConfigurationProvider {

    private sessionCount: number = 1;
    private command: vscode.Disposable;
    private examplesPath: string;

    constructor(context: ExtensionContext, private sessionManager: SessionManager) {
        // Register a debug configuration provider
        context.subscriptions.push(vscode.debug.registerDebugConfigurationProvider('PowerShell', this));
    }

    public setLanguageClient(languageClient: LanguageClient) {
    }

    public dispose() {
        this.command.dispose();
    }

    // DebugConfigurationProvider method
    resolveDebugConfiguration(
        folder: WorkspaceFolder | undefined,
        config: DebugConfiguration,
        token?: CancellationToken): ProviderResult<DebugConfiguration> {

        let currentDocument = vscode.window.activeTextEditor.document;
        let debugCurrentScript = (config.script === "${file}") || !config.request;
        let generateLaunchConfig = !config.request;

        var settings = Settings.load();
        let createNewIntegratedConsole = settings.debugging.createTemporaryIntegratedConsole;

        if (config.request === "attach") {
            let platformDetails = getPlatformDetails();
            let versionDetails = this.sessionManager.getPowerShellVersionDetails();

            if (versionDetails.edition.toLowerCase() === "core" &&
                platformDetails.operatingSystem !== OperatingSystem.Windows) {

                let msg = "PowerShell Core only supports attaching to a host process on Windows.";
                return vscode.window.showErrorMessage(msg).then(_ => {
                    return undefined;
                });
            }
        }

        if (generateLaunchConfig) {
            // No launch.json, create the default configuration for both unsaved (Untitled) and saved documents.
            config.type = 'PowerShell';
            config.name = 'PowerShell Launch Current File';
            config.request = 'launch';
            config.args = [];

            config.script =
                currentDocument.isUntitled
                    ? currentDocument.uri.toString()
                    : currentDocument.fileName;

            // For a folder-less workspace, vscode.workspace.rootPath will be undefined.
            // PSES will convert that undefined to a reasonable working dir.
            config.cwd =
                currentDocument.isUntitled
                    ? vscode.workspace.rootPath
                    : currentDocument.fileName;
        }

        if (config.request === 'launch') {

            // For debug launch of "current script" (saved or unsaved), warn before starting the debugger if either
            // A) the unsaved document's language type is not PowerShell or
            // B) the saved document's extension is a type that PowerShell can't debug.
            if (debugCurrentScript) {

                if (currentDocument.isUntitled) {
                    if (currentDocument.languageId === 'powershell') {
                        if (!generateLaunchConfig) {
                            // Cover the case of existing launch.json but unsaved (Untitled) document.
                            // In this case, vscode.workspace.rootPath will not be undefined.
                            config.script = currentDocument.uri.toString();
                            config.cwd = vscode.workspace.rootPath
                        }
                    }
                    else {
                        let msg = "To debug '" + currentDocument.fileName +
                                  "', change the document's language mode to PowerShell or save the file with a PowerShell extension.";
                        vscode.window.showErrorMessage(msg);
                        return;
                    }
                }
                else {
                    let isValidExtension = false;
                    let extIndex = currentDocument.fileName.lastIndexOf('.');
                    if (extIndex !== -1) {
                        let ext = currentDocument.fileName.substr(extIndex + 1).toUpperCase();
                        isValidExtension = (ext === "PS1" || ext === "PSM1");
                    }

                    if ((currentDocument.languageId !== 'powershell') || !isValidExtension) {
                        let path = currentDocument.fileName;
                        let workspaceRootPath = vscode.workspace.rootPath;
                        if (currentDocument.fileName.startsWith(workspaceRootPath)) {
                            path = currentDocument.fileName.substring(vscode.workspace.rootPath.length + 1);
                        }

                        let msg = "'" + path + "' is a file type that cannot be debugged by the PowerShell debugger.";
                        vscode.window.showErrorMessage(msg);
                        return;
                    }

                    if (config.script === "${file}") {
                        config.script = currentDocument.fileName;
                    }
                }
            }

            if (config.cwd === "${file}") {
                config.cwd = currentDocument.fileName;
            }

            if (config.createTemporaryIntegratedConsole !== undefined) {
                createNewIntegratedConsole = config.createTemporaryIntegratedConsole;
            }
        }

        // Prevent the Debug Console from opening
        config.internalConsoleOptions = "neverOpen";

        // Create or show the interactive console
        vscode.commands.executeCommand('PowerShell.ShowSessionConsole', true);

        var sessionFilePath = utils.getDebugSessionFilePath();

        if (createNewIntegratedConsole) {
            var debugProcess =
                this.sessionManager.createDebugSessionProcess(
                    sessionFilePath,
                    settings);

            debugProcess
                .start(`DebugSession-${this.sessionCount++}`)
                .then(
                    sessionDetails => {
{'fraction_non_alphanumeric': 0.10331224413844436, 'fraction_numerical': 0.001190919240788984, 'mean_word_length': 1.7392456676860346, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1755584', 'n_tokens_mistral': 3054, 'n_tokens_neox': 2901, 'n_words': 770}