Repository: VaibhavAcharya/code-gpt Branch: master Commit: f14d384a996e Files: 12 Total size: 12.2 KB Directory structure: gitextract_24340zrr/ ├── .eslintrc.js ├── .gitignore ├── .vscode/ │ ├── launch.json │ └── tasks.json ├── .vscodeignore ├── README.md ├── package.json ├── src/ │ ├── extension.ts │ └── utils/ │ ├── comments.ts │ ├── email.ts │ └── errors.ts └── tsconfig.json ================================================ FILE CONTENTS ================================================ ================================================ FILE: .eslintrc.js ================================================ /**@type {import('eslint').Linter.Config} */ // eslint-disable-next-line no-undef module.exports = { root: true, parser: '@typescript-eslint/parser', plugins: [ '@typescript-eslint', ], extends: [ 'eslint:recommended', 'plugin:@typescript-eslint/recommended', ], rules: { 'semi': [2, "always"], '@typescript-eslint/no-unused-vars': 0, '@typescript-eslint/no-explicit-any': 0, '@typescript-eslint/explicit-module-boundary-types': 0, '@typescript-eslint/no-non-null-assertion': 0, } }; ================================================ FILE: .gitignore ================================================ out node_modules .vscode-test/ *.vsix vscode.d.ts pnpm-lock.yaml ================================================ FILE: .vscode/launch.json ================================================ { "version": "0.2.0", "configurations": [{ "name": "Run Extension", "type": "extensionHost", "request": "launch", "runtimeExecutable": "${execPath}", "args": [ "--extensionDevelopmentPath=${workspaceFolder}" ], "outFiles": [ "${workspaceFolder}/out/**/*.js" ], "preLaunchTask": "npm: watch" }, { "name": "Run Extension Tests", "type": "extensionHost", "request": "launch", "runtimeExecutable": "${execPath}", "args": [ "--extensionDevelopmentPath=${workspaceFolder}", "--extensionTestsPath=${workspaceFolder}/out/test" ], "outFiles": [ "${workspaceFolder}/out/test/**/*.js" ], "preLaunchTask": "npm: watch" } ] } ================================================ FILE: .vscode/tasks.json ================================================ { "version": "2.0.0", "tasks": [ { "type": "npm", "script": "watch", "problemMatcher": "$tsc-watch", "isBackground": true, "presentation": { "reveal": "never" }, "group": { "kind": "build", "isDefault": true } } ] } ================================================ FILE: .vscodeignore ================================================ .vscode/** .vscode-test/** out/test/** out/**/*.map src/** .gitignore tsconfig.json vsc-extension-quickstart.md tslint.json ================================================ FILE: README.md ================================================ ![header](https://www.aiproducttools.com/images/codegpt/header.png) # [Code-GPT](https://marketplace.visualstudio.com/items?itemName=vaibhavacharya.code-gpt-va) — Make sense of any code, anytime. 🚀 ## Introduction 👋 Code-GPT is an extension for VS Code that provides you **instant explanations for your code** within the code editor using AI. With Code-GPT, you can: - 🧠 Get instant explanations for selected code in real-time - 💡 Increase your coding understanding and efficiency - ⏳ Save time and minimize frustration with clear code explanations - 🔍 Improve your coding skills with in-depth code analysis ## Demo 📽 [![demo video](https://www.aiproducttools.com/images/codegpt/demo.gif)](https://www.aiproducttools.com/images/codegpt/demo.mp4) ## Installation 📦 1. Open VS Code and click on the Extensions icon in the left sidebar 2. Search for "Code-GPT" in the Extensions Marketplace 3. Click on the Install button for "Code-GPT" ## How to Use Code-GPT 🛠 1. Select the code you want to understand in your VSCode editor 2. Open the Command Palette (press `Ctrl + Shift + P` or `Cmd + Shift + P` on Mac) 3. Type "Explain Selected Code" and select the command from the list 4. Enter your email address if prompted 5. Wait for the response and the explanation will be prepended to the selected code in your VSCode editor Enjoy the instant and comprehensive code explanations with Code-GPT! 🎉 ## Author ✏ - Twitter → [@VaibhavAcharya_](https://twitter.com/VaibhavAcharya_) - Website → [vaibhavacharya.github.io](https://vaibhavacharya.github.io) - GitHub → [VaibhavAcharya](https://github.com/VaibhavAcharya) ================================================ FILE: package.json ================================================ { "name": "code-gpt-va", "displayName": "Code-GPT — Make sense of any code, anytime. 🚀", "description": "Provides you instant explanations for your code within the code editor using AI.", "icon": "images/logo.png", "galleryBanner": { "color": "#000000", "theme": "dark" }, "version": "0.0.6", "publisher": "vaibhavacharya", "author": { "name": "Vaibhav Acharya", "email": "vaibhavacharya111@gmail.com", "url": "https://www.twitter.com/VaibhavAcharya_" }, "engines": { "vscode": "^1.74.0" }, "private": true, "license": "MIT", "repository": { "type": "git", "url": "https://github.com/VaibhavAcharya/code-gpt" }, "bugs": { "url": "https://github.com/VaibhavAcharya/code-gpt/issues" }, "categories": [ "Machine Learning", "Other" ], "keywords": [ "ai", "code", "explanation", "comments" ], "activationEvents": [ "*" ], "main": "./out/extension.js", "contributes": { "commands": [ { "command": "extension.commentSelectedCode", "title": "Code-GPT → Comment Selected Code" }, { "command": "extension.explainSelectedCode", "title": "Code-GPT → Explain Selected Code" } ], "menus": { "editor/context": [ { "command": "extension.commentSelectedCode", "when": "editorHasSelection" }, { "command": "extension.explainSelectedCode", "when": "editorHasSelection" } ] } }, "scripts": { "vscode:prepublish": "npm run compile", "compile": "tsc -p ./", "lint": "eslint \"src/**/*.ts\"", "watch": "tsc -watch -p ./" }, "devDependencies": { "@types/node": "^16.11.7", "@types/vscode": "1.74.0", "@typescript-eslint/eslint-plugin": "^5.42.0", "@typescript-eslint/parser": "^5.42.0", "eslint": "^8.26.0", "typescript": "^4.8.4" }, "dependencies": { "@octokit/rest": "^18.0.0", "axios": "^1.3.2" } } ================================================ FILE: src/extension.ts ================================================ import { type ExtensionContext, window, commands } from "vscode"; import axios from "axios"; import { checkEmail } from "./utils/email"; import { displayError } from "./utils/errors"; import { languageSupportsComments, styleAsComment } from "./utils/comments"; // Function to handle the selected code const commentSelectedCode = async (context: ExtensionContext) => { // Check if the email has been saved or not await checkEmail(context); const email = await context.globalState.get("email"); if (!email) return displayError("noEmail"); // Grab the editor window const editor = window.activeTextEditor; if (!editor) return displayError("noEditor"); // Aquire the selected text const selection = editor.selection; const selectedText = editor.document.getText(selection); if (!selectedText.length) return displayError("noSelection"); const lang = editor.document.languageId; const isLanguageSupported = languageSupportsComments(lang); if (!isLanguageSupported) return displayError("languageNotSupported"); // Send the selected code and email to the API try { const { data } = await axios.post("https://www.aiproducttools.com/api/codegpt", { email, input: selectedText, lang, }); if (!data.output) return displayError("serverSentNothing"); // Get the output from the API response const output = data.output.trim(); // Wrap the output in a comment const comment = styleAsComment(output, lang); // Replace the selected text with the output editor.edit((editBuilder) => { editBuilder.insert(selection.start, `${comment}\n`); }); } catch (error) { console.error(error); return displayError("unableToConnect"); } }; // Function to handle the selected code const explainSelectedCode = async (context: ExtensionContext) => { // Check if the email has been saved or not await checkEmail(context); const email = await context.globalState.get("email"); if (!email) return displayError("noEmail"); // Grab the editor window const editor = window.activeTextEditor; if (!editor) return displayError("noEditor"); // Aquire the selected text const selection = editor.selection; const selectedText = editor.document.getText(selection); if (!selectedText.length) return displayError("noSelection"); const lang = editor.document.languageId; const isLanguageSupported = languageSupportsComments(lang); if (!isLanguageSupported) return displayError("languageNotSupported"); // Send the selected code and email to the API try { const { data } = await axios.post("https://www.aiproducttools.com/api/codegpt", { email, input: selectedText, lang, }); if (!data.output) return displayError("serverSentNothing"); // Get the output from the API response const output = data.output.trim(); // Show the output in a message window.showInformationMessage(output) } catch (error) { console.error(error); return displayError("unableToConnect"); } }; // Register the "Explain Selected Code" command export function activate(context: ExtensionContext) { const registerComment = commands.registerCommand("extension.commentSelectedCode", () => commentSelectedCode(context)); const registerExplain = commands.registerCommand("extension.explainSelectedCode", () => explainSelectedCode(context)); context.subscriptions.push(registerComment); context.subscriptions.push(registerExplain); } // Deactivate the extension export function deactivate() { return null; } ================================================ FILE: src/utils/comments.ts ================================================ const singleLineCommentMap: Record = { abap: "--", bat: "::", bibtex: "%", clojure: ";", coffeescript: "#", c: "//", cpp: "//", csharp: "//", "cuda-cpp": "//", diff: "//", dockerfile: "#", fsharp: "//", "git-commit": "#", "git-rebase": "#", go: "//", groovy: "//", ini: ";", java: "//", javascript: "//", javascriptreact: "//", json: "//", jsonc: "//", latex: "%", less: "/*", lua: "--", makefile: "#", objectivec: "//", objectivecpp: "//", perl: "#", perl6: "#", php: "//", plaintext: "//", powershell: "#", jade: "//", pug: "//", python: "#", r: "#", razor: "@*", ruby: "#", rust: "//", sass: "//", shaderlab: "//", shellscript: "#", sql: "--", slim: "//", stylus: "//", swift: "//", typescript: "//", typescriptreact: "//", tex: "%", vb: "'", yaml: "#", }; const multiLineCommentMap: Record = { css: ["/*", "*/"], handlebars: ["{{!--", "--}}"], html: [""], markdown: [""], scss: ["/*", "*/"], xml: [""], xsl: [""], }; export const styleAsComment = (text: string, lang: string) => { const singleLineComment = singleLineCommentMap[lang]; const multiLineComment = multiLineCommentMap[lang]; if (!singleLineComment && !multiLineComment) return text; if (singleLineComment) return `${singleLineComment} ${text}`; if (multiLineComment) return `${multiLineComment[0]} ${text} ${multiLineComment[1]}`; }; export const languageSupportsComments = (lang: string) => { return !!singleLineCommentMap[lang] || !!multiLineCommentMap[lang]; }; ================================================ FILE: src/utils/email.ts ================================================ import { type ExtensionContext, window } from "vscode"; export const checkEmail = async (context: ExtensionContext) => { const email = await context.globalState.get("email"); // If the email has not been saved, ask the user for their email if (!email) { const newEmail = await window.showInputBox({ title: "Code-GPT", placeHolder: "📧 Enter your email", prompt: "To protect us from unlimited use. We won't spam your email, promise. 🤝", validateInput: (value) => { const emailRegex = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; if (!emailRegex.test(value)) { return "Error: Invalid email. ☹"; } return null; }, }); await context.globalState.update("email", newEmail); } }; ================================================ FILE: src/utils/errors.ts ================================================ import { window } from "vscode"; const errorMessages = { noEditor: "Error: Editor not found! 😵", noEmail: "Error: Email is required! It is to protect us from unlimited use. We won't spam your email, promise. 🤝", noSelection: "Nothing selected. 😅", languageNotSupported: "Error: Language not supported yet! 😵", unableToConnect: "Error: Unable to connect to server! 💀", serverSentNothing: "Server sent nothing! 🙃", }; export const displayError = (error: keyof typeof errorMessages) => { return window.showErrorMessage(errorMessages[error]); }; ================================================ FILE: tsconfig.json ================================================ { "compilerOptions": { "module": "commonjs", "target": "es2020", "outDir": "out", "sourceMap": true, "rootDir": "src", "strict": true }, "exclude": ["node_modules", ".vscode-test"] }