]> git.eshelyaron.com Git - emacs.git/commitdiff
Support snippet completions
authorJoão Távora <joaotavora@gmail.com>
Fri, 10 Aug 2018 01:29:26 +0000 (02:29 +0100)
committerJoão Távora <joaotavora@gmail.com>
Fri, 17 Aug 2018 23:27:25 +0000 (00:27 +0100)
* eglot.el (eglot-client-capabilities): Declare support for
snippet-based completions.
(eglot-completion-at-point): Expand snippet completions with
YASnippet if that is found.
(eglot-note, eglot-warning, eglot-error): Diagnostic
overlay priorities have to be slightly lower than yasnippet's,
which must be reasonably high.

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

lisp/progmodes/eglot.el

index 63fbce205d5fccf2e51db95d9df59f9be7575265..13c72abdc4e463be5facfd76b335f2a1e1b8b757 100644 (file)
@@ -202,7 +202,8 @@ let the buffer grow forever."
              :synchronization (list
                                :dynamicRegistration :json-false
                                :willSave t :willSaveWaitUntil t :didSave t)
-             :completion         `(:dynamicRegistration :json-false)
+             :completion      (list :dynamicRegistration :json-false
+                                    :completionItem `(:snippetSupport t))
              :hover              `(:dynamicRegistration :json-false)
              :signatureHelp      `(:dynamicRegistration :json-false)
              :references         `(:dynamicRegistration :json-false)
@@ -956,13 +957,15 @@ Uses THING, FACE, DEFS and PREPEND."
 (defalias 'eglot--make-diag 'flymake-make-diagnostic)
 (defalias 'eglot--diag-data 'flymake-diagnostic-data)
 
-(dolist (type '(eglot-error eglot-warning eglot-note))
-  (put type 'flymake-overlay-control
-       `((mouse-face . highlight)
-         (keymap . ,(let ((map (make-sparse-keymap)))
-                      (define-key map [mouse-1]
-                        (eglot--mouse-call 'eglot-code-actions))
-                      map)))))
+(cl-loop for i from 1
+         for type in '(eglot-note eglot-warning eglot-error )
+         do (put type 'flymake-overlay-control
+                 `((mouse-face . highlight)
+                   (priority . ,(+ 50 i))
+                   (keymap . ,(let ((map (make-sparse-keymap)))
+                                (define-key map [mouse-1]
+                                  (eglot--mouse-call 'eglot-code-actions))
+                                map)))))
 
 \f
 ;;; Protocol implementation (Requests, notifications, etc)
@@ -1384,7 +1387,7 @@ is not active."
                  (items (if (vectorp resp) resp (plist-get resp :items))))
             (mapcar
              (jsonrpc-lambda (&rest all &key label insertText &allow-other-keys)
-               (let ((insert (or insertText label)))
+               (let ((insert (or insertText (string-trim-left label))))
                  (add-text-properties 0 1 all insert)
                  (put-text-property 0 1 'eglot--lsp-completion all insert)
                  insert))
@@ -1425,9 +1428,17 @@ is not active."
                (erase-buffer)
                (insert (eglot--format-markup documentation))
                (current-buffer)))))
-       :exit-function (lambda (_string _status)
-                        (eglot--signal-textDocument/didChange)
-                        (eglot-eldoc-function))))))
+       :exit-function (lambda (obj _status)
+                        (cl-destructuring-bind (&key insertTextFormat
+                                                     insertText
+                                                     &allow-other-keys)
+                            (text-properties-at 0 obj)
+                          (when (and (eql insertTextFormat 2)
+                                     (fboundp 'yas-expand-snippet))
+                            (delete-region (- (point) (length obj)) (point))
+                            (funcall 'yas-expand-snippet insertText))
+                          (eglot--signal-textDocument/didChange)
+                          (eglot-eldoc-function)))))))
 
 (defvar eglot--highlights nil "Overlays for textDocument/documentHighlight.")