Repository: clojure-lsp/clojure-lsp (clojure)

Issue:
Add initialization options to disable formatting providers
Calva provides its own formatter that is a customized version of cljfmt. I think the plan is to eventually merge any changes we need into main cljfmt and use it from clojure-lsp, but for now having lsp provide formatting can cause some confusion (VS Code allows a user to choose which formatter to use, but since it sees them as both coming from Calva, they are named the same in the UI picker).

Until we can use cljfmt from lsp, being able to disable formatters from lsp is desirable.

Pull request description:
Fixes #206

Patch:
diff --git a/README.md b/README.md
index 96c27d390..90b909f0e 100644
--- a/README.md
+++ b/README.md
@@ -150,6 +150,10 @@ It is possible to pass some options to clojure-lsp through clients' `Initializat
 
 `auto-add-ns-to-new-files?` if true or not present, will automatically add the `ns` form in new files. 
 
+`document-formatting?` if true or not present, document formatting is provided.
+
+`document-range-formatting?` if true or not present, document range formatting is provided.
+
 `dependency-scheme` by default, dependencies are linked with vim's `zipfile://<zipfile>::<innerfile>` scheme, however you can use a scheme of `jar` to get urls compatible with java's JarURLConnection. You can have the client make an lsp extension request of `clojure/dependencyContents` with the jar uri and the server will return the jar entry's contents. [Similar to java clients](https://github.com/redhat-developer/vscode-java/blob/a24945453092e1c39267eac9367c759a6c7b0497/src/extension.ts#L290-L298)
 
 `cljfmt` json encoded configuration for https://github.com/weavejester/cljfmt
diff --git a/src/clojure_lsp/interop.clj b/src/clojure_lsp/interop.clj
index 45a1488e6..4ac721cc8 100644
--- a/src/clojure_lsp/interop.clj
+++ b/src/clojure_lsp/interop.clj
@@ -388,7 +388,9 @@
         (update :macro-defs clean-symbol-map)
         (update :project-specs #(->> % (mapv kwd-keys) not-empty))
         (update :cljfmt kwd-keys)
-        (update-in [:cljfmt :indents] clean-symbol-map))))
+        (update-in [:cljfmt :indents] clean-symbol-map)
+        (update :document-formatting? (fnil identity true))
+        (update :document-range-formatting? (fnil identity true)))))
 
 (defn document->decoded-uri [document]
   (-> document
diff --git a/src/clojure_lsp/main.clj b/src/clojure_lsp/main.clj
index d79ba64ed..e3b732c6a 100644
--- a/src/clojure_lsp/main.clj
+++ b/src/clojure_lsp/main.clj
@@ -391,11 +391,11 @@
     (^CompletableFuture initialize [^InitializeParams params]
       (go :initialize
           (end
-            (do
+            (let [c-settings (client-settings params)]
               (log/warn "Initialize")
               (#'handlers/initialize (.getRootUri params)
                                      (client-capabilities params)
-                                     (client-settings params))
+                                     c-settings)
               (CompletableFuture/completedFuture
                 (InitializeResult. (doto (ServerCapabilities.)
                                      (.setHoverProvider true)
@@ -404,8 +404,8 @@
                                      (.setReferencesProvider true)
                                      (.setRenameProvider true)
                                      (.setDefinitionProvider true)
-                                     (.setDocumentFormattingProvider true)
-                                     (.setDocumentRangeFormattingProvider true)
+                                     (.setDocumentFormattingProvider (:document-formatting? c-settings))
+                                     (.setDocumentRangeFormattingProvider (:document-range-formatting? c-settings))
                                      (.setDocumentSymbolProvider true)
                                      (.setDocumentHighlightProvider true)
                                      (.setWorkspaceSymbolProvider true)


Test patch:
diff --git a/test/clojure_lsp/main_test.clj b/test/clojure_lsp/main_test.clj
index a9f55bcda..b3a08f41c 100644
--- a/test/clojure_lsp/main_test.clj
+++ b/test/clojure_lsp/main_test.clj
@@ -11,4 +11,10 @@
     (let [params (InitializeParams.)]
       (.setInitializationOptions params nil)
       (is (= #{"src" "test"}
-             (get (main/client-settings params) :source-paths))))))
+             (get (main/client-settings params) :source-paths)))))
+  (testing "document-formatting? is set to true if not provided"
+    (let [params (InitializeParams.)]
+      (is (= true (:document-formatting? (main/client-settings params))))))
+  (testing "document-range-formatting? is set to true if not provided"
+    (let [params (InitializeParams.)]
+      (is (= true (:document-range-formatting? (main/client-settings params)))))))