-
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
Open
BrachystochroneSD
wants to merge
10
commits into
hzhangxyz:main
Choose a base branch
from
BrachystochroneSD:inline-completion
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
d711638
Proof of concept for a overlay completion system
88629e7
Working version
161bb2b
Typo
ecf755a
Fixed: prefix/prompt substring was trunacting the end of the string
43585cc
Added: Cache the json request
5ecc7b6
Change defun order
4fd0937
Merge codegeex-completion with main codegeex.el
609e5b7
check if cursor doesn't change between the http request and response
d8a51f8
REmove require statement
d03c551
Add missing requirement
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| ;;; 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)) | ||
| (setq codegeex-request-cache 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) 5000 ) | ||
| (setq prefix (substring prefix -5000))) | ||
| (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)))) | ||
|
|
||
| (provide 'codegeex-api) | ||
| ;;; codegeex-api.el ends here | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I use plist for convenience (and for its similarity with json object). The properties are highlighted and it is easier to manipulate