Browse Source

Fix command completion firing too often

Only fire at the beginning of a line or when starting a new word
Gabriel Horner 3 years ago
parent
commit
475d4ce7d9
2 changed files with 27 additions and 3 deletions
  1. 9 3
      src/main/frontend/handler/editor.cljs
  2. 18 0
      src/test/frontend/handler/editor_test.cljs

+ 9 - 3
src/main/frontend/handler/editor.cljs

@@ -311,7 +311,7 @@
         block (merge block
                      (block/parse-title-and-body uuid format pre-block? (:block/content block)))
         properties (:block/properties block)
-        properties (if (and (= format :markdown) 
+        properties (if (and (= format :markdown)
                             (number? (:heading properties)))
                      (dissoc properties :heading)
                      properties)
@@ -1810,6 +1810,10 @@
                  (state/set-editor-op! nil)))
              500))))
 
+(defn- start-of-new-word?
+  [input pos]
+  (contains? #{" " "\t"} (get (.-value input) (- pos 2))))
+
 (defn handle-last-input []
   (let [input           (state/get-input)
         pos             (cursor/pos input)
@@ -1822,8 +1826,10 @@
     (cond
       ;; By default, "/" is also used as namespace separator in Logseq.
       (and (= last-input-char (state/get-editor-command-trigger))
-           (not (contains? #{:page-search-hashtag} (state/sub :editor/action))))
+           #_(not (contains? #{:page-search-hashtag} (state/sub :editor/action)))
+           (or (= 1 pos) (start-of-new-word? input pos)))
       (do
+        (prn :NEW-WORD? (or (= 1 pos) (start-of-new-word? input pos)))
         (state/set-editor-action-data! {:pos (cursor/get-caret-pos input)})
         (commands/reinit-matched-commands!)
         (state/set-editor-show-commands!))
@@ -1856,7 +1862,7 @@
       ;; Open "Search page or New page" auto-complete
       (and (= last-input-char commands/hashtag)
            ;; Only trigger at beginning of line or before whitespace
-           (or (= 1 pos) (contains? #{" " "\t"} (get (.-value input) (- pos 2)))))
+           (or (= 1 pos) (start-of-new-word? input pos)))
       (do
         (state/set-editor-action-data! {:pos (cursor/get-caret-pos input)})
         (state/set-editor-last-pos! pos)

+ 18 - 0
src/test/frontend/handler/editor_test.cljs

@@ -98,6 +98,24 @@
     (is (= nil (state/get-editor-action))
         "Don't autocomplete properties if typing in a block where properties already exist"))
 
+  (testing "Command autocompletion"
+    (handle-last-input-handler {:value "/"})
+    (is (= :commands (state/get-editor-action))
+        "Command search if only / has been typed")
+
+    (handle-last-input-handler {:value "some words /"})
+    (is (= :commands (state/get-editor-action))
+        "Command search on start of new word")
+
+    (handle-last-input-handler {:value "https://"})
+    (is (= nil (state/get-editor-action))
+        "No command search in middle of a word")
+
+    (handle-last-input-handler {:value "#blah/"})
+    (is (= nil (state/get-editor-action))
+        "No command search after a tag search to allow for namespace completion")
+    )
+
   (testing "Tag autocompletion"
     (handle-last-input-handler {:value "#"
                                 :cursor-pos 1})