Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Prepare for MELPA 2 #287

Merged
merged 1 commit into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 28 additions & 16 deletions copilot-balancer.el
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
lisp-interaction-mode
scheme-mode
clojure-mode)
"List of lisp modes to balance.")
"List of Lisp modes to balance.")

(defvar copilot-balancer-lisp-pairs
(let ((h (make-hash-table :test 'equal :size 7)))
Expand All @@ -54,7 +54,7 @@
(puthash ?\N{QUOTATION MARK} ?\N{QUOTATION MARK} h)

h)
"Hash table of lisp pairs to balance.")
"Hash table of Lisp pairs to balance.")

(defvar copilot-balancer-closing-lisp-pairs
(let ((h (make-hash-table :test 'equal :size 4)))
Expand All @@ -63,7 +63,7 @@
(puthash ?\] t h)
(puthash ?\N{QUOTATION MARK} t h)
h)
"Hash table of closing lisp pairs, such as right parenthese, etc.")
"Hash table of closing Lisp pairs, such as right parenthese, etc.")

(defvar copilot-balancer-debug-buffer (get-buffer-create " *copilot-balancer*")
"Buffer for debugging copilot-balancer.")
Expand All @@ -76,6 +76,7 @@
vars)))

(defun copilot-balancer--debug (args)
"Debug ARGS."
(let* ((start (plist-get args :start))
(end (plist-get args :end))
(deleted-text (plist-get args :deleted-text))
Expand Down Expand Up @@ -127,13 +128,14 @@
nil)))

(defun copilot-balancer-remove-last (lst)
"Remove last item from LST."
(if (not (cdr lst))
nil
(cons (car lst) (copilot-balancer-remove-last (cdr lst)))))

(defun copilot-balancer-extract-pairs (s)
"Extract a list of pair characters from string s
like parentheses, braces, brackets, or double quotes.
"Extract a list of pair characters from string S.
Like parentheses, braces, brackets, or double quotes.

Note that pairs in the middle of strings are included, so take care."
(let* ((pairs '())
Expand All @@ -151,7 +153,7 @@ Note that pairs in the middle of strings are included, so take care."
(nreverse pairs)))

(defun copilot-balancer-trim-closing-pairs-at-end (s)
"Trim closing pairs from string s starting from the end.
"Trim closing pairs from string S starting from the end.
Stops when a non-close-pair character is found."
(let* ((n (length s))
(i (1- n))
Expand All @@ -168,9 +170,10 @@ Stops when a non-close-pair character is found."
(substring s 0 (1+ i))))

(defun copilot-balancer-collapse-matching-pairs (pairs in-string)
"Collapse matching pairs in list pairs.
"Collapse matching PAIRS in list pairs.

Special care has to be taken to ignore pairs in the middle of strings."
Special care has to be taken to ignore pairs in the middle
of strings (IN-STRING)."
(let ((filtered-pairs '()))
;; delete pairs in strings
(dolist (x pairs)
Expand All @@ -185,32 +188,36 @@ Special care has to be taken to ignore pairs in the middle of strings."
(dolist (x filtered-pairs)
(let ((y (gethash x copilot-balancer-lisp-pairs)))
(cond
((and (not (null collapsed-pairs))
((and collapsed-pairs
(eq (car collapsed-pairs) y))
(setq collapsed-pairs (cdr collapsed-pairs)))
(t
(push x collapsed-pairs)))))
(cons (nreverse collapsed-pairs) in-string))))

(defun copilot-balancer-get-other-pair (c)
"Get other pair by key C."
(gethash c copilot-balancer-lisp-pairs))

(defun copilot-balancer-trim-common-prefix (list1 list2)
"Trim common prefix from LIST1 and LIST2."
(if (and list1 list2 (equal (car list1) (car list2)))
(copilot-balancer-trim-common-prefix (cdr list1) (cdr list2))
(cons list1 list2)))

(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)
"Get top level beginning point by X."
(save-excursion
(save-restriction
(widen)
Expand All @@ -223,6 +230,7 @@ the line (not the actual newline character).")
(buffer-substring-no-properties (point) x))))

(defun copilot-balancer-get-point-to-top-level-form-end (x)
"Get point to top level by X."
(save-excursion
(save-restriction
(widen)
Expand All @@ -234,7 +242,7 @@ the line (not the actual newline character).")
(forward-line 1)
(beginning-of-line)
(while (and (not (funcall on-last-line?))
(< (point) (point-max))
(not (eobp))
(not (looking-at-p copilot-balancer-top-level-form-start-regexp)))
(forward-line 1)
(beginning-of-line))
Expand All @@ -246,7 +254,7 @@ the line (not the actual newline character).")
(unless (bolp) (backward-char))
(while (and (< 1 (line-number-at-pos (point)))
(< 1 (point))
(< (point) (point-max))
(not (eobp))
(not (looking-at-p copilot-balancer-form-end-regexp)))
(forward-line -1)
(end-of-line)
Expand All @@ -256,14 +264,15 @@ the line (not the actual newline character).")
(buffer-substring-no-properties x (point))))))

(defun copilot-balancer-see-string-end-p (p point-upper-bound)
"Look for the string at the end between P and POINT-UPPER-BOUND."
(save-excursion
(save-restriction
(widen)
(goto-char p)
(let ((flag t)
(ret nil))
(while (and flag
(< (point) (point-max))
(not (eobp))
(<= (point) point-upper-bound)) ; is it with or without equals?
(let ((c (char-after)))
(cond
Expand All @@ -278,6 +287,7 @@ the line (not the actual newline character).")
ret))))

(defun copilot-balancer-odd-dquote-count-p (s)
"Odd dquote count from S."
(let ((n (length s))
(i 0)
(count 0))
Expand All @@ -292,6 +302,7 @@ the line (not the actual newline character).")
(cl-oddp count)))

(defun copilot-balancer--fix-lisp (start end completion)
"Fix Lisp from START to END with COMPLETION."
(pcase-let*
((prefix (copilot-balancer-get-top-level-form-beginning-to-point start))
(prefix-pairs (copilot-balancer-extract-pairs prefix))
Expand Down Expand Up @@ -356,6 +367,7 @@ the line (not the actual newline character).")
(list start end new-completion)))

(defun copilot-balancer-fix-completion (start end completion)
"Fix COMPLETION from START to END."
(let* ()
(cond
((apply #'derived-mode-p copilot-balancer-lisp-modes)
Expand Down
44 changes: 27 additions & 17 deletions copilot.el
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,10 @@ performance."
:type '(repeat function))

(defcustom copilot-indent-offset-warning-disable nil
"Disable warning when copilot--infer-indentation-offset cannot find
indentation offset."
"Disable indentation warnings.

Warning occurs when the function `copilot--infer-indentation-offset' cannot
find indentation offset."
:group 'copilot
:type 'boolean)

Expand Down Expand Up @@ -162,7 +164,8 @@ You may adjust this variable at your own risk."
(defvar-local copilot--last-doc-version 0
"The document version of the last completion.")
(defvar-local copilot--doc-version 0
"The document version of the current buffer. Incremented after each change.")
"The document version of the current buffer.
Incremented after each change.")

(defun copilot--buffer-changed ()
"Return non-nil if the buffer has changed since last completion."
Expand Down Expand Up @@ -261,7 +264,11 @@ Use this for custom bindings in `copilot-mode'.")
(jsonrpc-notify copilot--connection ,@args)))

(cl-defmacro copilot--async-request (method params &rest args &key (success-fn #'copilot--ignore-response) &allow-other-keys)
"Send an asynchronous request to the copilot agent."
"Send an asynchronous request to the copilot agent.

Arguments METHOD, PARAMS and ARGS are used in function `jsonrpc-async-request'.

SUCCESS-FN is the CALLBACK."
`(progn
(unless (copilot--connection-alivep)
(copilot--start-agent))
Expand Down Expand Up @@ -679,7 +686,7 @@ To work around posn problems with after-string property.")
"Keymap for Copilot completion overlay.")

(defun copilot--posn-advice (&rest args)
"Remap posn if in copilot-mode."
"Remap posn if in copilot-mode with ARGS."
(when copilot-mode
(let ((pos (or (car-safe args) (point))))
(when (and copilot--real-posn
Expand Down Expand Up @@ -737,8 +744,8 @@ To work around posn problems with after-string property.")
(defun copilot--display-overlay-completion (completion uuid start end)
"Show COMPLETION with UUID between START and END.

(save-excursion) is not necessary since there is only one caller, and they are
already saving an excursion. This is also a private function."
`save-excursion' is not necessary since there is only one caller, and they are
already saving an excursion. This is also a private function."
(copilot-clear-overlay)
(when (and (s-present-p completion)
(or (= start (point)) ; up-to-date completion
Expand All @@ -752,7 +759,8 @@ already saving an excursion. This is also a private function."
(copilot--async-request 'notifyShown (list :uuid uuid)))))

(defun copilot-clear-overlay (&optional is-accepted)
"Clear Copilot overlay. If IS-ACCEPTED is nil, notify rejected."
"Clear Copilot overlay.
If IS-ACCEPTED is nil, notify rejected."
(interactive)
(when (copilot--overlay-visible)
(unless is-accepted
Expand All @@ -763,8 +771,9 @@ already saving an excursion. This is also a private function."
(setq copilot--real-posn nil)))

(defun copilot-accept-completion (&optional transform-fn)
"Accept completion. Return t if there is a completion.
Use TRANSFORM-FN to transform completion if provided."
"Accept completion.
Return t if there is a completion. Use TRANSFORM-FN to transform completion if
provided."
(interactive)
(when (copilot--overlay-visible)
(let* ((completion (overlay-get copilot--overlay 'completion))
Expand Down Expand Up @@ -836,7 +845,7 @@ Use TRANSFORM-FN to transform completion if provided."
(copilot--display-overlay-completion balanced-text uuid start end)))))))))

(defun copilot--on-doc-focus (window)
"Notify that the document has been focussed or opened."
"Notify that the document WINDOW has been focussed or opened."
;; When switching windows, this function is called twice, once for the
;; window losing focus and once for the window gaining focus. We only want to
;; send a notification for the window gaining focus and only if the buffer has
Expand All @@ -853,8 +862,10 @@ Use TRANSFORM-FN to transform completion if provided."
:text (copilot--get-source)))))))

(defun copilot--on-doc-change (&optional beg end chars-replaced)
"Notify that the document has changed."
(let* ((is-before-change (eq chars-replaced nil))
"Notify that the document has changed.

Arguments BEG, END, and CHARS-REPLACED are metadata for region changed."
(let* ((is-before-change (null chars-replaced))
(is-after-change (not is-before-change))
;; for a deletion, the post-change beginning and end are at the same place.
(is-insertion (and is-after-change (not (equal beg end))))
Expand Down Expand Up @@ -966,8 +977,8 @@ Copilot will show completions only if all predicates return t."

(defun copilot--self-insert (command)
"Handle the case where the char just inserted is the start of the completion.
If so, update the overlays and continue. COMMAND is the
command that triggered `post-command-hook'."
If so, update the overlays and continue. COMMAND is the command that triggered
in `post-command-hook'."
(when (and (eq command 'self-insert-command)
(copilot--overlay-visible)
(copilot--satisfy-display-predicates))
Expand Down Expand Up @@ -1016,8 +1027,7 @@ command that triggered `post-command-hook'."
(compilation-start
(mapconcat
#'shell-quote-argument
(-filter (lambda (cmd) (not (null cmd)))
command)
(-filter (lambda (cmd) cmd) command)
" ")
t
(lambda (&rest _)
Expand Down