All files / vsl/parser indicator.js

0% Statements 0/32
0% Branches 0/20
0% Functions 0/1
0% Lines 0/32
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 66 67 68 69 70                                                                                                                                           
/**
 * Highlights a given index range given a code
 *
 * @param {string} code - The specific code which will be used to determine the
 *                        create the index highlight
 * @param {number[]} index - An array in the form of [start, end] which specify
 *                           the indexes to highlight
 * 
 * @return {Object} contains the needed indices and lines
 * @property {string[]} lines the lines to view
 * @property {number} relativeLine the relative index of the starting line
 * @property {number} startIndex the index to start highlighting at
 * @property {number} relativeEnd the relative end to startIndex to end highlight
 */
export default function bound (code: string, index: number) {
    let i = 0,
        lines = [],
        lineNumber = 0,
        previous = '',
        numberOfBoundingLines = 2,
        keepCurrentLine = false,
        line = 0,
        relativeLine = -1,
        currentLineIndex = 0,
        start = 0,
        startIndex = 0;
        
    while (i <= code.length) {
        if (code[i] === '\n' || i === code.length) {
            // If excess lines & k is false (keep)
            if (lines.length >= numberOfBoundingLines && !keepCurrentLine) {
                lines.shift();
                start++;
            }
            
            if (relativeLine < 0 && keepCurrentLine)
                relativeLine = lines.length;
            lines.push(previous);
            
            lineNumber++;
            currentLineIndex = i + 1;
            
            // Stop if > t
            if (keepCurrentLine && lineNumber - line > numberOfBoundingLines)
                break;
            
            previous = '';
        } else {
            previous += code[i];
        }
        
        if (i === index[0]) {
            startIndex = i - currentLineIndex;
            keepCurrentLine = true;
        }
        
        if (i < index[1])
            line = lineNumber;
        
        i++;
    }
    
    return {
        lines,
        start,
        relativeLine,
        startIndex: startIndex,
        relativeEnd: index[1] - index[0]
    };
}