Skip to content

Commit

Permalink
Move entries pairwise in case, cond and similar functions (clojure-ls…
Browse files Browse the repository at this point in the history
…p#780)

Some functions are known to take a set of clauses which are binary
expressions. For example, `case` has clauses made up of pairs of a
test-constant and a result-expr. These clauses should move together.

```clojure
(case a
  1 :one
  |2 :two)

;; should become

(case a
  |2 :two
  1 :one)
```

This patch makes clauses move together in `assoc`, `assoc!`,
`case`, `cond`, `condp`, `cond->`, and `cond->>`.

It's common to find `assoc`, `assoc!`, `cond->`, `cond->>`, and `case`
within threading macros, and so this patch handles those situations too.
It also handles the optional default clause in `case` and `condp`. And
it handles the ternary expression syntax of `condp`. I believe that
usage is rare in the wild, but this code understands it.

This patch also includes a general fix for move-coll-entry, to treat
unevals as comments when moving entries. clj-rewrite calls the #_ reader
macro and its contents an "uneval" node. This commit duplicates several
zipper movement operators, changing them to treat uneval nodes as
comments. This fixes what would otherwise be invalid movements in forms
that use `#_=>` to visually separate long lines without adding blank
lines.
  • Loading branch information
mainej authored Mar 2, 2022
1 parent d3fcc58 commit ded2a75
Show file tree
Hide file tree
Showing 3 changed files with 680 additions and 248 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
- Fix cljfmt settings merge during refresh/classpath configs merge to avoid multiple config vectors on same symbol.

- Editor
- Fix wrong args on extract function from multi-arity fn. #683
- extract-function: Fix wrong args when extracting from multi-arity fn. #683
- move-coll-entry: clauses move intuitively in `assoc`, `case`, `cond`, and similar functions. #780 @mainej

## 2022.02.23-12.12.12

Expand Down
Loading

0 comments on commit ded2a75

Please sign in to comment.