Skip to content

Enable cljr-slash-uses-suggest-libspec by default #554

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 3 commits into from
Nov 16, 2023
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: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

* [#554](https://github.com/clojure-emacs/clj-refactor.el/pull/554): Enable `cljr-slash-uses-suggest-libspec` by default

## 3.10.0

* [#552](https://github.com/clojure-emacs/clj-refactor.el/issues/552): support TRAMP connections.
Expand Down
31 changes: 22 additions & 9 deletions clj-refactor.el
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,19 @@ Any other non-nil value means to add the form without asking."
(const :tag "prompt" :prompt)
(const :tag "false" nil)))

(defcustom cljr-slash-uses-suggest-libspec nil
(defcustom cljr-slash-uses-suggest-libspec t
"If t, `cljr-slash'' magic requires' functionality will use `cljr-suggest-libspec'.
It's a middleware op, newer than the `namespace-aliases' op."

The `suggest-libspec' middleware operation replaces the `namespace-aliases'
operation. It incorporates the requested namespace alias, buffer and scoped
language context (clj, cljs, cljc, etc), preferred-aliases from the
`cljr-magic-require-namespaces', with existing aliases from the project and
returns a candidate list of suitable libspec entries. This is passed to the
completion framework along with language context information to add a
require which will satisfy the alias for a given namespace.

Currently testing this flag as the default, remove associated deprecated
paths once this flag is removed."
:type 'boolean
:safe #'booleanp)

Expand Down Expand Up @@ -234,8 +244,7 @@ at `cider-jack-in' time."
:type 'boolean
:safe #'booleanp)

;; TODO: remove after `cljr--clj-context-p' is deprecated by enabling
;; `cljr-slash-uses-suggest-libspec'.
;; TODO: deprecated by `cljr-slash-uses-suggest-libspec'
(defcustom cljr-assume-language-context nil
"If set to `clj' or `cljs',
clj-refactor will use that value in situations where the language context is ambiguous.
Expand Down Expand Up @@ -1909,11 +1918,11 @@ but removes # in front of function literals and sets."

;; ------ magic requires -------

;; TODO: deprecated by `cljr-slash-uses-suggest-libspec'
(defun cljr--magic-requires-re ()
(regexp-opt (seq-map 'car cljr-magic-require-namespaces)))

;; TODO: remove after `cljr--get-aliases-from-middleware' is deprecated by
;; enabling `cljr-slash-uses-suggest-libspec'.
;; TODO: deprecated by `cljr-slash-uses-suggest-libspec'
(defun cljr--clj-context-p ()
"Is point in a clj context?"
(or (cljr--clj-file-p)
Expand Down Expand Up @@ -1973,12 +1982,14 @@ context. Valid outputs include, but are not limited to `:clj',
(defun cljr--aget (map key)
(cdr (assoc key map)))

;; TODO: deprecated by `cljr-slash-uses-suggest-libspec'
(defcustom cljr-suggest-namespace-aliases t
"If `t', `namespace-aliases' and `cljr-slash' will take into account suggested namespace aliases,
following this convention: `https://stuartsierra.com/2015/05/10/clojure-namespace-aliases'."
:group 'cljr
:type 'boolean)

;; TODO: deprecated by `cljr-slash-uses-suggest-libspec'
(defun cljr--call-middleware-for-namespace-aliases ()
(thread-first "namespace-aliases"
cljr--ensure-op-supported
Expand Down Expand Up @@ -2055,6 +2066,7 @@ is not set to `:prompt'."
;; this is like seq-first, but compatible with older Emacsen:
(seq-elt candidates 0))))

;; TODO: deprecated by `cljr-slash-uses-suggest-libspec'
(defun cljr--get-aliases-from-middleware ()
(when-let (aliases (cljr--call-middleware-for-namespace-aliases))
(if (cljr--clj-context-p)
Expand All @@ -2077,7 +2089,7 @@ is not set to `:prompt'."
'(re-search-forward "[0-9`':#]*" nil t))
(1- (point))))

;; TODO: deprecated after enabling `cljr-slash-uses-suggest-libspec'
;; TODO: deprecated by `cljr-slash-uses-suggest-libspec'
(defun cljr--magic-requires-lookup-alias (short)
"Generate a mapping from alias to candidate namespaces.

Expand Down Expand Up @@ -2161,14 +2173,14 @@ to the ns form."
(clojure-find-ns)
(cljr--unresolved-alias-ref (cljr--ns-alias-at-point))))
(if cljr-slash-uses-suggest-libspec
;; New path creates suggestions from `suggest-libspec' middleware op
;; creates suggestions from `suggest-libspec' middleware op
(when-let (libspec
(thread-first alias-ref
(cljr--call-middleware-suggest-libspec (cljr--language-context-at-point))
cljr--prompt-or-select-libspec))
;; only insert a require if a candidate exists and was selected
(cljr--insert-require-libspec libspec))
;; Old path creates suggestions from `namespace-aliases' middleware op
;; Deprecated, creates suggestions from `namespace-aliases' middleware op
(when-let (aliases (cljr--magic-requires-lookup-alias alias-ref))
(let ((short (cl-first aliases))
;; Ensure it's a list (and not a vector):
Expand All @@ -2181,6 +2193,7 @@ to the ns form."
(yes-or-no-p (format "Add %s :as %s to requires?" long short))))
(cljr--insert-require-libspec (format "[%s :as %s]" long short)))))))))

;; TODO: deprecated by `cljr-slash-uses-suggest-libspec'
(defun cljr--in-namespace-declaration-p (s)
(save-excursion
(cljr--goto-ns)
Expand Down
1 change: 1 addition & 0 deletions feature/feature.el
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
;; -*- lexical-binding: t -*-
(provide 'feature)
1 change: 1 addition & 0 deletions features/magic-requires-in-cljc-files.feature
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Given I have a project "cljr" in "tmp"
And I have a clojure-file "tmp/src/cljr/core.cljc"
And I open file "tmp/src/cljr/core.cljc"
And the `cljr-slash-uses-suggest-libspec' flag is disabled
And I clear the buffer

Scenario: Does the right thing when facing clj reader conditionals
Expand Down
1 change: 1 addition & 0 deletions features/magic-requires.feature
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Feature: Magic requires
Given I have a project "cljr" in "tmp"
And I have a clojure-file "tmp/src/cljr/core.clj"
And I open file "tmp/src/cljr/core.clj"
And the `cljr-slash-uses-suggest-libspec' flag is disabled
And I clear the buffer

Scenario: Require is inserted automagically
Expand Down
5 changes: 5 additions & 0 deletions features/step-definitions/clj-refactor-steps.el
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,11 @@
(cljr--change-function-signature (list (cljr--plist-to-hash (cl-second cljr--test-occurrences)))
cljr--baz-renamed-to-qux))))

;; TODO: deprecate dependent tests after `cljr-slash-uses-suggest-libspec' is the only path.
(Given "The `cljr-slash-uses-suggest-libspec' flag is disabled"
(lambda ()
(setq-local cljr-slash-uses-suggest-libspec nil)))

(Given "The cache of namespace aliases is populated"
(lambda ()
(defun cljr--call-middleware-for-namespace-aliases ()
Expand Down