<commit_before>// Copyright (c) 2014, 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.

library services.analyzer;

import 'dart:async';
import 'dart:io';

import 'package:analyzer/analyzer.dart';
import 'package:analyzer/src/context/builder.dart';
import 'package:analyzer/src/dart/sdk/sdk.dart';
import 'package:analyzer/src/generated/sdk.dart' as gen_sdk;
import 'package:analyzer/src/generated/constant.dart';
import 'package:analyzer/src/generated/element.dart';
import 'package:analyzer/src/generated/engine.dart' hide Logger;
import 'package:analyzer/src/generated/error.dart';
import 'package:analyzer/src/generated/parser.dart';
import 'package:analyzer/src/generated/source.dart';
import 'package:analyzer/src/string_source.dart';
import 'package:analyzer/file_system/physical_file_system.dart';

import 'package:logging/logging.dart';

import 'api_classes.dart';
import 'common.dart';
import 'pub.dart';

Logger _logger = new Logger('analyzer');

final Duration _MAX_ANALYSIS_DURATION = new Duration(seconds: 10);

// class MemoryResolver extends UriResolver {
//   Map<String, Source> sources = {};

//   @override
//   Source resolveAbsolute(Uri uri, [Uri actualUri]) {
//     return sources[uri.toString()];
//   }

//   void addFileToMap(StringSource file) {
//     sources[file.fullName] = file;
//   }

//   void clear() => sources.clear();
// }

class Analyzer {
  final Pub pub;
  bool strongMode;
  AnalysisContext _context;
  // MemoryResolver _resolver;
  String _sdkPath;

  ContentCache cache;

  Analyzer(this._sdkPath, {
    this.pub,
    this.strongMode: false
  }) {
    _reset();
    analyze('');
  }

  void _reset() {

    this.cache = new ContentCache();

    var sourceDirectory = Directory.systemTemp.createTempSync('analyzer');
    // _resolver = new MemoryResolver();

    PhysicalResourceProvider physicalResourceProvider =
              PhysicalResourceProvider.INSTANCE;

    AnalysisOptionsImpl options = new AnalysisOptionsImpl();
    options.enableGenericMethods = true;
    options.strongMode = strongMode;

    // var sdkCreator = gen_sdk.SdkCreator(options);

    var sdkManager = new gen_sdk.DartSdkManager(_sdkPath, true, (AnalysisOptions options) {
      FolderBasedDartSdk sdk = new FolderBasedDartSdk(
              physicalResourceProvider, physicalResourceProvider.getFolder(_sdkPath));
      sdk.analysisOptions = options;
    });

    // MemoryResourceProvider memResourceProvider = new MemoryResourceProvider();
    var builder = new ContextBuilder(physicalResourceProvider, sdkManager, cache);

    builder.defaultOptions = options;
    // builder.fileResolverProvider = (folder) {
    //   print (folder);
    //   return _resolver;
    // };
    
    _context = builder.buildContext(sourceDirectory.path);
<commit_msg>update ContextBuilder options<commit_after>// Copyright (c) 2014, 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.

library services.analyzer;

import 'dart:async';
import 'dart:io';

import 'package:analyzer/analyzer.dart';
import 'package:analyzer/src/context/builder.dart';
import 'package:analyzer/src/dart/sdk/sdk.dart';
import 'package:analyzer/src/generated/sdk.dart' as gen_sdk;
import 'package:analyzer/src/generated/constant.dart';
import 'package:analyzer/src/generated/element.dart';
import 'package:analyzer/src/generated/engine.dart' hide Logger;
import 'package:analyzer/src/generated/error.dart';
import 'package:analyzer/src/generated/parser.dart';
import 'package:analyzer/src/generated/source.dart';
import 'package:analyzer/src/string_source.dart';
import 'package:analyzer/file_system/physical_file_system.dart';

import 'package:logging/logging.dart';

import 'api_classes.dart';
import 'common.dart';
import 'pub.dart';

Logger _logger = new Logger('analyzer');

final Duration _MAX_ANALYSIS_DURATION = new Duration(seconds: 10);

// class MemoryResolver extends UriResolver {
//   Map<String, Source> sources = {};

//   @override
//   Source resolveAbsolute(Uri uri, [Uri actualUri]) {
//     return sources[uri.toString()];
//   }

//   void addFileToMap(StringSource file) {
//     sources[file.fullName] = file;
//   }

//   void clear() => sources.clear();
// }

class Analyzer {
  final Pub pub;
  bool strongMode;
  AnalysisContext _context;
  // MemoryResolver _resolver;
  String _sdkPath;

  ContentCache cache;

  Analyzer(this._sdkPath, {
    this.pub,
    this.strongMode: false
  }) {
    _reset();
    analyze('');
  }

  void _reset() {

    this.cache = new ContentCache();

    var sourceDirectory = Directory.systemTemp.createTempSync('analyzer');
    // _resolver = new MemoryResolver();

    PhysicalResourceProvider physicalResourceProvider =
              PhysicalResourceProvider.INSTANCE;

    AnalysisOptionsImpl analysisOptions = new AnalysisOptionsImpl();
    analysisOptions.strongMode = strongMode;

    ContextBuilderOptions builderOptions = new ContextBuilderOptions();
    builderOptions.defaultOptions = analysisOptions;

    // var sdkCreator = gen_sdk.SdkCreator(options);

    var sdkManager = new gen_sdk.DartSdkManager(_sdkPath, true, (AnalysisOptions options) {
      FolderBasedDartSdk sdk = new FolderBasedDartSdk(
              physicalResourceProvider, physicalResourceProvider.getFolder(_sdkPath));
      sdk.analysisOptions = options;
    });

    // MemoryResourceProvider memResourceProvider = new MemoryResourceProvider();
    var builder = new ContextBuilder(
        physicalResourceProvider, sdkManager, cache, options: builderOptions);

    // builder.fileResolverProvider = (folder) {
    //   print (folder);
    //   return _resolver;
    // };
    
    _context = builder.buildContext(sourceDirectory.path);

{'fraction_non_alphanumeric': 0.11607592810347724, 'fraction_numerical': 0.0020157903578027886, 'mean_word_length': 4.106346483704974, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '4972218', 'n_tokens_mistral': 1794, 'n_tokens_neox': 1739, 'n_words': 393}