Valid clojure code causes emacs hang
Opening a clojure file with the following function in clojure-ts-mode:
(defn unescape
[s]
(-> s
(string/replace "&" "&")
(string/replace "*" "*")
(string/replace "^" "^")
(string/replace "_" "_")
(string/replace "~" "~")
(string/replace "<" "<")
(string/replace ">" ">")
(string/replace "[" "[")
(string/replace "]" "]")
(string/replace "(" "(")
(string/replace ")" ")")
(string/replace """ "\"")))
..causes emacs to hang for over 10 minutes. Any attempt to edit the file once emacs has sprung back to life will cause another 10+ minutes of waiting. During these 10 minutes I can see emacs saturating a CPU core.
I have reproduced this with a minimal project containing just one file with just this one function, and with a stripped down emacs init.el just containing:
(package-install 'clojure-ts-mode)
(use-package clojure-ts-mode)
Interestingly clojure-ts-mode seems to be perfectly ok with the following rewrite:
(defn unescape
[s]
(string/replace s #"&|*|^|_|~|<|>|[|]|(|)|""
{"&" "&"
"*" "*"
"^" "^"
"_" "_"
"~" "~"
"<" "<"
">" ">"
"[" "["
"]" "]"
"(" "("
")" ")"
""" "\""}))
Expected behavior
File opens fine, like it does in clojure-mode
Actual behavior
Emacs hangs
Steps to reproduce the problem
Put above function into a file and open it in clojure-ts-mode
clojure-ts-mode version
0.5.1
tree-sitter-clojure grammar version
unstable-20250526 (the version emacs installs when starting clojure-ts-mode)
Emacs version
30.2
Operating system
Arch Linux
I'm not entirely sure, but it looks like an Emacs bug to me:
The more expressions I uncomment the less responsive it becomes. The profiler points at treesit-font-lock-fontify-region, but I couldn't find anything suspicious in this function's code. I tried to disable fontification of strings, but it didn't change anything.
I'll try to report it to Emacs' bug tracker and get some help.
https://debbugs.gnu.org/cgi/bugreport.cgi?bug=79823
@kthu the issue should be fixed in the next MELPA version.