Skip to content

Commit

Permalink
Treat treesitter modes just like the corresponding non-treesitter mode
Browse files Browse the repository at this point in the history
  • Loading branch information
davidshepherd7 committed Dec 1, 2024
1 parent 0d969bc commit 4b79ccf
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
11 changes: 9 additions & 2 deletions electric-operator.el
Original file line number Diff line number Diff line change
Expand Up @@ -220,18 +220,25 @@ Returns a modified copy of the rule list."
Returns a modified copy of the rule list."
(electric-operator--add-rule-list initial new-rules))

(defun electric-operator--convert-treesitter-mode (major-mode-symbol)
"Convert a treesitter mode name to the equivalent regular mode"
(let ((mode-string (symbol-name major-mode-symbol)))
(if (string-match-p "-ts-mode" mode-string)
(intern (string-replace "-ts-mode" "-mode" mode-string))
major-mode-symbol)))


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

(defun electric-operator-get-rules-for-mode (major-mode-symbol)
"Get the spacing rules for major mode"
(electric-operator--trie-get-all (electric-operator-get-rules-trie-for-mode major-mode-symbol)))
(electric-operator--trie-get-all (electric-operator-get-rules-trie-for-mode (electric-operator--convert-treesitter-mode major-mode-symbol))))

(defun electric-operator-get-rules-trie-for-mode (major-mode-symbol)
"Get the spacing rules for major mode"
(gethash major-mode-symbol electric-operator--mode-rules-table))
(gethash (electric-operator--convert-treesitter-mode major-mode-symbol) electric-operator--mode-rules-table))

(defun electric-operator-add-rules-for-mode (major-mode-symbol &rest new-rules)
"Replace or add spacing rules for major mode
Expand Down
8 changes: 8 additions & 0 deletions features/c-mode-basic-operators.feature
Original file line number Diff line number Diff line change
Expand Up @@ -261,3 +261,11 @@ Feature: C basic operators
Scenario: Multiplication with pointer deref
When I type "result = foo * *bar"
Then I should see "result = foo * *bar"

# We can't test this yet because emacs doesn't ship with treesitter grammars
# and there's no easy way to install them.
@known-failure
Scenario: Treesitter mode just works
When I turn on c-ts-mode
When I type "a-b"
Then I should see "a - b"
22 changes: 22 additions & 0 deletions test/electric-operator-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,28 @@ Aenean in sem ac leo mollis blandit. xyz /=" trie) " /= "))
(should (equal (electric-operator--trie-get-all trie)
(nreverse '(" * " ("1" "2" "3") "6" "xyz" "bar"))))))

(ert-deftest rules-for-treesitter-modes ()
(should (equal
(electric-operator--convert-treesitter-mode 'c-mode)
'c-mode))

(should (equal
(electric-operator--convert-treesitter-mode 'c-ts-mode)
'c-mode))

(should (equal
(electric-operator--convert-treesitter-mode 'ts-mode)
'ts-mode))

(should (equal
(electric-operator--convert-treesitter-mode 'erts-mode)
'erts-mode))

(should (equal
(electric-operator--convert-treesitter-mode 'tsx-ts-mode)
'tsx-mode))

)

;; Local Variables:
;; nameless-current-name: "electric-operator"
Expand Down

0 comments on commit 4b79ccf

Please sign in to comment.