syl20bnr / spacemacs

A community-driven Emacs distribution - The best editor is neither Emacs nor Vim, it's Emacs *and* Vim!
http://spacemacs.org
GNU General Public License v3.0
23.64k stars 4.89k forks source link

Clojure and LSP - spaces and return characters deleted when typing function calls #14389

Closed practicalli-johnny closed 2 years ago

practicalli-johnny commented 3 years ago

Description :octocat:

Clojure with LSP - unable to type spaces or return characters after function names when writing new lines of code. It does not seem to affect existing code expressions.

Reproduction guide :beetle:

No configuration outside of Spacemacs has been created for LSP. A REPL has not been started by cider-jack-in or connected to via cider-connect.

Observed behaviour: :eyes: :broken_heart: Any space character typed after a function call expression in Clojure is immediately deleted

Expected behaviour: :heart: :smile: I should be able to type (defn fubar []) without spaces being automatically deleted.

System Info :computer:

Backtrace :paw_prints:

<<BACKTRACE IF RELEVANT>>
practicalli-johnny commented 3 years ago

There seems to be a conflict with clojure-mode aggressive indent code automatically https://emacsredux.com/blog/2016/02/07/auto-indent-your-code-with-aggressive-indent-mode/

The following code in dotspacemacs/user-config enables aggressive indent mode.

 (add-hook 'clojure-mode-hook #'aggressive-indent-mode)

Removing this code stops the issue from occuring. However, this feature of clojure-mode is used extensively, so looking for a solution where this works with Clojure LSP.

One possibility is that clojure-lsp is also trying to do formatting as code it typed and therefore conflicting with aggressive-indent-mode from clojure-mode.

lebensterben commented 3 years ago

then this should be reported in upstream

practicalli-johnny commented 3 years ago

In further tests, the issue was resolved by disabling cljfmt that LSP is using.

Create a ~/.lsp/config.edn file will the following configuration

 :cljfmt {:remove-surrounding-whitespace? false}

As this issue seems to be a conflict between two Emacs packages that are competing to do the formatting, it does not seem an upstream issue. If only one method to automatically indent is used then there is no problem.

This seems to be the type of situation that Spacemacs was created to solve.

lebensterben commented 3 years ago

disagree.

Do you expect a typical Clojure developer to rely on aggressive indent mode so much that he would rather to turn off the language specific formatter, cljfmt?

practicalli-johnny commented 3 years ago

@lebensterben I don't understand your comment. I cannot speak for other developers and never claimed to be a typical Clojure developer, so find your comment strange and mildly hostile.

I have show how I have initially resolved the issue of not being able to type spaces and return character when using a the aggressive indent feature of Clojure mode.

I have used aggressive indent for several years to format the code I write with. So disabling cljfmt in LSP is the most appropriate approach for me.

Actually I have disable more settings in cljformat as it makes the code less readable to me.

I have not needed to use cljfmt, which the project quite clearly states is not claiming to be the defacto format tool for clojure. Cider and Clojure mode have managed my formatting needs very well for many years.

cljfmt format configuration is quite involved in the examples I have seen from those using LSP, so will take some time to review. For now it is not a feature I need from LSP.

alexey0308 commented 3 years ago

Hi @jr0cket

is it correct, that you added to your private user-config (add-hook 'clojure-mode-hook #'aggressive-indent-mode)? aggressive-indent is a separate package, it is not part of clojure-mode, although this package is mentioned by Bozhidar in the link you attached.

Does it help, if you configure lsp as

(lsp :variables lsp-enable-on-type-formatting nil)

?

practicalli-johnny commented 3 years ago

Thanks for the suggestion @alexey0308

is it correct, that you added to your private user-config (add-hook 'clojure-mode-hook #'aggressive-indent-mode)?

Yes. I have added this hook to dotspacemacs/user-config along with several other very useful settings that I rely on to format my Clojure code

  ;; Indentation of function forms
  ;; https://github.com/clojure-emacs/clojure-mode#indentation-of-function-forms
  (setq clojure-indent-style 'align-arguments)
  ;;
  ;; Vertically align s-expressions
  ;; https://github.com/clojure-emacs/clojure-mode#vertical-alignment
  (setq clojure-align-forms-automatically t)
  ;;
  ;; Auto-indent code automatically
  ;; https://emacsredux.com/blog/2016/02/07/auto-indent-your-code-with-aggressive-indent-mode/
  (add-hook 'clojure-mode-hook #'aggressive-indent-mode)

I've been using these configurations since early 2019 to make my code very readable.

aggressive-indent is a separate package, it is not part of clojure-mode, although this package is mentioned by Bozhidar in the link you attached.

Ah yes of course, its a hook into clojure-mode and not clojure mode itself.

Does it help, if you configure lsp as

(lsp :variables lsp-enable-on-type-formatting nil)

The above configuration alone does not resolve the issue of deleting spaces and return characters within an s-expression, although it is useful to know what lsp variables are available. I will look into what lsp variables are available over the weekend.

Until then I have disabled most of the cljfmt rules in a ~/.lsp/config/edn configuration file

 :cljfmt {:indentation?                          false
          :remove-trailing-whitespace?           false
          :remove-surrounding-whitespace?        false
          :remove-consecutive-blank-lines?       false
          :remove-multiple-non-indenting-spaces? false
          :split-keypairs-over-multiple-lines?   true}

This works for my preferences, although I am new to lsp and cljfmt so am sure this configuration will evolve. I am happy to replace aggressive indent if I can configure lsp / cljfmt to do the same thing.

github-actions[bot] commented 2 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Please let us know if this issue is still valid!