forked from Exafunction/codeium-sublime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisplay_completions.py
296 lines (245 loc) · 9.06 KB
/
display_completions.py
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# Copyright Exafunction, Inc.
import html
import logging
from threading import Lock
from threading import Thread
from Codeium.login import CodeiumSettings
from Codeium.protorequests import CancelRequestRequest
from Codeium.protorequests import GetCompletionsRequest
import sublime
import sublime_plugin
CODEIUM_STATE_SUCCESS = 3
COMPLETION_PART_TYPE_UNSPECIFIED = 0
COMPLETION_PART_TYPE_INLINE = 1
COMPLETION_PART_TYPE_BLOCK = 2
COMPLETION_PART_TYPE_INLINE_MASK = 3
class CodeiumCompletionPart:
def __init__(self, text, point):
self.text = text
self.point = point
class CodeiumCompletion:
def __init__(self):
self.inline_parts = []
self.block = None
def add_inline(self, text, point):
self.inline_parts.append(CodeiumCompletionPart(text, point))
def add_block(self, text, point):
self.block = CodeiumCompletionPart(text, point)
def is_active_view(obj):
return bool(obj and obj == sublime.active_window().active_view())
lock = Lock()
completions = []
index = 0
display = False
for_position = -1
def make_async_request(req, view):
global completions, index, for_position
print("sent completion request")
resp = req.send()
if resp and resp.state.state == CODEIUM_STATE_SUCCESS:
print("response is:", resp.state.message)
c = []
for item in resp.completion_items:
completion = CodeiumCompletion()
for part in item.completion_parts:
offset = view.text_point_utf8(0, part.offset)
if part.type == COMPLETION_PART_TYPE_INLINE:
completion.add_inline(part.text, offset)
elif part.type == COMPLETION_PART_TYPE_BLOCK:
completion.add_block(part.text, offset)
c.append(completion)
if len(c) > 0:
lock.acquire()
completions = c
index = -1
for_position = view.sel()[0].begin()
lock.release()
view.settings().set("Codeium.completion_active", True)
view.run_command("codeium_display_completion")
class RequestCompletionListener(sublime_plugin.EventListener):
def on_modified_async(self, view):
if (
is_active_view(view)
and CodeiumSettings.enable
and CodeiumSettings.api_key != ""
):
if hasattr(self, "req") and hasattr(getattr(self, "req"), "id"):
# cancel previous request
print("sent cancel")
CancelRequestRequest(getattr(self, "req").id).send()
self.req = GetCompletionsRequest(view)
## start the thread
t = Thread(target=make_async_request, args=[self.req, view])
t.start()
def on_selection_modified_async(self, view):
global for_position
if (
is_active_view(view)
and for_position != -1
and view.sel()[0].begin() != for_position
):
PhantomCompletion.hide(view)
for_position = -1
class CodeiumDisplayCompletionCommand(sublime_plugin.TextCommand):
def run(self, edit):
global completions, index, display
if for_position != -1:
lock.acquire()
index = (index + 1) % len(completions)
lock.release()
PhantomCompletion(self.view, completions[index]).show(edit)
class CodeiumDisplayPreviousCompletionCommand(sublime_plugin.TextCommand):
def run(self, edit):
global completions, index, display
if for_position != -1:
lock.acquire()
index = (index + len(completions) - 1) % len(completions)
lock.release()
PhantomCompletion(self.view, completions[index]).show(edit)
class CodeiumRejectCompletionCommand(sublime_plugin.TextCommand):
def run(self, edit):
global completions, index, display
PhantomCompletion.hide(self.view)
lock.acquire()
for_position = -1
lock.release()
self.view.settings().set("Codeium.completion_active", False)
class CodeiumAcceptCompletionCommand(sublime_plugin.TextCommand):
def run(self, edit):
global for_position
if for_position != -1:
PhantomCompletion.hide(self.view)
PhantomCompletion(self.view, completions[index]).make_real(edit)
lock.acquire()
for_position = -1
lock.release()
self.view.settings().set("Codeium.completion_active", False)
_view_to_phantom_set = {}
class PhantomCompletion:
PHANTOM_TEMPLATE = """
<body id="codeium-completion">
<style>
body {{
color: #808080;
font-style: italic;
}}
.codeium-completion-line {{
line-height: 0;
margin-top: {line_padding_top}px;
margin-bottom: {line_padding_bottom}px;
margin-left : 0;
margin-right : 0;
}}
.codeium-completion-line.first {{
margin-top: 0;
}}
</style>
{body}
</body>
"""
PHANTOM_LINE_TEMPLATE = '<div class="codeium-completion-line">{content}</div>'
def __init__(
self,
view: sublime.View,
completion,
) -> None:
self.view = view
self._settings = view.settings()
self._phantom_set = self._get_phantom_set(view)
self.completion = completion
@classmethod
def _get_phantom_set(cls, view: sublime.View) -> sublime.PhantomSet:
view_id = view.id()
# create phantom set if there is no existing one
if not _view_to_phantom_set.get(view_id):
_view_to_phantom_set[view_id] = sublime.PhantomSet(view)
return _view_to_phantom_set[view_id]
def normalize_phantom_line(self, line: str) -> str:
return (
html.escape(line)
.replace(" ", " ")
.replace("\t", " " * self._settings.get("tab_size"))
)
def _build_phantom(
self,
lines,
begin: int,
end=None,
*,
inline: bool = True
# format separator
) -> sublime.Phantom:
body = (
self.normalize_phantom_line(lines)
if isinstance(lines, str)
else "".join(
self.PHANTOM_LINE_TEMPLATE.format(
class_name=("rest" if index else "first"),
content=self.normalize_phantom_line(line),
)
for index, line in enumerate(lines)
)
)
return sublime.Phantom(
sublime.Region(begin, begin if end is None else end),
self.PHANTOM_TEMPLATE.format(
body=body,
line_padding_top=int(self._settings.get("line_padding_top"))
* 2, # TODO: play with this more
line_padding_bottom=int(self._settings.get("line_padding_bottom")) * 2,
),
sublime.LAYOUT_INLINE if inline else sublime.LAYOUT_BLOCK,
)
def _add_text(self, edit, text, point):
# region = sublime.Region(begin, begin if end is None else end)
self.view.insert(edit, point, text)
# return region.end()
def show(self, edit) -> None:
# first_line, *rest_lines = self.completion.text.splitlines()
assert self._phantom_set
self._phantom_set.update([])
cursor = self.view.sel()[0].begin()
completion = self.completion
phantom_set = []
for part in completion.inline_parts:
phantom_set.append(
self._build_phantom(part.text, part.point, self.view.size())
)
if completion.block:
phantom_set.append(
self._build_phantom(
completion.block.text.splitlines(),
completion.block.point,
inline=False,
)
)
else:
phantom_set.append(
self._build_phantom("", self.view.line(cursor).end(), inline=False)
)
self._phantom_set.update(phantom_set)
def make_real(self, edit):
line_ending = "\r\n" if "Windows" in self.view.line_endings() else "\n"
completion = self.completion
cursor = self.view.sel()[0].begin()
added = []
for part in completion.inline_parts:
shift = 0
for pos, amt in added:
if pos < part.point:
shift += amt
self._add_text(edit, part.text, part.point + shift)
added.append((part.point, len(part.text)))
# move cursor to the end of the line
self.view.sel().clear()
line = self.view.line(cursor)
self.view.sel().add(line.end())
if completion.block:
text = line_ending + completion.block.text
self._add_text(edit, text, self.view.line(completion.block.point).end())
@classmethod
def hide(cls, view: sublime.View) -> None:
cls._get_phantom_set(view).update([])
@classmethod
def close(cls, view: sublime.View) -> None:
del _view_to_phantom_set[view.id()]