<commit_before>'use strict';

var Generator = require('source-map').SourceMapGenerator;

/**
 * Apply changes to the content.
 * @param {string} content The content to process
 * @param {{additions:Array, removals:Array}} pending A hash of additions and removals
 * @param {string} quoteChar String literal character to use
 * @returns {{content:string, generator:SourceMapGenerator}} Amended content and source-map generator
 */
function applyAlterations(sourcePath, content, pending, quoteChar) {

  // additions will be the '"ngInject";' directive
  var directive = quoteChar + 'ngInject' + quoteChar + ';';

  // create a list containing each line and any line terminator relating to it
  //  changes will mutate this list
  var lines = content
    .split(/(\r?\n)/)
    .reduce(mergeByTwos, []);

  // create a source-map generator to track changes in the source
  //  changes will mutate this instance
  var generator = new Generator();

  // if we make later changes first then the remaining locations will not be changed
  pending.additions.map(encode(addition))
    .concat(pending.removals.map(encode(removal)))
    .sort(startLocDescending)
    .forEach(executeItem);

  return {
    content  : lines.join(''),
    generator: generator
  };

  /**
   * Add "ngInject" directive syntax at the given location.
   * @param {{line:number, column:number}} start A start location from where we might insert
   * @param {{line:number, column:number}} end An end location to where we might insert
   */
  function addition(start, end) {

    // zero-based start and end indices
    var startZero = zeroBased(start),
        endZero   = zeroBased(end);

    // look at the first line and determine whether we should annotate in the first line
    var fnDeclaration       = lines[startZero.line].slice(0, startZero.column),
        blockOuterFirstLine = lines[startZero.line].slice(startZero.column),
        blockOuterPrefix    = blockOuterFirstLine.match(/^\s*\{/)[0],
        blockInnerFirstLine = blockOuterFirstLine.slice(blockOuterPrefix.length),
        isInline            = !(/^\s*$/.test(blockInnerFirstLine));

    // comment in the same line
    if (isInline) {
      lines[startZero.line] = fnDeclaration + blockOuterPrefix + directive + blockInnerFirstLine;
      generator.addMapping({
        source   : sourcePath,
        original : start,
        generated: oneBased({
          line  : startZero.line,
          column: startZero.column + directive.length
        })
      });
    }
    // comment on a new line
    else {
      var iLineFirst = startZero.line + 1,
          iLineLast  = endZero.line,
          text       = lines[iLineFirst],
          indent     = lines.slice(iLineFirst, iLineLast).reduce(reduceIndent, text),
          eol        = text.match(/\r?\n/)[0],
          insertion  = indent + directive + eol;

      // text change
      lines.splice(iLineFirst, 0, insertion);

      // source-map change
      generator.addMapping({
        source   : sourcePath,
        original : start,
        generated: oneBased({
          line  : iLineFirst,
          column: startZero.column + insertion.length
        })
      });
    }
  }

  /**
   * Remove @ngInject comment at the given location.
   * @param {{line:number, column:number}} start A start location from where we might remove
   * @param {{line:number, column:number}} end An end location to where we might remove
   */
  function removal(start, end) {

    // zero-based start and end indices
    var startZero = zeroBased(start),
        endZero   = zeroBased(end);

    // text change
    lines.slice(endZero.line, endZero.line + 1)
      .forEach(eachLine);

    // source-map change
    generator.addMapping({
      source   : sourcePath,
      original : end,
      generated: start<commit_msg>working but source-maps are untested<commit_after>'use strict';

var Generator = require('source-map').SourceMapGenerator;

/**
 * Apply changes to the content.
 * @param {string} content The content to process
 * @param {{additions:Array, removals:Array}} pending A hash of additions and removals
 * @param {string} quoteChar String literal character to use
 * @returns {{content:string, generator:SourceMapGenerator}} Amended content and source-map generator
 */
function applyAlterations(sourcePath, content, pending, quoteChar) {

  // additions will be the '"ngInject";' directive
  var directive = quoteChar + 'ngInject' + quoteChar + ';';

  // create a list containing each line and any line terminator relating to it
  //  changes will mutate this list
  var lines = content
    .split(/(\r?\n)/)
    .reduce(mergeByTwos, []);

  // create a source-map generator to track changes in the source
  //  changes will mutate this instance
  var generator = new Generator();

  // if we make later changes first then the remaining locations will not be changed
  pending.additions.map(encode(addition))
    .concat(pending.removals.map(encode(removal)))
    .sort(startLocDescending)
    .forEach(executeItem);

  return {
    content  : lines.join(''),
    generator: generator
  };

  /**
   * Add "ngInject" directive syntax at the given location.
   * @param {{line:number, column:number}} start A start location from where we might insert
   * @param {{line:number, column:number}} end An end location to where we might insert
   */
  function addition(start, end) {

    // zero-based start and end indices
    var startZero = zeroBased(start),
        endZero   = zeroBased(end);

    // look at the first line and determine whether we should annotate in the first line
    var fnDeclaration       = lines[startZero.line].slice(0, startZero.column),
        blockOuterFirstLine = lines[startZero.line].slice(startZero.column),
        blockOuterPrefix    = blockOuterFirstLine.match(/^\s*\{/)[0],
        blockInnerFirstLine = blockOuterFirstLine.slice(blockOuterPrefix.length),
        isInline            = !(/^\s*$/.test(blockInnerFirstLine));

    // comment in the same line
    if (isInline) {
      lines[startZero.line] = fnDeclaration + blockOuterPrefix + directive + blockInnerFirstLine;
      generator.addMapping({
        source   : sourcePath,
        original : start,
        generated: {
          line  : start.line,
          column: start.column + directive.length
        }
      });
    }
    // comment on a new line
    else {
      var indexFirst = startZero.line + 1,
          indexLast  = endZero.line - 1,
          text       = lines[indexFirst],
          indent     = lines.slice(indexFirst, indexLast + 1).reduce(reduceIndent, text),
          eol        = text.match(/\r?\n/)[0],
          insertion  = indent + directive + eol;

      // text change
      lines.splice(indexFirst, 0, insertion);

      // source-map change
      generator.addMapping({
        source   : sourcePath,
        original : oneBased({
          line  : indexFirst,
          column: 0
        }),
        generated: oneBased({
          line  : indexFirst + 1,
          column: 0
        })
      });
    }
  }

  /**
   * Remove @ngInject comment at the given location.
   * @param {{line:number, column:number}} start A start location from where we might remove
   * @param {{line:number, column:number}} end An end location to where we might remove
   */
  function removal(start, end) {

    // zero-based start and end indices
    var startZero = zeroBased(start),
        endZero   = zeroBased(end);

    // text change
    lines.slice(endZero.line, endZero.line + 1)
      .forEach(eachLine);

    // source-map change
    generator.addMapping({
      source   : sourcePath,
      original : end,
      generated: start
{'fraction_non_alphanumeric': 0.10767019469489089, 'fraction_numerical': 0.0022213511041421666, 'mean_word_length': 2.6798076923076923, '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': '5968725', 'n_tokens_mistral': 2171, 'n_tokens_neox': 2112, 'n_words': 652}