<commit_before>import * as Yaml from 'yaml-ast-parser-custom-tags';
import { Schema, Type } from 'js-yaml';

import { filterInvalidCustomTags } from './arrUtils';
import { emit } from 'process';

export const DUPLICATE_KEY_REASON = 'duplicate key';

/**
 * An individual YAML diagnostic,
 * after formatting.
 */
export interface YAMLDocDiagnostic {
    message: string
    range: {
        start: {
            line: number
            character: number
        }
        end: {
            line: number
            character: number
        }
    }
    severity: number
}

/**
 * Convert a YAML node exception to a
 * language server diagnostic.
 */
function exceptionToDiagnostic(e: Yaml.YAMLException): YAMLDocDiagnostic {
    const exceptionSnippet = e.mark.getSnippet();
    const trimSnippet = exceptionSnippet
                                .replace(/ /g, '')
                                .replace(/\n/g, '')
                                .replace(/\^/g, '');
    const exception = {
        exception: e,
        string: e.toString(),
        mark: {
            mark: e.mark,
            string: e.mark.toString(),
            markBuffer: e.mark.buffer,
            snippet: {
                snippet: trimSnippet,
                length: trimSnippet.length
            },
            toLineEnd: e.mark.toLineEnd
        }
    };
    console.log(exception);

    const line = e.mark.line;
    /**
     * I think this calculation is wrong???
     */
    // const character = e.mark.position + e.mark.column === 0 ? 0 : e.mark.position + e.mark.column - 1;
    const startPos = e.mark.column;
    const endPos = e.mark.column + trimSnippet.length;
    /**
     * Something funny going on here -- why would these
     * errors start and end at the same position?
     */
    const diagnostic = {
        message: `${e.reason}`,
        range: {
            start: {
                line,
                character: startPos
            },
            end: {
                line,
                character: endPos
            },
        },
        severity: 2
    };
    console.log(diagnostic);
    return diagnostic;
}

/**
 * We have to convert the exceptions returned by the AST parser
 * into diagnostics for consumption by the server client.
 */
export function formatErrors(exceptions: Yaml.YAMLException[]) {
    return exceptions
            .filter(e => e.reason !== DUPLICATE_KEY_REASON && !e.isWarning)
            .map(e => exceptionToDiagnostic(e));
}

//Patch ontop of yaml-ast-parser to disable duplicate key message on merge key
export function isDuplicateAndNotMergeKey (error: Yaml.YAMLException, yamlText: string) {
    const errorStart = error.mark.position;
    const errorEnd = error.mark.position + error.mark.column;
    if (error.reason === DUPLICATE_KEY_REASON && yamlText.substring(errorStart, errorEnd).startsWith('<<')) {
        return false;
    }
    return true;
}

export function formatWarnings(exceptions: Yaml.YAMLException[], text: string) {
    return exceptions<commit_msg>Clean up, fix an error<commit_after>import * as Yaml from 'yaml-ast-parser-custom-tags';
import { Schema, Type } from 'js-yaml';

import { filterInvalidCustomTags } from './arrUtils';
import { emit } from 'process';

export const DUPLICATE_KEY_REASON = 'duplicate key';

/**
 * An individual YAML diagnostic,
 * after formatting.
 */
export interface YAMLDocDiagnostic {
    message: string
    range: {
        start: {
            line: number
            character: number
        }
        end: {
            line: number
            character: number
        }
    }
    severity: number
}

/**
 * Convert a YAML node exception to a
 * language server diagnostic.
 */
function exceptionToDiagnostic(e: Yaml.YAMLException): YAMLDocDiagnostic {
    // The exceptions from the AST produce WEIRD text snippets.
    // This undoes the strange formatting.
    const formatSnippet = (snippet: string) => snippet
                                                .replace(/ /g, '')
                                                .replace(/\n/g, '')
                                                .replace(/\^/g, '');
    const exceptionSnippet = e.mark.getSnippet();
    const snippet = formatSnippet(exceptionSnippet);

    const line = e.mark.line === 1 ? 0 : e.mark.line;
    let startPos;
    let endPos;

    // Use the snippet to calculate the diagnostic position
    // if it's available. Some exceptions return empty snippets.
    // In the event of an empty snippet, use the old logic.
    if (snippet.length > 0) {
        startPos = e.mark.column;
        endPos = e.mark.column + snippet.length;
    } else {
        const pos = e.mark.position + e.mark.column === 0 ? 0 : e.mark.position + e.mark.column - 1;
        startPos = pos;
        endPos = pos;
    }

    return {
        message: `${e.reason}`,
        range: {
            start: {
                line,
                character: startPos
            },
            end: {
                line,
                character: endPos
            },
        },
        severity: 2
    };
}

/**
 * We have to convert the exceptions returned by the AST parser
 * into diagnostics for consumption by the server client.
 */
export function formatErrors(exceptions: Yaml.YAMLException[]) {
    return exceptions
            .filter(e => e.reason !== DUPLICATE_KEY_REASON && !e.isWarning)
            .map(e => exceptionToDiagnostic(e));
}

//Patch ontop of yaml-ast-parser to disable duplicate key message on merge key
export function isDuplicateAndNotMergeKey (error: Yaml.YAMLException, yamlText: string) {
    const errorStart = error.mark.position;
    const errorEnd = error.mark.position + error.mark.column;
    if (error.reason === DUPLICATE_KEY_REASON && yamlText.substring(errorStart, errorEnd).startsWith('<<')) {
        return false;
    }
    return true;
}

export function formatWarnings(exceptions: Yaml.YAMLException[], text: string) {
    return exceptions
{'fraction_non_alphanumeric': 0.10744217230975528, 'fraction_numerical': 0.0018437814280925244, 'mean_word_length': 1.9671805072103432, 'pattern_counts': {'":': 0, '<': 7, '<?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': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3855866', 'n_tokens_mistral': 1696, 'n_tokens_neox': 1637, 'n_words': 465}