Skip to content

Commit 1dbda74

Browse files
Treat treesitter modes just like the corresponding non-treesitter mode
1 parent 273acd3 commit 1dbda74

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

electric-operator.el

+9-2
Original file line numberDiff line numberDiff line change
@@ -220,18 +220,25 @@ Returns a modified copy of the rule list."
220220
Returns a modified copy of the rule list."
221221
(electric-operator--add-rule-list initial new-rules))
222222

223+
(defun electric-operator--convert-treesitter-mode (major-mode-symbol)
224+
"Convert a treesitter mode name to the equivalent regular mode"
225+
(let ((mode-string (symbol-name major-mode-symbol)))
226+
(if (string-match-p "-ts-mode" mode-string)
227+
(intern (replace-regexp-in-string "-ts-mode" "-mode" mode-string))
228+
major-mode-symbol)))
229+
223230

224231
;; All rule manipulation should be done through these functions and not by
225232
;; using puthash/gethash directly because it's plausible that the
226233
;; underlying data structure could be changed (e.g. to an alist).
227234

228235
(defun electric-operator-get-rules-for-mode (major-mode-symbol)
229236
"Get the spacing rules for major mode"
230-
(electric-operator--trie-get-all (electric-operator-get-rules-trie-for-mode major-mode-symbol)))
237+
(electric-operator--trie-get-all (electric-operator-get-rules-trie-for-mode (electric-operator--convert-treesitter-mode major-mode-symbol))))
231238

232239
(defun electric-operator-get-rules-trie-for-mode (major-mode-symbol)
233240
"Get the spacing rules for major mode"
234-
(gethash major-mode-symbol electric-operator--mode-rules-table))
241+
(gethash (electric-operator--convert-treesitter-mode major-mode-symbol) electric-operator--mode-rules-table))
235242

236243
(defun electric-operator-add-rules-for-mode (major-mode-symbol &rest new-rules)
237244
"Replace or add spacing rules for major mode

features/c-mode-basic-operators.feature

+8
Original file line numberDiff line numberDiff line change
@@ -261,3 +261,11 @@ Feature: C basic operators
261261
Scenario: Multiplication with pointer deref
262262
When I type "result = foo * *bar"
263263
Then I should see "result = foo * *bar"
264+
265+
# We can't test this yet because emacs doesn't ship with treesitter grammars
266+
# and there's no easy way to install them.
267+
@known-failure
268+
Scenario: Treesitter mode just works
269+
When I turn on c-ts-mode
270+
When I type "a-b"
271+
Then I should see "a - b"

test/electric-operator-test.el

+22
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,28 @@ Aenean in sem ac leo mollis blandit. xyz /=" trie) " /= "))
6262
(should (equal (electric-operator--trie-get-all trie)
6363
(nreverse '(" * " ("1" "2" "3") "6" "xyz" "bar"))))))
6464

65+
(ert-deftest rules-for-treesitter-modes ()
66+
(should (equal
67+
(electric-operator--convert-treesitter-mode 'c-mode)
68+
'c-mode))
69+
70+
(should (equal
71+
(electric-operator--convert-treesitter-mode 'c-ts-mode)
72+
'c-mode))
73+
74+
(should (equal
75+
(electric-operator--convert-treesitter-mode 'ts-mode)
76+
'ts-mode))
77+
78+
(should (equal
79+
(electric-operator--convert-treesitter-mode 'erts-mode)
80+
'erts-mode))
81+
82+
(should (equal
83+
(electric-operator--convert-treesitter-mode 'tsx-ts-mode)
84+
'tsx-mode))
85+
86+
)
6587

6688
;; Local Variables:
6789
;; nameless-current-name: "electric-operator"

0 commit comments

Comments
 (0)