<commit_before>[![CircleCI](https://circleci.com/gh/clojure-emacs/refactor-nrepl/tree/master.svg?style=svg)](https://circleci.com/gh/clojure-emacs/refactor-nrepl/tree/master)
[![Dependencies Status](https://versions.deps.co/clojure-emacs/refactor-nrepl/status.svg)](https://versions.deps.co/clojure-emacs/refactor-nrepl)
[![cljdoc badge](https://cljdoc.org/badge/refactor-nrepl/refactor-nrepl)](https://cljdoc.org/d/refactor-nrepl/refactor-nrepl/CURRENT)
[![downloads badge](https://versions.deps.co/refactor-nrepl/refactor-nrepl/downloads.svg)](https://clojars.org/refactor-nrepl/refactor-nrepl)
[![Discord](https://img.shields.io/badge/chat-on%20discord-7289da.svg?sanitize=true)](https://discord.com/invite/nFPpynQPME)

# Refactor nREPL

[nREPL][] middleware to support refactorings in an editor agnostic way.

The role of this nREPL middleware is to provide refactoring support for clients such as [clj-refactor.el][].

## Usage

### With CIDER and clj-refactor

If you're using [CIDER][] and clj-refactor you don't have to do anything
except call `cider-jack-in`.  The dependencies are injected
automagically.

Be aware that this isn't the case if you connect to an already running REPL process. See the [CIDER documentation](http://cider.readthedocs.io/en/latest/installation/) for more details.

### Adding the middleware via Leiningen

Add the following, either in your project's `project.clj`,  or in the `:user` profile found at `~/.lein/profiles.clj`:

```clojure
:plugins [[refactor-nrepl "3.1.0"]
          [cider/cider-nrepl "0.25.9"]]
```

### Adding the middleware via Boot

Add the following in `~/.boot/profile.boot`:

```clojure
(require 'boot.repl)

(swap! boot.repl/*default-dependencies* conj
       '[refactor-nrepl "3.1.0"]
       '[cider/cider-nrepl "0.25.9"])

(swap! boot.repl/*default-middleware* conj
       'refactor-nrepl.middleware/wrap-refactor)
```

### Embedded nREPL

You may want launch your own nREPL server with CIDER and refactor-nrepl in it. You'll be able to [`cider-connect`](https://github.com/clojure-emacs/cider/blob/6a17686799b7ef97bc15fa041016421e5c875bfb/cider.el#L1150) to said server.

For that, you can use the following (more info can be found in the [nREPL Server docs](https://nrepl.org/nrepl/usage/server.html#embedding-nrepl) and [CIDER docs](https://docs.cider.mx/cider/basics/middleware_setup.html#using-embedded-nrepl-server)):

```clojure
(def custom-nrepl-handler
  "We build our own custom nrepl handler, mimicking CIDER's."
  (apply nrepl-server/default-handler
         (conj cider.nrepl.middleware/cider-middleware 'refactor-nrepl.middleware/wrap-refactor)))
         
(nrepl-server/start-server :port port :address bind-address :handler custom-nrepl-handler)
```

The `cider-middleware` is technically optional but highly recommended.

### Passing messages to and from refactor-nrepl

We've already called this a middleware, but we haven't really talked
about what that means.  refactor-nrepl is middleware for a REPL.
Specifically it's middleware for a networked REPL, which is managed by
[nREPL][].
refactor-nrepl uses the running REPL to to gain insight about your
project, in order to offer various refactorings.

Most likely you're already in an environment with a nREPL client available, so you don't have to worry about anything except sending an receiving messages:

```clj
=> (require '[nrepl.core :as repl])
nil
=> (with-open [conn (repl/connect :port 59258)]
     (-> (repl/client conn 1000)    ; message receive timeout required
       (repl/message {:op "eval" :code "(+ 2 3)"})
       repl/response-values))
;;=> [5]
```

In the example above we're talking to one of the built-in nREPL ops, `eval`, passing it the data `:code "(+ 2 3)"`.  The rest of the readme details or own nREPL ops which provide various refactoring support.

## Available features

### Configuration

Configuration settings are passed along with each msg, currently the recognized options are as follows:

```clj
{
 ;; Verbose setting for debugging.  The biggest effect this has is
 ;; to not catch any exceptions to provide meaningful error
 ;; messages for the client.

 :debug false

  ;; if `true`:<commit_msg>Fix a typo in README.md (#354)<commit_after>[![CircleCI](https://circleci.com/gh/clojure-emacs/refactor-nrepl/tree/master.svg?style=svg)](https://circleci.com/gh/clojure-emacs/refactor-nrepl/tree/master)
[![Dependencies Status](https://versions.deps.co/clojure-emacs/refactor-nrepl/status.svg)](https://versions.deps.co/clojure-emacs/refactor-nrepl)
[![cljdoc badge](https://cljdoc.org/badge/refactor-nrepl/refactor-nrepl)](https://cljdoc.org/d/refactor-nrepl/refactor-nrepl/CURRENT)
[![downloads badge](https://versions.deps.co/refactor-nrepl/refactor-nrepl/downloads.svg)](https://clojars.org/refactor-nrepl/refactor-nrepl)
[![Discord](https://img.shields.io/badge/chat-on%20discord-7289da.svg?sanitize=true)](https://discord.com/invite/nFPpynQPME)

# Refactor nREPL

[nREPL][] middleware to support refactorings in an editor agnostic way.

The role of this nREPL middleware is to provide refactoring support for clients such as [clj-refactor.el][].

## Usage

### With CIDER and clj-refactor

If you're using [CIDER][] and clj-refactor you don't have to do anything
except call `cider-jack-in`.  The dependencies are injected
automagically.

Be aware that this isn't the case if you connect to an already running REPL process. See the [CIDER documentation](http://cider.readthedocs.io/en/latest/installation/) for more details.

### Adding the middleware via Leiningen

Add the following, either in your project's `project.clj`,  or in the `:user` profile found at `~/.lein/profiles.clj`:

```clojure
:plugins [[refactor-nrepl "3.1.0"]
          [cider/cider-nrepl "0.25.9"]]
```

### Adding the middleware via Boot

Add the following in `~/.boot/profile.boot`:

```clojure
(require 'boot.repl)

(swap! boot.repl/*default-dependencies* conj
       '[refactor-nrepl "3.1.0"]
       '[cider/cider-nrepl "0.25.9"])

(swap! boot.repl/*default-middleware* conj
       'refactor-nrepl.middleware/wrap-refactor)
```

### Embedded nREPL

You may want launch your own nREPL server with CIDER and refactor-nrepl in it. You'll be able to [`cider-connect`](https://github.com/clojure-emacs/cider/blob/6a17686799b7ef97bc15fa041016421e5c875bfb/cider.el#L1150) to said server.

For that, you can use the following (more info can be found in the [nREPL Server docs](https://nrepl.org/nrepl/usage/server.html#embedding-nrepl) and [CIDER docs](https://docs.cider.mx/cider/basics/middleware_setup.html#using-embedded-nrepl-server)):

```clojure
(def custom-nrepl-handler
  "We build our own custom nrepl handler, mimicking CIDER's."
  (apply nrepl-server/default-handler
         (conj cider.nrepl.middleware/cider-middleware 'refactor-nrepl.middleware/wrap-refactor)))
         
(nrepl-server/start-server :port port :address bind-address :handler custom-nrepl-handler)
```

The `cider-middleware` is technically optional but highly recommended.

### Passing messages to and from refactor-nrepl

We've already called this a middleware, but we haven't really talked
about what that means.  refactor-nrepl is middleware for a REPL.
Specifically it's middleware for a networked REPL, which is managed by
[nREPL][].
refactor-nrepl uses the running REPL to to gain insight about your
project, in order to offer various refactorings.

Most likely you're already in an environment with a nREPL client available, so you don't have to worry about anything except sending and receiving messages:

```clj
=> (require '[nrepl.core :as repl])
nil
=> (with-open [conn (repl/connect :port 59258)]
     (-> (repl/client conn 1000)    ; message receive timeout required
       (repl/message {:op "eval" :code "(+ 2 3)"})
       repl/response-values))
;;=> [5]
```

In the example above we're talking to one of the built-in nREPL ops, `eval`, passing it the data `:code "(+ 2 3)"`.  The rest of the readme details or own nREPL ops which provide various refactoring support.

## Available features

### Configuration

Configuration settings are passed along with each msg, currently the recognized options are as follows:

```clj
{
 ;; Verbose setting for debugging.  The biggest effect this has is
 ;; to not catch any exceptions to provide meaningful error
 ;; messages for the client.

 :debug false

  ;; if `true`:
{'fraction_non_alphanumeric': 0.1271551724137931, 'fraction_numerical': 0.015924329501915708, 'mean_word_length': 4.477377049180328, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 26, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '34208', 'n_tokens_mistral': 2981, 'n_tokens_neox': 2808, 'n_words': 806}