-
Notifications
You must be signed in to change notification settings - Fork 5
/
yn-github-copilot.js
134 lines (112 loc) · 3.39 KB
/
yn-github-copilot.js
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
window.registerPlugin({
name: 'yank-note-github-copilot',
register: ctx => {
const extensionId = 'yank-note-github-copilot'
const enabled = ctx.lib.vue.ref(ctx.storage.get(`${extensionId}.enabled`) || true)
const loading = ctx.lib.vue.ref(false)
class CompletionProvider {
monaco = undefined
logger = ctx.utils.getLogger(extensionId)
constructor (monaco) {
this.monaco = monaco
}
freeInlineCompletions () {
loading.value = false
}
handleItemDidShow () {
loading.value = false
}
async provideInlineCompletions (
_model,
position,
context,
token,
) {
if (!enabled.value) {
return { items: [] }
}
const content = ctx.editor.getValue()
if (content.length > 1024 * 512 || content.length < 4) {
return []
}
await ctx.utils.sleep(1500)
return {
items: await this.provideSuggestions(content, position, context, token)
}
}
async provideSuggestions (content, position, context, token) {
if (token.isCancellationRequested) {
return []
}
token.onCancellationRequested(() => {
loading.value = false
})
try {
const headers = { 'Content-Type': 'application/json' }
const body = JSON.stringify({
language: 'markdown',
content,
triggerKind: context.triggerKind,
line: position.lineNumber - 1,
column: position.column - 1,
})
loading.value = true
const x = await ctx.api.proxyRequest(
'http://127.0.0.1:3223/calculateInlineCompletions',
{ headers, body: body, method: 'post' },
true
)
const res = await x.json()
if (token.isCancellationRequested) {
return []
}
if (!res || res.type !== 'success' || !res.items) {
return []
}
loading.value = false
return res.items.map(x => {
const range = new this.monaco.Range(
x.range[0].line + 1,
x.range[0].character + 1,
x.range[1].line + 1,
x.range[1].character + 1,
)
return {
text: x.displayText,
insertText: { snippet: x.insertText },
range,
}
})
} catch (error) {
loading.value = false
ctx.ui.useToast().show('warning', 'Github Copilot:' + (error.message || `${error}`), 5000)
throw error
}
}
}
ctx.editor.whenEditorReady().then(({ monaco }) => {
monaco.languages.registerInlineCompletionsProvider(
'markdown',
new CompletionProvider(monaco)
)
})
ctx.lib.vue.watchEffect(() => {
ctx.storage.set(`${extensionId}.enabled`, enabled.value)
})
ctx.lib.vue.watch(() => [enabled.value, loading.value], () => {
ctx.statusBar.refreshMenu()
}, { immediate: true })
ctx.statusBar.tapMenus(menus => {
menus[extensionId] = {
id: extensionId,
position: 'right',
order: -2048,
title: loading.value ? 'AI-Loading' : enabled.value ? 'AI-On' : 'AI-Off',
onClick: () => {
enabled.value = !enabled.value
},
list: []
}
})
}
});