Skip to content

Add cycling privacy, collection type, if/if-not #381

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
Jun 4, 2016
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

## master (unreleased)

### New features

* Port cycle privacy, cycle collection type and cycle if/if-not from clj-refactor.el.
* Rework cycle collection type into convert collection to list, quoted list, map, vector, set.

## 5.4.0 (2016-05-21)

### New features
Expand Down
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ specific `clojure-mode` release.**
- [Vertical alignment](#vertical-alignment)
- [Refactoring support](#refactoring-support)
- [Threading macros](#threading-macros-related-features)
- [Cycling things](#cycling-things)
- [Convert collection](#convert-collection)
- [Related packages](#related-packages)
- [REPL Interaction](#repl-interaction)
- [Basic REPL](#basic-repl)
Expand Down Expand Up @@ -258,6 +260,20 @@ argument do not thread the last form. See demonstration on the
Unwind and remove the threading macro. See demonstration on the
[clj-refactor.el wiki](https://github.com/clojure-emacs/clj-refactor.el/wiki/cljr-unwind-all).

### Cycling things

* Cycle privacy

Cycle privacy of `def`s or `defn`s. Use metadata explicitly with setting `clojure-use-metadata-for-privacy` to `t` for `defn`s too. See demonstration on the [clj-refactor.el wiki](https://github.com/clojure-emacs/clj-refactor.el/wiki/cljr-cycle-privacy).

* Cycle if/if-not

Find the closest if or if-not up the syntax tree and toggle it. Also transpose the "else" and "then" branches, keeping the semantics the same as before. See demonstration on the [clj-refactor.el wiki](https://github.com/clojure-emacs/clj-refactor.el/wiki/cljr-cycle-if).

### Convert collection

Convert any given collection at point to list, quoted list, map, vector or set.

## Related packages

* [clojure-mode-extra-font-locking][] provides additional font-locking
Expand Down
124 changes: 124 additions & 0 deletions clojure-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,20 @@ Out-of-the box clojure-mode understands lein, boot and gradle."
(define-key map (kbd "C-c C-r l") #'clojure-thread-last-all)
(define-key map (kbd "C-c C-r C-a") #'clojure-unwind-all)
(define-key map (kbd "C-c C-r a") #'clojure-unwind-all)
(define-key map (kbd "C-c C-r C-p") #'clojure-cycle-privacy)
(define-key map (kbd "C-c C-r p") #'clojure-cycle-privacy)
(define-key map (kbd "C-c C-r C-(") #'clojure-convert-collection-to-list)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A little silly, but could you write the C-( keybind above the ( keybind? Same for the other collections.

(define-key map (kbd "C-c C-r (") #'clojure-convert-collection-to-list)
(define-key map (kbd "C-c C-r C-'") #'clojure-convert-collection-to-quoted-list)
(define-key map (kbd "C-c C-r '") #'clojure-convert-collection-to-quoted-list)
(define-key map (kbd "C-c C-r C-{") #'clojure-convert-collection-to-map)
(define-key map (kbd "C-c C-r {") #'clojure-convert-collection-to-map)
(define-key map (kbd "C-c C-r C-[") #'clojure-convert-collection-to-vector)
(define-key map (kbd "C-c C-r [") #'clojure-convert-collection-to-vector)
(define-key map (kbd "C-c C-r C-#") #'clojure-convert-collection-to-set)
(define-key map (kbd "C-c C-r #") #'clojure-convert-collection-to-set)
(define-key map (kbd "C-c C-r C-i") #'clojure-cycle-if)
(define-key map (kbd "C-c C-r i") #'clojure-cycle-if)
(define-key map (kbd "C-c C-r n i") #'clojure-insert-ns-form)
(define-key map (kbd "C-c C-r n h") #'clojure-insert-ns-form-at-point)
(define-key map (kbd "C-c C-r n u") #'clojure-update-ns)
Expand All @@ -213,11 +227,19 @@ Out-of-the box clojure-mode understands lein, boot and gradle."
'("Clojure"
["Toggle between string & keyword" clojure-toggle-keyword-string]
["Align expression" clojure-align]
["Cycle privacy" clojure-cycle-privacy]
["Cycle if, if-not" clojure-cycle-if]
("ns forms"
["Insert ns form at the top" clojure-insert-ns-form]
["Insert ns form here" clojure-insert-ns-form-at-point]
["Update ns form" clojure-update-ns]
["Sort ns form" clojure-sort-ns])
("Convert collection"
["Convert to list" clojure-convert-collection-to-list]
["Convert to quoted list" clojure-convert-collection-to-quoted-list]
["Convert to map" clojure-convert-collection-to-map]
["Convert to vector" clojure-convert-collection-to-vector]
["Convert to set" clojure-convert-collection-to-set])
("Refactor -> and ->>"
["Thread once more" clojure-thread]
["Fully thread a form with ->" clojure-thread-first-all]
Expand Down Expand Up @@ -1629,6 +1651,8 @@ This will skip over sexps that don't represent objects, so that ^hints and
;; Refactoring support
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;; Threading macros related
(defcustom clojure-thread-all-but-last nil
"Non-nil means do not thread the last expression.
This means that `clojure-thread-first-all' and
Expand Down Expand Up @@ -1824,6 +1848,106 @@ When BUT-LAST is passed the last expression is not threaded."
(interactive "P")
(clojure--thread-all "->> " but-last))

;;; Cycling stuff

(defcustom clojure-use-metadata-for-privacy nil
"If nil, `clojure-cycle-privacy' will use (defn- f []).
If t, it will use (defn ^:private f [])."
:package-version '(clojure-mode . "5.5.0")
:safe #'booleanp
:type 'boolean)

;;;###autoload
(defun clojure-cycle-privacy ()
"Make public the current private def, or vice-versa.
See: https://github.com/clojure-emacs/clj-refactor.el/wiki/cljr-cycle-privacy"
(interactive)
(save-excursion
(ignore-errors (forward-char 7))
(search-backward-regexp "(defn?\\(-\\| ^:private\\)?\\_>")
(if (match-string 1)
(replace-match "" nil nil nil 1)
(goto-char (match-end 0))
(insert (if (or clojure-use-metadata-for-privacy
(equal (match-string 0) "(def"))
" ^:private"
"-")))))

(defun clojure--convert-collection (coll-open coll-close)
"Convert the collection at (point) by unwrapping it an wrapping it between COLL-OPEN and COLL-CLOSE."
(save-excursion
(while (and
(not (bobp))
(not (looking-at "(\\|{\\|\\[")))
(backward-char))
(when (or (eq ?\# (char-before))
(eq ?\' (char-before)))
(delete-char -1))
(when (and (bobp)
(not (memq (char-after) '(?\{ ?\( ?\[))))
(user-error "Beginning of file reached, collection is not found"))
(insert coll-open (substring (clojure-delete-and-extract-sexp) 1 -1) coll-close)))

;;;###autoload
(defun clojure-convert-collection-to-list ()
"Convert collection at (point) to list."
(interactive)
(clojure--convert-collection "(" ")"))

;;;###autoload
(defun clojure-convert-collection-to-quoted-list ()
"Convert collection at (point) to quoted list."
(interactive)
(clojure--convert-collection "'(" ")"))

;;;###autoload
(defun clojure-convert-collection-to-map ()
"Convert collection at (point) to map."
(interactive)
(clojure--convert-collection "{" "}"))

;;;###autoload
(defun clojure-convert-collection-to-vector ()
"Convert collection at (point) to vector."
(interactive)
(clojure--convert-collection "[" "]"))

;;;###autoload
(defun clojure-convert-collection-to-set ()
"Convert collection at (point) to set."
(interactive)
(clojure--convert-collection "#{" "}"))

(defun clojure--goto-if ()
(when (in-string-p)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm relatively sure we don't have this function. :-)

(while (or (not (looking-at "("))
(in-string-p))
(backward-char)))
(while (not (looking-at "\\((if \\)\\|\\((if-not \\)"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FTR, I like this method of going backward-up-list until we find what we're looking for (instead of doing search-backward). And I think this is what we should be using on all the other commands too (including the thread/unwind commands). But that's something for another time.

(condition-case nil
(backward-up-list)
(scan-error (user-error "No if or if-not found")))))

;;;###autoload
(defun clojure-cycle-if ()
"Change a surrounding if to if-not, or vice-versa.

See: https://github.com/clojure-emacs/clj-refactor.el/wiki/cljr-cycle-if"
(interactive)
(save-excursion
(clojure--goto-if)
(cond
((looking-at "(if-not")
(forward-char 3)
(delete-char 4)
(forward-sexp 2)
(transpose-sexps 1))
((looking-at "(if")
(forward-char 3)
(insert "-not")
(forward-sexp 2)
(transpose-sexps 1)))))


;;; ClojureScript
(defconst clojurescript-font-lock-keywords
Expand Down
75 changes: 75 additions & 0 deletions test/clojure-mode-convert-collection-test.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
;;; clojure-mode-convert-collection-test.el --- Clojure Mode: convert collection type -*- lexical-binding: t; -*-

;; Copyright (C) 2016 Benedek Fazekas <benedek.fazekas@gmail.com>

;; This file is not part of GNU Emacs.

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.

;;; Commentary:

;; The convert collection code originally was implemented
;; as cycling collection type in clj-refactor.el and is the work
;; of the clj-reafctor.el team.

;;; Code:

(require 'clojure-mode)
(require 'ert)

(def-refactor-test test-convert-collection-list-map
"(:a 1 :b 2)"
"{:a 1 :b 2}"
(backward-sexp)
(down-list)
(clojure-convert-collection-to-map))

(def-refactor-test test-convert-collection-map-vector
"{:a 1 :b 2}"
"[:a 1 :b 2]"
(backward-sexp)
(down-list)
(clojure-convert-collection-to-vector))

(def-refactor-test test-convert-collection-vector-set
"[1 2 3]"
"#{1 2 3}"
(backward-sexp)
(down-list)
(clojure-convert-collection-to-set))

(def-refactor-test test-convert-collection-set-list
"#{1 2 3}"
"(1 2 3)"
(backward-sexp)
(down-list)
(clojure-convert-collection-to-list))

(def-refactor-test test-convert-collection-set-quoted-list
"#{1 2 3}"
"'(1 2 3)"
(backward-sexp)
(down-list)
(clojure-convert-collection-to-quoted-list))

(def-refactor-test test-convert-collection-quoted-list-set
"'(1 2 3)"
"#{1 2 3}"
(backward-sexp)
(down-list)
(clojure-convert-collection-to-set))

(provide 'clojure-mode-convert-collection-test)

;;; clojure-mode-convert-collection-test.el ends here
118 changes: 118 additions & 0 deletions test/clojure-mode-cycling-test.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
;;; clojure-mode-cycling-test.el --- Clojure Mode: cycling things tests -*- lexical-binding: t; -*-

;; Copyright (C) 2016 Benedek Fazekas <benedek.fazekas@gmail.com>

;; This file is not part of GNU Emacs.

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.

;;; Commentary:

;; The cycling privacy and if/if-not code is ported from
;; clj-refactor.el and the work of the clj-reafctor.el team.

;;; Code:

(require 'clojure-mode)
(require 'ert)

(def-refactor-test test-cycle-privacy-public-defn-private-defn
"(defn add [a b]
(+ a b))"
"(defn- add [a b]
(+ a b))"
(clojure-cycle-privacy))

(def-refactor-test test-cycle-privacy-from-sexp-beg
"(defn- add [a b]
(+ a b))"
"(defn add [a b]
(+ a b))"
(backward-sexp)
(clojure-cycle-privacy))

(def-refactor-test test-cycle-privacy-public-defn-private-defn-metadata
"(defn add [a b]
(+ a b))"
"(defn ^:private add [a b]
(+ a b))"
(let ((clojure-use-metadata-for-privacy t))
(clojure-cycle-privacy)))

(def-refactor-test test-cycle-privacy-private-defn-public-defn
"(defn- add [a b]
(+ a b))"
"(defn add [a b]
(+ a b))"
(clojure-cycle-privacy))

(def-refactor-test test-cycle-privacy-private-defn-public-defn-metadata
"(defn ^:private add [a b]
(+ a b))"
"(defn add [a b]
(+ a b))"
(let ((clojure-use-metadata-for-privacy t))
(clojure-cycle-privacy)))

(def-refactor-test test-cycle-privacy-public-def-private-def
"(def ^:dynamic config
\"docs\"
{:env \"staging\"})"
"(def ^:private ^:dynamic config
\"docs\"
{:env \"staging\"})"
(clojure-cycle-privacy))

(def-refactor-test test-cycle-privacy-private-def-public-def
"(def ^:private config
\"docs\"
{:env \"staging\"})"
"(def config
\"docs\"
{:env \"staging\"})"
(clojure-cycle-privacy))

(def-refactor-test test-cycle-if-inner-if
"(if this
(if that
(then AAA)
(else BBB))
(otherwise CCC))"
"(if this
(if-not that
(else BBB)
(then AAA))
(otherwise CCC))"
(beginning-of-buffer)
(search-forward "BBB)")
(clojure-cycle-if))

(def-refactor-test test-cycle-if-outer-if
"(if-not this
(if that
(then AAA)
(else BBB))
(otherwise CCC))"
"(if this
(otherwise CCC)
(if that
(then AAA)
(else BBB)))"
(beginning-of-buffer)
(search-forward "BBB))")
(clojure-cycle-if))

(provide 'clojure-mode-cycling-test)

;;; clojure-mode-cycling-test.el ends here
Loading