<commit_before>import { editor } from './editor';
import { jumpToLine } from './codemirror/tools';
import { isEmptyString, getQueryStringObject, serializeToQueryString } from '../tools/helpers';

const HIGHLIGHT_CLASS = 'editor-highlight';

let prevHighlightedLine;
let manuallyHighlighted = false;

editor.on('gutterClick', function (cm, line, gutter, event) {
    // Do work when the click occurs for the left (or main) mouse button only
    if (event.button !== 0) {
        return;
    }

    // Do work only on the line number target element
    if (!event.target.classList.contains('CodeMirror-linenumber')) {
        return;
    }

    // Shift keys will allow highlighting of multiple lines.
    if (event.shiftKey === true && prevHighlightedLine !== undefined) {
        if (prevHighlightedLine < line) {
            highlightLines(prevHighlightedLine, line);
        }
        // Handle lines clicked in non-sequential order
        // If the previously highlighted line is greater than the currently
        // clicked one, then we flip the order of arguments.
        else {
            highlightLines(line, prevHighlightedLine);
        }

        // Remember state of how this happened
        manuallyHighlighted = true;
    }
    // If shift key is not pressed or there is not a previously selected line
    // (which you need to do the whole range) then select one line.
    else {
        // If the clicked line is the same as the one before, turn it off.
        if (line === prevHighlightedLine) {
            unhighlightAll();
            prevHighlightedLine = undefined;
        }
        else {
            highlightLines(line);
            prevHighlightedLine = line;
            manuallyHighlighted = true;
        }
    }
});

/**
 * Highlights a line or a range of lines by applying a highlight class.
 *
 * @param {Number|CodeMirror.Pos} from - Required. The line number to start to
 *          highlighting from, or a CodeMirror.Pos object with the signature
 *          of { line, ch }. Lines are zero-indexed.
 * @param {Number|CodeMirror.Pos} to - Optional. The line number to end
 *          highlighting on, or a CodeMirror.Pos object with the signature
 *          of { line, ch }. If undefined or null, only the "from" line is
 *          highlighted. Lines are zero-indexed.
 * @param {Boolean} clear - Optional. Defaults to `true`, where all existing
 *          highlights are cleared first. If set to false, previous Highlights
 *          are preserved.
 */
export function highlightLines (from, to, clear = true) {
    // First, remove all existing instances of the highlight class.
    if (clear === true) {
        // Pass `false` as the second parameter to prevent query string
        // from flashing
        unhighlightAll({ updateQueryString: false });
    }

    // Set the line to start highlighting from.
    const startLine = _getLineNumber(from);

    // The end line is the same as the start line if `to` is undefined.
    // Lines are zero-indexed, so do not use "falsy" checks -- `0` is a valid
    // value for `line`, so check if value is undefined or null specifically.
    const endLine = (to !== undefined && to !== null)
        ? _getLineNumber(to) : startLine;

    function _getLineNumber (arg) {
        // If `arg` is a CodeMirror position object, use its `line` property.
        // Lines are zero-indexed, so a don't use "falsy" checks for its
        // presence -- `0` is a valid value for `line`.
        // Make sure it is converted to a number object.
        if (typeof from === 'object' && from.hasOwnProperty('line') && typeof arg.line !== 'undefined') {
            return Number(arg.line);
        }
        // Otherwise, assume the value passed is a number or string, and
        // use the `arg` as provided.
        else {
            return Number(arg);
        }
    }

    // We assume `startLine` is less than or equal to `endLine`.
    // If a range is backwards, it is ignored.
    const doc = editor.getDoc();
    for (let currentLine = startLine; currentLine <= endLine; currentLine++) {
        doc.addLineClass(currentLine, 'gutter', HIGHLIGHT_CLASS);
        doc.addLineClass(currentLine, 'background', HIGHLIGHT_CLASS);
    }
<commit_msg>Handle line highlighting when "to" is NaN<commit_after>import { editor } from './editor';
import { jumpToLine } from './codemirror/tools';
import { isEmptyString, getQueryStringObject, serializeToQueryString } from '../tools/helpers';

const HIGHLIGHT_CLASS = 'editor-highlight';

let prevHighlightedLine;
let manuallyHighlighted = false;

editor.on('gutterClick', function (cm, line, gutter, event) {
    // Do work when the click occurs for the left (or main) mouse button only
    if (event.button !== 0) {
        return;
    }

    // Do work only on the line number target element
    if (!event.target.classList.contains('CodeMirror-linenumber')) {
        return;
    }

    // Shift keys will allow highlighting of multiple lines.
    if (event.shiftKey === true && prevHighlightedLine !== undefined) {
        if (prevHighlightedLine < line) {
            highlightLines(prevHighlightedLine, line);
        }
        // Handle lines clicked in non-sequential order
        // If the previously highlighted line is greater than the currently
        // clicked one, then we flip the order of arguments.
        else {
            highlightLines(line, prevHighlightedLine);
        }

        // Remember state of how this happened
        manuallyHighlighted = true;
    }
    // If shift key is not pressed or there is not a previously selected line
    // (which you need to do the whole range) then select one line.
    else {
        // If the clicked line is the same as the one before, turn it off.
        if (line === prevHighlightedLine) {
            unhighlightAll();
            prevHighlightedLine = undefined;
        }
        else {
            highlightLines(line);
            prevHighlightedLine = line;
            manuallyHighlighted = true;
        }
    }
});

/**
 * Highlights a line or a range of lines by applying a highlight class.
 *
 * @param {Number|CodeMirror.Pos} from - Required. The line number to start to
 *          highlighting from, or a CodeMirror.Pos object with the signature
 *          of { line, ch }. Lines are zero-indexed.
 * @param {Number|CodeMirror.Pos} to - Optional. The line number to end
 *          highlighting on, or a CodeMirror.Pos object with the signature
 *          of { line, ch }. If undefined or null, only the "from" line is
 *          highlighted. Lines are zero-indexed.
 * @param {Boolean} clear - Optional. Defaults to `true`, where all existing
 *          highlights are cleared first. If set to false, previous Highlights
 *          are preserved.
 */
export function highlightLines (from, to, clear = true) {
    // First, remove all existing instances of the highlight class.
    if (clear === true) {
        // Pass `false` as the second parameter to prevent query string
        // from flashing
        unhighlightAll({ updateQueryString: false });
    }

    // Set the line to start highlighting from.
    const startLine = _getLineNumber(from);

    // The end line is the same as the start line if `to` is undefined.
    // Lines are zero-indexed, so do not use "falsy" checks -- `0` is a valid
    // value for `line`, so check if value is undefined, null or isNaN
    // specifically.
    const endLine = (to !== undefined && to !== null && !Number.isNaN(to))
        ? _getLineNumber(to) : startLine;

    function _getLineNumber (arg) {
        // If `arg` is a CodeMirror position object, use its `line` property.
        // Lines are zero-indexed, so a don't use "falsy" checks for its
        // presence -- `0` is a valid value for `line`.
        // Make sure it is converted to a number object.
        if (typeof from === 'object' && from.hasOwnProperty('line') && typeof arg.line !== 'undefined') {
            return Number(arg.line);
        }
        // Otherwise, assume the value passed is a number or string, and
        // use the `arg` as provided.
        else {
            return Number(arg);
        }
    }

    // We assume `startLine` is less than or equal to `endLine`.
    // If a range is backwards, it is ignored.
    const doc = editor.getDoc();
    for (let currentLine = startLine; currentLine <= endLine; currentLine++) {
        doc.addLineClass(currentLine, 'gutter', HIGHLIGHT_CLASS);
        doc.addLineClass(currentLine, 'background', HIGHLIGHT_CLASS);
    }

{'fraction_non_alphanumeric': 0.09743228983468168, 'fraction_numerical': 0.0007034822370735139, 'mean_word_length': 2.3876092136616363, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '5494852', 'n_tokens_mistral': 2315, 'n_tokens_neox': 2252, 'n_words': 918}