<commit_before>// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'package:analysis_server/src/edit/nnbd_migration/migration_info.dart';
import 'package:analysis_server/src/edit/nnbd_migration/offset_mapper.dart';
import 'package:meta/meta.dart';
import 'package:mustache/mustache.dart' as mustache;
import 'package:path/path.dart' as path;

/// Instrumentation display output for a library that was migrated to use
/// non-nullable types.
class InstrumentationRenderer {
  /// Display information for a library.
  final LibraryInfo libraryInfo;

  /// Information for a whole migration, so that libraries can reference each
  /// other.
  final MigrationInfo migrationInfo;

  /// Creates an output object for the given library info.
  InstrumentationRenderer(this.libraryInfo, this.migrationInfo);

  /// Builds an HTML view of the instrumentation information in [libraryInfo].
  String render() {
    Map<String, dynamic> mustacheContext = {
      'units': <Map<String, dynamic>>[],
      'links': migrationInfo.libraryLinks(libraryInfo),
      'highlightJsPath': migrationInfo.highlightJsPath(libraryInfo),
      'highlightStylePath': migrationInfo.highlightStylePath(libraryInfo),
    };
    for (var compilationUnit in libraryInfo.units) {
      mustacheContext['units'].add({
        'path': compilationUnit.path,
        'regions': _computeRegions(compilationUnit),
        'targetRegions': _computeTargetRegions(compilationUnit),
      });
    }
    return _template.renderString(mustacheContext);
  }

  /// Return a list of Mustache context, based on the [unitInfo] for both
  /// unmodified and modified regions:
  ///
  /// * 'modified': Whether this region represents modified source, or
  ///   unmodified.
  /// * 'content': The textual content of this region.
  /// * 'explanation': The Mustache context for the tooltip explaining why the
  ///   content in this region was modified.
  List<Map> _computeRegions(UnitInfo unitInfo) {
    String content = unitInfo.content;
    List<Map> regions = [];
    int previousIndex = 0;
    for (var region in unitInfo.regions) {
      int offset = region.offset;
      int length = region.length;
      if (offset > previousIndex) {
        // Display a region of unmodified content.
        regions.add({
          'modified': false,
          'content': content.substring(previousIndex, offset),
        });
        previousIndex = offset + length;
      }
      List<Map> details = [];
      for (var detail in region.details) {
        details.add({
          'description': detail.description,
          'target': _uriForTarget(detail.target),
        });
      }
      regions.add({
        'modified': true,
        'content': content.substring(offset, offset + length),
        'explanation': region.explanation,
        'details': details,
      });
    }
    if (previousIndex < content.length) {
      // Last region of unmodified content.
      regions.add({
        'modified': false,
        'content': content.substring(previousIndex),
      });
    }
    return regions;
  }

  /// Return a list of Mustache context, based on the [unitInfo] for both
  /// target and non-target regions:
  ///
  /// * 'content': The content of the region.
  /// * 'isTarget': A flag indicating whether the region has a name associated
  ///   with it.
  /// * 'target': The name of the region, if the region has a name.
  List<Map> _computeTargetRegions(UnitInfo unitInfo) {
    String content = unitInfo.content;
    OffsetMapper mapper = unitInfo.offsetMapper;
    List<NavigationTarget> targets = unitInfo.targets.toList();
    targets.sort((first, second) => first.offset.compareTo(second.offset));
    List<Map> targetRegions = [];
    int previousIndex = 0;
    for (NavigationTarget target in targets) {
      int offset = mapper.map(target.offset);
      int length = target.length;
      if (offset > previousIndex) {
        // Display a non-target region.
        targetRegions.add({
          'content': content.substring(previousIndex, offset),
          'isTarget': false,
        });
        // Add a target region.
        targetRegions.add({
          'content': content.substring(offset, offset + length),
          'isTarget': true,
          'target': 'o${target.offset}',
        });
        previousIndex = offset + length;
      }
    }
    if (previousIndex < content.length) {
      // Last non-target region.
      targetRegions.add({
        'content': content.substring(previousIndex),
        'isTarget': false,
      });
    }
    return targetRegions;
  }

  /// Return the URL that will navigate to the given [target].
  String _uriForTarget(NavigationTarget target) {
    path.Context pathContext = migrationInfo.pathContext;<commit_msg>Rename some local variables per earlier feedback<commit_after>// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'package:analysis_server/src/edit/nnbd_migration/migration_info.dart';
import 'package:analysis_server/src/edit/nnbd_migration/offset_mapper.dart';
import 'package:meta/meta.dart';
import 'package:mustache/mustache.dart' as mustache;
import 'package:path/path.dart' as path;

/// Instrumentation display output for a library that was migrated to use
/// non-nullable types.
class InstrumentationRenderer {
  /// Display information for a library.
  final LibraryInfo libraryInfo;

  /// Information for a whole migration, so that libraries can reference each
  /// other.
  final MigrationInfo migrationInfo;

  /// Creates an output object for the given library info.
  InstrumentationRenderer(this.libraryInfo, this.migrationInfo);

  /// Builds an HTML view of the instrumentation information in [libraryInfo].
  String render() {
    Map<String, dynamic> mustacheContext = {
      'units': <Map<String, dynamic>>[],
      'links': migrationInfo.libraryLinks(libraryInfo),
      'highlightJsPath': migrationInfo.highlightJsPath(libraryInfo),
      'highlightStylePath': migrationInfo.highlightStylePath(libraryInfo),
    };
    for (var compilationUnit in libraryInfo.units) {
      mustacheContext['units'].add({
        'path': compilationUnit.path,
        'regions': _computeRegions(compilationUnit),
        'targetRegions': _computeTargetRegions(compilationUnit),
      });
    }
    return _template.renderString(mustacheContext);
  }

  /// Return a list of Mustache context, based on the [unitInfo] for both
  /// unmodified and modified regions:
  ///
  /// * 'modified': Whether this region represents modified source, or
  ///   unmodified.
  /// * 'content': The textual content of this region.
  /// * 'explanation': The Mustache context for the tooltip explaining why the
  ///   content in this region was modified.
  List<Map> _computeRegions(UnitInfo unitInfo) {
    String content = unitInfo.content;
    List<Map> regions = [];
    int previousOffset = 0;
    for (var region in unitInfo.regions) {
      int offset = region.offset;
      int length = region.length;
      if (offset > previousOffset) {
        // Display a region of unmodified content.
        regions.add({
          'modified': false,
          'content': content.substring(previousOffset, offset),
        });
        previousOffset = offset + length;
      }
      List<Map> details = [];
      for (var detail in region.details) {
        details.add({
          'description': detail.description,
          'target': _uriForTarget(detail.target),
        });
      }
      regions.add({
        'modified': true,
        'content': content.substring(offset, offset + length),
        'explanation': region.explanation,
        'details': details,
      });
    }
    if (previousOffset < content.length) {
      // Last region of unmodified content.
      regions.add({
        'modified': false,
        'content': content.substring(previousOffset),
      });
    }
    return regions;
  }

  /// Return a list of Mustache context, based on the [unitInfo] for both
  /// target and non-target regions:
  ///
  /// * 'content': The content of the region.
  /// * 'isTarget': A flag indicating whether the region has a name associated
  ///   with it.
  /// * 'target': The name of the region, if the region has a name.
  List<Map> _computeTargetRegions(UnitInfo unitInfo) {
    String content = unitInfo.content;
    OffsetMapper mapper = unitInfo.offsetMapper;
    List<NavigationTarget> targets = unitInfo.targets.toList();
    targets.sort((first, second) => first.offset.compareTo(second.offset));
    List<Map> targetRegions = [];
    int previousOffset = 0;
    for (NavigationTarget target in targets) {
      int offset = mapper.map(target.offset);
      int length = target.length;
      if (offset > previousOffset) {
        // Display a non-target region.
        targetRegions.add({
          'content': content.substring(previousOffset, offset),
          'isTarget': false,
        });
        // Add a target region.
        targetRegions.add({
          'content': content.substring(offset, offset + length),
          'isTarget': true,
          'target': 'o${target.offset}',
        });
        previousOffset = offset + length;
      }
    }
    if (previousOffset < content.length) {
      // Last non-target region.
      targetRegions.add({
        'content': content.substring(previousOffset),
        'isTarget': false,
      });
    }
    return targetRegions;
  }

  /// Return the URL that will navigate to the given [target].
  String _uriForTarget(NavigationTarget target) {
    path.Context pathContext = migrationInfo.pathContext;
{'fraction_non_alphanumeric': 0.11295073641442356, 'fraction_numerical': 0.0012188928390045707, 'mean_word_length': 2.83859649122807, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 27, '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': '6650285', 'n_tokens_mistral': 2684, 'n_tokens_neox': 2644, 'n_words': 805}