From fd1c57a96c1b71ccd1a430234030a700d468b80e Mon Sep 17 00:00:00 2001 From: Jim Myhrberg Date: Mon, 26 Sep 2022 14:33:35 +0100 Subject: [PATCH] fix(completion/copilot): custom accept fun ignoring trailing "{" chars In languages that uses "{" for blocks, accepting a Copilot completion that ends with "{", causing a structural imbalance, which structural-based packages like smartparens does not like and causes headaches. With the custom accept function, trailing "{" chars along with any whitespace before it, are ignored from Copilot completion. An exception is made for if the removal of trailing "{" and whitespace yields an empty completion, in which case the completion is accepted as is. --- modules/completion/siren-copilot.el | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/modules/completion/siren-copilot.el b/modules/completion/siren-copilot.el index e8822fe..7894069 100644 --- a/modules/completion/siren-copilot.el +++ b/modules/completion/siren-copilot.el @@ -13,8 +13,8 @@ (prog-mode . copilot-mode) :general - ("C-" 'siren-copilot-accept-completion-dwim) - ("" 'siren-copilot-accept-completion-dwim) + ("C-" 'siren-copilot-accept-completion-without-trailing-opening-curly-bracket-dwim) + ("" 'siren-copilot-accept-completion-without-trailing-opening-curly-bracket-dwim) ("M-F" 'siren-copilot-accept-completion-by-word-dwim) ("M-E" 'siren-copilot-accept-completion-by-line-dwim) (:keymaps 'copilot-completion-map @@ -43,6 +43,20 @@ (apply f args) (copilot-complete))) + (defun siren-copilot-accept-completion-without-trailing-opening-curly-bracket-dwim () + "Accept the current completion or trigger copilot-compilot." + (interactive) + (siren-copilot-complete-or-call 'siren-copilot-accept-completion-without-trailing-opening-curly-bracket)) + + (defun siren-copilot-accept-completion-without-trailing-opening-curly-bracket () + "Accept completion removing ` {' or `{' from the end of the completion." + (interactive) + (copilot-accept-completion (lambda (completion) + (let ((index (string-match-p "\s*{\\'" completion))) + (if (and index (> index 0)) + (substring completion 0 index) + completion))))) + :config (with-eval-after-load 'company ;; Use company popup even when there's only one result