@@ -160,6 +160,14 @@ current sexp."
160160 :safe #'booleanp
161161 :type 'boolean )
162162
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+
163171(defcustom clojure-ts-align-reader-conditionals nil
164172 " Whether to align reader conditionals, as if they were maps."
165173 :package-version '(clojure-ts-mode . " 0.4" )
@@ -1480,6 +1488,13 @@ If JUSTIFY is non-nil, justify as well as fill the paragraph."
14801488 " map_lit" " ns_map_lit" " vec_lit" " set_lit" )
14811489 " A regular expression that matches nodes that can be treated as lists." )
14821490
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+
14831498(defconst clojure-ts--markdown-inline-sexp-nodes
14841499 '(" inline_link" " full_reference_link" " collapsed_reference_link"
14851500 " uri_autolink" " email_autolink" " shortcut_link" " image"
@@ -1490,7 +1505,8 @@ If JUSTIFY is non-nil, justify as well as fill the paragraph."
14901505 `((clojure
14911506 (sexp ,(regexp-opt clojure-ts--sexp-nodes))
14921507 (list ,(regexp-opt clojure-ts--list-nodes))
1493- (text ,(regexp-opt '(" comment" ))))
1508+ (text ,(regexp-opt '(" comment" )))
1509+ (defun ,#'clojure-ts--defun-node-p ))
14941510 (when clojure-ts-use-markdown-inline
14951511 (markdown-inline
14961512 (sexp ,(regexp-opt clojure-ts--markdown-inline-sexp-nodes))))))
@@ -1991,6 +2007,23 @@ value is `clojure-ts-thread-all-but-last'."
19912007 (interactive " P" )
19922008 (clojure-ts--thread-all " ->> " but-last))
19932009
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+
19942027(defvar clojure-ts-refactor-map
19952028 (let ((map (make-sparse-keymap )))
19962029 (keymap-set map " C-t" #'clojure-ts-thread )
@@ -2001,6 +2034,8 @@ value is `clojure-ts-thread-all-but-last'."
20012034 (keymap-set map " f" #'clojure-ts-thread-first-all )
20022035 (keymap-set map " C-l" #'clojure-ts-thread-last-all )
20032036 (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 )
20042039 map)
20052040 " Keymap for `clojure-ts-mode' refactoring commands." )
20062041
@@ -2012,6 +2047,7 @@ value is `clojure-ts-thread-all-but-last'."
20122047 (easy-menu-define clojure-ts-mode-menu map " Clojure[TS] Mode Menu"
20132048 '(" Clojure"
20142049 [" Align expression" clojure-ts-align]
2050+ [" Cycle privacy" clojure-ts-cycle-privacy]
20152051 (" Refactor -> and ->>"
20162052 [" Thread once more" clojure-ts-thread]
20172053 [" Fully thread a form with ->" clojure-ts-thread-first-all]
0 commit comments