<commit_before>(ns chat.client.views.new-message
  (:require [om.core :as om]
            [om.dom :as dom]
            [clojure.string :as string]
            [chat.client.views.helpers :as helpers]
            [chat.client.dispatcher :refer [dispatch!]]
            [chat.client.store :as store])
  (:import [goog.events KeyCodes]))

(defn new-message-view [config owner]
  (reify
    om/IInitState
    (init-state [_]
      {:text ""
       :highlighted-result-index -1})
    om/IRenderState
    (render-state [_ {:keys [text highlighted-result-index] :as state}]
      (let [clear-tags! (fn []
                          (om/set-state! owner :text (string/replace text #"( *#\S*)" "")))
            constrain (fn [x a z]
                        (cond
                          (> x z) z
                          (< x a) a
                          :else x))
            thread-tag-ids (-> (store/id->thread (config :thread-id))
                               :tag-ids
                               set)
            partial-tag (second (re-matches #"(?:.|\n)*#(\S*)" text))
            results (->> (store/all-tags)
                         (remove (fn [t]
                                   (contains? thread-tag-ids (t :id))))
                         (filter (fn [t]
                                   (not= -1 (.indexOf (t :name) partial-tag)))))
            highlight-next!
            (fn []
              (om/update-state! owner :highlighted-result-index
                                #(constrain (inc %) 0 (dec (count results)))))
            highlight-prev!
            (fn []
              (om/update-state! owner :highlighted-result-index
                                #(constrain (dec %) 0 (dec (count results)))))
            highlight-clear!
            (fn []
              (om/set-state! owner :highlighted-result-index -1))
            clear-text!
            (fn []
              (om/set-state! owner :text ""))
            close-autocomplete!
            (fn []<commit_msg>make tag matching more flexible<commit_after>(ns chat.client.views.new-message
  (:require [om.core :as om]
            [om.dom :as dom]
            [clojure.string :as string]
            [chat.client.views.helpers :as helpers]
            [chat.client.dispatcher :refer [dispatch!]]
            [chat.client.store :as store])
  (:import [goog.events KeyCodes]))

(defn fuzzy-matches?
  [s m]
  (letfn [(normalize [s]
            (-> (.toLowerCase s) (string/replace #"\s" "")))]
    (not= -1 (.indexOf (normalize s) (normalize m)))))

(defn new-message-view [config owner]
  (reify
    om/IInitState
    (init-state [_]
      {:text ""
       :highlighted-result-index -1})
    om/IRenderState
    (render-state [_ {:keys [text highlighted-result-index] :as state}]
      (let [clear-tags! (fn []
                          (om/set-state! owner :text (string/replace text #"( *#\S*)" "")))
            constrain (fn [x a z]
                        (cond
                          (> x z) z
                          (< x a) a
                          :else x))
            thread-tag-ids (-> (store/id->thread (config :thread-id))
                               :tag-ids
                               set)
            partial-tag (second (re-matches #"(?:.|\n)*#(\S*)" text))
            results (->> (store/all-tags)
                         (remove (fn [t]
                                   (contains? thread-tag-ids (t :id))))
                         (filter (fn [t]
                                   (fuzzy-matches? (t :name) partial-tag))))
            highlight-next!
            (fn []
              (om/update-state! owner :highlighted-result-index
                                #(constrain (inc %) 0 (dec (count results)))))
            highlight-prev!
            (fn []
              (om/update-state! owner :highlighted-result-index
                                #(constrain (dec %) 0 (dec (count results)))))
            highlight-clear!
            (fn []
              (om/set-state! owner :highlighted-result-index -1))
            clear-text!
            (fn []
              (om/set-state! owner :text ""))
            close-autocomplete!
            (fn []
{'fraction_non_alphanumeric': 0.14531548757170173, 'fraction_numerical': 0.002390057361376673, 'mean_word_length': 1.2236981934112645, 'pattern_counts': {'":': 0, '<': 5, '<?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': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6591384', 'n_tokens_mistral': 1229, 'n_tokens_neox': 1175, 'n_words': 276}