<commit_before>part of input_autocomplete;



/**
 * autocomplete component for input fields.
 */
class InputAutocompleteComponent extends WebComponent {
  bool inputHasFocus = false;
  List<AutocompleteChoice> _matches;
  AutocompleteChoiceRenderer _renderer;
  AutocompleteDatasource _datasource;
  int _focusedItemIndex = -1;
  
  InputAutocompleteComponent();
  
  InputAutocompleteComponent.forElement(Element element) : super.forElement(element) {
    //super.forElement(element);
  }
  
  void set renderer (AutocompleteChoiceRenderer renderer) {
    _renderer = renderer;
    print("a renderer was set: ${_renderer}");
  }
  
  AutocompleteChoiceRenderer get renderer {
    if (_renderer == null) {
      _renderer = new AutocompleteChoiceRendererImpl();
    }
    return _renderer;
  }
  
  void set datasource (AutocompleteDatasource ds) {
    _datasource = ds;
  }
  
  AutocompleteDatasource get datasource => _datasource;
  
  bool isFocused(AutocompleteChoice choice) {
    if (_focusedItemIndex < 0 || _focusedItemIndex >= _matches.length) {
      return false;
    }
    //print("isFocused? ${_matches[_focusedItemIndex] == choice}");
    return _matches[_focusedItemIndex] == choice;
  }
  
  void set choices(List choices) {
    this.datasource = new SimpleStringDatasource(choices);
    print('Choices have been set.');
  }
  
  List<AutocompleteChoice> get matches {
    if (_matches == null) {
      _doSearch();
    }
    return _matches;
  }
  
  
  void _focusNext(int next) {
    var newfocus = _focusedItemIndex + next;
    if (newfocus < 0) {
      newfocus = 0;
    }
    if (newfocus >= _matches.length) {
      newfocus = _matches.length - 1;
    }
    _focusedItemIndex = newfocus;
    watchers.dispatch();
  }
  
  void keyDown(KeyEvent event) {
    switch(event.keyCode) {
      case KeyCode.DOWN:
        _focusNext(1);
        event.preventDefault();
        break;
      case KeyCode.UP:
        _focusNext(-1);
        event.preventDefault();
        break;
      case KeyCode.ENTER:
        selectCurrentFocus();
        break;
    }
  }
  
  void selectCurrentFocus() {
    selectChoice(_matches[_focusedItemIndex]);
  }
  
  void selectChoice(AutocompleteChoice choice) {
    print("We have selected a choice: ${choice.key}");
    _input.value = choice.key;<commit_msg>use KeyboardEvent instead of KeyEvent in keyDown<commit_after>part of input_autocomplete;



/**
 * autocomplete component for input fields.
 */
class InputAutocompleteComponent extends WebComponent {
  bool inputHasFocus = false;
  List<AutocompleteChoice> _matches;
  AutocompleteChoiceRenderer _renderer;
  AutocompleteDatasource _datasource;
  int _focusedItemIndex = -1;
  
  InputAutocompleteComponent();
  
  InputAutocompleteComponent.forElement(Element element) : super.forElement(element) {
    //super.forElement(element);
  }
  
  void set renderer (AutocompleteChoiceRenderer renderer) {
    _renderer = renderer;
    print("a renderer was set: ${_renderer}");
  }
  
  AutocompleteChoiceRenderer get renderer {
    if (_renderer == null) {
      _renderer = new AutocompleteChoiceRendererImpl();
    }
    return _renderer;
  }
  
  void set datasource (AutocompleteDatasource ds) {
    _datasource = ds;
  }
  
  AutocompleteDatasource get datasource => _datasource;
  
  bool isFocused(AutocompleteChoice choice) {
    if (_focusedItemIndex < 0 || _focusedItemIndex >= _matches.length) {
      return false;
    }
    //print("isFocused? ${_matches[_focusedItemIndex] == choice}");
    return _matches[_focusedItemIndex] == choice;
  }
  
  void set choices(List choices) {
    this.datasource = new SimpleStringDatasource(choices);
    print('Choices have been set.');
  }
  
  List<AutocompleteChoice> get matches {
    if (_matches == null) {
      _doSearch();
    }
    return _matches;
  }
  
  
  void _focusNext(int next) {
    var newfocus = _focusedItemIndex + next;
    if (newfocus < 0) {
      newfocus = 0;
    }
    if (newfocus >= _matches.length) {
      newfocus = _matches.length - 1;
    }
    _focusedItemIndex = newfocus;
    watchers.dispatch();
  }
  
  void keyDown(KeyboardEvent event) {
    switch(event.keyCode) {
      case KeyCode.DOWN:
        _focusNext(1);
        event.preventDefault();
        break;
      case KeyCode.UP:
        _focusNext(-1);
        event.preventDefault();
        break;
      case KeyCode.ENTER:
        selectCurrentFocus();
        break;
    }
  }
  
  void selectCurrentFocus() {
    selectChoice(_matches[_focusedItemIndex]);
  }
  
  void selectChoice(AutocompleteChoice choice) {
    print("We have selected a choice: ${choice.key}");
    _input.value = choice.key;
{'fraction_non_alphanumeric': 0.1004723057106054, 'fraction_numerical': 0.0030055817947617003, 'mean_word_length': 3.0832602979842245, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, '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': '6332897', 'n_tokens_mistral': 1469, 'n_tokens_neox': 1412, 'n_words': 309}