Skip to content

Commit

Permalink
refactor: unified coding style
Browse files Browse the repository at this point in the history
  • Loading branch information
Kekschen committed Jul 8, 2022
1 parent 3f94ead commit 6c3910f
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 49 deletions.
4 changes: 2 additions & 2 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ type IConfig = {
export function getConfig() {
const config = vscode.workspace.getConfiguration("captainStack");

let sites = {
const sites = {
"stackoverflow.com": config.settings.sites.stackoverflow,
"gist.github.com": config.settings.sites.githubGist
}
};

return {
settings: {
Expand Down
62 changes: 31 additions & 31 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,39 @@ import { matchSearchPhrase } from './utils/matchSearchPhrase';

export function activate(_: vscode.ExtensionContext) {

const provider: vscode.CompletionItemProvider = {
// @ts-ignore
provideInlineCompletionItems: async (document, position, context, token) => {
const provider: vscode.CompletionItemProvider = {
// @ts-ignore
provideInlineCompletionItems: async (document, position, context, token) => {

const textBeforeCursor = document.getText(
new vscode.Range(position.with(undefined, 0), position)
);
const textBeforeCursor = document.getText(
new vscode.Range(position.with(undefined, 0), position)
);

const match = matchSearchPhrase(textBeforeCursor);
let items: any[] = [];
const match = matchSearchPhrase(textBeforeCursor);
let items: any[] = [];

if (match) {
let rs;
try {
rs = await search(match.searchPhrase);
if (rs) {
items = rs.results.map(item => {
const output = `\n${match.commentSyntax} Source: ${item.sourceURL} ${match.commentSyntaxEnd}\n${item.code}`;
return {
text: output,
insertText: output,
range: new vscode.Range(position.translate(0, output.length), position)
};
});
}
} catch (err: any) {
vscode.window.showErrorMessage(err.toString());
}
}
return { items };
},
};
if (match) {
let rs;
try {
rs = await search(match.searchPhrase);
if (rs) {
items = rs.results.map(item => {
const output = `\n${match.commentSyntax} Source: ${item.sourceURL} ${match.commentSyntaxEnd}\n${item.code}`;
return {
text: output,
insertText: output,
range: new vscode.Range(position.translate(0, output.length), position)
};
});
}
} catch (err: any) {
vscode.window.showErrorMessage(err.toString());
}
}
return {items};
},
};

// @ts-ignore
vscode.languages.registerInlineCompletionItemProvider({ pattern: "**" }, provider);
// @ts-ignore
vscode.languages.registerInlineCompletionItemProvider({pattern: "**"}, provider);
}
8 changes: 4 additions & 4 deletions src/utils/extractors/ExtractorAbstract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ export default abstract class ExtractorAbstract {
}

/**
* Return a list of Source URLs from Google Search's result
*/
* Return a list of Source URLs from Google Search's result
*/
extractURLFromKeyword = (keyword: string): Promise<string[]> => {

return new Promise((resolve, reject) => {
Expand All @@ -28,10 +28,10 @@ export default abstract class ExtractorAbstract {
})
.catch(reject);
});
}
};

// Extract snippets from URL content
abstract extractSnippets: (options: FetchPageResult) => SnippetResult[]
abstract extractSnippets: (options: FetchPageResult) => SnippetResult[];
}

export type SnippetResult = {
Expand Down
10 changes: 5 additions & 5 deletions src/utils/extractors/ExtractorGithubGist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { isCodeValid } from "./utils";

export default class ExtractorGithubGist extends ExtractorAbstract {

name = "Github Gist"
URL = "gist.github.com"
name = "Github Gist";
URL = "gist.github.com";

extractSnippets = (options: FetchPageResult): SnippetResult[] => {
const target = parseHTML(options.textContent);
Expand All @@ -27,14 +27,14 @@ export default class ExtractorGithubGist extends ExtractorAbstract {
};

return [item];
}
};
}

/**
* Github Gist use table to display code, which produces a bunch of unnecessary characters.
* This feature is used to them clean up
* @param input
* @returns
* @param input
* @returns
*/
function cleanContent(input: string) {
return input.replace(/\n {6}\n {8}\n {8}/g, "");
Expand Down
6 changes: 3 additions & 3 deletions src/utils/extractors/ExtractorStackOverflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import { FetchPageResult } from "../fetchPageContent";

export default class ExtractorStackOverflow extends ExtractorAbstract {

name = "Stackoverflow"
name = "Stackoverflow";
URL = "stackoverflow.com";

extractSnippets = (options: FetchPageResult): SnippetResult[] => {
const target = parseHTML(options.textContent);

const answersWithCodeBlock = Array.from(target.window.document.querySelectorAll(".answer"))
.filter((item: any) => item.querySelector("code") != null);

Expand All @@ -32,7 +32,7 @@ export default class ExtractorStackOverflow extends ExtractorAbstract {
results.sort(sortSnippetResultFn);

return results;
}
};
}

function sortSnippetResultFn(a: SnippetResult, b: SnippetResult) {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/fetchPageContent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export function fetchPageTextContent(url: string): Promise<FetchPageResult> {
return new Promise((resolve, reject) => {
return fetch(url)
.then(rs => rs.text())
.then(textContent => resolve({ textContent, url }))
.then(textContent => resolve({textContent, url}))
.catch(reject);
});
}
4 changes: 2 additions & 2 deletions src/utils/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export async function search(keyword: string): Promise<null | { results: Snippet


if (keyword in cachedResults) {
return Promise.resolve({ results: cachedResults[keyword] });
return Promise.resolve({results: cachedResults[keyword]});
}

const config = getConfig();
Expand Down Expand Up @@ -55,7 +55,7 @@ export async function search(keyword: string): Promise<null | { results: Snippet

cachedResults[keyword] = results;

resolve({ results });
resolve({results});
} catch (err) {
reject(err);
}
Expand Down
2 changes: 1 addition & 1 deletion test/aiValidation.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {isCodeValid} from '../src/utils/extractors/utils';
import { isCodeValid } from '../src/utils/extractors/utils';

require('jest');

Expand Down

0 comments on commit 6c3910f

Please sign in to comment.