Skip to content

Commit

Permalink
Add back fix from logseq#6147 along with its test cases
Browse files Browse the repository at this point in the history
Also fix testing sexp and reset state
  • Loading branch information
logseq-cldwalker authored and tiensonqin committed Nov 23, 2022
1 parent 674022d commit 3440a3d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 deletions.
4 changes: 3 additions & 1 deletion src/main/frontend/handler/editor.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -1859,7 +1859,9 @@
(state/clear-editor-action!)

;; Open "Search page or New page" auto-complete
(= last-input-char commands/hashtag)
(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)))))
(do
(state/set-editor-action-data! {:pos (cursor/get-caret-pos input)})
(state/set-editor-last-pos! pos)
Expand Down
39 changes: 30 additions & 9 deletions src/test/frontend/handler/editor_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -96,24 +96,45 @@
(handle-last-input-handler {:value "first \nfoo::bar"
:cursor-pos (dec (count "first "))})
(is (= nil (state/get-editor-action))
"Don't autocomplete properties if typing in a block where properties already exist")
"Don't autocomplete properties if typing in a block where properties already exist"))

(testing "Tag autocompletion"
(handle-last-input-handler {:value "#"
:cursor-pos 1})
(is (= :page-search-hashtag (state/get-editor-action))
"Page search if only hashtags has been typed")
"Page search if only hashtag has been typed")

(handle-last-input-handler {:value "foo bar#"
:cursor-pos 8})
(handle-last-input-handler {:value "foo #"
:cursor-pos 5})
(is (= :page-search-hashtag (state/get-editor-action))
"Page search if hashtag has been typed at EOL")

(handle-last-input-handler {:value "#Some words"
:cursor-pos 1})
(is (= :page-search-hashtag (state/get-editor-action))
"Page search if hashtags has been typed as EOL")
"Page search if hashtag is at start of line and there are existing words")

(handle-last-input-handler {:value "foo #"
:cursor-pos 5})
(is (= :page-search-hashtag (state/get-editor-action))
"Page search if hashtags has been typed after a space")
"Page search if hashtag is at EOL and after a space")

(handle-last-input-handler {:value "foo#bar"
:cursor-pos 4})
(handle-last-input-handler {:value "foo #bar"
:cursor-pos 5})
(is (= :page-search-hashtag (state/get-editor-action))
"Page search if hashtags has been typed in the middle of a line")))
"Page search if hashtag is in middle of line and after a space")

(handle-last-input-handler {:value "String#" :cursor-pos 7})
(is (= nil (state/get-editor-action))
"No page search if hashtag has been typed at end of a word")

(handle-last-input-handler {:value "foo#bar" :cursor-pos 4})
(is (= nil (state/get-editor-action))
"No page search if hashtag is in middle of word")

(handle-last-input-handler {:value "`String#gsub and String#`"
:cursor-pos (dec (count "`String#gsub and String#`"))})
(is (= nil (state/get-editor-action))
"No page search within backticks"))
;; Reset state
(state/set-editor-action! nil))

0 comments on commit 3440a3d

Please sign in to comment.