|
| 1 | +import * as vscode from 'vscode' |
| 2 | +import * as https from 'http' |
| 3 | + |
| 4 | +async function fetch(prompt: string) { |
| 5 | + const data = JSON.stringify({ prompt: prompt }) |
| 6 | + |
| 7 | + return new Promise<any>((resolve, reject) => { |
| 8 | + let respone_data = '' |
| 9 | + const req = https.request('http://localhost:5000/autocomplete', { |
| 10 | + method: 'POST', |
| 11 | + headers: { |
| 12 | + 'Content-Type': 'application/json', |
| 13 | + 'Content-Length': data.length |
| 14 | + } |
| 15 | + }, (res) => { |
| 16 | + res.on('data', (d) => { |
| 17 | + respone_data += d |
| 18 | + }) |
| 19 | + |
| 20 | + res.on('end', () => { |
| 21 | + resolve(JSON.parse(respone_data)) |
| 22 | + }) |
| 23 | + }) |
| 24 | + req.on('error', (error) => { |
| 25 | + console.error(error) |
| 26 | + reject() |
| 27 | + }) |
| 28 | + req.write(data) |
| 29 | + req.end() |
| 30 | + }) |
| 31 | +} |
| 32 | + |
| 33 | +function getPrompt(document: vscode.TextDocument, position: vscode.Position) { |
| 34 | + const start = Math.max(0, position.line - 20) |
| 35 | + let text = '' |
| 36 | + for (let i = start; i < position.line; ++i) { |
| 37 | + text += document.lineAt(i).text + '\n' |
| 38 | + } |
| 39 | + const line = document.lineAt(position).text |
| 40 | + text += line.substr(0, position.character) |
| 41 | + |
| 42 | + return text |
| 43 | +} |
| 44 | + |
| 45 | +export function activate(context: vscode.ExtensionContext) { |
| 46 | + const provider = vscode.languages.registerCompletionItemProvider('python', { |
| 47 | + async provideCompletionItems(document: vscode.TextDocument, position: vscode.Position, token: vscode.CancellationToken, context: vscode.CompletionContext) { |
| 48 | + const prompt = getPrompt(document, position) |
| 49 | + let response |
| 50 | + |
| 51 | + try { |
| 52 | + response = await fetch(prompt) |
| 53 | + } catch(e) { |
| 54 | + return [] |
| 55 | + } |
| 56 | + |
| 57 | + // Failure |
| 58 | + if (!response.success) { |
| 59 | + return [] |
| 60 | + } |
| 61 | + |
| 62 | + let prediction: string = response.prediction |
| 63 | + |
| 64 | + // Remove new lines because it's a bit annoying? |
| 65 | + let nl = prediction.indexOf('\n') |
| 66 | + if (nl !== -1) { |
| 67 | + prediction = prediction.substr(0, nl) |
| 68 | + } |
| 69 | + |
| 70 | + if (prediction === '') { |
| 71 | + // If at end of a line just predict new line, to avoid annoying default vscode predictions |
| 72 | + if (nl !== -1) { |
| 73 | + const simpleCompletion = new vscode.CompletionItem('\n') |
| 74 | + simpleCompletion.kind = vscode.CompletionItemKind.Text |
| 75 | + simpleCompletion.command = { command: 'editor.action.triggerSuggest', title: 'Re-trigger completions...' } |
| 76 | + return [simpleCompletion] |
| 77 | + } |
| 78 | + return [] |
| 79 | + } |
| 80 | + |
| 81 | + // Add any word prefix from text (because thats how vscode works) |
| 82 | + let range = document.getWordRangeAtPosition(position) |
| 83 | + if (range != null) { |
| 84 | + const line = document.lineAt(position).text |
| 85 | + let prefix = line.substring(range.start.character, position.character) |
| 86 | + prediction = prefix + prediction |
| 87 | + } |
| 88 | + |
| 89 | + // Create a completion |
| 90 | + const simpleCompletion = new vscode.CompletionItem(prediction) |
| 91 | + simpleCompletion.kind = vscode.CompletionItemKind.Text |
| 92 | + // Dont trigger autocompletion if we hit a new line |
| 93 | + if (nl === -1) { |
| 94 | + simpleCompletion.command = { command: 'editor.action.triggerSuggest', title: 'Re-trigger completions...' } |
| 95 | + } |
| 96 | + |
| 97 | + return [simpleCompletion] |
| 98 | + } |
| 99 | + }) |
| 100 | + |
| 101 | + context.subscriptions.push(provider) |
| 102 | +} |
0 commit comments