From 2b1db175a9d64385c531980a5c53b782121291b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20T=C3=A1vora?= Date: Fri, 10 Aug 2018 02:29:26 +0100 Subject: [PATCH] Close #50: Support snippet completions * 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.el | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/eglot.el b/eglot.el index 7f0e91db..e4eb9bdd 100644 --- a/eglot.el +++ b/eglot.el @@ -177,7 +177,8 @@ lasted more than that many seconds." :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) @@ -1286,7 +1287,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)) @@ -1327,9 +1328,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.")