Skip to content
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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

- [#4219](https://github.com/clojure-emacs/cider/pull/4219): Add `cider-ns-clean` (`C-c M-n c`), which removes the requires, aliases, referred names and imports the code doesn't use from the `ns` form, using cider-nrepl's `cider/clean-ns` op, and leaves the rest of the form as it's written.
- [#4218](https://github.com/clojure-emacs/cider/pull/4218): Add magic requires (`cider-ns-magic-requires`, off by default): typing a slash after an alias the `ns` form lacks adds the require it needs, going by the aliases the project already uses, with `cider-ns-magic-require-aliases` as the fallback.
- [#4217](https://github.com/clojure-emacs/cider/pull/4217): Add `cider-ns-add-missing` (`C-c M-n m`), which adds the require or import the symbol at point needs, using cider-nrepl's `cider/resolve-missing` op.
- [#4217](https://github.com/clojure-emacs/cider/pull/4217), [#4222](https://github.com/clojure-emacs/cider/issues/4222): Add `cider-ns-add-missing` (`C-c M-n m`), which adds the require or import the symbol at point needs, using cider-nrepl's `cider/resolve-missing` op, or the namespace the alias of an auto-resolved keyword like `::config/aero` stands for.
- [#4216](https://github.com/clojure-emacs/cider/pull/4216): Add `cider-ns-add-require` (`C-c M-n a`) and `cider-ns-add-import` (`C-c M-n i`), which add a require or an import to the `ns` form, completing against the REPL when connected; `cider-ns-edit-actions` controls whether the `ns` form then gets sorted and evaluated.
- [#4206](https://github.com/clojure-emacs/cider/pull/4206): Show test progress while the tests run: with `cider-test-stream-results` (on by default) the echo area keeps a running count and failing tests land in the report as soon as they finish.
- [#4179](https://github.com/clojure-emacs/cider/pull/4179): Add `cider-flash-evaluated-region` (off by default), which briefly flashes the region an evaluation command sent, so you can see which form it picked.
Expand Down
6 changes: 6 additions & 0 deletions doc/modules/ROOT/pages/usage/ns_form.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,19 @@ already has the alias or refers the name. A type made by `deftype` or
`defrecord` gets imported, and the namespace that defines it gets required as
well, because the class doesn't exist until that namespace is loaded.

On an auto-resolved keyword like `::config/aero`, it requires the namespace
`config` stands for, going by the same namespaces as
<<magic-requires,magic requires>>. When there's more than one, it asks which.
A plain keyword like `:config/aero` needs no require, so there's nothing to do.

Only namespaces that are loaded in the REPL count, so load the one you're after
first if it isn't. In ClojureScript buffers the ClojureScript REPL gets asked,
and it knows about namespaces but not about JavaScript classes.

This needs cider-nrepl 0.64.0 or newer. With an older one, the command says
the op it needs is missing.

[#magic-requires]
== Magic Requires

With magic requires on, typing a slash after an alias the `ns` form doesn't
Expand Down
173 changes: 137 additions & 36 deletions lisp/cider-ns.el
Original file line number Diff line number Diff line change
Expand Up @@ -510,14 +510,21 @@ the buffer when there's none before point."
"Return non-nil if a list or a vector starts at POS."
(memq (char-after pos) '(?\( ?\[)))

(defun cider-ns-edit--clause-p (pos keyword)
"Return non-nil if a KEYWORD clause starts at POS.
KEYWORD is a string like \":require\"."
(and (eq (char-after pos) ?\()
(save-excursion
(goto-char (1+ pos))
(cider-ns-edit--skip-blanks)
(and (not (eq (char-after) ?\)))
(equal (cider-ns-edit--text (point)) keyword)))))

(defun cider-ns-edit--clauses (ns-start keyword)
"Return the starts of the KEYWORD clauses of the ns form at NS-START.
KEYWORD is a string like \":require\". Clauses nested in reader
conditionals aren't considered."
(seq-filter (lambda (pos)
(and (eq (char-after pos) ?\()
(when-let* ((head (car (cider-ns-edit--elements pos))))
(equal (cider-ns-edit--text head) keyword))))
(seq-filter (lambda (pos) (cider-ns-edit--clause-p pos keyword))
(cider-ns-edit--elements ns-start)))

(defun cider-ns-edit--clause (ns-start keyword)
Expand Down Expand Up @@ -669,6 +676,50 @@ element for a vector or a list."
(setq elements (cdr elements)))
aliases)))

(defun cider-ns-edit--reader-conditional (pos)
"Return (SPLICING . BRANCHES) for the reader conditional at POS, or nil.
BRANCHES are the starts of the forms after the platform keywords of
#?(...) or #?@(...), and SPLICING is non-nil for #?@(...)."
(save-excursion
(goto-char pos)
(when (looking-at "#\\?\\(@\\)?(")
(let ((splicing (match-beginning 1))
(elements (cider-ns-edit--elements (1- (match-end 0))))
branches)
(while elements
(when (cadr elements)
(push (cadr elements) branches))
(setq elements (cddr elements)))
(cons splicing (nreverse branches))))))

(defun cider-ns-edit--collect-libspecs (pos in-require)
"Return the starts of the libspecs in the form at POS.
IN-REQUIRE non-nil means the form is an element of a :require clause.
Reader conditionals count with all their branches."
(if-let* ((conditional (cider-ns-edit--reader-conditional pos)))
(mapcan (lambda (branch)
(if (car conditional)
;; #?@ splices the elements of its branches in
(when (cider-ns-edit--seq-p branch)
(mapcan (lambda (element)
(cider-ns-edit--collect-libspecs element in-require))
(cider-ns-edit--elements branch)))
(cider-ns-edit--collect-libspecs branch in-require)))
(cdr conditional))
(cond
(in-require (list pos))
((cider-ns-edit--clause-p pos ":require")
(mapcan (lambda (element) (cider-ns-edit--collect-libspecs element t))
(cdr (cider-ns-edit--elements pos)))))))

(defun cider-ns-edit--all-libspecs (ns-start)
"Return the starts of all the libspecs of the ns form at NS-START.
That's the ones of every :require clause, reader conditionals included,
where the edits only change the first :require clause outside of them.
An alias or a name taken anywhere can't be taken again, though."
(mapcan (lambda (element) (cider-ns-edit--collect-libspecs element nil))
(cdr (cider-ns-edit--elements ns-start))))

(defun cider-ns-edit--libspec (ns alias refer)
"Return a libspec that requires NS as ALIAS and refers REFER from it.
ALIAS and REFER can be nil."
Expand Down Expand Up @@ -767,8 +818,9 @@ nil when NS is already required by LIBSPEC as asked. Signal an error
when NS has another alias, or another namespace has ALIAS or REFER."
(let* ((clause (cider-ns-edit--clause ns-start ":require"))
(libspecs (and clause (cdr (cider-ns-edit--elements clause))))
(others (seq-remove (lambda (pos) (equal (cider-ns-edit--libspec-ns pos) ns))
libspecs))
(others (when (or alias refer)
(seq-remove (lambda (pos) (equal (cider-ns-edit--libspec-ns pos) ns))
(cider-ns-edit--all-libspecs ns-start))))
(existing (seq-find (lambda (pos)
(and (equal (cider-ns-edit--libspec-ns pos) ns)
(cider-ns-edit--loads-p pos)))
Expand Down Expand Up @@ -969,7 +1021,10 @@ What gets added depends on how SYMBOL is written:
- for alias/foo, require the namespace as alias, as in [some.ns :as alias];
- for some.ns/foo, require some.ns;
- for a class, import it, and require the namespace that defines it too
when it's made by `deftype' or `defrecord'.
when it's made by `deftype' or `defrecord';
- for an auto-resolved keyword like ::alias/foo, require the namespace
alias stands for, going by the same namespaces magic requires do (see
`cider-ns-magic-requires').

A namespace that's already required gets the alias or the refer added
to its libspec. A second alias for it, or an alias or a refer that
Expand All @@ -988,18 +1043,56 @@ After a change, do what `cider-ns-edit-actions' asks for."
(let ((symbol (string-trim symbol)))
(when (string-empty-p symbol)
(user-error "No symbol given"))
(when (string-prefix-p ":" symbol)
(user-error "%s is a keyword, not a symbol" symbol))
(let* ((candidates (or (cider-sync-request:resolve-missing symbol (cider-current-ns))
(user-error "Found nothing to add for %s" symbol)))
(candidate (cider-ns-edit--choose symbol candidates)))
(pcase-let ((`(,changed . ,texts)
(cider-ns-edit--edit
(lambda (start) (cider-ns-edit--add-missing start symbol candidate)))))
(if changed
(message "Resolved %s with %s" symbol (string-join texts " and "))
(message "Nothing to add for %s, the ns form has %s"
symbol (string-join texts " and ")))))))
(cond
((string-match "\\`::\\([^/]*\\)/" symbol)
(cider-ns-edit--add-keyword-alias symbol (match-string 1 symbol)))
((string-prefix-p ":" symbol)
(user-error "%s needs no require, only an auto-resolved keyword like ::alias/name does"
symbol))
(t (cider-ns-edit--add-missing-symbol symbol)))))

(defun cider-ns-edit--add-keyword-alias (keyword alias)
"Require the namespace ALIAS stands for in the auto-resolved KEYWORD.
The candidates are the ones magic requires go by, see
`cider-ns-edit--alias-candidates'."
(when (string-empty-p alias)
(user-error "%s has no alias" keyword))
(let* ((ns-start (cider-ns-edit--check))
;; unlike magic requires, a command reaches past the narrowing
(taken (save-restriction
(widen)
(cider-ns-edit--has-alias-p
ns-start
(or (ignore-errors (scan-sexps ns-start 1))
(user-error "The ns form isn't balanced"))
alias))))
(if taken
(message "Nothing to add for %s, the ns form has the alias %s" keyword alias)
(let* ((candidates (or (cider-ns-edit--alias-candidates alias)
(user-error "Found no namespace for the alias %s" alias)))
(ns (if (cdr candidates)
(cider-ns-edit--read-alias-ns
(format "Require as %s: " alias) candidates)
(nrepl-dict-get (car candidates) "ns"))))
(when (or (null ns) (string-empty-p ns))
(user-error "Nothing chosen"))
(pcase-let ((`(,status . ,libspec)
(cider-ns-edit--edit
(lambda (start) (cider-ns-edit--add-require start ns alias)))))
(cider-ns-edit--require-message ns status libspec))))))

(defun cider-ns-edit--add-missing-symbol (symbol)
"Require or import what SYMBOL needs, as `cider-ns-add-missing' does."
(let* ((candidates (or (cider-sync-request:resolve-missing symbol (cider-current-ns))
(user-error "Found nothing to add for %s" symbol)))
(candidate (cider-ns-edit--choose symbol candidates)))
(pcase-let ((`(,changed . ,texts)
(cider-ns-edit--edit
(lambda (start) (cider-ns-edit--add-missing start symbol candidate)))))
(if changed
(message "Resolved %s with %s" symbol (string-join texts " and "))
(message "Nothing to add for %s, the ns form has %s"
symbol (string-join texts " and "))))))

;;; Cleaning the ns form

Expand Down Expand Up @@ -1393,28 +1486,36 @@ Ask which when there's more than one, or `cider-ns-magic-requires' says
to ask. Picking none, or quitting, declines ALIAS for the buffer."
(if (and (null (cdr candidates)) (eq cider-ns-magic-requires t))
(nrepl-dict-get (car candidates) "ns")
(let* ((table (mapcar (lambda (c) (cons (nrepl-dict-get c "ns") c)) candidates))
(annotate (lambda (ns)
(when-let* ((candidate (cdr (assoc ns table))))
(pcase (nrepl-dict-get candidate "source")
("project" (let ((n (nrepl-dict-get candidate "count")))
(format " used in %s file%s" n (if (eql n 1) "" "s"))))
(source (concat " " source))))))
(choice (condition-case nil
(completing-read
(format "Require as %s (empty for none): " alias)
(lambda (string pred action)
(if (eq action 'metadata)
`(metadata (annotation-function . ,annotate)
(display-sort-function . identity))
(complete-with-action action table string pred)))
nil t)
(quit ""))))
(let ((choice (condition-case nil
(cider-ns-edit--read-alias-ns
(format "Require as %s (empty for none): " alias) candidates)
(quit ""))))
(if (string-empty-p choice)
(progn (push alias cider-ns-edit--declined-aliases)
nil)
choice))))

(defun cider-ns-edit--read-alias-ns (prompt candidates)
"Read one of the namespaces in CANDIDATES with PROMPT.
CANDIDATES are dicts like the ones of `cider-ns-edit--alias-candidates',
kept in their order and annotated with where they come from. Return the
namespace, or an empty string when none got picked."
(let* ((table (mapcar (lambda (c) (cons (nrepl-dict-get c "ns") c)) candidates))
(annotate (lambda (ns)
(when-let* ((candidate (cdr (assoc ns table))))
(pcase (nrepl-dict-get candidate "source")
("project" (let ((n (nrepl-dict-get candidate "count")))
(format " used in %s file%s" n (if (eql n 1) "" "s"))))
(source (concat " " source)))))))
(completing-read
prompt
(lambda (string pred action)
(if (eq action 'metadata)
`(metadata (annotation-function . ,annotate)
(display-sort-function . identity))
(complete-with-action action table string pred)))
nil t)))

(defun cider-ns-edit--magic-require ()
"Require the namespace the alias before the slash at point stands for.
See `cider-ns-magic-requires'. This runs as the user types, so instead
Expand Down
Loading
Loading