<commit_before>import { window, workspace } from "vscode";
import { TextEditor, TextEditorEdit, ViewColumn } from "vscode";

import * as constants from "../../constants";
import { ExtensionState } from "../../extension";

import { EvaluatorResult, EvaluatorResultType, XPathEvaluator } from "../xpath-evaluator";

class HistoricQuery {
    constructor(uri: string, query: string) {
        this.uri = uri;
        this.query = query;
    }

    uri: string;
    query: string;
}

export async function evaluateXPath(editor: TextEditor, edit: TextEditorEdit): Promise<void> {
    const config = workspace.getConfiguration(constants.extensionPrefix);

    // if there is no workspace, we will track queries in the global Memento
    const memento = ExtensionState.workspace || ExtensionState.global;

    // get the xpath persistence setting
    const persistQueries = config.get<boolean>(constants.configKeys.persistXPathQuery, true);

    // get the last query if there is one for this document
    // if not, try pulling the last query ran, regardless of document
    // NOTE: if the user has focus on the output channel when opening the xquery prompt, the channel is the "active" document
    const history = memento.get<HistoricQuery[]>(constants.stateKeys.xpathQueryHistory, new Array<HistoricQuery>());
    const globalLastQuery = memento.get<string>(constants.stateKeys.xPathQueryLast, "");

    const lastQuery = history.find(x => {
        return (x.uri === editor.document.uri.toString());
    });

    // set the inital display value and prompt the user
    let query = (lastQuery) ? lastQuery.query : globalLastQuery;

    query = await window.showInputBox({
        placeHolder: "XPath Query",
        prompt: "Please enter an XPath query to evaluate.",
        value: query
    });

    // showInputBox() will return undefined if the user dimissed the prompt
    if (!query) {
        return;
    }

    const ignoreDefaultNamespace = config.get<boolean>(constants.configKeys.ignoreDefaultNamespace, true);

    // run the query
    const xml = editor.document.getText();
    let evalResult: EvaluatorResult;

    try {
        evalResult = XPathEvaluator.evaluate(query, xml, ignoreDefaultNamespace);
    }

    catch (error) {
        console.error(error);
        window.showErrorMessage(`Something went wrong while evaluating the XPath: ${error}`);
        return;
    }

    // show the results to the user
    const outputChannel = window.createOutputChannel("XPath Results");

    outputChannel.clear();

    outputChannel.appendLine(`XPath Query: ${query}`);
    outputChannel.append("\n");

    if (evalResult.type === EvaluatorResultType.NODE_COLLECTION) {
        (evalResult.result as Node[]).forEach((node: any) => {
            outputChannel.appendLine(`[Line ${node.lineNumber}] ${node.localName}: ${node.textContent}`);
        });
    }

    else {
        outputChannel.appendLine(`[Result]: ${evalResult.result}`);
    }

    outputChannel.show(ViewColumn.Three);

    if (persistQueries) {
        const historicQuery = new HistoricQuery(editor.document.uri.toString(), query);<commit_msg>Remove Deprecated Method Call<commit_after>import { window, workspace } from "vscode";
import { TextEditor, TextEditorEdit, ViewColumn } from "vscode";

import * as constants from "../../constants";
import { ExtensionState } from "../../extension";

import { EvaluatorResult, EvaluatorResultType, XPathEvaluator } from "../xpath-evaluator";

class HistoricQuery {
    constructor(uri: string, query: string) {
        this.uri = uri;
        this.query = query;
    }

    uri: string;
    query: string;
}

export async function evaluateXPath(editor: TextEditor, edit: TextEditorEdit): Promise<void> {
    const config = workspace.getConfiguration(constants.extensionPrefix);

    // if there is no workspace, we will track queries in the global Memento
    const memento = ExtensionState.workspace || ExtensionState.global;

    // get the xpath persistence setting
    const persistQueries = config.get<boolean>(constants.configKeys.persistXPathQuery, true);

    // get the last query if there is one for this document
    // if not, try pulling the last query ran, regardless of document
    // NOTE: if the user has focus on the output channel when opening the xquery prompt, the channel is the "active" document
    const history = memento.get<HistoricQuery[]>(constants.stateKeys.xpathQueryHistory, new Array<HistoricQuery>());
    const globalLastQuery = memento.get<string>(constants.stateKeys.xPathQueryLast, "");

    const lastQuery = history.find(x => {
        return (x.uri === editor.document.uri.toString());
    });

    // set the inital display value and prompt the user
    let query = (lastQuery) ? lastQuery.query : globalLastQuery;

    query = await window.showInputBox({
        placeHolder: "XPath Query",
        prompt: "Please enter an XPath query to evaluate.",
        value: query
    });

    // showInputBox() will return undefined if the user dimissed the prompt
    if (!query) {
        return;
    }

    const ignoreDefaultNamespace = config.get<boolean>(constants.configKeys.ignoreDefaultNamespace, true);

    // run the query
    const xml = editor.document.getText();
    let evalResult: EvaluatorResult;

    try {
        evalResult = XPathEvaluator.evaluate(query, xml, ignoreDefaultNamespace);
    }

    catch (error) {
        console.error(error);
        window.showErrorMessage(`Something went wrong while evaluating the XPath: ${error}`);
        return;
    }

    // show the results to the user
    const outputChannel = window.createOutputChannel("XPath Results");

    outputChannel.clear();

    outputChannel.appendLine(`XPath Query: ${query}`);
    outputChannel.append("\n");

    if (evalResult.type === EvaluatorResultType.NODE_COLLECTION) {
        (evalResult.result as Node[]).forEach((node: any) => {
            outputChannel.appendLine(`[Line ${node.lineNumber}] ${node.localName}: ${node.textContent}`);
        });
    }

    else {
        outputChannel.appendLine(`[Result]: ${evalResult.result}`);
    }

    outputChannel.show(false);

    if (persistQueries) {
        const historicQuery = new HistoricQuery(editor.document.uri.toString(), query);
{'fraction_non_alphanumeric': 0.11409825572091535, 'fraction_numerical': 0.0, 'mean_word_length': 3.3736878936319106, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7040960', 'n_tokens_mistral': 1714, 'n_tokens_neox': 1695, 'n_words': 490}