From: Michal Krzywkowski Date: Fri, 15 Jun 2018 22:47:52 +0000 (+0200) Subject: Implement formatting () X-Git-Tag: emacs-29.0.90~1616^2~524^2~4^2~501 X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=504389181d15880179f668e4e11143be62487c9b;p=emacs.git Implement formatting () 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 --- diff --git a/lisp/progmodes/eglot.el b/lisp/progmodes/eglot.el index ecfde4a022f..98802dea60b 100644 --- a/lisp/progmodes/eglot.el +++ b/lisp/progmodes/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))