-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlsp-uniteai.el
190 lines (161 loc) · 5.92 KB
/
lsp-uniteai.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
;;; lsp-uniteai.el --- LSP Clients for UniteAI -*- lexical-binding: t; -*-
;; Copyright (C) 2023 Josh Freckleton
;; Copyright (C) 2023-2025 Shen, JenChieh
;; Author: Josh Freckleton <[email protected]>
;; Shen, Jen-Chieh <[email protected]>
;; Maintainer: JenChieh <[email protected]>
;; URL: https://github.com/emacs-openai/lsp-uniteai
;; Version: 0.1.0
;; Package-Requires: ((emacs "27.1") (lsp-mode "6.1"))
;; Keywords: convenience ai
;; This file is not part of GNU Emacs.
;; 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:
;;
;; LSP Clients for UniteAI.
;;
;;; Code:
(require 'lsp-mode)
(defgroup lsp-uniteai nil
"Settings for the UniteAI Language Server."
:group 'lsp-mode
:link '(url-link "https://github.com/emacs-openai/lsp-uniteai"))
(defcustom lsp-uniteai-active-modes
'( markdown-mode org-mode python-mode bibtex-mode clojure-mode coffee-mode
c-mode c++-mode csharp-mode css-mode diff-mode dockerfile-mode fsharp-mode
go-mode groovy-mode html-mode web-mode java-mode js-mode js2-mode json-mode
LaTeX-mode less-css-mode lua-mode makefile-mode objc-mode perl-mode
php-mode text-mode powershell-mode ess-mode ruby-mode rust-mode scss-mode
sass-mode sh-mode sql-mode swift-mode typescript-mode TeX-mode nxml-mode
yaml-mode sh-mode toml-mode)
"List of major mode that work with UniteAI."
:type 'list
:group 'lsp-uniteai)
(defcustom lsp-uniteai-connection-method 'stdio
"Method to connect to the UniteAI language server."
:type '(choice (const :tag "--stdio" stdio)
(const :tag "--tcp" tcp))
:group 'lsp-uniteai)
;;
;; (@* "Client" )
;;
(defun lsp-uniteai--install-server (_client callback error-callback update?)
"Install/update UniteAI language server using `pip'.
Will invoke CALLBACK or ERROR-CALLBACK based on result.
Will update if UPDATE? is t"
(lsp-async-start-process
callback
error-callback
"pip" (if update? "install --upgrade" "install") "uniteai[all]"))
;;;###autoload
(defun lsp-uniteai-update-server ()
"Update UniteAI Language Server.
On Windows, if the server is running, the updating will fail.
After stopping or killing the process, retry to update."
(interactive)
(lsp-uniteai--install-server nil #'ignore #'lsp--error t))
(lsp-register-client
(make-lsp-client
:new-connection
(cl-case lsp-uniteai-connection-method
(`stdio (lsp-stdio-connection "uniteai_lsp --stdio"
(lambda (&rest _)
(zerop (shell-command "uniteai_lsp")))))
(`tcp (lsp-tcp-connection
(lambda (port)
`("uniteai_lsp" "--tcp" "--lsp_port" ,(number-to-string port))))))
:priority -2
:major-modes lsp-uniteai-active-modes
:server-id 'uniteai-lsp
:add-on? t
:download-server-fn #'lsp-uniteai--install-server))
;;
;; (@* "Util" )
;;
(defun lsp-uniteai--range ()
"Return the current region in LSP scope."
(unless (region-active-p)
(user-error "No region selected"))
(list :start (lsp--point-to-position (region-beginning))
:end (lsp--point-to-position (region-end))))
;;
;; (@* "Commands" )
;;
;;
;;; Global stopping
(defun lsp-uniteai-stop ()
"Stop the UniteAI."
(interactive)
(let ((doc (lsp--text-document-identifier)))
(lsp-request "workspace/executeCommand"
`( :command "command.stop"
:arguments ,(vector doc)))))
;;
;;; Example Counter
(defun lsp-uniteai-example-counter ()
"TODO: .."
(interactive)
(let ((doc (lsp--text-document-identifier))
(pos (lsp--cur-position)))
(lsp-request "workspace/executeCommand"
`( :command "command.exampleCounter"
:arguments ,(vector doc pos)))))
;;
;;; Document
(defun lsp-uniteai-document ()
"TODO: .."
(interactive)
(let ((doc (lsp--text-document-identifier))
(range (lsp-uniteai--range)))
(lsp-request "workspace/executeCommand"
`( :command "command.document"
:arguments ,(vector doc range)))))
;;
;;; Local LLM
(defun lsp-uniteai-local-llm ()
"TODO: .."
(interactive)
(let* ((doc (lsp--text-document-identifier))
(range (lsp-uniteai--range)))
(lsp-request "workspace/executeCommand"
`( :command "command.localLlmStream"
:arguments ,(vector doc range)))))
;;
;;; Transcription
(defun lsp-uniteai-transcribe ()
"TODO: .."
(interactive)
(let ((doc (lsp--text-document-identifier))
(pos (lsp--cur-position)))
(lsp-request "workspace/executeCommand"
`( :command "command.transcribe"
:arguments ,(vector doc pos)))))
;;
;;; OpenAI
(defun lsp-uniteai-openai-gpt ()
"TODO: .."
(interactive)
(let ((doc (lsp--text-document-identifier))
(range (lsp-uniteai--range)))
(lsp-request "workspace/executeCommand"
`( :command "command.openaiAutocompleteStream"
:arguments ,(vector doc range "FROM_CONFIG_COMPLETION" "FROM_CONFIG")))))
(defun lsp-uniteai-openai-chatgpt ()
"TODO: .."
(interactive)
(let ((doc (lsp--text-document-identifier))
(range (lsp-uniteai--range)))
(lsp-request "workspace/executeCommand"
`(:command "command.openaiAutocompleteStream"
:arguments ,(vector doc range "FROM_CONFIG_CHAT" "FROM_CONFIG")))))
(provide 'lsp-uniteai)
;;; lsp-uniteai.el ends here