Skip to content

Commit

Permalink
cleanup + add logo
Browse files Browse the repository at this point in the history
  • Loading branch information
hieunc229 committed Jun 30, 2021
1 parent 47096e9 commit 4b69d50
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 22 deletions.
Binary file added icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 9 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
{
"enableProposedApi": true,
"name": "inline-completion-sample",
"displayName": "Inline Completion Sample",
"description": "Sample showing how to implement an inline completion provider",
"name": "copilot-clone",
"displayName": "Copilot Clone",
"description": "Find snippets codes from Stackoverflow",
"version": "0.0.1",
"publisher": "vscode-samples",
"publisher": "hieunc229",
"icon": "icon.png",
"repository": "https://github.com/Microsoft/vscode-extension-samples",
"engines": {
"vscode": "^1.34.0"
},
"license": "MIT",
"categories": [
"Other"
],
Expand All @@ -19,14 +21,14 @@
"contributes": {
"commands": [
{
"command": "extension.inline-completion-settings",
"title": "Inline Completion Settings"
"command": "extension.copilot-clone-settings",
"title": "Copilot-clone Settings"
}
],
"menus": {
"editor/inlineCompletions/actions": [
{
"command": "extension.inline-completion-settings"
"command": "extension.copilot-clone-settings"
}
]
}
Expand Down
12 changes: 10 additions & 2 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const KEYWORD = `//find`

export function activate(context: vscode.ExtensionContext) {
const disposable = vscode.commands.registerCommand(
'extension.inline-completion-settings',
'extension.copilot-clone-settings',
() => {
vscode.window.showInformationMessage('Show settings');
}
Expand Down Expand Up @@ -34,7 +34,15 @@ export function activate(context: vscode.ExtensionContext) {

if (textBeforeCursor.indexOf(KEYWORD) == 0 && textBeforeCursor[textBeforeCursor.length - 1] === ".") {

const rs = await search(textBeforeCursor)
let rs;

try {
rs = await search(textBeforeCursor)
} catch (err) {
vscode.window.showInformationMessage(err.toString());
return { items:[] }
}


if (rs == null) {
return { items: [] }
Expand Down
44 changes: 31 additions & 13 deletions src/search.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import fetch from "node-fetch";
import { JSDOM } from "jsdom";


const GURL = `https://www.google.com/search?q=site%3Astackoverflow.com+`;

type SnippetResult = {
results: { votes: number, code:string, textContent: string }[],
type SnippetResult = { votes: number, code: string, textContent: string }
type SnippetPageResult = {
results: SnippetResult[],
url: string
}

Expand All @@ -22,6 +24,9 @@ function getGoogleResults(keyword: string): Promise<string[] | null> {
}

async function getStackoverflowContent(url: string): Promise<{ content: string, url: string }> {



return new Promise((resolve, reject) => {
return fetch(url)
.then(rs => rs.text())
Expand All @@ -31,7 +36,7 @@ async function getStackoverflowContent(url: string): Promise<{ content: string,
}

// Extract and sort stackoverflow answers
function getSnippetResults(options: { content: string, url: string }): SnippetResult {
function getSnippetResults(options: { content: string, url: string }): SnippetPageResult {
var doc = new JSDOM(options.content)

let els = Array.from(doc.window.document.querySelectorAll(".answer"))
Expand All @@ -44,22 +49,35 @@ function getSnippetResults(options: { content: string, url: string }): SnippetRe
}))

// Sort results by code length
results.sort((a,b) => b.code.length - a.code.length)
results.sort((a, b) => b.code.length - a.code.length)

return { url: options.url, results }
}

// Send search query to google, get answers from stackoverflow
// then extract and return code results
export async function search(keyword: string) {
const urls = await getGoogleResults(keyword)
export async function search(keyword: string): Promise<null | { results: SnippetResult[] }> {

return new Promise(async (resolve, reject) => {

if (urls === null) {
return null
}
let urls = await getGoogleResults(keyword)

// Iterate through all urls if you want
// I also use the first results to avoid having too many requests
const snippets = await getStackoverflowContent(urls[0]);
return getSnippetResults(snippets)
if (urls === null) {
return Promise.resolve(null)
}

let results: SnippetResult[] = [];

try {
for (const i in urls.splice(0, 2)) {
const snippets = await getStackoverflowContent(urls[i]);
results = results.concat(getSnippetResults(snippets).results);
}

resolve({ results })
} catch (err) {
reject(err)
}

})
}

0 comments on commit 4b69d50

Please sign in to comment.