Skip to content

Commit 735adf5

Browse files
Refactor code structure for improved readability and maintainability
1 parent e9580a2 commit 735adf5

6 files changed

Lines changed: 114 additions & 10 deletions

File tree

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ CodeCharm is your intelligent VS Code extension that generates **inline code com
2525
- ⌨️ **Shortcut support**:
2626
- `Ctrl + Win + J` → Add Inline Comments
2727
- `Ctrl + Win + G` → Refactor Code
28+
- `Ctrl + Win + H` → Comment + Refactor (adds inline emoji comments AND refactors the code)
29+
- `Ctrl + Win + K` → Run only the instruction inside `cmd()` (no other changes)
2830
- 🔁 **Model Fallback Support**: Uses the following Gemini models in this order:
2931
1. `gemini-2.0-flash-lite`
3032
2. `gemini-2.0-flash`
@@ -40,7 +42,8 @@ CodeCharm is your intelligent VS Code extension that generates **inline code com
4042
- 🆕 **Execute `cmd()` instructions inside your code**
4143
- Add a line like: `cmd(import pandas)` at the top of your code block.
4244
- CodeCharm will first apply your instruction (e.g., add imports, modify setup)
43-
and then generate inline comments or refactor the code.
45+
and then generate inline comments or refactor the code. You can also run the combined action with `Ctrl + Win + H` to perform both at once.
46+
- Press `Ctrl + Win + K` to run only the `cmd()` instruction found in the selection (the extension sends only the `cmd()` instruction to the assistant and returns the modified code).
4447
> **Tip:** You can add a `cmd()` instruction before running CodeCharm
4548
> Example:
4649
> ```js
@@ -60,6 +63,8 @@ To use CodeCharm, you'll need your **own Google Gemini API key**:
6063
2. Copy your API key.
6164
3. Open VS Code.
6265
4. Press `Ctrl + Win + J` or `Ctrl + Win + G` for the first time.
66+
- Or press `Ctrl + Win + H` to run both commenting and refactoring in one step.
67+
- Press `Ctrl + Win + K` to run only the instruction inside `cmd()` in your selection.
6368
5. A secure input box will appear → paste your key there.
6469
6. It will be saved automatically in:
6570
> **Tip:** You can add a `cmd()` instruction before running CodeCharm
@@ -82,6 +87,8 @@ To use CodeCharm, you'll need your **own Google Gemini API key**:
8287
2. Press:
8388
- `Ctrl + Win + J` → for inline comments
8489
- `Ctrl + Win + G` → to refactor code
90+
- `Ctrl + Win + H` → to both refactor and add inline emoji comments (follows any `cmd()` instructions)
91+
- `Ctrl + Win + K` → to only execute the `cmd()` instruction and return the modified code (no other changes)
8592
3. CodeCharm will generate and replace your selection with an improved version.
8693
> **Tip:** You can add a `cmd()` instruction before running CodeCharm
8794
> Example:

node_modules/.package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "CodeCharm",
33
"displayName": "CodeCharm",
44
"description": "AI-powered code comment generator for VS Code using Gemini",
5-
"version": "0.4.7",
5+
"version": "0.4.8",
66
"publisher": "DeveloperPuneet",
77
"icon": "images/Icon.png",
88
"engines": {
@@ -24,11 +24,16 @@
2424
"inline comments",
2525
"developer tools",
2626
"productivity",
27-
"vs code extension"
27+
"vs code extension",
28+
"tools",
29+
"shortcuts",
30+
"code generator"
2831
],
2932
"activationEvents": [
3033
"onCommand:CodeCharm.commentFunction",
3134
"onCommand:CodeCharm.suggestReadableCode",
35+
"onCommand:CodeCharm.commentAndRefactor",
36+
"onCommand:CodeCharm.runCmdOnly",
3237
"onCommand:CodeCharm.setApiKey"
3338
],
3439
"main": "./src/extension.js",
@@ -45,6 +50,15 @@
4550
{
4651
"command": "CodeCharm.setApiKey",
4752
"title": "🔑 CodeCharm: Set Gemini API Key"
53+
},
54+
{
55+
"command": "CodeCharm.commentAndRefactor",
56+
"title": "✨ CodeCharm: Comment & Refactor (Ctrl+Win+H)"
57+
}
58+
,
59+
{
60+
"command": "CodeCharm.runCmdOnly",
61+
"title": "⚡ CodeCharm: Run cmd() Instruction Only (Ctrl+Win+K)"
4862
}
4963
],
5064
"keybindings": [
@@ -58,6 +72,18 @@
5872
"key": "ctrl+win+g",
5973
"when": "editorTextFocus"
6074
}
75+
,
76+
{
77+
"command": "CodeCharm.commentAndRefactor",
78+
"key": "ctrl+win+h",
79+
"when": "editorTextFocus"
80+
}
81+
,
82+
{
83+
"command": "CodeCharm.runCmdOnly",
84+
"key": "ctrl+win+k",
85+
"when": "editorTextFocus"
86+
}
6187
],
6288
"configuration": {
6389
"title": "CodeCharm Settings",

src/extension.js

Lines changed: 76 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,67 @@ async function activate(context) {
8383
});
8484
});
8585

86+
const combinedCommand = vscode.commands.registerCommand('CodeCharm.commentAndRefactor', async function () {
87+
const editor = vscode.window.activeTextEditor;
88+
if (!editor) return;
89+
90+
const selection = editor.selection;
91+
const selectedText = editor.document.getText(selection);
92+
93+
if (!selectedText) {
94+
vscode.window.showInformationMessage("No code selected!");
95+
return;
96+
}
97+
98+
// First request a refactor to ensure code is improved
99+
const refactored = await getCommentFromGemini(selectedText, "readability");
100+
if (!refactored) {
101+
vscode.window.showErrorMessage("Gemini refactor failed.");
102+
return;
103+
}
104+
105+
// Then add concise inline emoji comments to the refactored result
106+
const commented = await getCommentFromGemini(refactored, "comment");
107+
if (!commented) {
108+
vscode.window.showErrorMessage("Gemini commenting failed after refactor.");
109+
return;
110+
}
111+
112+
editor.edit(editBuilder => {
113+
editBuilder.replace(selection, commented);
114+
});
115+
});
116+
117+
const runCmdCommand = vscode.commands.registerCommand('CodeCharm.runCmdOnly', async function () {
118+
const editor = vscode.window.activeTextEditor;
119+
if (!editor) return;
120+
121+
const selection = editor.selection;
122+
const selectedText = editor.document.getText(selection);
123+
124+
if (!selectedText) {
125+
vscode.window.showInformationMessage("No code selected!");
126+
return;
127+
}
128+
129+
const match = selectedText.match(/cmd\(([^)]+)\)/);
130+
if (!match) {
131+
vscode.window.showInformationMessage("No cmd() instruction found in selection.");
132+
return;
133+
}
134+
135+
const instruction = match[1].trim();
136+
const result = await getCommentFromGemini(selectedText, "cmd", instruction);
137+
if (!result) {
138+
vscode.window.showErrorMessage("Gemini cmd execution failed.");
139+
return;
140+
}
141+
142+
editor.edit(editBuilder => {
143+
editBuilder.replace(selection, result);
144+
});
145+
});
146+
86147
const setApiKeyCommand = vscode.commands.registerCommand('CodeCharm.setApiKey', async function () {
87148
const userInput = await vscode.window.showInputBox({
88149
prompt: "Enter your Gemini API Key",
@@ -99,18 +160,28 @@ async function activate(context) {
99160
}
100161
});
101162

102-
context.subscriptions.push(commentCommand, readableCommand, setApiKeyCommand); // Add subscriptions to context
163+
context.subscriptions.push(commentCommand, readableCommand, combinedCommand, runCmdCommand, setApiKeyCommand); // Add subscriptions to context
103164

104165
vscode.window.showInformationMessage("💫 CodeCharm loaded!"); // Show startup message
105166
}
106167

107168
Additional_functionalies = `Before doing anything, check if the provided code contains a pattern like cmd(...); if found, treat the text inside cmd() as an instruction to modify the code first (for example, cmd(import pandas library) means you should add "import pandas as pd" at the top). If no cmd() is found, do not print any messages or placeholders—simply continue processing the code silently.`
108169

109170
// 🧠 Get Gemini Comment or Refactor
110-
async function getCommentFromGemini(code, mode = "comment") {
111-
let prompt = mode === "comment"
112-
? `You are a strict code-commenting assistant. Only add inline comments (4–5 words max) using emojis. Do NOT explain or return markdown. Return ONLY the modified code. ${Additional_functionalies} \n\n${code}`
113-
: `Refactor this code for readability. Rename unclear variables, break down complex parts. DO NOT return explanation. ONLY the cleaned code. ${Additional_functionalies} \n\n${code}`;
171+
async function getCommentFromGemini(code, mode = "comment", cmdInstruction = null) {
172+
let prompt;
173+
if (mode === "comment") {
174+
prompt = `You are a strict code-commenting assistant. Only add inline comments (4–5 words max) using emojis. Do NOT explain or return markdown. Return ONLY the modified code. ${Additional_functionalies} \n\n${code}`;
175+
} else if (mode === "readability") {
176+
prompt = `Refactor this code for readability. Rename unclear variables, break down complex parts. DO NOT return explanation. ONLY the cleaned code. ${Additional_functionalies} \n\n${code}`;
177+
} else if (mode === "both") {
178+
prompt = `You are a code refactoring assistant. Thoroughly refactor the provided code to maximize readability while preserving behavior: rename unclear variables to descriptive names, simplify complex expressions and control flow, extract repeated or large blocks into helper functions, and improve formatting. After refactoring, add concise inline comments (4–5 words max) must using emojis to clarify intent. Do NOT include explanations or markdown — return ONLY the modified code (remove any cmd() lines or placeholders). ${Additional_functionalies} \n\n${code}`;
179+
} else if (mode === "cmd") {
180+
const instr = cmdInstruction ? cmdInstruction : "";
181+
prompt = `You are a code assistant. There is an instruction inside cmd(): "${instr}". Apply ONLY that instruction to the provided code. Do NOT refactor, add comments, or provide explanations. Remove the cmd() line from the output. Return ONLY the modified code. ${Additional_functionalies} \n\n${code}`;
182+
} else {
183+
prompt = `${Additional_functionalies} \n\n${code}`;
184+
}
114185

115186
const apiKey = await getOrPromptAPIKey();
116187
if (!apiKey) return null;

0 commit comments

Comments
 (0)