clojure-emacs / clj-refactor.el

A CIDER extension that provides powerful commands for refactoring Clojure code.
GNU General Public License v3.0
771 stars 111 forks source link

Add Project Dependency should check for duplicates #510

Open dgr opened 2 years ago

dgr commented 2 years ago

I added a dependency to a project but forgot to :require it in the namespace form. A few days later, thinking that I needed it again, I re-added it via ap. Later, when looking at the dips.edn file, I noticed that it was added twice.

Expected behavior

It should just add it to the project file (deps.edn, project.clj, etc.) once. If a duplicate is detected, do not add it to the project file, and possibly inform the user so that she doesn't think that she needs to restart the REPL to include the "new" dependency.

Actual behavior

Adds the dependency to the project file more than once. I have not checked whether this happens to all types of project files (e.g., deps.edn and project.clj) or just deps.edn.

Steps to reproduce the problem

Add a project to the project file twice using ap.

Environment & Version information

clj-refactor.el version information

cli-refactor 3.3.2 (package 20220213.1906), refactor-nrepl 3.3.2

CIDER version information

;; CIDER 1.3.0-snapshot (package: 20220213.1329), nREPL 0.9.0
;; Clojure 1.10.3, Java 17.0.2

Leiningen or Boot version

Emacs version

Emacs 28.0.91

Operating system

MacOS 12.2.1

vemv commented 2 years ago

Sounds like a resaonsable request.

The code that does the insertion in question is https://github.com/clojure-emacs/clj-refactor.el/blob/542225248e8d27111d6164292b37058ad5194984/clj-refactor.el#L2288-L2296

Doing an artifact search between the beginning and the end of the :deps "block" doesn't seem too hard. You can use Paredit commands for determining these boundaries.

PR welcome otherwise this might linger for quite a while.

Cheers - V