]> git.eshelyaron.com Git - emacs.git/commitdiff
Implement formatting ()
authorMichal Krzywkowski <k.michal@zoho.com>
Fri, 15 Jun 2018 22:47:52 +0000 (00:47 +0200)
committerJoão Távora <joaotavora@gmail.com>
Fri, 15 Jun 2018 22:47:52 +0000 (23:47 +0100)
Implement textDocument/formatting

* eglot.el (eglot-format-buffer): New command to format
current buffer.

* eglot-tests.el (formatting): New test.

GitHub-reference: https://github.com/joaotavora/eglot/issues/19

lisp/progmodes/eglot.el

index ecfde4a022fa77a6f630490680c701540c41b888..98802dea60b380d6f62e628b6631ae9a21784862 100644 (file)
@@ -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))