OpenAI Powered Snippets Beta
A Visual Studio Code extension that allows developers to create code snippets using the power of OpenAI
Usage
- Get a OpenAI API key from https://platform.openai.com/account/api-keys
- Create snippet file contains prompts, variable and functions.
- Set your API key and snippet files path in the extension VSCode settings
File>Preferences>Settings>Extensions>OpenAI Powered Snippets
- In the editor, left-click and select "Run OpenAI Snippet" or use the keyboard shortcut Ctrl+O+A.
Snippet Files
To use prompts you need create snippet and set path in vscode settings.
You can create snippet files using either YAML, JSON, or JavaScript.
To use created snippet files you need to set full path, or containg folder in vscode settings
these all valid for path settings:
- C:\snippet_file_folder
- C:\snippet_file_folder\name_of_snippet.yaml
- https://remote_address/name_of_snippet.yaml
See sample files : https://github.com/faaydemir/openai-powered-snippets/tree/main/sample-snippet-files
See snippet editor: https://faaydemir.github.io/openai-powered-snippets/
Here is a YAML sample file:
commands:
refactor:
name: refactor
prompt: >
Refactor given {system.language} code.
code:
{system.selection}
handler: replace
createUnitTest:
name: Create unit test.
prompt: >
Create unit test in {user.unitTestFramework} framework for following function.
code:
{system.selection}
handler:
func: writeFile
args:
filePath: user.testFileName
variables:
testFileName:
js: ({ baseFolder,fileName,fileExtension }) => `${baseFolder}\\tests\\${fileName}.test.${fileExtension}`
unitTestFramework: jest
JSON
{
"commands": {
"refactor": {
"name": "refactor",
"prompt": "Refactor given {system.language} code. code: {system.selection}\n",
"handler": "replace"
},
"createUnitTest": {
"name": "Create unit test.",
"prompt": "Create unit test in {user.unitTestFramework} framework for following function. code: {system.selection}`\n",
"handler": {
"func": "writeFile",
"args": {
"filePath": "user.testFileName"
}
}
}
},
"variables": {
"testFileName": {
"js": "({ baseFolder,fileName,fileExtension }) => `${baseFolder}\\\\tests\\\\${fileName}.test.${fileExtension}`"
},
"unitTestFramework": "jest"
}
}
Javascript
module.exports = {
commands: [
{
name: "Write jsdoc",
template: `Write jsdoc for following functions.
code:
{system.selection}`,
handler: {
func: 'append',
args: {
position: 'start'
}
}
},
],
variables: [
{
name: "unitTestFramework",
value: "jest"
},
{
name: "testFileName",
value: ({ baseFolder,fileName,fileExtension }) => `${baseFolder}\\tests\\${fileName}.test.${fileExtension}`
},
],
functions: [function extractTypeName({ code, system }) {/**/}],
};
Snippet Files in detail
Snippet files contain the following properties : Commands, Functions, and Variables.
commands
Each command in a snippet file has the following properties
name
The name of the command. Required
prompt or template
prompt template to use for creating an OpenAI request.
Use can use system or user defined variable in template. variables will replaced with proper value while preparing request
To use system variable add {system.*variableName*}
variableName can be one of Predefined System Variables. See below
To use user variable add {user.*variableName*}
. variableName must be in variables field in snippet file.
handler
The handler is used to handle the OpenAI response. The default function used is replace. The handler function can be one of the predefined system functions or a user-defined function (although user-defined functions may be buggy).
You can set the handler in the following ways:
Just the function name: the function will run with default values:
handler:'replace'
With arguments, to set the function arguments:
handler: {
func: 'replace',
args: {
textToReplace: 'user.answerModified'
}
}
Functions
TODO
Variables
Any of the items in variables can be used in a command template. User-defined values must have the "user" prefix. For example, if testFileName is defined in variables, it can be used as user.TestFileName in the template file or passed to a function.
Variable values can be static or dynamic. For dynamic values, you should create a getter method. When calling the variable getter, system variables (see the predefined system variables) and functions are passed as arguments. The first argument is a system variable, and the second one is a function.
module.exports = {
variables: [
{
//static
name: "testingFramework",
value: "xUnit"
},
{
//dynamic
name: "typeNameInResponse",
value: ({ answer/*system variable*/ }, { extractTypeName/*user defined function*/ }) => extractTypeName({ code: answer })
},
]
functions: [function extractTypeName({ code, system }) {/**/}],
commands: [
{
name: "Create DTO",
template: `Create unit test with {user.testingFramework} for following class.
class:
{system.selection}`,
handler: {
func: 'writeFile',
args: {
filePath: 'user.typeNameInResponse'/*usage for function arg*/
}
}
}
]
}
In YAML or JSON snippet files, the value can also be a dynamic JavaScript method. To use a JavaScript method for a value, set it as string as follows:
variables:
testFileName:
js: ({ baseFolder,fileName,fileExtension }) => `${baseFolder}\\tests\\${fileName}.test.${fileExtension}`
System variables and functions
Predefined System Variables
Variable Name |
Description |
system.selection |
Selected text in editor |
system.prompt |
Prepared OpenAI prompt |
system.answer |
OpenAI answer |
system.language |
Programming language of active file |
system.baseFolder |
Project base path |
system.fileName |
Name of active file |
system.filePath |
Full path of active file |
system.fileExtension |
Extension of active file |
Predefined System Function
Function Name |
Description |
params and default values |
append |
Append Text |
textToAppend : system.answer postion :'end' can be 'start' or 'end' |
replace |
Replace selected text |
textToReplace : system.answer |
showWebView |
Show Webview |
prompt: system.prompt answer: system.answer |
writeConsole |
Write text to console |
content: system.answer |
writeFile |
Write text to file |
filePath: content: system.answer |
Replace
Replace text with selection. Take optional parameter textToReplace
In default value equals to OpenAI answer
Default Usage
...
commands: [
{
name: "Refactor",
template: `Refactor following function.
function:
{system.selection}`
handler:'replace'
},
],
Usage with params
...
commands: [
{
name: "Refactor",
template: `Refactor following function.
function:
{system.selection}`
handler:{
func: 'replace',
args: {
textToReplace: 'user.answerModified'
}
}
},
],
variables: [
{
name: "answerModified",
value: ({answer})=>`/*\n${anwer}\n*/`
},
],
Append
Append text with selection. Take optional parameter textToAppend
and postion
. postion
can be start
or end
In default textToAppend
equals to OpenAI postion
is end of selection
Sample usage
...
commands: [
{
name: "Append",
template: `Write jsdoc for following function.
function:
{system.selection}`
handler:{
func: 'append',
args: {
position: 'start'
}
}
},
],