UNPKG

@eagleoutice/flowr-dev

Version:

Static Dataflow Analyzer and Program Slicer for the R Programming Language

51 lines 2.61 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.sliceDiffAnsi = sliceDiffAnsi; const range_1 = require("../../util/range"); const assert_1 = require("../../util/assert"); const ansi_1 = require("../../util/text/ansi"); function grayOut() { return ansi_1.ansiFormatter.getFormatString({ color: 7 /* Colors.White */, effect: ansi_1.ColorEffect.Foreground, style: 2 /* FontStyles.Faint */ }); } function mergeJointRangesInSorted(loc) { return loc.reduce((acc, curr) => { const last = acc[acc.length - 1]; if (range_1.SourceRange.overlap(last.location, curr.location)) { acc[acc.length - 1] = { selected: curr.selected || last.selected, location: range_1.SourceRange.merge([last.location, curr.location]) }; } else { acc.push(curr); } return acc; }, [loc[0]]); } function highlight(s, selected) { const primary = ansi_1.ansiFormatter.format(s, { color: 3 /* Colors.Yellow */, effect: ansi_1.ColorEffect.Foreground, style: 1 /* FontStyles.Bold */ }); return selected ? ansi_1.ansiFormatter.format(primary, { style: 4 /* FontStyles.Underline */ }) : primary; } /** * Print a slice diff with ANSI colors */ function sliceDiffAnsi(slice, normalized, criteriaIds, originalCode) { let importantLocations = Array.from(normalized.idMap.entries()) .filter(([id, { location }]) => slice.has(id) && (0, assert_1.isNotUndefined)(location)) .map(([id, { location }]) => ({ selected: criteriaIds.has(id), location: location })); if (importantLocations.length === 0) { return `${grayOut()}${originalCode}${ansi_1.ansiFormatter.reset()}`; } // we sort all locations from back to front so that replacements do not screw up the indices importantLocations.sort((a, b) => -range_1.SourceRange.compare(a.location, b.location)); // we need to merge all ranges that overlap, otherwise even reversed traversal can still crew us up importantLocations = mergeJointRangesInSorted(importantLocations); const lines = originalCode.split('\n'); for (const { selected, location } of importantLocations) { const [sl, sc, , ec] = location; const line = lines[sl - 1]; lines[sl - 1] = `${line.slice(0, Math.max(0, sc - 1))}${ansi_1.ansiFormatter.reset()}${highlight(line.slice(Math.max(0, sc - 1), ec), selected)}${grayOut()}${line.slice(Math.max(0, ec))}`; } return `${grayOut()}${lines.join('\n')}${ansi_1.ansiFormatter.reset()}`; } //# sourceMappingURL=slice-diff-ansi.js.map