Skip to content

Introduce clojure-ts-toggle-keyword-string #91

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
- [#89](https://github.com/clojure-emacs/clojure-ts-mode/pull/89): Introduce `clojure-ts-thread`, `clojure-ts-thread-first-all` and
`clojure-ts-thread-last-all`.
- [#90](https://github.com/clojure-emacs/clojure-ts-mode/pull/90): Introduce `clojure-ts-cycle-privacy`.
- [#91](https://github.com/clojure-emacs/clojure-ts-mode/pull/91): Introduce `clojure-ts-cycle-keyword-string`.

## 0.3.0 (2025-04-15)

Expand Down
20 changes: 12 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -393,20 +393,24 @@ threading macro.

### Cycling things

`clojure-ts-cycle-keyword-string`: Convert the string at point to a keyword and
vice versa.

`clojure-ts-cycle-privacy`: Cycle privacy of `def`s or `defn`s. Use metadata
explicitly with setting `clojure-ts-use-metadata-for-defn-privacy` to `t` for
`defn`s too.

### Default keybindings

| Keybinding | Command |
|:----------------------------|:------------------------------|
| `C-c SPC` | `clojure-ts-align` |
| `C-c C-r t` / `C-c C-r C-t` | `clojure-ts-thread` |
| `C-c C-r u` / `C-c C-r C-u` | `clojure-ts-unwind` |
| `C-c C-r f` / `C-c C-r C-f` | `clojure-ts-thread-first-all` |
| `C-c C-r l` / `C-c C-r C-l` | `clojure-ts-thread-last-all` |
| `C-c C-r p` / `C-c C-r C-p` | `clojure-ts-cycle-privacy` |
| Keybinding | Command |
|:----------------------------|:----------------------------------|
| `C-:` | `clojure-ts-cycle-keyword-string` |
| `C-c SPC` | `clojure-ts-align` |
| `C-c C-r t` / `C-c C-r C-t` | `clojure-ts-thread` |
| `C-c C-r u` / `C-c C-r C-u` | `clojure-ts-unwind` |
| `C-c C-r f` / `C-c C-r C-f` | `clojure-ts-thread-first-all` |
| `C-c C-r l` / `C-c C-r C-l` | `clojure-ts-thread-last-all` |
| `C-c C-r p` / `C-c C-r C-p` | `clojure-ts-cycle-privacy` |

### Customize refactoring commands prefix

Expand Down
18 changes: 18 additions & 0 deletions clojure-ts-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -2032,6 +2032,22 @@ value is `clojure-ts-thread-all-but-last'."
"-"))))
(user-error "No defun at point")))

(defun clojure-ts-cycle-keyword-string ()
"Convert the string at point to a keyword, or vice versa."
(interactive)
(let ((node (treesit-thing-at-point 'sexp 'nested))
(pos (point)))
(cond
((clojure-ts--string-node-p node)
(if (string-match-p " " (treesit-node-text node t))
(user-error "Cannot convert a string containing spaces to keyword")
(insert ?: (substring (clojure-ts--delete-and-extract-sexp) 1 -1))))
((clojure-ts--keyword-node-p node)
(insert ?\" (substring (clojure-ts--delete-and-extract-sexp) 1) ?\"))
(t
(user-error "No string or keyword at point")))
(goto-char pos)))

(defvar clojure-ts-refactor-map
(let ((map (make-sparse-keymap)))
(keymap-set map "C-t" #'clojure-ts-thread)
Expand All @@ -2050,10 +2066,12 @@ value is `clojure-ts-thread-all-but-last'."
(defvar clojure-ts-mode-map
(let ((map (make-sparse-keymap)))
;;(set-keymap-parent map clojure-mode-map)
(keymap-set map "C-:" #'clojure-ts-cycle-keyword-string)
(keymap-set map "C-c SPC" #'clojure-ts-align)
(keymap-set map clojure-ts-refactor-map-prefix clojure-ts-refactor-map)
(easy-menu-define clojure-ts-mode-menu map "Clojure[TS] Mode Menu"
'("Clojure"
["Toggle between string & keyword" clojure-ts-cycle-keyword-string]
["Align expression" clojure-ts-align]
["Cycle privacy" clojure-ts-cycle-privacy]
("Refactor -> and ->>"
Expand Down
31 changes: 31 additions & 0 deletions test/clojure-ts-mode-cycling-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,37 @@
(require 'buttercup)
(require 'test-helper "test/test-helper")

(describe "clojure-ts-cycle-keyword-string"
(when-refactoring-with-point-it "should convert string to keyword"
"\"hel|lo\""

":hel|lo"

(clojure-ts-cycle-keyword-string))

(when-refactoring-with-point-it "should convert keyword to string"
":|hello"

"\"|hello\""

(clojure-ts-cycle-keyword-string))

(it "should signal a user error when there is nothing to convert at point"
(with-clojure-ts-buffer "[true false]"
(goto-char 2)
(expect (clojure-ts-cycle-keyword-string)
:to-throw
'user-error
'("No string or keyword at point"))))

(it "should signal a user error when string at point contains spaces"
(with-clojure-ts-buffer "\"Hello world\""
(goto-char 2)
(expect (clojure-ts-cycle-keyword-string)
:to-throw
'user-error
'("Cannot convert a string containing spaces to keyword")))))

(describe "clojure-ts-cycle-privacy"

(when-refactoring-it "should turn a public defn into a private defn"
Expand Down