Skip to content

Refactor Code with Clojure LSPλ︎

, r opens Clojure LSP refactor menu

Clojure Refactor menu with Clojure LSP refactor menu - light Clojure Refactor menu with Clojure LSP refactor menu - dark

The majority of refactor tools from Clojure LSP are code actions, providing context specific tools that are only available when relevant.

Renamingλ︎

Symbols such as namespace names, function definitions, names bound to values and keywords can be renamed across a whole project

, r r to rename symbol at point

Clojure LSP Refactor - Rename symbol

Refactor namespaceλ︎

, rr (lsp-rename) with the cursor on a namespace will prompt for changes to that namespace.

Enter to save the change to the namespace, updating

  • nsnamespace definition name
  • file name of the current file, including converting - to _ in the file name where relevant
  • all reference to the changed namespace name across all source code files in the project (on the class path)

Code Actionsλ︎

, a a opens the LSP code actions menu to provide context specific refactor tools

lsp-ui sidebar shows available code actions when the following variables are added to the lsp layer in the .spacemacs layer configuration:

lsp-ui-sideline-enable t
lsp-ui-sideline-show-code-actions t

Clojure LSP features includes examples of code actions.

Converting Parensλ︎

Convert between literal hash-map {}, vector [], set #{} and list () forms

Clojure LSP Refactor - Convert between literal hash-map vector set and list forms

Add library namespace to current namespaceλ︎

Add the suggested library to the ns namespace definition using a require form with an alias

Clojure LSP refactor - require suggested library with an alias in the ns definition

Add import form for known Java methods and classes

Clojure LSP refactor - add Java library import form

Move to let bindingλ︎

Move an expression to be a value of a new local name in an existing let form

Clojure LSP refactor - move expression to new let binding name

Move a value to a new local name in an existing let binding (inline symbol)

Clojure LSP refactor - inline symbol

Extract expression to new functionλ︎

Create a new public function definition with the expression at point the body of that new function.

Replace the expression at point with a call to the new function with arguments extracted from the expression at point where relevant.

Clojure LSP refactor - extract current expression to a new public function definition

Convert to thread macroλ︎

Convert (nested) expression to a thread first or thread last form

Clojure LSP refactor - convert expression to thread macro

Convert function definition to privateλ︎

Clojure LSP refactor - convert function to private

Toggle function formλ︎

Toggle between short and normal forms of a function definition

Clojure LSP refactor - toggle function definition form between short and normal

Re-order data structuresλ︎

Move elements of a data structure using lsp-clojure-coll-move-entry-up and lsp-clojure-coll-move-entry-up, which have no key binding in Spacemacs by default (Doom Emacs uses Meta up/down arrow keys)

Clojure LSP - move elements in data structures

WARNING: does not seem to work and does not show as a code action