@@ -160,6 +160,14 @@ current sexp."
160
160
:safe #'booleanp
161
161
:type 'boolean )
162
162
163
+ (defcustom clojure-ts-use-metadata-for-privacy nil
164
+ " If nil, `clojure-ts-cycle-privacy' will use (defn- f []).
165
+
166
+ If t, it will use (defn ^:private f [])."
167
+ :package-version '(clojure-ts-mode . " 0.4.0" )
168
+ :safe #'booleanp
169
+ :type 'boolean )
170
+
163
171
(defcustom clojure-ts-align-reader-conditionals nil
164
172
" Whether to align reader conditionals, as if they were maps."
165
173
:package-version '(clojure-ts-mode . " 0.4" )
@@ -1480,6 +1488,13 @@ If JUSTIFY is non-nil, justify as well as fill the paragraph."
1480
1488
" map_lit" " ns_map_lit" " vec_lit" " set_lit" )
1481
1489
" A regular expression that matches nodes that can be treated as lists." )
1482
1490
1491
+ (defun clojure-ts--defun-node-p (node )
1492
+ " Return TRUE if NODE is a function or a var definition."
1493
+ (and (clojure-ts--list-node-p node)
1494
+ (let ((sym (clojure-ts--node-child-skip-metadata node 0 )))
1495
+ (string-match-p (rx bol (or " def" " defn" " defn-" ) eol)
1496
+ (clojure-ts--named-node-text sym)))))
1497
+
1483
1498
(defconst clojure-ts--markdown-inline-sexp-nodes
1484
1499
'(" inline_link" " full_reference_link" " collapsed_reference_link"
1485
1500
" uri_autolink" " email_autolink" " shortcut_link" " image"
@@ -1490,7 +1505,8 @@ If JUSTIFY is non-nil, justify as well as fill the paragraph."
1490
1505
`((clojure
1491
1506
(sexp ,(regexp-opt clojure-ts--sexp-nodes))
1492
1507
(list ,(regexp-opt clojure-ts--list-nodes))
1493
- (text ,(regexp-opt '(" comment" ))))
1508
+ (text ,(regexp-opt '(" comment" )))
1509
+ (defun ,#'clojure-ts--defun-node-p ))
1494
1510
(when clojure-ts-use-markdown-inline
1495
1511
(markdown-inline
1496
1512
(sexp ,(regexp-opt clojure-ts--markdown-inline-sexp-nodes))))))
@@ -1991,6 +2007,23 @@ value is `clojure-ts-thread-all-but-last'."
1991
2007
(interactive " P" )
1992
2008
(clojure-ts--thread-all " ->> " but-last))
1993
2009
2010
+ (defun clojure-ts-cycle-privacy ()
2011
+ " Make a definition at point public or private."
2012
+ (interactive )
2013
+ (if-let* ((node-at-point (treesit-node-at (point ) 'clojure t ))
2014
+ (defun-node (treesit-parent-until node-at-point 'defun t )))
2015
+ (save-excursion
2016
+ (goto-char (treesit-node-start defun-node))
2017
+ (search-forward-regexp (rx " (def" (? " n" ) (? (group (or " -" " ^:private" )))))
2018
+ (if (match-string 1 )
2019
+ (replace-match " " nil nil nil 1 )
2020
+ (goto-char (match-end 0 ))
2021
+ (insert (if (or clojure-ts-use-metadata-for-privacy
2022
+ (equal (match-string 0 ) " (def" ))
2023
+ " ^:private"
2024
+ " -" ))))
2025
+ (user-error " No defun at point" )))
2026
+
1994
2027
(defvar clojure-ts-refactor-map
1995
2028
(let ((map (make-sparse-keymap )))
1996
2029
(keymap-set map " C-t" #'clojure-ts-thread )
@@ -2001,6 +2034,8 @@ value is `clojure-ts-thread-all-but-last'."
2001
2034
(keymap-set map " f" #'clojure-ts-thread-first-all )
2002
2035
(keymap-set map " C-l" #'clojure-ts-thread-last-all )
2003
2036
(keymap-set map " l" #'clojure-ts-thread-last-all )
2037
+ (keymap-set map " C-p" #'clojure-ts-cycle-privacy )
2038
+ (keymap-set map " p" #'clojure-ts-cycle-privacy )
2004
2039
map)
2005
2040
" Keymap for `clojure-ts-mode' refactoring commands." )
2006
2041
@@ -2012,6 +2047,7 @@ value is `clojure-ts-thread-all-but-last'."
2012
2047
(easy-menu-define clojure-ts-mode-menu map " Clojure[TS] Mode Menu"
2013
2048
'(" Clojure"
2014
2049
[" Align expression" clojure-ts-align]
2050
+ [" Cycle privacy" clojure-ts-cycle-privacy]
2015
2051
(" Refactor -> and ->>"
2016
2052
[" Thread once more" clojure-ts-thread]
2017
2053
[" Fully thread a form with ->" clojure-ts-thread-first-all]
0 commit comments