-
Notifications
You must be signed in to change notification settings - Fork 1
Inline completion with overlay #2
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
base: main
Are you sure you want to change the base?
Changes from 2 commits
d711638
88629e7
161bb2b
ecf755a
43585cc
5ecc7b6
4fd0937
609e5b7
d8a51f8
d03c551
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| ;;; codegeex-api.el --- CodeGeeX For Emacs -*- lexical-binding: t; -*- | ||
|
|
||
| ;; Copyright (C) 2023 Samuel D | ||
|
|
||
| ;; Author: Samuel D <samueld@mailo.com> | ||
| ;; Keywords: codegeex, completion | ||
|
|
||
| ;; This program is free software; you can redistribute it and/or modify | ||
| ;; it under the terms of the GNU General Public License as published by | ||
| ;; the Free Software Foundation, either version 3 of the License, or | ||
| ;; (at your option) any later version. | ||
|
|
||
| ;; This program is distributed in the hope that it will be useful, | ||
| ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| ;; GNU General Public License for more details. | ||
|
|
||
| ;; You should have received a copy of the GNU General Public License | ||
| ;; along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
|
||
| ;;; Commentary: | ||
|
|
||
| ;; This is handling the api of codegeex | ||
|
|
||
| ;;; Code: | ||
|
|
||
| ;; COMPLETION | ||
|
|
||
| (defun codegeex-api--get-completion (prefix suffix lang callback) | ||
| "Invoke CodeGeeX completion API. | ||
|
|
||
| This function will complete code between PREFIX and SUFFIX, which are usually | ||
| the content before cursor and after cursor, and put the result to the current | ||
| buffer. LANG is the programming lanuauge of the code | ||
| CALLBACK is launched with the content of the buffer." | ||
| (let ((url (concat codegeex-endpoint "multilingual_code_generate_adapt")) | ||
| (data (codegeex-api--generate-json-data prefix suffix lang))) | ||
| (codegeex-api--url-retrieve url data callback))) | ||
|
|
||
| (defun codegeex-api--url-retrieve (url json-data callback) | ||
| "Default url retrieve as POST with json data" | ||
| (let ((url-request-method "POST") | ||
| (url-request-extra-headers | ||
| '(("Content-Type" . "application/json"))) | ||
| (url-request-data json-data)) | ||
| (url-retrieve | ||
| url | ||
| (lambda (status init-buffer callback) | ||
| (let ((result (codegeex-api--get-json-result))) | ||
| (setq codegeex-response-cache result) | ||
| (with-current-buffer init-buffer | ||
| (funcall callback result)))) | ||
| `(,(current-buffer) ,callback) t))) | ||
|
|
||
| (defun codegeex-api--get-json-result () | ||
| "Get the code string from the json response" | ||
| (goto-char (point-min)) | ||
| (re-search-forward "^$") | ||
| (let ((json-string (buffer-substring (point) (point-max)))) | ||
| (setq codegeex-json-string-cache json-string) | ||
| (json-parse-string json-string | ||
| :object-type 'plist | ||
| :array-type 'list))) | ||
|
|
||
| (defun codegeex-api--generate-json-data (prefix suffix lang) | ||
| "Create Json-encoded data to send to codegeex API" | ||
| (let ((n-factor | ||
| (cond | ||
| ((<= (length prefix) 300) 3) | ||
| ((> (length prefix) 600) 2) | ||
| ((> (length prefix) 900) 1)))) | ||
| (when (> (length prefix) 1200 ) | ||
| (setq prefix (substring prefix 0 1200))) | ||
|
||
| (json-encode | ||
| `(:prompt ,prefix | ||
| :suffix ,suffix | ||
| :n ,n-factor | ||
| :apikey ,codegeex-apikey | ||
| :apisecret ,codegeex-apisecret | ||
| :temperature ,codegeex-temperature | ||
| :top_p ,codegeex-top_p | ||
| :top_k ,codegeex-top_k | ||
| :isFimEnabled ,(not (equal suffix "")) | ||
| :lang ,lang | ||
| :ext ,codegeex-extinfo)))) | ||
|
|
||
|
Comment on lines
+75
to
+87
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I use plist for convenience (and for its similarity with json object). The properties are highlighted and it is easier to manipulate |
||
| (provide 'codegeex-api) | ||
| ;;; codegeex-api.el ends here | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| ;;; codegeex-completion.el --- CodeGeeX For Emacs -*- lexical-binding: t; -*- | ||
|
|
||
| ;; Copyright (C) 2023 Samuel D | ||
|
|
||
| ;; Author: Samuel D <samueld@mailo.com> | ||
| ;; Keywords: codegeex, completion | ||
|
|
||
| ;; This program is free software; you can redistribute it and/or modify | ||
| ;; it under the terms of the GNU General Public License as published by | ||
| ;; the Free Software Foundation, either version 3 of the License, or | ||
| ;; (at your option) any later version. | ||
|
|
||
| ;; This program is distributed in the hope that it will be useful, | ||
| ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| ;; GNU General Public License for more details. | ||
|
|
||
| ;; You should have received a copy of the GNU General Public License | ||
| ;; along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
|
||
| ;;; Commentary: | ||
|
|
||
| ;; Heavily inspired by copilot.el | ||
|
|
||
| ;;; Code: | ||
|
|
||
| (require 'codegeex-api) | ||
| (require 'codegeex-overlay) | ||
|
|
||
| (defun codegeex-completion--show-completion (completion) | ||
| (save-excursion | ||
| (save-restriction | ||
| (widen) | ||
| (let* ((p (point)) | ||
| (start p) | ||
| (end (+ p (length completion)))) | ||
| (codegeex--display-overlay-completion | ||
| completion start end))))) | ||
|
|
||
| (defun codegeex-completion--get-completion (callback) | ||
| "Retrieve context (prefix and suffix) and language and invoke `codegeex-api--get-completion' | ||
| CALLBACK is launched with json result of the call" | ||
| (let ((prefix (buffer-substring (point-min) (point))) | ||
| (suffix (buffer-substring (point) (point-max))) | ||
| (language (codegeex-language))) | ||
| (codegeex-api--get-completion | ||
| prefix suffix language callback))) | ||
|
|
||
| (provide 'codegeex-completion) | ||
| ;;; codegeex-completion.el ends here |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| ;;; codegeex-overlay.el --- Codegeex for Emacs -*- lexical-binding: t; -*- | ||
|
|
||
| ;; Copyright (C) 2023 Samuel D | ||
|
|
||
| ;; Author: Samuel D <samueld@mailo.com> | ||
| ;; Keywords: convenience | ||
|
|
||
| ;; This program is free software; you can redistribute it and/or modify | ||
| ;; it under the terms of the GNU General Public License as published by | ||
| ;; the Free Software Foundation, either version 3 of the License, or | ||
| ;; (at your option) any later version. | ||
|
|
||
| ;; This program is distributed in the hope that it will be useful, | ||
| ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| ;; GNU General Public License for more details. | ||
|
|
||
| ;; You should have received a copy of the GNU General Public License | ||
| ;; along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
|
||
| ;;; Commentary: | ||
|
|
||
| ;; Taken from copilot.el | ||
| ;; Create and handle the overlay shown during | ||
| ;; buffer completion process | ||
|
|
||
| ;;; Code: | ||
|
|
||
| (defface codegeex-overlay-face | ||
| '((t :inherit shadow)) | ||
| "Face for codegeex overlay.") | ||
|
|
||
| (defvar-local codegeex--overlay nil | ||
| "Overlay for Codegeex completion.") | ||
|
|
||
| (defvar-local codegeex--real-posn nil | ||
| "Posn information without overlay. | ||
| To work around posn problems with after-string property.") | ||
|
|
||
| (defconst codegeex-overlay-completion-map (make-sparse-keymap) | ||
| "Keymap for Codegeex completion overlay.") | ||
|
|
||
| (defun codegeex--get-overlay () | ||
| "Create or get overlay for Codegeex." | ||
| (unless (overlayp codegeex--overlay) | ||
| (setq codegeex--overlay (make-overlay 1 1 nil nil t)) | ||
| (overlay-put codegeex--overlay | ||
| 'keymap codegeex-overlay-completion-map) | ||
| (overlay-put codegeex--overlay 'priority 100)) | ||
| codegeex--overlay) | ||
|
|
||
| (defun codegeex--overlay-end (ov) | ||
| "Return the end position of overlay OV." | ||
| (- (line-end-position) (overlay-get ov 'tail-length))) | ||
|
|
||
| (defun codegeex--set-overlay-text (ov completion) | ||
| "Set overlay OV with COMPLETION." | ||
| (move-overlay ov (point) (line-end-position)) | ||
| (let* ((tail (buffer-substring (codegeex--overlay-end ov) (line-end-position))) | ||
| (p-completion (concat (propertize completion 'face 'codegeex-overlay-face) | ||
| tail))) | ||
| (if (eolp) | ||
| (progn | ||
| (overlay-put ov 'after-string "") ; make sure posn is correct | ||
| (setq codegeex--real-posn (cons (point) (posn-at-point))) | ||
| (put-text-property 0 1 'cursor t p-completion) | ||
| (overlay-put ov 'display "") | ||
| (overlay-put ov 'after-string p-completion)) | ||
| (overlay-put ov 'display (substring p-completion 0 1)) | ||
| (overlay-put ov 'after-string (substring p-completion 1))) | ||
| (overlay-put ov 'completion completion) | ||
| (overlay-put ov 'start (point)))) | ||
|
|
||
| (defun codegeex--display-overlay-completion (completion start end) | ||
| "Show COMPLETION between START and END." | ||
| (setq end start) | ||
| (codegeex-clear-overlay) | ||
| (when (and (s-present-p completion) | ||
| (or (= start (point)) ; up-to-date completion | ||
| (and (< start (point)) ; special case for removing indentation | ||
| (s-blank-p (s-trim (buffer-substring-no-properties start (point))))))) | ||
| (goto-char start) ; indentation | ||
| (let ((ov (codegeex--get-overlay))) | ||
| (overlay-put ov 'tail-length (- (line-end-position) end)) | ||
| (codegeex--set-overlay-text ov completion)))) | ||
|
|
||
| (defun codegeex-clear-overlay () | ||
| "Clear Codegeex overlay" | ||
| (interactive) | ||
| (when (codegeex--overlay-visible) | ||
| (delete-overlay codegeex--overlay) | ||
| (setq codegeex--real-posn nil))) | ||
|
|
||
| (defsubst codegeex--overlay-visible () | ||
| "Return whether the `codegeex--overlay' is avaiable." | ||
| (and (overlayp codegeex--overlay) | ||
| (overlay-buffer codegeex--overlay))) | ||
|
|
||
|
|
||
| (provide 'codegeex-overlay) | ||
| ;;; codegeex-overlay.el ends here |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will be the main api fetcher. With custom callback that take the json-result as argument. Can be used for the other api call that you have implemented by the way.