-
Notifications
You must be signed in to change notification settings - Fork 27
/
extension.js
54 lines (42 loc) · 1.56 KB
/
extension.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
const vscode = require('vscode');
const superagent = require("superagent")
/**
* @param {vscode.ExtensionContext} context
*/
function activate(context) {
const editor = vscode.window.activeTextEditor;
async function findSnippets(data, selections) {
//validation for no text being selected
if (data.length == 0){
vscode.window.showWarningMessage("No text selected! Please select some text to get snippet")
}
const forumURL = "https://www.codegrepper.com/api/search.php?q=" + data + "&search_options=search_titles";
// callbacks
superagent
.get(forumURL)
.end((error, response) => {
let data = JSON.parse(response.text);
console.log(data)
if (data["answers"].length == 0) {
vscode.window.showWarningMessage("Server returned no code - Search with words that are simple !!")
}
editor.edit(editBuilder => {
editBuilder.replace(selections, String(data["answers"][0]["answer"]));
});
})
}
let disposable = vscode.commands.registerCommand('clara-copilot.searchcode', async function() {
if (editor) {
const document = editor.document;
const selection = editor.selection;
const word = document.getText(selection);
await findSnippets(word, selection);
}
});
context.subscriptions.push(disposable);
}
function deactivate() {}
module.exports = {
activate,
deactivate
}