Skip to content

Commit

Permalink
Merge pull request #688 from kongds/master
Browse files Browse the repository at this point in the history
support copilot
  • Loading branch information
manateelazycat committed Jul 27, 2023
2 parents 7819285 + 9bab424 commit 4088c58
Show file tree
Hide file tree
Showing 8 changed files with 409 additions and 2 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ lsp-bridge first looks for the content of the first *.pub file in the `~/.ssh` d
- `acm-enable-icon`: Whether the completion menu displays icons (Many macOS users have reported that emacs-plus28 cannot display icons properly, showing colored squares instead. There are two ways to solve this: install Emacs Mac Port or add the `--with-rsvg` option to the brew command when compiling Emacs yourself)
- `acm-enable-tabnine`: Enable tabnine support, enable by default, when enable need execute `lsp-bridge-install-tabnine` command to install TabNine, and it can be used. TabNine will consume huge CPUs, causing your entire computer to be slow. If the computer performance is not good, it is not recommended to enable this option
- `acm-enable-codeium`: Enable Codeium support, when enable need execute `lsp-bridge-install-update-codeium` command to install Codeium, then execute `lsp-bridge-codeium-auth` command to get auth token and execute `lsp-bridge-codeium-input-auth-token` command to get API Key, and it can be used.
- `acm-enable-copilot`: Enable copilot support, when enable need install agent first `npm install -g copilot-node-server`, then execute `lsp-bridge-copilot-auth` command to login, and it can be used.
- `acm-enable-search-file-words`: Whether the complete menu display the word of the file, enable by default
- `acm-enable-quick-access`: Whether to display an index after the icon, quickly select candidate words using Alt + Number, default is off
- `acm-quick-access-use-number-select`: Whether to use number keys for quick selection of candidate words, default is off, turning on this option may sometimes interfere with number input or accidentally select candidate words
Expand Down Expand Up @@ -368,6 +369,7 @@ The following is the directory structure of the lsp-bridge project:
| core/hanlder/ | Implementation of LSP message sending and receiving, where `__init__.py` is the base class. |
| core/tabnine.py | The backend searches and completes with TabNine. |
| core/codeium.py | The backend searches and completes with Codeium. |
| core/copilot.py | The backend searches and completes with Copilot. |
| core/search_file_words.py | Asynchronous search backend for file words. |
| core/search_paths.py | Asynchronous search backend for file paths. |
| core/search_sdcv_words.py | English word search backend, interchangeable with other language’s StarDict dictionaries. |
Expand Down
2 changes: 2 additions & 0 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ lsp-bridge 优先从`~/.ssh`目录下找第一个 *.pub 文件的内容作为远
- `acm-enable-doc-markdown-render`: 对补全文档中的 Markdown 内容进行语法着色, 你可以选择`'async`, `t` 或者 `nil`. 当选择`'async` 时, lsp-bridge 会采用异步渲, 当选择 `t` 时, lsp-bridge 会采用同步渲染, 同步渲染会降低补全速度, 默认是 `async` 选项
- `acm-enable-tabnine`: 是否打开 tabnine 补全支持, 默认打开, 打开后需要运行命令 `lsp-bridge-install-tabnine` 来安装 tabnine 后就可以使用了。 TabNine 会消耗巨大的 CPU, 导致你整个电脑都卡顿, 如果电脑性能不好, 不建议开启此选项
- `acm-enable-codeium`: 是否打开 Codeium 补全支持, 打开后需要运行命令 `lsp-bridge-install-update-codeium` 来安装 Codeium, 再运行命令 `lsp-bridge-codeium-auth` 来获取 auth token 再运行命令 `lsp-bridge-codeium-input-auth-token` 获取 API Key 后就可以使用了。
- `acm-enable-copilot`: 是否打开 Copilot 补全支持, 打开后需要运行终端命令 `npm install -g copilot-node-server` 来安装 Copilot, 再运行命令 `lsp-bridge-copilot-auth` 来登录。
- `acm-enable-search-file-words`: 补全菜单是否显示打开文件的单词, 默认打开
- `acm-enable-quick-access`: 是否在图标后面显示索引, 通过 Alt + Number 来快速选择候选词, 默认关闭
- `acm-quick-access-use-number-select`: 是否用数字键快速选择候选词, 默认关闭, 打开这个选项会导致有时候干扰数字输入或误选候选词
Expand Down Expand Up @@ -369,6 +370,7 @@ lsp-bridge 每种语言的服务器配置存储在 [lsp-bridge/langserver](https
| core/hanlder/ | LSP 消息发送和接受的实现, 其中 `__init__.py` 是基类 |
| core/tabnine.py | TabNine 后端搜索和补全 |
| core/codeium.py | Codeium 后端搜索和补全 |
| core/copilot.py | Copilot 后端搜索和补全 |
| core/search_file_words.py | 文件单词异步搜索后端 |
| core/search_paths.py | 文件路径异步搜索后端 |
| core/search_sdcv_words.py | 英文单词搜索后端, 可更换为其他语言的 StarDict 词典 |
Expand Down
85 changes: 85 additions & 0 deletions acm/acm-backend-copilot.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
;;; acm-backend-copilot.el --- Description -*- lexical-binding: t; -*-
;;
;; Copyright (C) 2023 royokong
;;
;; Author: royokong <[email protected]>
;; Maintainer: royokong <[email protected]>
;; Created: 七月 22, 2023
;; Modified: 七月 22, 2023
;; Version: 0.0.1
;; Keywords: abbrev bib c calendar comm convenience data docs emulations extensions faces files frames games hardware help hypermedia i18n internal languages lisp local maint mail matching mouse multimedia news outlines processes terminals tex tools unix vc wp
;; Homepage: https://github.com/royokong/acm-backend-copilot
;; Package-Requires: ((emacs "24.3"))
;;
;; This file is not part of GNU Emacs.
;;
;;; Commentary:
;;
;; Description
;;
;;; Code:

(defcustom acm-enable-copilot nil
"Enable copilot support."
:type 'boolean
:group 'acm-backend-copilot)

(defcustom acm-backend-copilot-node-path "node"
"The path to store Codeium API Key."
:type 'string
:group 'acm-backend-copilot)

(defcustom acm-backend-copilot-accept nil
"Send accept request."
:type 'boolean
:group 'acm-backend-copilot)

(defcustom acm-backend-copilot-network-proxy nil
" from `copilot.el'
Network proxy to use for Copilot. Nil means no proxy.
Format: '(:host \"127.0.0.1\" :port 80 :username \"username\" :password \"password\")
Username and password are optional.
If you are using a MITM proxy which intercepts TLS connections, you may need to disable
TLS verification. This can be done by setting a pair ':rejectUnauthorized :json-false'
in the proxy plist. For example:
(:host \"127.0.0.1\" :port 80 :rejectUnauthorized :json-false)
"
:type '(plist :tag "Uncheck all to disable proxy" :key-type symbol)
:options '((:host string) (:port integer) (:username string) (:password string))
:group 'acm-backend-copilot)


(defvar-local acm-backend-copilot-items nil)

(defun acm-backend-copilot-check-node-version ()
(and (locate-file acm-backend-copilot-node-path exec-path)
;; following copilot.el to check node version only >= 16
(>= (->> (with-output-to-string
(call-process acm-backend-copilot-node-path nil standard-output nil "--version"))
(s-trim)
(s-chop-prefix "v")
(string-to-number)) 16)))

(defun acm-backend-copilot-candidates (keyword)
acm-backend-copilot-items)


(defun acm-backend-copilot-candidate-expand (candidate-info bound-start &optional preview)
;; We need replace whole area with copilot label.
(let ((end-position (line-end-position)))
(forward-line (- (plist-get candidate-info :line) (count-lines (point-min) (line-beginning-position))))
(if preview
(acm-preview-create-overlay (point) end-position (plist-get candidate-info :label))
(delete-region (point) end-position)
(insert (plist-get candidate-info :label))
(when acm-backend-copilot-accept
(lsp-bridge-call-async
"copilot_completion_accept" (plist-get candidate-info :id))))))

(defun acm-backend-copilot-candidate-doc (candidate)
(plist-get candidate :documentation))

(provide 'acm-backend-copilot)
;;; acm-backend-copilot.el ends here
14 changes: 12 additions & 2 deletions acm/acm.el
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@
(require 'acm-backend-tailwind)
(require 'acm-backend-citre)
(require 'acm-backend-codeium)
(require 'acm-backend-copilot)
(require 'acm-quick-access)

;;; Code:
Expand Down Expand Up @@ -180,6 +181,7 @@
(defcustom acm-completion-backend-merge-order '("mode-first-part-candidates"
"template-first-part-candidates"
"tabnine-candidates"
"copilot-candidates"
"codeium-candidates"
"template-second-part-candidates"
"mode-second-part-candidates")
Expand Down Expand Up @@ -370,6 +372,7 @@ Only calculate template candidate when type last character."
yas-candidates
tabnine-candidates
codeium-candidates
copilot-candidates
tempel-candidates
mode-candidates
mode-first-part-candidates
Expand All @@ -385,6 +388,9 @@ Only calculate template candidate when type last character."
(when acm-enable-codeium
(setq codeium-candidates (acm-backend-codeium-candidates keyword)))

(when acm-enable-copilot
(setq copilot-candidates (acm-backend-copilot-candidates keyword)))

(if acm-enable-search-sdcv-words
;; Completion SDCV if option `acm-enable-search-sdcv-words' is enable.
(setq candidates (acm-backend-search-sdcv-words-candidates keyword))
Expand Down Expand Up @@ -462,6 +468,7 @@ Only calculate template candidate when type last character."
("template-first-part-candidates" template-first-part-candidates)
("tabnine-candidates" tabnine-candidates)
("codeium-candidates" codeium-candidates)
("copilot-candidates" copilot-candidates)
("template-second-part-candidates" template-second-part-candidates)
("mode-second-part-candidates" mode-second-part-candidates)
))
Expand Down Expand Up @@ -694,7 +701,10 @@ The key of candidate will change between two LSP results."
(when acm-preview-overlay (delete-overlay acm-preview-overlay))
(if (and (fboundp candidate-expand)
;; check if candidate-expand support preview.
(string-match " PREVIEW" (documentation candidate-expand t)))
(let ((doc (documentation candidate-expand t)))
(if doc
(string-match " PREVIEW" doc)
(member 'preview (help-function-arglist candidate-expand)))))
(save-excursion
(setq acm-preview-overlay (funcall candidate-expand candidate-info beg t)))
(setq acm-preview-overlay (acm-preview-create-overlay beg (point) cand)))
Expand Down Expand Up @@ -854,7 +864,7 @@ The key of candidate will change between two LSP results."
(visual-line-mode 1))

;; Only render markdown styling when idle 200ms, because markdown render is expensive.
(when (member backend '("lsp" "codeium"))
(when (member backend '("lsp" "codeium" "copilot"))
(acm-cancel-timer acm-markdown-render-timer)
(cl-case acm-enable-doc-markdown-render
(async (setq acm-markdown-render-timer
Expand Down
Loading

0 comments on commit 4088c58

Please sign in to comment.