From a06d27a24dd6a90ece41b29be743dd9710df19e8 Mon Sep 17 00:00:00 2001 From: Kang Tu Date: Wed, 31 Jan 2024 00:09:49 -0800 Subject: [PATCH 1/8] Add jsonrpc dependency info (#248) * Add jsonrpc dependency info Based on 1. https://github.com/copilot-emacs/copilot.el/issues/232 2. https://github.com/copilot-emacs/copilot.el/issues/244 --- readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/readme.md b/readme.md index add986e..55d63bf 100644 --- a/readme.md +++ b/readme.md @@ -14,7 +14,7 @@ Retired maintainer: [@zerolfx](https://github.com/zerolfx). ## Installation -0. Ensure your Emacs version is at least 27, and the dependency package `editorconfig` ([melpa](https://melpa.org/#/editorconfig)) is also installed. +0. Ensure your Emacs version is at least 27, the dependency package `editorconfig` ([melpa](https://melpa.org/#/editorconfig)) and `jsonrpc` ([elpa](https://elpa.gnu.org/packages/jsonrpc.html), >= 1.0.24) are both installed. 1. Install [Node.js](https://nodejs.org/en/download/) v18+. (You can specify the path to `node` executable by setting `copilot-node-executable`.) From 2c1c425761e3cdaf139c9e2fa531f942cfb14b16 Mon Sep 17 00:00:00 2001 From: Danny Willems Date: Wed, 31 Jan 2024 09:10:57 +0100 Subject: [PATCH 2/8] Cut at 80 characters (#234) --- copilot-balancer.el | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/copilot-balancer.el b/copilot-balancer.el index 8f5b1f3..0d25595 100644 --- a/copilot-balancer.el +++ b/copilot-balancer.el @@ -168,12 +168,13 @@ Special care has to be taken to ignore pairs in the middle of strings." (defvar copilot-balancer-top-level-form-start-regexp (rx line-start (or (literal "(") (literal "[") (literal "{"))) - "Regexp for the start of a top level form. Assumes cursor is at the start of a line.") + "Regexp for the start of a top level form. Assumes cursor is at the start +of a line.") (defvar copilot-balancer-form-end-regexp (rx (or (literal "}") (literal "]") (literal ")")) line-end) - "Regexp for the end of a form. Assumes cursor is at the last character of the line -(not the actual newline character).") + "Regexp for the end of a form. Assumes cursor is at the last character of +the line (not the actual newline character).") (defun copilot-balancer-get-top-level-form-beginning-to-point (x) (save-excursion From 22f0446a17753716456e0cb8ee33f47731fc4ec3 Mon Sep 17 00:00:00 2001 From: Kelvie Wong Date: Sun, 4 Feb 2024 06:01:22 -0800 Subject: [PATCH 3/8] Fix using numbers in copilot-indentation-alist (#253) * Fix using numbers in copilot-indentation-alist --- copilot.el | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/copilot.el b/copilot.el index 234b25e..8b8750e 100644 --- a/copilot.el +++ b/copilot.el @@ -323,8 +323,9 @@ automatically, browse to %s." user-code verification-uri)) (setq mode (get mode 'derived-mode-parent)))) (when mode (cl-some (lambda (s) - (when (and (boundp s) (numberp (symbol-value s))) - (symbol-value s))) + ;; s can be a symbol or a number. + (cond ((numberp s) s) + ((and (boundp s) (numberp (symbol-value s))) (symbol-value s)))) (alist-get mode copilot-indentation-alist)))) (progn (when (and From 581cadd6f4229223fd5c57984f9595aeb86c84f7 Mon Sep 17 00:00:00 2001 From: Tim Harper Date: Sun, 11 Feb 2024 04:59:21 -0700 Subject: [PATCH 4/8] Use 1-character-long invisible overlay to activate copilot-completion-map (#255) * Use 1-character-long invisible keymap overlay to activate copilot-completion-map 0-length overlays cannot contain the point, and if another overlay does contain the point, the keymap from the 0-length overlay will not be active. As a workaround, we create two overlays for copilot completion: one to display the completion candidate, and another to activate the copilot-completion-map. --- copilot.el | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/copilot.el b/copilot.el index 8b8750e..8773ddc 100644 --- a/copilot.el +++ b/copilot.el @@ -103,6 +103,9 @@ indentation offset." (defvar-local copilot--overlay nil "Overlay for Copilot completion.") +(defvar-local copilot--keymap-overlay nil + "Overlay used to surround point and make copilot-completion-keymap activate.") + (defvar copilot--connection nil "Copilot agent jsonrpc connection instance.") @@ -531,12 +534,20 @@ To work around posn problems with after-string property.") (defconst copilot-completion-map (make-sparse-keymap) "Keymap for Copilot completion overlay.") +(defun copilot--get-or-create-keymap-overlay () + "Make or return the local copilot--keymap-overlay." + (unless (overlayp copilot--keymap-overlay) + (setq copilot--keymap-overlay (make-overlay 1 1 nil nil t)) + (overlay-put copilot--keymap-overlay 'keymap copilot-completion-map) + (overlay-put copilot--keymap-overlay 'priority 101)) + copilot--keymap-overlay) + (defun copilot--get-overlay () "Create or get overlay for Copilot." (unless (overlayp copilot--overlay) (setq copilot--overlay (make-overlay 1 1 nil nil t)) - (overlay-put copilot--overlay 'keymap copilot-completion-map) - (overlay-put copilot--overlay 'priority 100)) + (overlay-put + copilot--overlay 'keymap-overlay (copilot--get-or-create-keymap-overlay))) copilot--overlay) (defun copilot--overlay-end (ov) @@ -546,6 +557,16 @@ To work around posn problems with after-string property.") (defun copilot--set-overlay-text (ov completion) "Set overlay OV with COMPLETION." (move-overlay ov (point) (line-end-position)) + + ;; set overlay position for the keymap, to activate copilot-completion-map + ;; + ;; if the point is at the end of the buffer, we will create a + ;; 0-length buffer. But this is ok, since the keymap will still + ;; activate _so long_ as no other overlay contains the point. + ;; + ;; see https://github.com/copilot-emacs/copilot.el/issues/251 for details. + (move-overlay (overlay-get ov 'keymap-overlay) (point) (min (point-max) (+ 1 (point)))) + (let* ((tail (buffer-substring (copilot--overlay-end ov) (line-end-position))) (p-completion (concat (propertize completion 'face 'copilot-overlay-face) tail))) @@ -586,6 +607,7 @@ already saving an excursion. This is also a private function." (copilot--async-request 'notifyRejected (list :uuids `[,(overlay-get copilot--overlay 'uuid)]))) (delete-overlay copilot--overlay) + (delete-overlay copilot--keymap-overlay) (setq copilot--real-posn nil))) (defun copilot-accept-completion (&optional transform-fn) From 228be50d84537ab2af4329ab89a31d5fcc77c716 Mon Sep 17 00:00:00 2001 From: emil-vdw Date: Sun, 11 Feb 2024 13:22:07 +0100 Subject: [PATCH 5/8] Fix agent executable path for Linux --- copilot.el | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/copilot.el b/copilot.el index 6832872..f4a59ac 100644 --- a/copilot.el +++ b/copilot.el @@ -102,9 +102,9 @@ indentation offset." :type 'directory :group 'copilot) -(defconst copilot--dist-dir +(defconst copilot--server-executable (if (eq system-type 'windows-nt) - (f-join copilot-install-dir "node_modules" "copilot-node-server" "copilot" "dist") + (f-join copilot-install-dir "node_modules" "copilot-node-server" "copilot" "dist" "agent.js") (f-join copilot-install-dir "bin" "copilot-node-server")) "The dist directory containing agent.js file.") @@ -205,7 +205,7 @@ Please upgrade the server via `M-x copilot-reinstall-server`")) :notification-dispatcher #'copilot--handle-notification :process (make-process :name "copilot agent" :command (list copilot-node-executable - (concat copilot--dist-dir "/agent.js")) + copilot--server-executable) :coding 'utf-8-emacs-unix :connection-type 'pipe :stderr (get-buffer-create "*copilot stderr*") From 32a78619a2f08e2fecb242030de2a94e4f9c554c Mon Sep 17 00:00:00 2001 From: emil-vdw Date: Sun, 11 Feb 2024 13:26:16 +0100 Subject: [PATCH 6/8] Improve formatting --- copilot.el | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/copilot.el b/copilot.el index f4a59ac..9181d8d 100644 --- a/copilot.el +++ b/copilot.el @@ -912,12 +912,17 @@ command that triggered `post-command-hook'." (defun copilot-async-start-process (callback error-callback &rest command) "Start async process COMMAND with CALLBACK and ERROR-CALLBACK." (let ((name (cl-first command))) - (with-current-buffer (compilation-start (mapconcat #'shell-quote-argument (-filter (lambda (cmd) - (not (null cmd))) - command) - " ") t - (lambda (&rest _) - (generate-new-buffer-name "*copilot-install-server*"))) + (with-current-buffer + (compilation-start + (mapconcat + #'shell-quote-argument + (-filter + (lambda (cmd) + (not (null cmd))) + command) " ") + t + (lambda (&rest _) + (generate-new-buffer-name "*copilot-install-server*"))) (view-mode +1) (add-hook 'compilation-finish-functions From 3fb251cd25f99319ba5cd378d84316e4b7e748f9 Mon Sep 17 00:00:00 2001 From: emil-vdw Date: Sun, 11 Feb 2024 13:26:29 +0100 Subject: [PATCH 7/8] Only call callbacks when non-nil --- copilot.el | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/copilot.el b/copilot.el index 9181d8d..27b078b 100644 --- a/copilot.el +++ b/copilot.el @@ -928,11 +928,13 @@ command that triggered `post-command-hook'." 'compilation-finish-functions (lambda (_buf status) (if (string= "finished\n" status) - (condition-case err - (funcall callback) - (error - (funcall error-callback (error-message-string err)))) - (funcall error-callback (s-trim-right status)))) + (when callback + (condition-case err + (funcall callback) + (error + (funcall error-callback (error-message-string err))))) + (when error-callback + (funcall error-callback (s-trim-right status))))) nil t)))) ;;;###autoload From 8379e610a89eb4ce41e59789a001db9b212d0908 Mon Sep 17 00:00:00 2001 From: emil-vdw Date: Sun, 11 Feb 2024 13:50:36 +0100 Subject: [PATCH 8/8] Fix wrong copilot-installed-version Linux path --- copilot.el | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/copilot.el b/copilot.el index c703dbc..29293a6 100644 --- a/copilot.el +++ b/copilot.el @@ -922,13 +922,16 @@ command that triggered `post-command-hook'." (defun copilot-installed-version () "Return the version number of currently installed `copilot-node-server'." (when-let* - ((package-json (f-join copilot-install-dir "node_modules" "copilot-node-server" "package.json")) + ((package-json + (if (eq system-type 'windows-nt) + (f-join copilot-install-dir "node_modules" "copilot-node-server" "package.json") + (f-join copilot-install-dir "lib" "node_modules" "copilot-node-server" "package.json"))) ((file-exists-p package-json))) (with-temp-buffer (insert-file-contents package-json) - (goto-char (point-min)) - (when (search-forward "\"version\": \"" nil t) - (buffer-substring-no-properties (point) (- (line-end-position) 2)))))) + (save-match-data + (when (re-search-forward "\"version\": \"\\([0-9]+\.[0-9]+\.[0-9]+\\)") + (match-string 1)))))) ;; XXX: This function is modified from `lsp-mode'; see `lsp-async-start-process' ;; function for more information.