Code Function Analysis Extension
Version: 1.8.0
Author: Nero
Table of Contents
Introduction
Code Function Analysis is a Visual Studio Code extension that provides intelligent, context-aware analysis of your code. Leveraging advanced AI through OpenAI's GPT-4, models from Hugging Face, and Google's Gemini AI, this tool offers comprehensive feedback on various aspects of your code, including:
- Performance optimization
- Naming conventions
- Code quality
- Opportunities for improvement
- Unit Test Generation: Generate unit tests for selected functions using AI models
- Frontend Code Generation: Automatically generate CSS and JavaScript code from selected HTML snippets
- Commit Message Generation: Automatically generate commit messages based on your staged Git changes
Transform your coding experience with mentor-like suggestions and best practices to enhance your code's readability, maintainability, and efficiency.
Features
- Big O Complexity Analysis: Understand the time and space complexity of your functions.
- Variable Naming and Style Checks: Ensure compliance with language-specific style guides (e.g., PEP 8 for Python).
- Code Elegance Evaluation: Receive suggestions to make your code more concise and readable.
- Mentor-Like Feedback: Get constructive advice with clear explanations for each recommendation.
- Interactive Learning: Access relevant documentation and examples for further learning.
- Feedback Customization: Tailor the analysis focus and feedback level to your preferences.
- Multi-Language Support: Supports Python, JavaScript, Java, C++, HTML, and more.
- Automatic Selection Analysis: Automatically analyze selected code with debouncing to prevent excessive requests.
- Retry Logic for Analysis Requests: Automatically retries analysis if the initial request fails, with enhanced handling for models that are loading.
- Unit Test Generation: Automatically generate unit tests for selected functions.
- Frontend Code Generation: Generate CSS and JavaScript code from selected HTML snippets to enhance functionality and appearance.
- Commit Message Generation: Generate concise and descriptive commit messages based on your staged Git changes.
- Status Bar Updates: Real-time status updates during code analysis, unit test generation, frontend code generation, and commit message generation.
- Supports Multiple AI Models:
- OpenAI Models: Use GPT-4 for sophisticated analysis and generation tasks.
- Hugging Face Models: Access a variety of models, including
nvidia/NVLM-D-72B .
- Google Gemini Models: Choose from different variants like Gemini 1.5 Flash, Flash-8B, Pro, and Gemini 1.0 Pro for AI analysis and generation.
- Custom Model Support: Ability to specify any Hugging Face model by entering the model name in the extension settings.
Installation
From the VS Code Marketplace
Install Code Function Analysis
Manual Installation
Clone the Repository:
git clone https://github.com/Neroli1108/code-function-analysis-vscode-ext.git
Navigate to the Extension Directory:
cd code-function-analysis-vscode-ext
Install Dependencies:
npm install
Build the Extension:
npm run compile
Install the Extension in VS Code:
- Open Visual Studio Code.
- Go to
Extensions (Ctrl+Shift+X or Cmd+Shift+X ).
- Click on the
... menu and select Install from VSIX... .
- Navigate to the
code-function-analysis-vscode-ext directory and select the generated .vsix file.
Requirements
- Visual Studio Code version
1.94.0 or higher.
- Node.js version
14.x or higher.
- An API Key for OpenAI, Hugging Face, or Google Gemini, depending on the models you want to use.
- Git must be installed and accessible in your system's PATH for commit message generation.
Getting Started
Setting Up the OpenAI, Hugging Face, or Google Gemini API Key
Obtain an API Key:
- For OpenAI: Sign up or log in to the OpenAI Platform, navigate to the API keys section, and generate a new secret key.
- For Hugging Face: Sign up or log in to the Hugging Face Platform, and navigate to your account settings to generate an API key.
- For Google Gemini: Follow the setup instructions to obtain your Gemini AI API key (visit Google AI Platform for more information).
Configure the API Key in VS Code:
- Open Visual Studio Code.
- Go to
File > Preferences > Settings (or Code > Preferences > Settings on macOS).
- Search for
Code Function Analysis .
- Enter your API key in the appropriate field for the AI service you wish to use (OpenAI, Hugging Face, or Google Gemini).
Note: Keep your API key secure and do not share it publicly.
Configuring Extension Settings
Usage
Analyzing a Function
Open a Code File:
- Open a code file in a supported language (e.g., Python, JavaScript).
Select a Function:
- Highlight the function code you want to analyze.
Run the Analysis Command:
- Right-click and select "Analyze Selected Function" from the context menu.
- Or open the Command Palette (
Ctrl+Shift+P or Cmd+Shift+P on macOS), type Analyze Selected Function , and select the command.
View the Analysis:
- A new panel will display the analysis, including suggestions and improvements.
Status Bar:
- The status bar at the bottom left will display real-time updates, such as "Analyzing...", "Complete", or "Retrying..." when applicable.
Generating Unit Tests
Open a Code File:
- Open a code file in a supported language.
Select a Function:
- Highlight the function code you want to generate unit tests for.
Run the Generate Unit Test Command:
- Right-click and select "Generate Unit Test for Selected Function" from the context menu.
- Or open the Command Palette, type
Generate Unit Test for Selected Function , and select the command.
View the Generated Unit Tests:
- A new panel will display the generated unit tests, including test cases and edge cases where applicable.
Generating Frontend Code
Open an HTML File:
- Open an HTML file containing the code you want to enhance.
Select an HTML Snippet:
- Highlight the HTML code for which you want to generate CSS and JavaScript.
Run the Generate Frontend Code Command:
- Right-click and select "Generate Frontend Code from HTML" from the context menu.
- Or open the Command Palette, type
Generate Frontend Code from HTML , and select the command.
View the Generated Code:
- A new panel will display the generated CSS and JavaScript code to enhance the functionality and appearance of your HTML snippet.
Generating Commit Messages
Stage Your Changes:
- Use Git to stage the changes you want to commit.
Run the Generate Commit Message Command:
- Right-click in the editor and select "Generate Commit Message" from the context menu.
- Or open the Command Palette, type
Generate Commit Message , and select the command.
View and Confirm the Commit Message:
- A new panel will display the generated commit message.
- You can choose to Commit, Edit Message, or Cancel.
Commit the Changes:
- If you choose Commit, the extension will commit the staged changes using the generated message.
- If you choose Edit Message, you can modify the message before committing.
Status Bar:
- The status bar will display updates like "Generating...", "Complete", or "Failed" during the process.
Note: Ensure that Git is installed and properly configured in your system for this feature to work.
Commands
Analyze Selected Function:
- Command ID:
code-function-analysis.analyzeFunction
- Description: Analyzes the selected code function and provides feedback.
Generate Unit Test for Selected Function:
- Command ID:
code-function-analysis.generateUnitTest
- Description: Generates unit tests for the selected function using AI models.
Generate Frontend Code from HTML:
- Command ID:
code-function-analysis.generateFrontendCode
- Description: Generates CSS and JavaScript code from selected HTML snippets.
Generate Commit Message:
- Command ID:
code-function-analysis.generateCommitMessage
- Description: Generates a commit message based on your staged Git changes.
Extension Settings
code-function-analysis.apiProvider :
- Type:
string
- Options:
"OpenAI" , "HuggingFace" , "GoogleGemini"
- Description: Select the AI service provider for code analysis and generation tasks.
- Default:
"GoogleGemini"
code-function-analysis.openAIApiKey :
- Type:
string
- Description: Your OpenAI API key.
- Default:
"" (empty string)
code-function-analysis.huggingFaceApiKey :
- Type:
string
- Description: Your Hugging Face API key.
- Default:
"" (empty string)
code-function-analysis.huggingFaceModel :
- Type:
string
- Options: Predefined models or
"custom"
- Description: Select a predefined Hugging Face model or set
"custom" to use your own model.
- Default:
"EleutherAI/gpt-neo-2.7B"
code-function-analysis.huggingFaceCustomModel :
- Type:
string
- Description: Specify a custom Hugging Face model name if
"custom" is selected.
- Default:
"" (empty string)
code-function-analysis.googleGeminiApiKey :
- Type:
string
- Description: Your Google Gemini API key.
- Default:
"" (empty string)
code-function-analysis.googleGeminiModel :
- Type:
string
- Options:
"gemini-1.5-flash" , "gemini-1.5-flash-8b" , "gemini-1.5-pro" , "gemini-1.0-pro"
- Description: Select the Google Gemini model to use.
- Default:
"gemini-1.5-flash"
code-function-analysis.feedbackLevel :
- Type:
string
- Options:
"simple" , "verbose"
- Description: Set the level of detail in feedback.
- Default:
"simple"
code-function-analysis.focusAreas :
- Type:
array
- Items:
"performance" , "style" , "readability" , "complexity"
- Description: Select the focus areas for feedback.
- Default:
["performance", "style", "readability", "complexity"]
code-function-analysis.autoAnalyzeOnSelection :
- Type:
boolean
- Description: Automatically analyze code when it is selected.
- Default:
false
code-function-analysis.expandSelectionToFunction :
- Type:
boolean
- Description: Expand selection to the full function when a single line is selected.
- Default:
true
Development
Building the Extension
Install Dependencies:
npm install
Compile the Extension:
npm run compile
This command performs type checking, linting, and builds the extension using esbuild .
For Production Build:
npm run package
Running the Extension
Open the Extension in VS Code:
- Open the project directory in Visual Studio Code.
Start Debugging:
- Press
F5 to launch the extension in a new Extension Development Host window.
Test the Extension:
- Follow the usage instructions to test the extension's functionality.
Troubleshooting
Contributing
Contributions are welcome! Please follow these steps:
Fork the Repository:
- Click the "Fork" button at the top right corner of the repository page.
Create a New Branch:
git checkout -b feature/your-feature-name
Make Changes and Commit:
git commit -am "Add your feature"
Push to Your Fork:
git push origin feature/your-feature-name
Submit a Pull Request:
- Go to the original repository and click on "New Pull Request".
License
This project is licensed under the MIT License. See the LICENSE file for details.
Acknowledgments
- OpenAI for providing the GPT-4 API.
- Hugging Face for AI models.
- Google for providing the Gemini AI models.
- Visual Studio Code for the extension platform.
- Community Contributors for their valuable feedback and contributions.
Disclaimer: This extension sends code snippets and diffs to third-party APIs for analysis and generation tasks. Please ensure compliance with your organization's policies regarding code sharing and avoid sharing sensitive or proprietary code.
Note: Always keep your API keys secure and avoid committing them to source control.
| |