<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.completion.computer.dart.combinator;

import 'dart:async';

import 'package:analysis_server/src/protocol_server.dart' hide Element,
    ElementKind;
import 'package:analysis_server/src/services/completion/dart_completion_manager.dart';
import 'package:analysis_server/src/services/completion/suggestion_builder.dart';
import 'package:analyzer/src/generated/ast.dart';
import 'package:analyzer/src/generated/element.dart';

/**
 * A computer for calculating `completion.getSuggestions` request results
 * for the import combinators show and hide.
 */

class CombinatorComputer extends DartCompletionComputer {

  @override
  bool computeFast(DartCompletionRequest request) {
    return false;
  }

  @override
  Future<bool> computeFull(DartCompletionRequest request) {
    return request.node.accept(new _CombinatorAstVisitor(request));
  }
}

/**
 * A visitor for determining which imported classes and top level variables
 * should be suggested and building those suggestions.
 */
class _CombinatorAstVisitor extends GeneralizingAstVisitor<Future<bool>> {
  final DartCompletionRequest request;

  _CombinatorAstVisitor(this.request);

  @override
  Future<bool> visitCombinator(Combinator node) {
    return _addCombinatorSuggestions(node);
  }

  @override
  Future<bool> visitNode(AstNode node) {
    return new Future.value(false);
  }

  @override
  Future<bool> visitSimpleIdentifier(SimpleIdentifier node) {
    return node.parent.accept(this);
  }

  Future _addCombinatorSuggestions(Combinator node) {
    var directive = node.getAncestor((parent) => parent is NamespaceDirective);
    if (directive is NamespaceDirective) {
      LibraryElement library = directive.uriElement;
      LibraryElementSuggestionBuilder.suggestionsFor(
          request,
          CompletionSuggestionKind.IDENTIFIER,
          library);
      return new Future.value(true);
    }
    return new Future.value(false);
  }
}<commit_msg>move combinator suggestion ast traversal into computeFast<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.completion.computer.dart.combinator;

import 'dart:async';

import 'package:analysis_server/src/protocol_server.dart' hide Element,
    ElementKind;
import 'package:analysis_server/src/services/completion/dart_completion_manager.dart';
import 'package:analysis_server/src/services/completion/suggestion_builder.dart';
import 'package:analyzer/src/generated/ast.dart';
import 'package:analyzer/src/generated/element.dart';

/**
 * A computer for calculating `completion.getSuggestions` request results
 * for the import combinators show and hide.
 */
class CombinatorComputer extends DartCompletionComputer {
  _CombinatorSuggestionBuilder builder;

  @override
  bool computeFast(DartCompletionRequest request) {
    builder = request.node.accept(new _CombinatorAstVisitor(request));
    return builder == null;
  }

  @override
  Future<bool> computeFull(DartCompletionRequest request) {
    if (builder != null) {
      return builder.execute(request.node);
    }
    return new Future.value(false);
  }
}

/**
 * A visitor for determining which imported classes and top level variables
 * should be suggested and building those suggestions.
 */
class _CombinatorAstVisitor extends
    GeneralizingAstVisitor<_CombinatorSuggestionBuilder> {
  final DartCompletionRequest request;

  _CombinatorAstVisitor(this.request);

  @override
  _CombinatorSuggestionBuilder visitCombinator(Combinator node) {
    return new _CombinatorSuggestionBuilder(
        request,
        CompletionSuggestionKind.IDENTIFIER);
  }

  @override
  _CombinatorSuggestionBuilder visitNode(AstNode node) {
    return null;
  }

  @override
  _CombinatorSuggestionBuilder visitSimpleIdentifier(SimpleIdentifier node) {
    return node.parent.accept(this);
  }
}

/**
 * A `_CombinatorSuggestionBuilder` determines which imported classes
 * and top level variables should be suggested and builds those suggestions.
 * This operation is instantiated during `computeFast`
 * and calculates the suggestions during `computeFull`.
 */
class _CombinatorSuggestionBuilder extends LibraryElementSuggestionBuilder {

  _CombinatorSuggestionBuilder(DartCompletionRequest request,
      CompletionSuggestionKind kind)
      : super(request, kind);

  Future<bool> execute(AstNode node) {
    var directive = node.getAncestor((parent) => parent is NamespaceDirective);
    if (directive is NamespaceDirective) {
      LibraryElement library = directive.uriElement;
      if (library != null) {
        library.visitChildren(this);
      }
    }
    return new Future.value(false);
  }
}
{'fraction_non_alphanumeric': 0.08233890214797136, 'fraction_numerical': 0.0015910898965791568, 'mean_word_length': 4.650561797752809, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 14, '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': '5937842', 'n_tokens_mistral': 1451, 'n_tokens_neox': 1357, 'n_words': 367}