<commit_before>import Component from '@ember/component';
import { bindKeyboardShortcuts, unbindKeyboardShortcuts } from 'ember-keyboard-shortcuts';
import { isPresent } from '@ember/utils';
import { scheduleOnce } from '@ember/runloop';

export default Component.extend({

  tagName: 'section',
  elementId: 'new-message',

  message: '',
  usernames: null,

  keyboardShortcuts: Object.freeze({
    'tab': 'completeUsername'
  }),

  didInsertElement () {
    this._super(...arguments);
    bindKeyboardShortcuts(this);
  },

  willDestroyElement () {
    this._super(...arguments);
    unbindKeyboardShortcuts(this);
  },

  actions: {

    completeUsername () {
      let input = this.element.querySelector('#message-field');
      let cursorPosition = input.selectionStart;
      let textBeforeCursor = this.message.slice(0, cursorPosition);
      let textAfterCursor = this.message.slice(cursorPosition);
      let words = textBeforeCursor.split(' ');
      let searchWord = words.pop();

      if (isPresent(searchWord)) {
        let username = this.usernames.find(username => {
          return username.toLowerCase().startsWith(searchWord.toLowerCase());
        });

        if (isPresent(username)) {
          // add a colon when inserting the username in the beginning
          if (words.length === 0) {
            username = `${username}: `;
          }

          let lengthDiff = username.length - searchWord.length;
          let newCursorPosition = cursorPosition + lengthDiff;

          words.push(username);
          let newMessage = `${words.join(' ')}${textAfterCursor}`;
          this.set('message', newMessage);

          // set the cursor right behind the inserted username,
          // but we have to wait for the update of the input first
          scheduleOnce('afterRender', this, function () {
            input.focus();
            input.setSelectionRange(newCursorPosition, newCursorPosition);
          });
        }
      }
    }
<commit_msg>Simplify completeUsername function and use consts<commit_after>import Component from '@ember/component';
import { bindKeyboardShortcuts, unbindKeyboardShortcuts } from 'ember-keyboard-shortcuts';
import { isPresent } from '@ember/utils';
import { scheduleOnce } from '@ember/runloop';

export default Component.extend({

  tagName: 'section',
  elementId: 'new-message',

  message: '',
  usernames: null,

  keyboardShortcuts: Object.freeze({
    'tab': 'completeUsername'
  }),

  didInsertElement () {
    this._super(...arguments);
    bindKeyboardShortcuts(this);
  },

  willDestroyElement () {
    this._super(...arguments);
    unbindKeyboardShortcuts(this);
  },

  actions: {

    completeUsername () {
      const input = this.element.querySelector('#message-field');
      const cursorPosition = input.selectionStart;
      const textBeforeCursor = this.message.slice(0, cursorPosition);
      const textAfterCursor = this.message.slice(cursorPosition);
      const words = textBeforeCursor.split(' ');
      const searchWord = words.pop();

      if (isEmpty(searchWord)) return;

      let username = this.usernames.find(username => {
        return username.toLowerCase().startsWith(searchWord.toLowerCase());
      });

      if (isEmpty(username)) return;

      // add a colon when inserting the username in the beginning
      if (words.length === 0) {
        username = `${username}: `;
      }

      const lengthDiff = username.length - searchWord.length;
      const newCursorPosition = cursorPosition + lengthDiff;

      words.push(username);
      const newMessage = `${words.join(' ')}${textAfterCursor}`;
      this.set('message', newMessage);

      // set the cursor right behind the inserted username,
      // but we have to wait for the update of the input first
      scheduleOnce('afterRender', this, function () {
        input.focus();
        input.setSelectionRange(newCursorPosition, newCursorPosition);
      });
    }

{'fraction_non_alphanumeric': 0.12271805273833672, 'fraction_numerical': 0.0010141987829614604, 'mean_word_length': 3.0337423312883436, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '3093543', 'n_tokens_mistral': 1064, 'n_tokens_neox': 1049, 'n_words': 253}