Fix tag completion at start of line or whitespace before cursor

This commit is contained in:
Gabriel Horner
2022-07-22 17:17:35 -04:00
committed by Andelf
parent bc69e7d5a1
commit 6a64cdf4b3
2 changed files with 34 additions and 15 deletions

View File

@@ -2758,7 +2758,7 @@
(do (util/stop e)
(autopair input-id key format nil))
(and hashtag? (re-find #"\s+$" value))
(and hashtag? (or (zero? pos) (re-matches #"\s" (get value (dec pos)))))
(do
(commands/handle-step [:editor/search-page-hashtag])
(if (= key "#")

View File

@@ -47,31 +47,50 @@
(defn- keydown-not-matched-handler
"Spied version of editor/keydown-not-matched-handler"
[{:keys [value key format] :or {format "markdown"}}]
(with-redefs [util/get-selected-text (constantly false)
state/get-input (constantly #js {:value value})
cursor/get-caret-pos (constantly {})]
((editor/keydown-not-matched-handler format)
#js {:key key} nil)))
[{:keys [value key format cursor-pos] :or {key "#" format "markdown"}}]
;; Reset editor action in order to test result
(state/set-editor-action! nil)
;; Default cursor pos to end of line
(let [pos (or cursor-pos (count value))]
(with-redefs [util/get-selected-text (constantly false)
state/get-input (constantly #js {:value value})
cursor/pos (constantly pos)
cursor/get-caret-pos (constantly {})]
((editor/keydown-not-matched-handler format)
#js {:key key} nil))))
(deftest keydown-not-matched-handler-test
(testing "Tag autocompletion"
(keydown-not-matched-handler {:value "Some words " :key "#"})
(keydown-not-matched-handler {:value "Some words "})
(is (= :page-search-hashtag (state/get-editor-action))
"Autocomplete tags if starting new word")
(state/set-editor-action! nil)
(keydown-not-matched-handler {:value "String" :key "#"})
(keydown-not-matched-handler {:value ""})
(is (= :page-search-hashtag (state/get-editor-action))
"Autocomplete tags if starting a new line")
(keydown-not-matched-handler {:value "Some words" :cursor-pos 0})
(is (= :page-search-hashtag (state/get-editor-action))
"Autocomplete tags if there is are existing words and cursor is at start of line")
(keydown-not-matched-handler {:value "Some words" :cursor-pos 5})
(is (= :page-search-hashtag (state/get-editor-action))
"Autocomplete tags if there is whitespace before cursor")
(keydown-not-matched-handler {:value "String"})
(is (= nil (state/get-editor-action))
"Don't autocomplete tags if not starting a new word")
(state/set-editor-action! nil)
"Don't autocomplete tags if at end of word")
(keydown-not-matched-handler {:value "`One backtick " :key "#"})
(keydown-not-matched-handler {:value "String" :cursor-pos 3})
(is (= nil (state/get-editor-action))
"Don't autocomplete tags if in middle of word")
(keydown-not-matched-handler {:value "`One backtick "})
(is (= :page-search-hashtag (state/get-editor-action))
"Autocomplete tags if only one backtick")
(state/set-editor-action! nil)
(keydown-not-matched-handler {:value "`String#gsub and String`" :key "#"})
(keydown-not-matched-handler {:value "`String#gsub and String`"
:cursor-pos (dec (count "`String#gsub and String`"))})
(is (= nil (state/get-editor-action))
"Don't autocomplete tags within backticks")
(state/set-editor-action! nil)))