Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
rbourgeat committed Mar 14, 2024
1 parent b48ecc8 commit 1e8ee7d
Show file tree
Hide file tree
Showing 5 changed files with 170 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*.vsix
node_modules
package-lock.json
7 changes: 7 additions & 0 deletions .vscodeignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.vscode/**
.vscode-test/**
out/test/**
src/**
.gitignore
**/*.map
**/*.ts
35 changes: 34 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,34 @@
# refacto
# Refacto

Refacto is a Visual Studio Code extension that allows you to refactor your code using an **local AI model**. The extension sends your selected code to a server running the Llama CPP model, which returns a refactored version of your code.

## Features

- **Refactor Selected Code**: Simply select the code you want to refactor, right-click, and choose "✨ Refacto selected code" from the context menu.

## Installation

1. Install Visual Studio Code 1.50.0 or higher
2. Launch Code
3. From the command palette `Ctrl-Shift-P` (Windows, Linux) or `Cmd-Shift-P` (OSX)
4. Select `Install Extension`
5. Choose the extension `refacto`
6. Reload Visual Studio Code

## Usage

1. Select the code you want to refactor.
2. Right-click and choose "✨ Refacto selected code" from the context menu.
3. The refactored code will replace your selected code.

## Configuration

- `llama-cpp.serverUrl`: URL of the Llama CPP server. Default is `http://127.0.0.1:8080/completion`.

## Contributing

If you find any bugs or have a feature request, please open an issue on [github](https://github.com/rbourgeat/refacto)!

## License

[MIT](https://github.com/rbourgeat/refacto/blob/main/LICENSE)
75 changes: 75 additions & 0 deletions extension.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
const vscode = require('vscode');
const axios = require('axios');

function extractCode(str) {
const start = str.indexOf('```');
const end = str.indexOf('```', start + 3);
if (start !== -1 && end !== -1) {
let code = str.slice(start + 3, end);
const firstNewLine = code.indexOf('\n');
if (firstNewLine !== -1) {
code = code.slice(firstNewLine + 1);
}
return code;
}
return str;
}

function activate(context) {
let disposable = vscode.commands.registerCommand('extension.refacto', async function () {
let editor = vscode.window.activeTextEditor;
if (editor) {
let document = editor.document;
let selection = editor.selection;
let selected_text = document.getText(selection);
let config = vscode.workspace.getConfiguration('llama-cpp');
let serverUrl = config.get('serverUrl');

if (serverUrl) {
let prompt = `<s>[INST] You are a developer who is working \
on a project and wants to write the most beautiful code. \
Refacto the following code: \
\`\`\`${selected_text}\`\`\`[/INST]</s>`;

vscode.window.withProgress({
location: vscode.ProgressLocation.Notification,
title: "Code refactoring in progress...",
cancellable: false
}, async (progress) => {
try {
let response = await axios.post(serverUrl, {
prompt,
n_predict: -1,
})

vscode.window.showInformationMessage(`Response data content: ${JSON.stringify(response.data, null, 2)}`);

if (response.status === 200) {
if (response.data.content != "") {
editor.edit(editBuilder => {
editBuilder.replace(selection, extractCode(response.data.content));
});
} else {
vscode.window.showErrorMessage('Llama cpp server send empty response...');
}
} else {
vscode.window.showErrorMessage('Llama cpp server connection error !');
}
} catch (error) {
vscode.window.showErrorMessage('An error occurred: ' + error);
}
});
} else {
vscode.window.showErrorMessage(`You don't have the llama cpp server url set up !`);
}
}
});
context.subscriptions.push(disposable);
}

function deactivate() {}

module.exports = {
activate,
deactivate
}
51 changes: 51 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{
"name": "refacto",
"version": "1.0.0",
"publisher": "rbourgeat",
"repository": "https://github.com/rbourgeat/refacto",
"engines": {
"vscode": "^1.50.0"
},
"activationEvents": [
"onCommand:extension.refacto"
],
"main": "./extension.js",
"contributes": {
"commands": [
{
"command": "extension.refacto",
"title": "✨ Refacto selected code"
}
],
"menus": {
"editor/context": [
{
"command": "extension.refacto",
"group": "navigation"
}
]
},
"configuration": {
"title": "Refacto",
"properties": {
"llama-cpp.serverUrl": {
"type": "string",
"default": "http://127.0.0.1:8080/completion",
"description": "URL of the Llama CPP server"
}
}
}
},
"scripts": {
"compile": "vsce package"
},
"devDependencies": {
"@types/mocha": "^8.0.4",
"@types/node": "^12.11.1",
"eslint": "^7.22.0",
"vscode": "^1.1.34"
},
"dependencies": {
"axios": "^1.6.7"
}
}

0 comments on commit 1e8ee7d

Please sign in to comment.