<commit_before>/*jshint asi:true */
'use strict';

var fs = require('fs')
  , path = require('path')
  , highlight = require('..').highlight
  , colorize = require('../colorize')
  , diffFile = path.join(__dirname, 'fixtures', 'git-diff.txt') 
  , diff = fs.readFileSync(diffFile, 'utf-8')


// @@ is not a valid js token, so when we see it, we can be sure that we are dealing with a git or svn diff
var diffRegex = /^@@[^@]+@@$/m;
var diffIndRegex = /^(@@[^@]+@@)(.*)$/;
var lines = diff.split('\n');

function isDiff(lines) {
  return !!lines
    .filter(function (line) { 
      return diffRegex.test(line); 
    })
    .length;
}
var diff = isDiff(lines);
function tryHighlight(code) {
  try {
    return highlight(code);
  } catch (e) {
    return code; 
  }
}

function highlightDiff(line) {
  var matches = diffIndRegex.exec(line)
    , highlightedDiffInd = colorize.brightBlue(matches[1])
    , code = matches[2];

  return code ? highlightedDiffInd + tryHighlight(code) : highlightedDiffInd;
}

console.log(
  highlightDiff('@@ -25,22 +31,47 @@ function resolveTheme (config) { }')
)

var highlightedLines = lines
  .map(function (line) {
    var code = diff ? line.slice(1) : code
      , highlighted;
    try {
      highlighted = highlight(code);
    } catch (e) {
      highlighted = code;
    }
    return diff ? colorize.yellow(line[0]) + highlighted : highlighted;
  });

//console.log(highlightedLines.join('\n'));<commit_msg>fixing incomplete lines<commit_after>/*jshint asi:true */
'use strict';

var fs = require('fs')
  , path = require('path')
  , highlight = require('..').highlight
  , colorize = require('../colorize')
  , diffFile = path.join(__dirname, 'fixtures', 'git-diff.txt') 
  , diff = fs.readFileSync(diffFile, 'utf-8')


// @@ is not a valid js token, so when we see it, we can be sure that we are dealing with a git or svn diff
var diffRegex = /^@@[^@]+@@$/m;
var diffIndRegex = /^(@@[^@]+@@)(.*)$/;
var addRemRegex = /^[+\-]/;
var lines = diff.split('\n');

function isDiff(lines) {
  return !!lines
    .filter(function (line) { 
      return diffRegex.test(line); 
    })
    .length;
}

var diff = isDiff(lines);
function tryHighlight(code) {

  function tryAppending(appended, tryNext) {
    var success;
    try {
       success = highlight(code + appended);
       return success;
    } catch (e) {
      return tryNext(code);
    }
  }

  function tryRemoveLeadingComma(tryNext) {
    var success;
    try {
       success = highlight(code.replace(/^( +),(.+)$/, '$1 $2'));
       return success;
    } catch (e) {
      return tryNext(code);
    }
  }

  function tryPlain() { return tryAppending('', tryCloseMustache); }

  function tryCloseMustache() { return tryAppending('}', tryCloseParen); }

  function tryCloseParen() { return tryAppending(')', tryCloseMustacheParen); }

  function tryCloseMustacheParen() { return tryAppending('})', tryRemovingCommas);}

  function tryRemovingCommas() { return tryRemoveLeadingComma(giveUp); }

  function giveUp() { return code; }

  return tryPlain();
}

function highlightDiffInd(line, matches) {
  var highlighted = colorize.brightBlue(matches[1])
    , code = matches[2];
  return code ? highlighted + tryHighlight(code) : highlighted;
}

function colorizeAddRemove(c) {
  return addRemRegex.test(c) ? colorize.yellow(c) : c;
}

function highlightDiff(line) {
  var diffIndMatches = diffIndRegex.exec(line);

  return diffIndMatches 
    ? highlightDiffInd(line, diffIndMatches)
    : colorizeAddRemove(line[0]) + tryHighlight(line.slice(1));
}

console.log(
  highlightDiff('@@ -25,22 +31,47 @@ function resolveTheme (config) { }')
)

var highlighter = diff ? highlightDiff : tryHighlight;
var highlightedLines = lines.map(highlighter);

console.log(highlightedLines.join('\n'));
{'fraction_non_alphanumeric': 0.16158940397350993, 'fraction_numerical': 0.007417218543046358, 'mean_word_length': 3.714107365792759, 'pattern_counts': {'":': 0, '<': 3, '<?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': '1869689', 'n_tokens_mistral': 1286, 'n_tokens_neox': 1231, 'n_words': 295}