emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[elpa] externals/eglot a9010f1 01/12: Implement formatting (#19)


From: João Távora
Subject: [elpa] externals/eglot a9010f1 01/12: Implement formatting (#19)
Date: Fri, 22 Jun 2018 11:52:15 -0400 (EDT)

branch: externals/eglot
commit a9010f1924fa31f686824585cbcf5057c8892afc
Author: mkcms <address@hidden>
Commit: João Távora <address@hidden>

    Implement formatting (#19)
    
    Implement textDocument/formatting
    
    * eglot.el (eglot-format-buffer): New command to format
    current buffer.
    
    * eglot-tests.el (formatting): New test.
---
 eglot-tests.el | 20 ++++++++++++++++++++
 eglot.el       | 29 +++++++++++++++++++++++++++++
 2 files changed, 49 insertions(+)

diff --git a/eglot-tests.el b/eglot-tests.el
index 1a25371..602606c 100644
--- a/eglot-tests.el
+++ b/eglot-tests.el
@@ -375,6 +375,26 @@ Pass TIMEOUT to `eglot--with-timeout'."
         (while (not eldoc-last-message) (accept-process-output nil 0.1))
         (should (string-match "^exit" eldoc-last-message))))))
 
+(ert-deftest formatting ()
+  "Test document formatting in a python LSP"
+  (skip-unless (and (executable-find "pyls")
+                    (or (executable-find "yapf")
+                        (executable-find "autopep8"))))
+  (eglot--with-dirs-and-files
+      '(("project" . (("something.py" . "def foo():pass"))))
+    (eglot--with-timeout 4
+      (with-current-buffer
+          (eglot--find-file-noselect "project/something.py")
+        (should (eglot--tests-connect))
+        (search-forward ":")
+        (eglot-format-buffer)
+        (should (looking-at "pass"))
+        (should (or
+                 ;; yapf
+                 (string= (buffer-string) "def foo():\n    pass\n")
+                 ;; autopep8
+                 (string= (buffer-string) "def foo(): pass\n")))))))
+
 (ert-deftest javascript-basic ()
   "Test basic autocompletion in a python LSP"
   (skip-unless (executable-find "~/.yarn/bin/javascript-typescript-stdio"))
diff --git a/eglot.el b/eglot.el
index ecfde4a..98802de 100644
--- a/eglot.el
+++ b/eglot.el
@@ -1382,6 +1382,35 @@ DUMMY is ignored."
                             :workspace/symbol
                             (list :query pattern)))))
 
+(defun eglot-format-buffer ()
+  "Format contents of current buffer."
+  (interactive)
+  (unless (eglot--server-capable :documentFormattingProvider)
+    (eglot--error "Server can't format!"))
+  (let* ((server (eglot--current-server-or-lose))
+         (resp
+          (eglot--request
+           server
+           :textDocument/formatting
+           (list :textDocument (eglot--TextDocumentIdentifier)
+                 :options (list
+                           :tabSize tab-width
+                           :insertSpaces (not indent-tabs-mode)))
+           :textDocument/formatting))
+         (before-point
+          (buffer-substring (max (- (point) 60) (point-min)) (point)))
+         (after-point
+          (buffer-substring (point) (min (+ (point) 60) (point-max))))
+         (regexp (and (not (bobp))
+                      (replace-regexp-in-string
+                       "[\s\t\n\r]+" "[\s\t\n\r]+"
+                       (concat "\\(" (regexp-quote after-point) "\\)")))))
+    (when resp
+      (save-excursion
+        (eglot--apply-text-edits resp))
+      (when (and (bobp) regexp (search-forward-regexp regexp nil t))
+        (goto-char (match-beginning 1))))))
+
 (defun eglot-completion-at-point ()
   "EGLOT's `completion-at-point' function."
   (let ((bounds (bounds-of-thing-at-point 'symbol))



reply via email to

[Prev in Thread] Current Thread [Next in Thread]