diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ca140b1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +.DS_Store +out +dist +node_modules +.vscode-test/ +*.vsix +*.js.map +src/localconfig.ts \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..4d8df21 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,47 @@ +# Change Log + +v1.0.7 (2022-11-28) + +- Add status bar command and user setting to disable specific languages + +- Add preference for user to decide generation code line by line or automatic + +- Add welcome page, new user will see the welcome page first when activate the extension or you can commmand it in command palette by codegeex.welcome-page + +- Fix some known issues + +v1.0.6 (2022-11-11) + +- Add new mode Translation mode for user to translate code + +- Add three parameters for user to change in order to get better code suggestions + +- Add commands of three other modes to the right click menu + +- Fix some known issues + +v1.0.5 (2022-11-04) + +- Add new command to get new suggestion + +- Add more information to the status bar + +v1.0.4 (2022-10-28) + +- Fixed some known issues + +v1.0.3 (2022-10-20) + +- Fixed some known issues + +v1.0.2 (2022-09-22) + +- Fixed some known issues + +v1.0.1 (2022-09-21) + +- Fixed some known issues + +v1.0.0 (2022-09-20) + +- Initial release \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..4cfbedc --- /dev/null +++ b/README.md @@ -0,0 +1,112 @@ +![codegeex_logo](https://lfs.aminer.cn/misc/wangshan/pretrain/codegeex/codegeex_logo.png) + +🌐 中文 + +![CodeGeeX license](https://img.shields.io/github/license/THUDM/CodeGeeX?colorA=0B9FE0&colorB=brightgreen) +![CodeGeeX vscode extension version](https://img.shields.io/visual-studio-marketplace/v/aminer.codegeex?colorA=0B9FE0&colorB=brightgreen) +![CodeGeeX download](https://img.shields.io/visual-studio-marketplace/d/aminer.codegeex?colorA=0B9FE0&colorB=brightgreen) +![CodeGeeX API calls per week](https://img.shields.io/badge/dynamic/json?label=API%20calls&query=%24.result.count&suffix=%2Fweek&url=http%3A%2F%2Ftianqi.aminer.cn%2Fapi%2Fv1%2Fapi%2Fcodegeex%2Fdashboard%3Ftime_type%3Dweeks&colorA=0B9FE0&colorB=brightgreen) +![CodeGeeX vscode extension rating](https://img.shields.io/visual-studio-marketplace/stars/aminer.codegeex?colorA=0B9FE0&colorB=brightgreen) +![CodeGeeX vscode extension last update](https://img.shields.io/visual-studio-marketplace/last-updated/aminer.codegeex?colorA=0B9FE0&colorB=brightgreen) +[![CodeGeeX github stars](https://img.shields.io/github/stars/THUDM/CodeGeeX?style=social)](https://github.com/THUDM/CodeGeeX) + +We introduce CodeGeeX, a open source large-scale multilingual code generation model with 13 billion parameters, pretrained on a large code corpus of more than 20 programming languages. With CodeGeeX, we can generate codes by only providing natural language descriptions, complete any code snippet, or translate codes to other programming languages, etc. CodeGeeX also provides customizable features (**Prompt Mode**) to help you configure your own programming assistant. Happy coding! + +For more information, please check out our [Homepage](https://models.aminer.cn/codegeex/) and [GitHub repo](https://github.com/THUDM/CodeGeeX). + +Please kindly let us know if you encounter any problem or have any suggestion, via [codegeex@aminer.cn](mailto:codegeex@aminer.cn). + +## Basic Usage +Make sure vscode version is >= 1.68.0. Install the extension and enable it globally. There are three modes of usage: + +- **Stealth mode**: Keep CodeGeeX activated, it will start generating codes when you stop writing (the icon at the bottom of VSCode starts spinning). When the generated code is shown in gray, just press ``Tab`` to insert the generated codes. +- **Interactive mode**: Press ``Ctrl+Enter`` to activate the interactive mode, CodeGeeX will generate ``X`` candidates and show them in the right panel (``X`` can be modified in extension settings ``Candidate Num``). Then, select the best candidate by clicking on it. +- **Translation mode**: Select code, and press ``Ctrl+Alt+T`` to activate the translation mode. Then, choose the language of the selected code. You will get the code translated into the same language as your current editor. Click on the ``use code`` button to insert the result. You can also configure in the settings whether to comment out the original code or to replace it. +- **Prompt mode**: Select codes to be used as input, then press ``Alt/Option+t`` to trigger the prompt mode. It will show a list of pre-defined prompt templates and choose one to generate codes with your input. This mode is fully customizable, you can add your own templates in the extension settings ``Prompt Templates``. + +## Privacy + +We highly respect the privacy of your code. The code is only used as the input of CodeGeeX to assist your programming. At the first time of usage, we will ask if you agree to share the generated code only for research purpose (**disabled** by default). + +## Guidance +Please see the details and examples for how to use the three modes in CodeGeeX: +### Stealth mode +In this mode, CodeGeeX will start generating codes when you stop writing (the icon at the bottom of VSCode starts spinning). When the generated code is shown in gray, just press ``Tab`` to insert the generated codes. You can also press ``Alt/Option+[`` or ``]`` to change between candidates. If you are not satisfied with the current suggestions, you can also press ``Alt/Option+N`` to get new suggestions. Change the number of candidates in the extension settings ``Candidate Num`` (more candidates will slow down the generation speed). **Note**: The generation always starts at the current position of your cursor, thus if you modify the code before the generation is finished, it will probably cause bugs. We keep working on making the generation faster. + +![image](https://lfs.aminer.cn/misc/wangshan/pretrain/codegeex/bubble_sort_go.gif) + +### Interactive mode +In this mode, press ``Ctrl+Enter`` to generate codes and visualize the candidates in another panel. Then, click on the best candidate to insert the generated codes to the current position of cursor. + +![image](https://lfs.aminer.cn/misc/wangshan/pretrain/codegeex/interactive_mode2.gif) + +### Translation mode +In this mode, paste or tape some code from another language to the current file, select the code, and press ``Ctrl+Alt+T``. Then, choose the language of the selected code. Wait for a few seconds, you will get the code translated into the same language as your current editor. Click on the ``use code`` button to insert the result into the current position of your cursor. You can also configure in the settings whether to comment out the original code or to replace it. + +![image](https://lfs.aminer.cn/misc/wangshan/pretrain/codegeex/translation_cpp_to_python.gif) + +### Prompt mode +In this mode, you can add extra prompts to the input and implement some cool features, like code explanation, summarization, generation with specific coding style, and more. The principle behind is the few-shot ability of CodeGeeX. When you provide a few examples as extra prompts in the input, CodeGeeX will imitate what are done by these examples and generate codes accordingly. For example, you can give an example that explains each line of code. Select the code you want to explain, then press ``Alt/Option+t`` to trigger the prompt mode. It will show a list of pre-defined prompt templates and choose the ``explanation`` to generate codes with your input. Magically, the codes will be explained line by line. + +![image](https://lfs.aminer.cn/misc/wangshan/pretrain/codegeex/explanation_python.gif) + +The template of the above example looks like the following, which contains ``[Example code]``, ````, ``[Example code with explanation]`` and ``[Explanation head]``. ```` is where the selected code will be inserted. ```` means the first line of your input (which is used here to ensure the same function will be explained). When you use the prompt mode, CodeGeeX will combine your input with the template and use them all as the input to generate codes. + +```python +# language: Python + +def sum_squares(lst): + sum = 0 + for i in range(len(lst)): + if i % 3 == 0: + lst[i] = lst[i]**2 + elif i % 4 == 0: + lst[i] = lst[i]**3 + sum += lst[i] + return sum + + + +# Explain the code line by line +def sum_squares(lst): + # initialize sum + sum = 0 + # loop through the list + for i in range(len(lst)): + # if the index is a multiple of 3 + if i % 3 == 0: + # square the entry + lst[i] = lst[i]**2 + # if the index is a multiple of 4 + elif i % 4 == 0: + # cube the entry + lst[i] = lst[i]**3 + # add the entry to the sum + sum += lst[i] + # return the sum + return sum + +# Explain the code line by line + +``` + +And here is another example for python docstring generation +```python +def add_binary(a, b): + ''' + Returns the sum of two decimal numbers in binary digits. + + Parameters: + a (int): A decimal integer + b (int): Another decimal integer + + Returns: + binary_sum (str): Binary string of the sum of a and b + ''' + binary_sum = bin(a+b)[2:] + return binary_sum + + +``` + +The templates are fully customizable, you can add your own templates in the extension settings ``Prompt Templates``. ``key`` is the name that you want to show in the list of templates, ``value`` is the path to the template file (``.txt``, ``.py``, ``.h``, etc). Try this feature and write your own templates, you can make the generated codes follow your coding style, generate with a specific function name, or add a specific comment, etc. diff --git a/README_zh.md b/README_zh.md new file mode 100644 index 0000000..b51e232 --- /dev/null +++ b/README_zh.md @@ -0,0 +1,108 @@ +![codegeex_logo](https://lfs.aminer.cn/misc/wangshan/pretrain/codegeex/codegeex_logo.png) + +CodeGeeX是一个具有130亿参数的多编程语言代码生成预训练模型,使用超过二十种编程语言训练得到。基于CodeGeeX开发的插件可以实现通过描述生成代码、补全代码、代码翻译等一系列功能。CodeGeeX同样提供可以定制的**提示模式(Prompt Mode)**,构建专属的编程助手。Happy Coding! + +VS Code插件市场搜索"codegeex"即可免费使用,更多关于CodeGeeX信息请见我们的[主页](https://models.aminer.cn/codegeex/) and [GitHub仓库](https://github.com/THUDM/CodeGeeX)。 + +如使用过程中遇到问题或有任何改进意见,欢迎发送邮件到[codegeex@aminer.cn](mailto:codegeex@aminer.cn)反馈! + +## 基本用法 +安装插件并全局激活CodeGeeX,有以下三种使用模式: + +- **隐匿模式**: 保持CodeGeeX处于激活状态,当您停止输入时,会从当前光标处开始生成(右下角CodeGeeX图标转圈表示正在生成)。 生成完毕之后会以灰色显示,按``Tab``即可插入生成结果。 +- **交互模式**: 按``Ctrl+Enter``激活交互模式,CodeGeeX将生成``X``个候选,并显示在右侧窗口中(``X`` 数量可以在设置的``Candidate Num``中修改)。 点击候选代码上方的``use code``即可插入。 +- **翻译模式**: 选择代码,然后按下``Ctrl+Alt+T``激活翻译模式,CodeGeeX会把该代码翻译成匹配您当前编辑器语言的代码。点击翻译结果上方的``use code``插入。您还可以在设置中选择您希望插入的时候如何处理被翻译的代码,您可以选择注释它们或者覆盖它们。 +- **提示模式**: 选择需要作为输入的代码,按``Alt/Option+t``触发提示模式,会显示预定义模板列表,选择其中一个模板,即可将代码插入到模板中进行生成。 这个模式高度自定义,可以在设置中 ``Prompt Templates``修改或添加模板内容,为模型加入额外的提示。 + +## 隐私声明 + +我们高度尊重用户代码的隐私,代码仅用来辅助编程。在您第一次使用时,我们会询问您是否同意将生成的代码用于研究用途,帮助CodeGeeX变得更好(该选项默认**关闭**)。 + +## 使用指南 + +以下是CodeGeeX几种模式的详细用法: + +###隐匿模式 + +在该模式中,CodeGeeX将在您停止输入时,从光标处开始生成(右下角CodeGeeX图标转圈表示正在生成)。生成完毕之后会以灰色显示,按``Tab``即可插入生成结果。 在生成多个候选的情况下,可以使用``Alt/Option+[`` 或 ``]``在几个候选间进行切换。如果你对现有建议不满意,可以使用``Alt/Option+N``去获得新的候选。可以在设置中改变``Candidate Num``(增加个数会导致生成速度相对变慢)。**注意**:生成总是从当前光标位置开始,如果您在生成结束前移动光标位置,可能会导致一些bugs。我们正在努力使生成速度变得更快以提升用户体验。 + +![image](https://lfs.aminer.cn/misc/wangshan/pretrain/codegeex/bubble_sort_go.gif) + +### 交互模式 + +在该模式中,按``Ctrl+Enter``激活交互模式,CodeGeeX将生成``X``个候选,并显示在右侧窗口中(``X`` 数量可以在设置的``Candidate Num``中修改)。 点击候选代码上方的``use code``即可插入结果到为当前光标位置。 + +![image](https://lfs.aminer.cn/misc/wangshan/pretrain/codegeex/interactive_mode2.gif) + +### 翻译模式 + +在当前的语言的文本编辑器中输入或者粘贴其他语言的代码,您用鼠标选择这些代码,然后按下``Ctrl+Alt+T``激活翻译模式,您根据提示选择该代码的语言,然后CodeGeeX会帮您把该代码翻译成匹配您当前编辑器语言的代码。点击翻译结果上方的``use code``即可插入。您还可以在设置中选择您希望插入的时候如何处理被翻译的代码,您可以选择注释它们或者覆盖它们。 + +![image](https://lfs.aminer.cn/misc/wangshan/pretrain/codegeex/translation_cpp_to_python.gif) + +### 提示模式 + +在该模式中,您可以在输入中添加额外的提示来实现一些有趣的功能,包括并不限于代码解释、概括、以特定风格生成等。该模式的原理是利用了CodeGeeX强大的少样本生成能力。当您在输入中提供一些例子时,CodeGeeX会模仿这些例子并实现相应的功能。比如,您可以自定义模板中提供一段逐行解释代码的例子。选择您想要解释的代码,按``Alt/Option+t``触发提示模式,选择您写好的模板(如``explanation``),CodeGeeX就会解释您输入的代码。以下我们会详细介绍如何制作模板。 + +![image](https://lfs.aminer.cn/misc/wangshan/pretrain/codegeex/explanation_python.gif) + +上述例子中的模板如下图所示,由``[示例代码]``, ````, ``[带解释的示例代码]`` and ``[输出函数头]`` 。````表示您选中的代码将会插入的位置。 ```` 这一句用来保证模型解释的是同一个函数。当使用提示模式时,CodeGeeX会将您选择的代码(插入到部分)和模板代码相结合,一起作为模型的输入。 + +```python +# language: Python + +def sum_squares(lst): + sum = 0 + for i in range(len(lst)): + if i % 3 == 0: + lst[i] = lst[i]**2 + elif i % 4 == 0: + lst[i] = lst[i]**3 + sum += lst[i] + return sum + + + +# Explain the code line by line +def sum_squares(lst): + # initialize sum + sum = 0 + # loop through the list + for i in range(len(lst)): + # if the index is a multiple of 3 + if i % 3 == 0: + # square the entry + lst[i] = lst[i]**2 + # if the index is a multiple of 4 + elif i % 4 == 0: + # cube the entry + lst[i] = lst[i]**3 + # add the entry to the sum + sum += lst[i] + # return the sum + return sum + +# Explain the code line by line + +``` + +以下是另一个Python文档字符串生成的例子,CodeGeeX在您写新函数时会模仿该注释的格式: +```python +def add_binary(a, b): + ''' + Returns the sum of two decimal numbers in binary digits. + + Parameters: + a (int): A decimal integer + b (int): Another decimal integer + + Returns: + binary_sum (str): Binary string of the sum of a and b + ''' + binary_sum = bin(a+b)[2:] + return binary_sum + + +``` + +模板文件是高度自定义化的,您可以将自定义模板添加到插件设置中的``Prompt Templates``中。 ``key``表示模板的名字, ``value``是模板文件的路径(可以是您电脑上的任一路径,``.txt``, ``.py``, ``.h``, 等格式文件均可)。通过该功能,您可以让CodeGeeX生成具有特定风格或功能的代码,快尝试定义自己的专属模板吧! diff --git a/codegeex.woff b/codegeex.woff new file mode 100644 index 0000000..d918356 Binary files /dev/null and b/codegeex.woff differ diff --git a/codegeex_logo.png b/codegeex_logo.png new file mode 100644 index 0000000..f8af939 Binary files /dev/null and b/codegeex_logo.png differ diff --git a/package.json b/package.json new file mode 100644 index 0000000..be36935 --- /dev/null +++ b/package.json @@ -0,0 +1,200 @@ +{ + "publisher": "AMiner", + "name": "codegeex", + "displayName": "CodeGeeX", + "description": "We introduce CodeGeeX, a large-scale multilingual code generative model with 13 billion parameters, pretrained on a large code corpus of more than 20 programming languages.", + "homepage": "https://models.aminer.cn/codegeex", + "repository": { + "type": "git", + "url": "https://github.com/THUDM/CodeGeeX" + }, + "icon": "codegeex_logo.png", + "version": "1.0.8", + "engines": { + "vscode": "^1.68.0" + }, + "categories": [ + "Programming Languages", + "Snippets", + "Machine Learning", + "Other" + ], + "activationEvents": [ + "*" + ], + "main": "./out/extension.js", + "contributes": { + "commands": [ + { + "command": "codegeex.interactive-mode", + "title": "CodeGeeX: Interactive mode" + }, + { + "command": "codegeex.disable-enable", + "title": "Disable or enable CodeGeeX" + }, + { + "command": "codegeex.prompt-mode", + "title": "CodeGeeX: Prompt mode(Experimental)", + "description": "Generate code with prompts by choosing a template" + }, + { + "command": "codegeex.translate-mode", + "title": "CodeGeeX: Translation mode" + } + ], + "configuration": [ + { + "title": "CodeGeeX", + "properties": { + "Codegeex.CandidateNum": { + "type": "string", + "enum": [ + "1 (fast)", + "3 (medium)", + "5 (slow)" + ], + "default": "1 (fast)", + "description": "The candidate list of code. The more the slower the inference." + }, + "Codegeex.PromptTemplates(Experimental)": { + "type": "object", + "description": "Prompts for code generation. There is a few prompts provided by default. Add a mapping here to use your own. For example: {\"myCustomPrompt\": \"/Users/foobar/Downloads/prompt.txt\"}", + "additionalProperties": "string", + "default": { + "explanation": "" + } + }, + "Codegeex.Privacy": { + "type": "boolean", + "description": "Accept sharing the generated code only for research purposes to make CodeGeeX better. Otherwise, the code won't be stored and is only used to assist your programming.", + "default": null + }, + "Codegeex.Translation": { + "type": "string", + "enum": [ + "replace", + "comment" + ], + "default": "comment", + "description": "When inserting code translated, you want to ___ original code" + }, + "Codegeex.DecodingStrategies.temp": { + "type": "number", + "default": 0.8, + "maximum": 1, + "minimum": 0.01, + "description": "Temp controls the randomness of output, range: [0.01, 1]. Higher temperature means more randomness, and the model will return creative results. " + }, + "Codegeex.DecodingStrategies.topp": { + "type": "number", + "default": 0.95, + "maximum": 1, + "minimum": 0, + "description": "Top-p keeps the candidate tokens whose probabilities sum to p, range [0, 1]. Top-p=0 means disabled." + }, + "Codegeex.DecodingStrategies.topk": { + "type": "number", + "default": 0, + "maximum": 40, + "minimum": 0, + "description": "Top-k keeps the k candidate tokens with the highest probabilities, range [0, 40]. Top-k=0 means disabled." + }, + "Codegeex.DisabledFor": { + "type": "object", + "additionalProperties": "string", + "default": { + }, + "description": "DisabledFor is a list of specific languages that will be disabled temporarily for the extension, you can set manully language* as the key and then set the value true to disable a language or false to re-enable it in workspace settings (this will need to restart vscode) or do this by clicking icon in the status bar.\n*language should be a valid vscode language id, like python, shellscript, csharp, objective-cpp and etc. You can click the language option in status bar to find the list and language ids are in the parentheses." + }, + "Codegeex.GenerationPreference":{ + "type":"string", + "enum": [ + "automatic", + "line by line" + ], + "description": "You can choose the preference when generating code. If you choose automatic, the extension will generate whether a block or a line depending on your input. If you choose line by line, the extension will generate a code line, a comment line or a code line and a comment line for you each time.", + "default":"automatic" + }, + "Codegeex.EnableExtension":{ + "type":"boolean", + "default":true + } + } + } + ], + "keybindings": [ + { + "command": "codegeex.interactive-mode", + "key": "ctrl+enter", + "mac": "ctrl+enter" + }, + { + "command": "codegeex.prompt-mode", + "key": "Alt+T" + }, + { + "command": "codegeex.new-completions", + "key": "Alt+N" + }, + { + "command": "codegeex.translate-mode", + "key": "Alt+ctrl+T" + }, + { + "command": "codegeex.welcome-page", + "key": "Alt+ctrl+W" + } + ], + "menus": { + "editor/context": [ + { + "command": "codegeex.translate-mode", + "group": "group1@1" + }, + { + "command": "codegeex.interactive-mode", + "group": "group1@2" + }, + { + "command": "codegeex.prompt-mode", + "group": "group1@3" + } + ] + }, + "icons": { + "codegeex-dark": { + "description": "CodeGeeX icon", + "default": { + "fontPath": "./codegeex.woff", + "fontCharacter": "\\E001" + } + } + } + }, + "scripts": { + "vscode:prepublish": "npm run compile", + "compile": "tsc -p ./", + "watch": "tsc -watch -p ./", + "pretest": "npm run compile && npm run lint", + "lint": "eslint src --ext ts", + "test": "node ./out/test/runTest.js" + }, + "devDependencies": { + "@types/glob": "^7.2.0", + "@types/mocha": "^9.1.1", + "@types/node": "16.x", + "@types/vscode": "^1.68.0", + "@typescript-eslint/eslint-plugin": "^5.31.0", + "@typescript-eslint/parser": "^5.31.0", + "@vscode/test-electron": "^2.1.5", + "eslint": "^8.20.0", + "glob": "^8.0.3", + "mocha": "^10.0.0", + "typescript": "^4.7.4" + }, + "dependencies": { + "axios": "^0.24.0", + "vsce": "^1.100.2" + } +} diff --git a/src/config.ts b/src/config.ts new file mode 100644 index 0000000..a04a401 --- /dev/null +++ b/src/config.ts @@ -0,0 +1,21 @@ +const CSConfig = { + SEARCH_PHARSE_END: [ + ".", + ",", + "{", + "(", + " ", + "-", + "_", + "+", + "-", + "*", + "=", + "/", + "?", + "<", + ">", + ], +}; + +export default CSConfig; diff --git a/src/disableEnable.ts b/src/disableEnable.ts new file mode 100644 index 0000000..605f57b --- /dev/null +++ b/src/disableEnable.ts @@ -0,0 +1,87 @@ +import * as vscode from "vscode"; +import { disabledFor, enableExtension } from "./param/configures"; +import changeIconColor from "./utils/changeIconColor"; +import { updateStatusBarItem } from "./utils/updateStatusBarItem"; + +let g_isEnable = enableExtension; +export default async function disableEnable( + myStatusBarItem: vscode.StatusBarItem, + g_isLoading: boolean, + originalColor: string | vscode.ThemeColor | undefined +) { + if (g_isEnable) { + const lang = vscode.window.activeTextEditor?.document.languageId || ""; + if ( + (disabledFor as any)[lang] || + (disabledFor as any)[lang] === "true" + ) { + const answer = await vscode.window.showInformationMessage( + "Would you like to disable CodeGeeX?", + "Disable Globally", + `Enable for ${lang}` + ); + if (answer === "Disable Globally") { + // Run function + changeIconColor(false, myStatusBarItem, originalColor); + g_isEnable = false; + const configuration = vscode.workspace.getConfiguration( + "Codegeex", + undefined + ); + configuration.update("EnableExtension", false); + console.log(configuration); + } + if (answer === `Enable for ${lang}`) { + // Run function + const configuration = vscode.workspace.getConfiguration( + "Codegeex", + undefined + ); + (disabledFor as any)[lang] = false; + configuration.update("DisabledFor", disabledFor); + } + } else { + const answer = await vscode.window.showInformationMessage( + "Would you like to disable CodeGeeX?", + "Disable Globally", + `Disable for ${lang}` + ); + + if (answer === "Disable Globally") { + // Run function + changeIconColor(false, myStatusBarItem, originalColor); + const configuration = vscode.workspace.getConfiguration( + "Codegeex", + undefined + ); + g_isEnable = false; + configuration.update("EnableExtension", false); + } + if (answer === `Disable for ${lang}`) { + // Run function + const configuration = vscode.workspace.getConfiguration( + "Codegeex", + undefined + ); + (disabledFor as any)[lang] = true; + configuration.update("DisabledFor", disabledFor); + updateStatusBarItem(myStatusBarItem, g_isLoading, false, ""); + } + } + } else { + const answer = await vscode.window.showInformationMessage( + "Would you like to enable CodeGeeX?", + "Enable Globally" + ); + if (answer === "Enable Globally") { + // Run function + changeIconColor(true, myStatusBarItem, originalColor); + const configuration = vscode.workspace.getConfiguration( + "Codegeex", + undefined + ); + g_isEnable = true; + configuration.update("EnableExtension", true); + } + } +} diff --git a/src/extension.ts b/src/extension.ts new file mode 100644 index 0000000..8951f88 --- /dev/null +++ b/src/extension.ts @@ -0,0 +1,151 @@ +process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0"; +import * as vscode from "vscode"; +import { myScheme } from "./param/constparams"; +import { checkPrivacy } from "./utils/checkPrivacy"; +import { getEndData, getOpenExtensionData } from "./utils/statisticFunc"; +import { updateStatusBarItem } from "./utils/updateStatusBarItem"; +import { generateWithPromptMode } from "./mode/generationWithPrompMode"; +import welcomePage from "./welcomePage"; +import generationWithTranslationMode from "./mode/generationWithTranslationMode"; +import { codelensProvider } from "./provider/codelensProvider"; +import generationWithInteractiveMode from "./mode/generationWithInteractiveMode"; +import chooseCandidate from "./utils/chooseCandidate"; +import disableEnable from "./disableEnable"; +import { textDocumentProvider } from "./provider/textDocumentProvider"; +import inlineCompletionProvider from "./provider/inlineCompletionProvider"; +import { enableExtension } from "./param/configures"; +import changeIconColor from "./utils/changeIconColor"; + +let g_isLoading = false; +let originalColor: string | vscode.ThemeColor | undefined; +let myStatusBarItem: vscode.StatusBarItem; + +export async function activate(context: vscode.ExtensionContext) { + console.log('Congratulations, your extension "CodeGeeX" is now active!'); + await getOpenExtensionData(); + context.subscriptions.push( + vscode.commands.registerCommand("codegeex.welcome-page", async () => { + await welcomePage(context); + }) + ); + if (vscode.env.isNewAppInstall) { + vscode.commands.executeCommand("codegeex.welcome-page"); + } + checkPrivacy(); + let targetEditor: vscode.TextEditor; + + //subscribe interactive-mode command + context.subscriptions.push( + vscode.commands.registerCommand( + "codegeex.interactive-mode", + async () => { + const editor = vscode.window.activeTextEditor; + if (!editor) { + vscode.window.showInformationMessage( + "Please open a file first to use CodeGeeX." + ); + return; + } + targetEditor = editor; + generationWithInteractiveMode( + editor, + myStatusBarItem, + g_isLoading + ); + } + ) + ); + //subscribe translation-mode command + context.subscriptions.push( + vscode.commands.registerCommand("codegeex.translate-mode", async () => { + const editor = vscode.window.activeTextEditor; + if (!editor) { + vscode.window.showInformationMessage( + "Please open a file first to use CodeGeeX translation." + ); + return; + } + targetEditor = editor; + generationWithTranslationMode( + myStatusBarItem, + g_isLoading, + targetEditor + ); + }) + ); + context.subscriptions.push( + vscode.commands.registerCommand("codegeex.prompt-mode", () => { + const editor = vscode.window.activeTextEditor; + if (!editor) { + vscode.window.showInformationMessage( + "Please open a file first to use CodeGeeX translation." + ); + return; + } + generateWithPromptMode(myStatusBarItem, g_isLoading, editor); + }) + ); + context.subscriptions.push( + vscode.workspace.registerTextDocumentContentProvider( + myScheme, + textDocumentProvider + ) + ); + context.subscriptions.push( + vscode.commands.registerCommand( + "CodeGeeX.chooseCandidate", + (fn, mode, commandid) => { + chooseCandidate(targetEditor, fn, mode, commandid); + } + ) + ); + + context.subscriptions.push( + vscode.languages.registerCodeLensProvider( + { scheme: myScheme }, + codelensProvider + ) + ); + + const statusBarItemCommandId = "codegeex.disable-enable"; + context.subscriptions.push( + vscode.commands.registerCommand("codegeex.disable-enable", () => { + disableEnable(myStatusBarItem, g_isLoading, originalColor); + }) + ); + // create a new status bar item that we can now manage + myStatusBarItem = vscode.window.createStatusBarItem( + vscode.StatusBarAlignment.Right, + 100 + ); + myStatusBarItem.command = statusBarItemCommandId; + context.subscriptions.push(myStatusBarItem); + //initialiser statusbar + changeIconColor(enableExtension, myStatusBarItem, originalColor); + updateStatusBarItem(myStatusBarItem, g_isLoading, false, ""); + + //command after insert a suggestion in stealth mode + context.subscriptions.push( + vscode.commands.registerCommand( + "verifyInsertion", + (id, completions, acceptItem) => { + getEndData(id, "", "Yes", acceptItem, completions); + } + ) + ); + context.subscriptions.push( + vscode.languages.registerInlineCompletionItemProvider( + { pattern: "**" }, + inlineCompletionProvider(g_isLoading, myStatusBarItem, false) + ) + ); + context.subscriptions.push( + vscode.commands.registerCommand("codegeex.new-completions", () => { + vscode.languages.registerInlineCompletionItemProvider( + { pattern: "**" }, + inlineCompletionProvider(g_isLoading, myStatusBarItem, true) + ); + }) + ); +} +export function deactivate() {} diff --git a/src/messages.ts b/src/messages.ts new file mode 100644 index 0000000..2d5452b --- /dev/null +++ b/src/messages.ts @@ -0,0 +1,20 @@ +import { window } from "vscode"; + +export type MessageOptions = { + messageId: string; + messageText: string; + buttonText: string; + action: () => void; +}; + +export default async function showMessage( + options: MessageOptions +): Promise { + await window + .showInformationMessage(options.messageText, options.buttonText) + .then((selected) => { + if (selected === options.buttonText) { + options.action(); + } + }); +} diff --git a/src/mode/generationWithInteractiveMode.ts b/src/mode/generationWithInteractiveMode.ts new file mode 100644 index 0000000..f02aa0a --- /dev/null +++ b/src/mode/generationWithInteractiveMode.ts @@ -0,0 +1,44 @@ +import * as vscode from "vscode"; +import { codegeexCodeGen } from "../utils/codegeexCodeGen"; +import { updateStatusBarItem } from "../utils/updateStatusBarItem"; + +const addSignal = "<|add|>"; +const andSignal = "<|and|>"; +const hash = "<|hash|>"; + +export default async function generationWithInteractiveMode( + editor: vscode.TextEditor, + myStatusBarItem: vscode.StatusBarItem, + g_isLoading: boolean +) { + const document = editor.document; + let selection: vscode.Selection; + + const cursorPosition = editor.selection.active; + selection = new vscode.Selection( + 0, + 0, + cursorPosition.line, + cursorPosition.character + ); + let code_block = document.getText(selection); + code_block = code_block + .replaceAll("#", hash) + .replaceAll("+", addSignal) + .replaceAll("&", andSignal); + console.log("code_block", code_block); + updateStatusBarItem(myStatusBarItem, g_isLoading, true, ""); + await codegeexCodeGen(code_block.trim()) + .then(() => + updateStatusBarItem(myStatusBarItem, g_isLoading, false, "Done") + ) + .catch((err) => { + console.log(err); + updateStatusBarItem( + myStatusBarItem, + g_isLoading, + false, + "No Suggestion" + ); + }); +} diff --git a/src/mode/generationWithPrompMode.ts b/src/mode/generationWithPrompMode.ts new file mode 100644 index 0000000..add6e9b --- /dev/null +++ b/src/mode/generationWithPrompMode.ts @@ -0,0 +1,71 @@ +import * as vscode from "vscode"; +import { templateExplanation } from "../templates/explanation"; +import codeGenByTemplate from "../utils/codeGenByTemplate"; +import readTemplate from "../utils/readTemplate"; + +export async function generateWithPromptMode( + myStatusBarItem: vscode.StatusBarItem, + g_isLoading: boolean, + editor: vscode.TextEditor +) { + var items: vscode.QuickPickItem[] = []; + + let currentDocument = editor.document; + let configuration = vscode.workspace.getConfiguration( + "", + currentDocument?.uri + ); + let templates = configuration.get("Codegeex.PromptTemplates", {}); + const keys = Object.keys(templates); + + items.push({ + label: "explanation", + description: "Explain the selection line by line", + }); + let custom_prompts = {}; + for (let key of keys) { + if (key != "explanation") { + items.push({ label: key, description: "" }); + // @ts-ignore + custom_prompts[key] = await readTemplate(templates[key]); + } + } + console.log(custom_prompts); + + vscode.window.showQuickPick(items).then((selection) => { + if (!selection) { + return; + } + let e = vscode.window.activeTextEditor; + let d = e?.document; + let sel = e?.selections; + + switch (selection.label) { + case "explanation": + codeGenByTemplate( + editor, + templateExplanation, + myStatusBarItem, + g_isLoading + ); + break; + + default: + if (keys.indexOf(selection.label) !== -1) { + // @ts-ignore + let templateStr = custom_prompts[selection.label]; + console.log("templateStr:"); + console.log(templateStr); + codeGenByTemplate( + editor, + templateStr, + myStatusBarItem, + g_isLoading + ); + } else { + // console.log("no selection") + } + break; + } + }); +} diff --git a/src/mode/generationWithTranslationMode.ts b/src/mode/generationWithTranslationMode.ts new file mode 100644 index 0000000..c433083 --- /dev/null +++ b/src/mode/generationWithTranslationMode.ts @@ -0,0 +1,81 @@ +import * as vscode from "vscode"; +import { hash, languageList } from "../param/constparams"; +import { codegeexCodeTranslation } from "../utils/codegeexCodeTranslation"; +import { getCommentSignal } from "../utils/commentCode"; +import { getCodeTranslation } from "../utils/getCodeTranslation"; +import getDocumentLanguage from "../utils/getDocumentLanguage"; +import { showQuickPick } from "../utils/showQuickPick"; +import { getStartData } from "../utils/statisticFunc"; +import { updateStatusBarItem } from "../utils/updateStatusBarItem"; + +export default async function generationWithTranslationMode( + myStatusBarItem: vscode.StatusBarItem, + g_isLoading: boolean, + targetEditor: vscode.TextEditor +) { + let dstLang = getDocumentLanguage(targetEditor); + const document = targetEditor.document; + let selection: vscode.Selection; + + const cursorPosition = targetEditor.selection.active; + const anchorPosition = targetEditor.selection.anchor; + selection = new vscode.Selection(anchorPosition, cursorPosition); + //vscode.commands.executeCommand('editor.action.addCommentLine') + + let text = document.getText(selection); + if (text && text.trim().length > 0) { + let srcLang = + (await showQuickPick( + languageList, + "Please choose the language to be translated." + )) || ""; + let translation; + if ( + languageList.indexOf(srcLang) >= 0 && + languageList.indexOf(dstLang) >= 0 + ) { + updateStatusBarItem( + myStatusBarItem, + g_isLoading, + true, + " Translating" + ); + let commandid = getStartData( + text, + text, + `${srcLang}->${dstLang}`, + "translation" + ); + translation = await getCodeTranslation(text, srcLang, dstLang).then( + async (res) => { + await codegeexCodeTranslation( + dstLang, + res.translation[0].replaceAll("#", hash), + await commandid + ).then(() => { + updateStatusBarItem( + myStatusBarItem, + g_isLoading, + false, + " Done" + ); + }); + } + ); + } + if (languageList.indexOf(srcLang) < 0) { + vscode.window.showInformationMessage( + "Please choose the language to be translated." + ); + } + if (languageList.indexOf(dstLang) < 0) { + vscode.window.showInformationMessage( + "Sorry, the target language is not supported." + ); + } + } else { + vscode.window.showInformationMessage( + "Please select some code to be translated" + ); + } +} diff --git a/src/param/configures.ts b/src/param/configures.ts new file mode 100644 index 0000000..d80d247 --- /dev/null +++ b/src/param/configures.ts @@ -0,0 +1,65 @@ +import { workspace } from "vscode"; + +const configuration = workspace.getConfiguration("Codegeex", undefined); +console.log(configuration); + +export const generationPreference = configuration.get("GenerationPreference"); +export const disabledFor = configuration.get("DisabledFor", new Object()); + +export const disabledLangs = () => { + const disabledFor = configuration.get("DisabledFor", new Object()); + let disabledLangs = []; + const keys = Object.keys(disabledFor); + for (let i = 0; i < keys.length; i++) { + let key = keys[i]; + if ( + (disabledFor as any)[key] === true || + (disabledFor as any)[key] === "true" + ) { + disabledLangs.push(key); + } + } + return disabledLangs; +}; + +const defaultConfig = { + temp: 0.8, + topp: 0.95, + topk: 0, +}; +const modelConfig = configuration.get("DecodingStrategies", defaultConfig); +export const temp = modelConfig.temp; +export const topk = modelConfig.topk; +export const topp = modelConfig.topp; +//get number of candidates +const candidateNum_str = String(configuration.get("CandidateNum", "1")); +export const candidateNum = parseInt(candidateNum_str); +export const needGuide = configuration.get("NeedGuide"); +export const translationInsertMode = configuration.get("Translation"); +export const enableExtension = configuration.get("EnableExtension", true); +export const controls = { + interactiveMode: { + mac: "Control + Enter", + win: "Ctrl + Enter", + }, + promptMode: { + mac: "Option + T", + win: "Ctrl + T", + }, + translationMode: { + mac: "Option + Control + T", + win: "Alt + Ctrl + T", + }, + nextSuggestion: { + mac: "Option + ]", + win: "Alt + ]", + }, + previousSuggestion: { + mac: "Option + [", + win: "Alt + [", + }, + newSuggestion: { + mac: "Option + N", + win: "Alt + N", + }, +}; diff --git a/src/param/constparams.ts b/src/param/constparams.ts new file mode 100644 index 0000000..6650026 --- /dev/null +++ b/src/param/constparams.ts @@ -0,0 +1,44 @@ +import { tianqiApiKey, tianqiApiSecret } from "../localconfig"; + +export const extensionId = "aminer.codegeex"; +export const extensionVersion = "1.0.7"; +export const myScheme = "codegeex"; + +//configure the key and Secret of tianqi +export const apiKey = tianqiApiKey; +export const apiSecret = tianqiApiSecret; + +//api to do the statistics of data +export const apiHerf = + process.env.NODE_ENV === "development" + ? "http://10.50.74.216:32621" + : "https://maas.aminer.cn"; + +//language accepted by the model +export const languageList = [ + "C++", + "C", + "C#", + "Cuda", + "Objective-C", + "Objective-C++", + "Python", + "Java", + "TeX", + "HTML", + "PHP", + "JavaScript", + "TypeScript", + "Go", + "Shell", + "Rust", + "CSS", + "SQL", + "R", +]; + +//const to replace specfic characters +export const comment = "<|comment|>"; +export const addSignal = "<|add|>"; +export const andSignal = "<|and|>"; +export const hash = "<|hash|>"; diff --git a/src/proposedAPI.ts b/src/proposedAPI.ts new file mode 100644 index 0000000..1dd334d --- /dev/null +++ b/src/proposedAPI.ts @@ -0,0 +1,74 @@ +import { promises as fs } from "fs"; +import * as vscode from "vscode"; +import * as path from "path"; +import * as os from "os"; +import showMessage from "./messages"; + +const EXTENSION_ID = "Aminer.cogcode"; +const ARGV_FILE_NAME = "argv.json"; +const PRODUCT_FILE_NAME = "product.json"; +const PRODUCT_FILE_PATH = path.join(vscode.env.appRoot, PRODUCT_FILE_NAME); +const ENABLE_PROPOSED_API = [ + "", + ` "enable-proposed-api": ["${EXTENSION_ID}"]`, + "}", +]; + +export default async function enableProposed(): Promise { + return handleProposed().catch((error) => { + console.error("failed to enable proposedAPI", error); + return false; + }); +} + +async function getDataFolderName(): Promise { + const data = await fs.readFile(PRODUCT_FILE_PATH); + const file = JSON.parse(data.toString("utf8")) as { + dataFolderName?: string; + }; + return file?.dataFolderName; +} + +function getArgvResource(dataFolderName: string): string { + const vscodePortable = process.env.VSCODE_PORTABLE; + if (vscodePortable) { + return path.join(vscodePortable, ARGV_FILE_NAME); + } + + return path.join(os.homedir(), dataFolderName, ARGV_FILE_NAME); +} +async function handleProposed(): Promise { + const dataFolderName = await getDataFolderName(); + + if (dataFolderName) { + const argvResource = getArgvResource(dataFolderName); + const argvString = (await fs.readFile(argvResource)).toString(); + + if (argvString.includes(`${EXTENSION_ID}`)) { + return true; + } + + const modifiedArgvString = modifyArgvFileContent(argvString); + await fs.writeFile(argvResource, Buffer.from(modifiedArgvString)); + askForReload(); + } + return false; +} + +function askForReload() { + void showMessage({ + messageId: "inline-update", + messageText: `Please reload the window for the CogCode inline completions to take effect.`, + buttonText: "Reload", + action: () => + void vscode.commands.executeCommand( + "workbench.action.reloadWindow" + ), + }); +} + +function modifyArgvFileContent(argvString: string) { + return argvString + .substring(0, argvString.length - 2) + .concat(",\n", ENABLE_PROPOSED_API.join("\n")); +} diff --git a/src/provider/codelensProvider.ts b/src/provider/codelensProvider.ts new file mode 100644 index 0000000..510e0e8 --- /dev/null +++ b/src/provider/codelensProvider.ts @@ -0,0 +1,31 @@ +import * as vscode from "vscode"; + +export const codelensProvider = new (class { + codelenses: vscode.CodeLens[]; + constructor() { + this.codelenses = []; + } + addEl(lineNum: number, text: string, commandid: string, mode?: string) { + let range; + console.log(text[0] === "\n"); + + range = new vscode.Range(lineNum, 0, lineNum, 0); + + this.codelenses.push( + new vscode.CodeLens(range, { + title: "Use code", + command: "CodeGeeX.chooseCandidate", + arguments: [text, mode, commandid], + tooltip: "Choose this snippet", + }) + ); + console.log(this.codelenses); + } + clearEls() { + this.codelenses = []; + } + + provideCodeLenses() { + return this.codelenses; + } +})(); diff --git a/src/provider/inlineCompletionProvider.ts b/src/provider/inlineCompletionProvider.ts new file mode 100644 index 0000000..e0ec8d1 --- /dev/null +++ b/src/provider/inlineCompletionProvider.ts @@ -0,0 +1,317 @@ +import * as vscode from "vscode"; +import { candidateNum, disabledFor } from "../param/configures"; +import { apiKey, apiSecret } from "../param/constparams"; +import { Trie } from "../trie"; +import { getCodeCompletions } from "../utils/getCodeCompletions"; +import getDocumentLanguage from "../utils/getDocumentLanguage"; +import { updateStatusBarItem } from "../utils/updateStatusBarItem"; + +let lastRequest = null; +let trie = new Trie([]); +let prompts: string[] = []; +let someTrackingIdCounter = 0; + +interface MyInlineCompletionItem extends vscode.InlineCompletionItem { + trackingId: number; +} +export default function inlineCompletionProvider( + g_isLoading: boolean, + myStatusBarItem: vscode.StatusBarItem, + reGetCompletions: boolean +) { + const provider: vscode.InlineCompletionItemProvider = { + provideInlineCompletionItems: async ( + document, + position, + context, + token + ) => { + console.log("new event!"); + const enableExtension = vscode.workspace + .getConfiguration("Codegeex", undefined) + .get("EnableExtension", undefined); + if (!enableExtension) { + return; + } + const editor = vscode.window.activeTextEditor; + if (!editor) { + vscode.window.showInformationMessage( + "Please open a file first to use CodeGeeX." + ); + return; + } + let selection: vscode.Selection; + const languageId = editor.document.languageId || "undefined"; + console.log(disabledFor); + console.log( + vscode.workspace.getConfiguration("Codegeex.DisabledFor") + ); + if ( + (disabledFor as any)[languageId] === true || + (disabledFor as any)[languageId] === "true" || + !enableExtension + ) { + return; + } + const cursorPosition = editor.selection.active; + selection = new vscode.Selection( + 0, + 0, + cursorPosition.line, + cursorPosition.character + ); + let textBeforeCursor = document.getText(selection); + if(vscode.window.activeNotebookEditor){ + const cells = vscode.window.activeNotebookEditor.notebook.getCells(); + const currentCell = vscode.window.activeNotebookEditor.selection.start; + let str = '' + for(let i = 0;i(); + let lastLine = document.lineAt(document.lineCount - 1); + for ( + let i = 0; + i < + Math.min( + Math.min(completions.length, candidateNum) + 1, + completions.length + ); + i++ + ) { + let insertText = useTrim + ? completions[i].replace( + textBeforeCursor.trimEnd(), + "" + ) + : completions[i].replace(textBeforeCursor, ""); + console.log(insertText); + let needRequest = ["", "\n", "\n\n"]; + if ( + needRequest.includes(insertText) || + insertText.trim() === "" + ) { + continue; + } + if (useTrim) { + const lines = insertText.split("\n"); + let nonNullIndex = 0; + while (lines[nonNullIndex].trim() === "") { + nonNullIndex++; + } + console.log(nonNullIndex); + let newInsertText = ""; + for ( + let j = nonNullIndex; + j < lines.length; + j++ + ) { + newInsertText += lines[j]; + if (j !== lines.length - 1) { + newInsertText += "\n"; + } + } + console.log(newInsertText); + if ( + textBeforeCursor[ + textBeforeCursor.length - 1 + ] === "\n" || + nonNullIndex === 0 + ) { + insertText = newInsertText; + } else { + insertText = "\n" + newInsertText; + } + } + + items.push({ + insertText, + range: new vscode.Range( + position.translate(0, completions.length), + position + ), + // range: new vscode.Range(endPosition.translate(0, completions.length), endPosition), + trackingId: someTrackingIdCounter++, + }); + if (useTrim) { + trie.addWord( + textBeforeCursor.trimEnd() + insertText + ); + } else { + trie.addWord(textBeforeCursor + insertText); + } + } + if (items.length === 0) { + continue; + } else { + updateStatusBarItem( + myStatusBarItem, + g_isLoading, + false, + " Done" + ); + return items; + } + } + } + } + if (enableExtension && textBeforeCursor.length > 8) { + console.log("try to get"); + let requestId = new Date().getTime(); + lastRequest = requestId; + await new Promise((f) => setTimeout(f, 500)); + if (lastRequest !== requestId) { + return { items: [] }; + } + console.log("real to get"); + console.log("new command"); + let rs; + let lang = ""; + const configuration = vscode.workspace.getConfiguration( + "Codegeex", + undefined + ); + const num_str = String(configuration.get("CandidateNum", "1")); + const num = parseInt(num_str); + try { + if (editor) { + lang = getDocumentLanguage(editor); + } + updateStatusBarItem(myStatusBarItem, g_isLoading, true, ""); + rs = await getCodeCompletions( + textBeforeCursor, + num, + lang, + apiKey, + apiSecret, + "inlinecompletion" + ); + } catch (err) { + if (err) { + console.log("intended error"); + console.log(err); + } + updateStatusBarItem( + myStatusBarItem, + g_isLoading, + false, + " No Suggestion" + ); + return { items: [] }; + } + if (rs === null) { + updateStatusBarItem( + myStatusBarItem, + g_isLoading, + false, + " No Suggestion" + ); + return { items: [] }; + } + prompts.push(textBeforeCursor); + // Add the generated code to the inline suggestion list + let items = new Array(); + let cursorPosition = editor.selection.active; + for (let i = 0; i < rs.completions.length; i++) { + //rs.completions[i].replace('from\'','from \'') + console.log("test", rs.completions[i]); + items.push({ + insertText: rs.completions[i], + // range: new vscode.Range(endPosition.translate(0, rs.completions.length), endPosition), + range: new vscode.Range( + cursorPosition.translate(0, rs.completions.length), + cursorPosition + ), + trackingId: someTrackingIdCounter++, + }); + trie.addWord(textBeforeCursor + rs.completions[i]); + } + for (let j = 0; j < items.length; j++) { + items[j].command = { + command: "verifyInsertion", + title: "Verify Insertion", + arguments: [ + rs.commandid, + rs.completions, + items[j].insertText, + ], + }; + } + if (rs.completions.length === 0) { + updateStatusBarItem( + myStatusBarItem, + g_isLoading, + false, + " No Suggestion" + ); + } else { + updateStatusBarItem( + myStatusBarItem, + g_isLoading, + false, + " Done" + ); + } + return items; + } + updateStatusBarItem( + myStatusBarItem, + g_isLoading, + false, + " No Suggestion" + ); + return { items: [] }; + }, + }; + return provider; +} diff --git a/src/provider/textDocumentProvider.ts b/src/provider/textDocumentProvider.ts new file mode 100644 index 0000000..cf7c3c4 --- /dev/null +++ b/src/provider/textDocumentProvider.ts @@ -0,0 +1,117 @@ +import * as vscode from "vscode"; +import * as https from "https"; + +import { codelensProvider } from "./codelensProvider"; +import { candidateNum } from "../param/configures"; +import { + addSignal, + andSignal, + apiKey, + apiSecret, + comment, + hash, +} from "../param/constparams"; +import { getCommentSignal } from "../utils/commentCode"; +import getDocumentLanguage from "../utils/getDocumentLanguage"; +import { getGPTCode } from "../utils/getGPTCode"; +import { getCodeCompletions } from "../utils/getCodeCompletions"; + +let myStatusBarItem: vscode.StatusBarItem; +let g_isLoading: boolean; + +export const textDocumentProvider = new (class { + async provideTextDocumentContent(uri: vscode.Uri) { + const params = new URLSearchParams(uri.query); + if (params.get("loading") === "true") { + return `/* CodeGeeX is generating ... */\n`; + } + const mode = params.get("mode"); + + if (mode === "translation") { + let transResult = params.get("translation_res") || ""; + transResult = transResult + .replaceAll(addSignal, "+") + .replaceAll(andSignal, "&"); + console.log("transResult", transResult); + const editor = vscode.window.activeTextEditor; + if (!editor) { + vscode.window.showInformationMessage( + "Please open a file first to use CodeGeeX." + ); + return; + } + codelensProvider.clearEls(); + let commandid = params.get("commandid") || ""; + let commentSignal = getCommentSignal(editor.document.languageId); + transResult = transResult + .replaceAll(hash, "#") + .replaceAll(comment, commentSignal.line || "#"); + codelensProvider.addEl(0, transResult, commandid, "translation"); + return transResult; + } else { + let code_block = params.get("code_block") ?? ""; + + try { + code_block = code_block + .replaceAll(hash, "#") + .replaceAll(addSignal, "+") + .replaceAll(andSignal, "&"); + // 'lang': 'Python', + if (code_block.length > 1200) { + code_block = code_block.slice(code_block.length - 1200); + } + const editor = vscode.window.activeTextEditor; + if (!editor) { + vscode.window.showInformationMessage( + "Please open a file first to use CodeGeeX." + ); + return; + } + let payload = {}; + const num = candidateNum; + let lang = getDocumentLanguage(editor); + if (lang.length == 0) { + payload = { + prompt: code_block, + n: num, + apikey: apiKey, + apisecret: apiSecret, + }; + } else { + payload = { + lang: lang, + prompt: code_block, + n: num, + apikey: apiKey, + apisecret: apiSecret, + }; + } + // } + const agent = new https.Agent({ + rejectUnauthorized: false, + }); + const { commandid, completions } = await getCodeCompletions( + code_block, + num, + lang, + apiKey, + apiSecret, + "interactive" + ); + if (completions.length > 0) { + return getGPTCode( + completions, + commandid, + myStatusBarItem, + g_isLoading + ); + } else { + return "No result to show"; + } + } catch (err) { + console.log("Error sending request", err); + return "There was an error sending the request\n" + err; + } + } + } +})(); diff --git a/src/templates/docstring.ts b/src/templates/docstring.ts new file mode 100644 index 0000000..7bed7db --- /dev/null +++ b/src/templates/docstring.ts @@ -0,0 +1,17 @@ +export const templateDocstring = ` +def add_binary(a, b): + ''' + Returns the sum of two decimal numbers in binary digits. + + Parameters: + a (int): A decimal integer + b (int): Another decimal integer + + Returns: + binary_sum (str): Binary string of the sum of a and b + ''' + binary_sum = bin(a+b)[2:] + return binary_sum + + +`; diff --git a/src/templates/explanation.ts b/src/templates/explanation.ts new file mode 100644 index 0000000..8873e70 --- /dev/null +++ b/src/templates/explanation.ts @@ -0,0 +1,36 @@ +export const templateExplanation = ` +# language: Python + +def sum_squares(lst): + sum = 0 + for i in range(len(lst)): + if i % 3 == 0: + lst[i] = lst[i]**2 + elif i % 4 == 0: + lst[i] = lst[i]**3 + sum += lst[i] + return sum + + + +# Explain the code line by line +def sum_squares(lst): + # initialize sum + sum = 0 + # loop through the list + for i in range(len(lst)): + # if the index is a multiple of 3 + if i % 3 == 0: + # square the entry + lst[i] = lst[i]**2 + # if the index is a multiple of 4 + elif i % 4 == 0: + # cube the entry + lst[i] = lst[i]**3 + # add the entry to the sum + sum += lst[i] + # return the sum + return sum + +# Explain the code line by line +`; diff --git a/src/test/runTest.js b/src/test/runTest.js new file mode 100644 index 0000000..07340e1 --- /dev/null +++ b/src/test/runTest.js @@ -0,0 +1,22 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +const path = require("path"); +const test_electron_1 = require("@vscode/test-electron"); +async function main() { + try { + // The folder containing the Extension Manifest package.json + // Passed to `--extensionDevelopmentPath` + const extensionDevelopmentPath = path.resolve(__dirname, '../../'); + // The path to test runner + // Passed to --extensionTestsPath + const extensionTestsPath = path.resolve(__dirname, './suite/index'); + // Download VS Code, unzip it and run the integration test + await (0, test_electron_1.runTests)({ extensionDevelopmentPath, extensionTestsPath }); + } + catch (err) { + console.error('Failed to run tests'); + process.exit(1); + } +} +main(); +//# sourceMappingURL=runTest.js.map \ No newline at end of file diff --git a/src/test/runTest.ts b/src/test/runTest.ts new file mode 100644 index 0000000..27b3ceb --- /dev/null +++ b/src/test/runTest.ts @@ -0,0 +1,23 @@ +import * as path from 'path'; + +import { runTests } from '@vscode/test-electron'; + +async function main() { + try { + // The folder containing the Extension Manifest package.json + // Passed to `--extensionDevelopmentPath` + const extensionDevelopmentPath = path.resolve(__dirname, '../../'); + + // The path to test runner + // Passed to --extensionTestsPath + const extensionTestsPath = path.resolve(__dirname, './suite/index'); + + // Download VS Code, unzip it and run the integration test + await runTests({ extensionDevelopmentPath, extensionTestsPath }); + } catch (err) { + console.error('Failed to run tests'); + process.exit(1); + } +} + +main(); diff --git a/src/test/suite/extension.test.js b/src/test/suite/extension.test.js new file mode 100644 index 0000000..d88089e --- /dev/null +++ b/src/test/suite/extension.test.js @@ -0,0 +1,15 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +const assert = require("assert"); +// You can import and use all API from the 'vscode' module +// as well as import your extension to test it +const vscode = require("vscode"); +// import * as myExtension from '../../extension'; +suite('Extension Test Suite', () => { + vscode.window.showInformationMessage('Start all tests.'); + test('Sample test', () => { + assert.strictEqual(-1, [1, 2, 3].indexOf(5)); + assert.strictEqual(-1, [1, 2, 3].indexOf(0)); + }); +}); +//# sourceMappingURL=extension.test.js.map \ No newline at end of file diff --git a/src/test/suite/extension.test.ts b/src/test/suite/extension.test.ts new file mode 100644 index 0000000..4ca0ab4 --- /dev/null +++ b/src/test/suite/extension.test.ts @@ -0,0 +1,15 @@ +import * as assert from 'assert'; + +// You can import and use all API from the 'vscode' module +// as well as import your extension to test it +import * as vscode from 'vscode'; +// import * as myExtension from '../../extension'; + +suite('Extension Test Suite', () => { + vscode.window.showInformationMessage('Start all tests.'); + + test('Sample test', () => { + assert.strictEqual(-1, [1, 2, 3].indexOf(5)); + assert.strictEqual(-1, [1, 2, 3].indexOf(0)); + }); +}); diff --git a/src/test/suite/index.js b/src/test/suite/index.js new file mode 100644 index 0000000..f0da16d --- /dev/null +++ b/src/test/suite/index.js @@ -0,0 +1,40 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.run = void 0; +const path = require("path"); +const Mocha = require("mocha"); +const glob = require("glob"); +function run() { + // Create the mocha test + const mocha = new Mocha({ + ui: 'tdd', + color: true + }); + const testsRoot = path.resolve(__dirname, '..'); + return new Promise((c, e) => { + glob('**/**.test.js', { cwd: testsRoot }, (err, files) => { + if (err) { + return e(err); + } + // Add files to the test suite + files.forEach(f => mocha.addFile(path.resolve(testsRoot, f))); + try { + // Run the mocha test + mocha.run(failures => { + if (failures > 0) { + e(new Error(`${failures} tests failed.`)); + } + else { + c(); + } + }); + } + catch (err) { + console.error(err); + e(err); + } + }); + }); +} +exports.run = run; +//# sourceMappingURL=index.js.map \ No newline at end of file diff --git a/src/test/suite/index.ts b/src/test/suite/index.ts new file mode 100644 index 0000000..7029e38 --- /dev/null +++ b/src/test/suite/index.ts @@ -0,0 +1,38 @@ +import * as path from 'path'; +import * as Mocha from 'mocha'; +import * as glob from 'glob'; + +export function run(): Promise { + // Create the mocha test + const mocha = new Mocha({ + ui: 'tdd', + color: true + }); + + const testsRoot = path.resolve(__dirname, '..'); + + return new Promise((c, e) => { + glob('**/**.test.js', { cwd: testsRoot }, (err, files) => { + if (err) { + return e(err); + } + + // Add files to the test suite + files.forEach(f => mocha.addFile(path.resolve(testsRoot, f))); + + try { + // Run the mocha test + mocha.run(failures => { + if (failures > 0) { + e(new Error(`${failures} tests failed.`)); + } else { + c(); + } + }); + } catch (err) { + console.error(err); + e(err); + } + }); + }); +} diff --git a/src/trie-prefix-tree.d.ts b/src/trie-prefix-tree.d.ts new file mode 100644 index 0000000..4577a45 --- /dev/null +++ b/src/trie-prefix-tree.d.ts @@ -0,0 +1 @@ +declare module "trie-prefix-tree"; diff --git a/src/trie.ts b/src/trie.ts new file mode 100644 index 0000000..4698a28 --- /dev/null +++ b/src/trie.ts @@ -0,0 +1,192 @@ +const config = { + END_WORD: "$", + PERMS_MIN_LEN: 2, +}; + +export class Trie { + /** + * + * + * @internal + * @type {*} + * @memberOf Trie + */ + private _trie: any; + + constructor(input?: string[]) { + this._trie = Trie._create(input); + } + + public getIndex() { + return this._trie; + } + + public setIndex(trie: any) { + this._trie = trie; + } + + public addWord(word: string) { + const reducer = ( + previousValue: any, + currentValue: string, + currentIndex: number, + array: string[] + ) => { + return Trie._append( + previousValue, + currentValue, + currentIndex, + array + ); + }; + + const input: string[] = word /*.toLowerCase()*/ + .split(""); + input.reduce(reducer, this._trie); + return this; + } + + public removeWord(word: string) { + const { prefixFound, prefixNode } = Trie._checkPrefix(this._trie, word); + + if (prefixFound) { + delete prefixNode[config.END_WORD]; + } + + return this; + } + + public getWords() { + return Trie._recursePrefix(this._trie, ""); + } + + public getPrefix(strPrefix: string) { + // strPrefix = strPrefix.toLowerCase(); + if (!this._isPrefix(strPrefix)) { + return []; + } + + const { prefixNode } = Trie._checkPrefix(this._trie, strPrefix); + + return Trie._recursePrefix(prefixNode, strPrefix); + } + + /** + * + * + * @internal + * @param {any} prefix + * @returns + * + * @memberOf Trie + */ + private _isPrefix(prefix: any) { + const { prefixFound } = Trie._checkPrefix(this._trie, prefix); + + return prefixFound; + } + + /** + * + * + * @internal + * @static + * @param {any} trie + * @param {any} letter + * @param {any} index + * @param {any} array + * @returns + * + * @memberOf Trie + */ + private static _append(trie: any, letter: any, index: any, array: any) { + trie[letter] = trie[letter] || {}; + trie = trie[letter]; + + if (index === array.length - 1) { + trie[config.END_WORD] = 1; + } + + return trie; + } + + /** + * + * + * @internal + * @static + * @param {any} prefixNode + * @param {string} prefix + * @returns + * + * @memberOf Trie + */ + private static _checkPrefix(prefixNode: any, prefix: string) { + const input: string[] = prefix /*.toLowerCase()*/ + .split(""); + const prefixFound = input.every((letter, index) => { + if (!prefixNode[letter]) { + return false; + } + return (prefixNode = prefixNode[letter]); + }); + + return { + prefixFound, + prefixNode, + }; + } + + /** + * + * + * @internal + * @static + * @param {any} input + * @returns + * + * @memberOf Trie + */ + private static _create(input: any) { + const trie = (input || []).reduce((accumulator: any, item: any) => { + item + /*.toLowerCase()*/ + .split("") + .reduce(Trie._append, accumulator); + + return accumulator; + }, {}); + + return trie; + } + + /** + * + * + * @internal + * @static + * @param {any} node + * @param {any} prefix + * @param {string[]} [prefixes=[]] + * @returns + * + * @memberOf Trie + */ + private static _recursePrefix( + node: any, + prefix: any, + prefixes: string[] = [] + ) { + let word = prefix; + + for (const branch in node) { + if (branch === config.END_WORD) { + prefixes.push(word); + word = ""; + } + Trie._recursePrefix(node[branch], prefix + branch, prefixes); + } + + return prefixes.sort(); + } +} diff --git a/src/utils/changeIconColor.ts b/src/utils/changeIconColor.ts new file mode 100644 index 0000000..090bacb --- /dev/null +++ b/src/utils/changeIconColor.ts @@ -0,0 +1,18 @@ +import * as vscode from "vscode"; + +export default function changeIconColor( + isEnable: boolean, + myStatusBarItem: vscode.StatusBarItem, + originalColor: string | vscode.ThemeColor | undefined +): void { + myStatusBarItem.show(); + if (isEnable) { + myStatusBarItem.backgroundColor = originalColor; + } else { + originalColor = myStatusBarItem.backgroundColor; + // myStatusBarItem.backgroundColor = "#7B5F00"; + myStatusBarItem.backgroundColor = new vscode.ThemeColor( + "statusBarItem.warningBackground" + ); + } +} diff --git a/src/utils/checkPrivacy.ts b/src/utils/checkPrivacy.ts new file mode 100644 index 0000000..739e14a --- /dev/null +++ b/src/utils/checkPrivacy.ts @@ -0,0 +1,23 @@ +import { workspace, window } from "vscode"; + +//check if the user accept to share codes +export async function checkPrivacy() { + const configuration = workspace.getConfiguration("Codegeex", undefined); + let privacy = configuration.get("Privacy"); + console.log(privacy); + if (privacy === null) { + const selection = await window.showInformationMessage( + "We highly respect the privacy of your code. Do you accept sharing the generated code only for research purposes to make CodeGeeX better? Otherwise, the code won't be stored and is only used to assist your programming.", + "Accept", + "Decline" + ); + if (selection !== undefined && selection === "Accept") { + configuration + .update("Privacy", true, true) + .then((res) => console.log(res)); + } + if (selection !== undefined && selection === "Decline") { + configuration.update("Privacy", false, true); + } + } +} diff --git a/src/utils/chooseCandidate.ts b/src/utils/chooseCandidate.ts new file mode 100644 index 0000000..c0965c8 --- /dev/null +++ b/src/utils/chooseCandidate.ts @@ -0,0 +1,50 @@ +import * as vscode from "vscode"; +import { translationInsertMode } from "../param/configures"; +import { comment } from "../param/constparams"; +import commentCode, { getCommentSignal } from "./commentCode"; +import { getEndData } from "./statisticFunc"; + +//function to insert code when user click 'use code' +export default function chooseCandidate( + targetEditor: vscode.TextEditor, + fn: string, + mode: string, + commandid: string +) { + if (!targetEditor) return; + try { + targetEditor + .edit((editBuilder) => { + var s = targetEditor.selection; + if (s.start.character == 0 && fn.slice(0, 1) == "\n") { + fn = fn.slice(1); + } + if (mode === "translation") { + let selection = new vscode.Selection(s.start, s.end); + const text = targetEditor.document.getText(selection); + const lang = targetEditor.document.languageId; + const commentSignal = getCommentSignal(lang); + if (translationInsertMode === "comment") { + const commentedText = commentCode( + text, + lang, + "line" + ).replaceAll(comment, commentSignal.line || "#"); + console.log(commentedText); + editBuilder.replace(s, commentedText + "\n" + fn); + } else { + editBuilder.replace(s, fn); + } + } else { + editBuilder.replace(s, fn); + } + getEndData(commandid, "", "Yes", fn); + }) + .then((success) => { + var postion = targetEditor.selection.end; + targetEditor.selection = new vscode.Selection(postion, postion); + }); + } catch (e) { + console.log(e); + } +} diff --git a/src/utils/codeGenByTemplate.ts b/src/utils/codeGenByTemplate.ts new file mode 100644 index 0000000..0b22d5f --- /dev/null +++ b/src/utils/codeGenByTemplate.ts @@ -0,0 +1,100 @@ +import * as vscode from "vscode"; +import { getCodeCompletions } from "./getCodeCompletions"; +import getDocumentLanguage from "./getDocumentLanguage"; +import { apiKey, apiSecret } from "../param/constparams"; +import { updateStatusBarItem } from "./updateStatusBarItem"; + +export default async function codeGenByTemplate( + editor: vscode.TextEditor, + templateStr: string, + myStatusBarItem: vscode.StatusBarItem, + g_isLoading: boolean +) { + let prompt_input = ""; + let document = editor.document; + let sel = editor.selections; + for (var x = 0; x < sel.length; x++) { + let txt: string = document.getText( + new vscode.Range(sel[x].start, sel[x].end) + ); + // console.log(txt) + prompt_input += txt; + } + var selection = editor.selection; + let rs: any; + let prompt = ""; + let lang = ""; + try { + let promptInputArr = prompt_input.split("\n"); + const re = //g; + let iter = re.exec(templateStr); + prompt = templateStr; + while (iter) { + if (iter[0] == "") { + prompt = prompt.replace(iter[0], prompt_input); + } else if (iter[0].indexOf(":") != -1) { + if (iter[0].indexOf(",") != -1) { + let rangeStr = iter[0].split(":")[1]; + rangeStr = rangeStr.slice(0, -1); + let fromLine = Number(rangeStr.split(",")[0]); + let toLine = Number(rangeStr.split(",")[1]); + prompt = prompt.replace( + iter[0], + promptInputArr.slice(fromLine, toLine).join("\n") + ); + } else { + let rangeStr = Number(iter[0].split(":")[1].slice(0, -1)); + prompt = prompt.replace( + iter[0], + promptInputArr.slice(rangeStr).join("\n") + ); + } + } else { + vscode.window.showInformationMessage( + "There is an error in markup." + ); + return; + } + iter = re.exec(templateStr); + } + console.log("about to request"); + console.log(prompt); + lang = getDocumentLanguage(editor); + updateStatusBarItem(myStatusBarItem, g_isLoading, true, ""); + rs = await getCodeCompletions( + prompt, + 1, + lang, + apiKey, + apiSecret, + "prompt" + ); + } catch (err) { + updateStatusBarItem( + myStatusBarItem, + g_isLoading, + false, + " No Suggestion" + ); + + return; + } + + if (rs && rs.completions.length > 0) { + editor.edit(function (edit: any) { + let pos = selection.end; + edit.insert( + new vscode.Position(pos.line + 3, 0), + `${rs.completions[0]}` + ); + }); + updateStatusBarItem(myStatusBarItem, g_isLoading, false, " Done"); + } else { + updateStatusBarItem( + myStatusBarItem, + g_isLoading, + false, + " No Suggestion" + ); + } +} diff --git a/src/utils/codegeexCodeGen.ts b/src/utils/codegeexCodeGen.ts new file mode 100644 index 0000000..7311948 --- /dev/null +++ b/src/utils/codegeexCodeGen.ts @@ -0,0 +1,17 @@ +import * as vscode from "vscode"; +import { myScheme } from "../param/constparams"; +import { navUri } from "./navUri"; + +//generate uri for interactive mode +export const codegeexCodeGen = async (code_block: string) => { + let loading = vscode.Uri.parse( + `${myScheme}:CodeGeeX?loading=true&mode=gen&code_block=${code_block}`, + true + ); + await navUri(loading, "python", "CodeGeeX"); + let uri = vscode.Uri.parse( + `${myScheme}:CodeGeeX?loading=false&mode=gen&code_block=${code_block}`, + true + ); + await navUri(uri, "python", "CodeGeeX"); +}; diff --git a/src/utils/codegeexCodeTranslation.ts b/src/utils/codegeexCodeTranslation.ts new file mode 100644 index 0000000..875dfae --- /dev/null +++ b/src/utils/codegeexCodeTranslation.ts @@ -0,0 +1,22 @@ +import getDocumentLangId from "./getDocumentLangId"; +import { Uri } from "vscode"; +import { navUri } from "./navUri"; +import { addSignal, andSignal, myScheme } from "../param/constparams"; + +//generate uri for translation mode +export const codegeexCodeTranslation = async ( + dstLang: string, + translationRes: string, + commandid: string +) => { + let documentLangId; + documentLangId = getDocumentLangId(dstLang); + let uri = Uri.parse( + `${myScheme}:CodeGeeX_translation?loading=false&mode=translation&commandid=${commandid}&translation_res=${translationRes + .replaceAll("+", addSignal) + .replaceAll("&", andSignal)}`, + true + ); + + await navUri(uri, documentLangId, "CodeGeeX_translation"); +}; diff --git a/src/utils/commentCode.ts b/src/utils/commentCode.ts new file mode 100644 index 0000000..669d032 --- /dev/null +++ b/src/utils/commentCode.ts @@ -0,0 +1,112 @@ +const comment = "<|comment|>"; + +//function to comment code for different language +export default function commentCode( + input: string, + lang: string, + mode?: string +) { + if (input.trim() === "") { + return input; + } + const commentSignal = getCommentSignal(lang); + if ( + mode === "block" && + commentSignal.blockLeft && + commentSignal.blockRight + ) { + return commentSignal.blockLeft + input + commentSignal.blockRight; + } + if (mode === "line" && commentSignal.line) { + return comment + input.replaceAll("\n", "\n" + comment); + } + if (commentSignal.blockLeft && commentSignal.blockRight) { + console.log("test1"); + return commentSignal.blockLeft + input + commentSignal.blockRight; + } + if (commentSignal.line) { + console.log("test2"); + return comment + input.replaceAll("\n", "\n" + comment); + } + console.log("test3"); + return input; +} +export function getCommentSignal(lang: string) { + let commentSignal; + switch (lang) { + case "javascript": + case "javascriptreact": + case "typescriptreact": + case "typescript": + case "java": + case "c": + case "csharp": + case "cpp": + case "cuda-cpp": + case "objective-c": + case "objective-cpp": + case "rust": + case "go": + commentSignal = { + blockLeft: "/*", + blockRight: "*/", + line: "//", + }; + break; + case "css": + case "less": + case "sass": + case "scss": + commentSignal = { + blockLeft: "/*", + blockRight: "*/", + line: null, + }; + break; + case "python": + console.log("python"); + commentSignal = { + blockLeft: '"""', + blockRight: '"""', + line: "#", + }; + break; + case "tex": + commentSignal = { + blockLeft: null, + blockRight: null, + line: "%", + }; + break; + case "shellscript": + case "r": + commentSignal = { + blockLeft: null, + blockRight: null, + line: "#", + }; + break; + case "sql": + commentSignal = { + blockLeft: "/*", + blockRight: "*/", + line: "--", + }; + break; + case "html": + case "php": + commentSignal = { + blockLeft: "", + line: null, + }; + break; + default: + commentSignal = { + blockLeft: null, + blockRight: null, + line: null, + }; + } + return commentSignal; +} diff --git a/src/utils/createWebView.ts b/src/utils/createWebView.ts new file mode 100644 index 0000000..7795317 --- /dev/null +++ b/src/utils/createWebView.ts @@ -0,0 +1,50 @@ +import { ExtensionContext, ViewColumn, WebviewPanel, window } from "vscode"; + +let panel: WebviewPanel | undefined; + +//To create web view in vscode +export default async function createWebView( + context: ExtensionContext, + content?: string, + view?: string +): Promise { + if (!panel) { + panel = window.createWebviewPanel( + "CodeGeeX.keys", + "CodeGeeX Guide", + { viewColumn: ViewColumn.Active, preserveFocus: false }, + { + retainContextWhenHidden: true, + enableFindWidget: true, + enableCommandUris: true, + enableScripts: true, + } + ); + + if (panel) { + panel.webview.html = ` + + + + + + CodeGeeX Guide + + +
+
${content}
+
+ +`; + panel.onDidDispose( + () => { + panel = undefined; + }, + null, + context.subscriptions + ); + } + } + + return panel; +} diff --git a/src/utils/getCodeCompletions.ts b/src/utils/getCodeCompletions.ts new file mode 100644 index 0000000..129714c --- /dev/null +++ b/src/utils/getCodeCompletions.ts @@ -0,0 +1,126 @@ +import axios from "axios"; +import * as https from "https"; +import * as vscode from "vscode"; +import { temp, topp, topk, generationPreference } from "../param/configures"; +import { getEndData, getStartData } from "./statisticFunc"; + +export type GetCodeCompletions = { + completions: Array; + commandid: string; +}; + +export function getCodeCompletions( + prompt: string, + num: Number, + lang: string, + apiKey: string, + apiSecret: string, + mode: string +): Promise { + let API_URL = ""; + if (mode === "prompt") { + API_URL = `https://tianqi.aminer.cn/api/v2/multilingual_code_generate_block`; + } else if (mode === "interactive") { + API_URL = `https://tianqi.aminer.cn/api/v2/multilingual_code_generate_adapt`; + } else { + if (generationPreference === "line by line") { + API_URL = `https://tianqi.aminer.cn/api/v2/multilingual_code_generate`; + } else { + API_URL = `https://tianqi.aminer.cn/api/v2/multilingual_code_generate_adapt`; + } + } + return new Promise(async (resolve, reject) => { + let n = 0; + if (prompt.length <= 300) { + n = 3; + } else if (prompt.length > 600 && prompt.length <= 900) { + n = 2; + } else if (prompt.length > 900 && prompt.length <= 1200) { + n = 1; + } else if (prompt.length > 1200) { + prompt = prompt.slice(prompt.length - 1200); + n = 1; + } + let payload = {}; + if (lang.length == 0) { + payload = { + prompt: prompt, + n: num, + apikey: apiKey, + apisecret: apiSecret, + temperature: temp, + top_p: topp, + top_k: topk, + }; + } else { + payload = { + lang: lang, + prompt: prompt, + n: num, + apikey: apiKey, + apisecret: apiSecret, + temperature: temp, + top_p: topp, + top_k: topk, + }; + } + const agent = new https.Agent({ + rejectUnauthorized: false, + }); + let editor = vscode.window.activeTextEditor; + let document = editor?.document; + let lastLine = document?.lineAt(document.lineCount - 1); + let endPosition = lastLine?.range.end; + let inputText; + if (endPosition) { + let input = + new vscode.Selection( + 0, + 0, + endPosition.line, + endPosition.character + ) || new vscode.Selection(0, 0, 0, 0); + inputText = document?.getText(input) || ""; + } else { + inputText = prompt; + } + console.log(inputText); + let commandid: string; + try { + commandid = await getStartData(inputText, prompt, lang, mode); + } catch (err) { + console.log(err); + commandid = ""; + } + try { + axios + .post(API_URL, payload, { httpsAgent: agent, timeout: 120000 }) + .then((res) => { + console.log(res); + if (res?.data.status === 0) { + let codeArray = res?.data.result.output.code; + const completions = Array(); + for (let i = 0; i < codeArray.length; i++) { + const completion = codeArray[i]; + let tmpstr = completion; + if (tmpstr.trim() === "") continue; + + completions.push(completion); + } + resolve({ completions, commandid }); + } else { + console.log(res); + getEndData(commandid, res.data.message, "No"); + reject(res.data.message); + } + }) + .catch((err) => { + getEndData(commandid, err.message, "No"); + reject(err); + }); + } catch (e) { + getEndData(commandid, "", "No"); + reject(e); + } + }); +} diff --git a/src/utils/getCodeTranslation.ts b/src/utils/getCodeTranslation.ts new file mode 100644 index 0000000..94f1081 --- /dev/null +++ b/src/utils/getCodeTranslation.ts @@ -0,0 +1,61 @@ +import axios from "axios"; +import * as https from "https"; +import * as vscode from "vscode"; +import { apiKey, apiSecret, apiHerf } from "../param/constparams"; +import { temp, topk, topp } from "../param/configures"; +export type GetCodeTranslation = { + translation: Array; +}; +export function getCodeTranslation( + prompt: string, + src_lang: string, + dst_lang: string +): Promise { + //const API_URL = `https://tianqi.aminer.cn/api/v2/multilingual_code/translate`; + const API_URL = `https://tianqi.aminer.cn/api/v2/multilingual_code_translate`; //`https://wudao.aminer.cn/os/api/api/v2/multilingual_code/translate`; + + return new Promise((resolve, reject) => { + let payload = {}; + payload = { + prompt: prompt, + n: 1, + src_lang: src_lang, + dst_lang: dst_lang, + stop: [], + userid: "", + apikey: apiKey, + apisecret: apiSecret, + temperature: temp, + top_p: topp, + top_k: topk, + }; + const agent = new https.Agent({ + rejectUnauthorized: false, + }); + + axios + .post(API_URL, payload, { httpsAgent: agent, timeout: 120000 }) + .then((res) => { + console.log(res); + if (res?.data.status === 0) { + let codeArray = res?.data.result.output.code; + const translation = Array(); + for (let i = 0; i < codeArray.length; i++) { + const translationStr = codeArray[i]; //.trimStart() + let tmpstr = translationStr; + if (tmpstr.trim() === "") continue; + + translation.push(translationStr); + } + console.log("translation"); + console.log(translation); + resolve({ translation }); + } else { + console.log(res); + } + }) + .catch((err) => { + reject(err); + }); + }); +} diff --git a/src/utils/getDocumentLangId.ts b/src/utils/getDocumentLangId.ts new file mode 100644 index 0000000..24a33b3 --- /dev/null +++ b/src/utils/getDocumentLangId.ts @@ -0,0 +1,15 @@ +export default function getDocumentLangId(lang: string) { + let id; + lang = lang.replace("++", "pp").replace("#", "sharp"); + switch (lang) { + case "Cuda": + id = "cuda-cpp"; + break; + case "Shell": + id = "shellscript"; + break; + default: + id = lang.toLowerCase(); + } + return id; +} diff --git a/src/utils/getDocumentLanguage.ts b/src/utils/getDocumentLanguage.ts new file mode 100644 index 0000000..a6ddf79 --- /dev/null +++ b/src/utils/getDocumentLanguage.ts @@ -0,0 +1,86 @@ +import { TextEditor } from "vscode"; +export default function getDocumentLanguage(editor: TextEditor) { + const documentLanguageId: string = editor.document.languageId; + console.log("documentLanguageId"); + console.log(documentLanguageId); + let lang = ""; + switch (documentLanguageId) { + case "cpp": + lang = "C++"; + break; + case "c": + lang = "C"; + break; + case "csharp": + lang = "C#"; + break; + case "cuda-cpp": + lang = "Cuda"; + break; + case "objective-c": + lang = "Objective-C"; + break; + case "objective-cpp": + lang = "Objective-C++"; + break; + case "python": + lang = "Python"; + break; + case "java": + lang = "Java"; + break; + case "tex": + lang = "TeX"; + break; + case "html": + lang = "HTML"; + break; + case "php": + lang = "PHP"; + break; + case "javascript": + case "javascriptreact": + lang = "JavaScript"; + break; + case "typescript": + case "typescriptreact": + lang = "TypeScript"; + break; + case "go": + lang = "Go"; + break; + case "shellscript": + lang = "Shell"; + break; + case "rust": + lang = "Rust"; + break; + case "css": + case "less": + case "sass": + case "scss": + lang = "CSS"; + break; + case "sql": + lang = "SQL"; + break; + // case 'html': + // lang = 'Kotlin' + // break + // case 'html': + // lang = 'Pascal' + // break + case "r": + lang = "R"; + break; + // case 'html': + // lang = 'Fortran' + // break + // case 'html': + // lang = 'Lean' + // break + default: + lang = ""; + } + return lang; +} diff --git a/src/utils/getGPTCode.ts b/src/utils/getGPTCode.ts new file mode 100644 index 0000000..7f7237b --- /dev/null +++ b/src/utils/getGPTCode.ts @@ -0,0 +1,44 @@ +import { codelensProvider } from "../provider/codelensProvider"; +import { updateStatusBarItem } from "./updateStatusBarItem"; +import { StatusBarItem } from "vscode"; + +export const getGPTCode = ( + candidateList: Array, + commandid: string, + myStatusBarItem: StatusBarItem, + g_isLoading: boolean +) => { + codelensProvider.clearEls(); + let content = `/* The candidate list of code generated by CodeGeeX */\n`; + if (candidateList.length === 0) { + updateStatusBarItem( + myStatusBarItem, + g_isLoading, + false, + " No Suggestion" + ); + return content; + } + for (let i = 0; i < candidateList.length; i++) { + const lineNum = content.split("\n").length; + console.log(candidateList[i]); + if (i === 0) { + codelensProvider.addEl(lineNum, candidateList[i], commandid); + } else { + codelensProvider.addEl(lineNum, candidateList[i], commandid); + } + if (candidateList[i][0] === "\n") { + content += candidateList[i]; + } else { + content += "\n" + candidateList[i]; + } + + if ( + i < + candidateList.length - 1 /*&& candidateList[i].slice(-1) != '\n'*/ + ) + content += "\n"; + } + updateStatusBarItem(myStatusBarItem, g_isLoading, false, " Done"); + return content; +}; diff --git a/src/utils/navUri.ts b/src/utils/navUri.ts new file mode 100644 index 0000000..4d199fa --- /dev/null +++ b/src/utils/navUri.ts @@ -0,0 +1,15 @@ +import * as vscode from "vscode"; + +export const navUri = async ( + uri: vscode.Uri, + language: string, + mode: string +) => { + const doc = await vscode.workspace.openTextDocument(uri); + await vscode.window.showTextDocument(doc, { + viewColumn: vscode.ViewColumn.Beside, + preview: true, + preserveFocus: true, + }); + vscode.languages.setTextDocumentLanguage(doc, language); +}; diff --git a/src/utils/readTemplate.ts b/src/utils/readTemplate.ts new file mode 100644 index 0000000..529f1e0 --- /dev/null +++ b/src/utils/readTemplate.ts @@ -0,0 +1,5 @@ +import * as vscode from "vscode"; +export default async function readTemplate(path: string) { + const readData = await vscode.workspace.fs.readFile(vscode.Uri.parse(path)); + return Buffer.from(readData).toString("utf8"); +} diff --git a/src/utils/showQuickPick.ts b/src/utils/showQuickPick.ts new file mode 100644 index 0000000..9126c07 --- /dev/null +++ b/src/utils/showQuickPick.ts @@ -0,0 +1,11 @@ +import { window } from "vscode"; + +export async function showQuickPick(list: Array, description: string) { + const result = await window.showQuickPick(list, { + placeHolder: description, + onDidSelectItem: (item) => { + //window.showInformationMessage(`You've chosen ${item}`) + }, + }); + return result; +} diff --git a/src/utils/statisticFunc.ts b/src/utils/statisticFunc.ts new file mode 100644 index 0000000..abc7543 --- /dev/null +++ b/src/utils/statisticFunc.ts @@ -0,0 +1,97 @@ +import * as vscode from "vscode"; +import axios from "axios"; +import * as os from "os"; +//import welcomePage from './welcomePage'; +import { apiHerf, extensionId, extensionVersion } from "../param/constparams"; + +const privacy = vscode.workspace.getConfiguration("Codegeex").get("Privacy"); + +export function getOpenExtensionData(): Promise { + return new Promise((resolve) => { + try { + axios + .post(`${apiHerf}/tracking/insertVscodeStartRecord`, { + vscodeMachineId: vscode.env.machineId, + vscodeSessionId: vscode.env.sessionId, + platformVersion: os.release(), + systemOs: os.type(), + extensionId: extensionId, + extensionVersion: extensionVersion, + nodeArch: os.arch(), + isNewAppInstall: vscode.env.isNewAppInstall, + vscodeVersion: vscode.version, + product: vscode.env.appHost, + uikind: vscode.env.uiKind, + remoteName: vscode.env.remoteName, + }) + .then((res) => { + console.log(res); + resolve(res.data.msg); + }) + .catch((err) => { + resolve("error"); + }); + } catch (e) { + resolve("error"); + } + }); +} +export function getStartData( + inputText: string, + prompt: string, + lang: string, + mode?: string +): Promise { + return new Promise((resolve) => { + const startParam = { + vscodeMachineId: vscode.env.machineId, + vscodeSessionId: vscode.env.sessionId, + requestPhase: "start", + inputContent: privacy ? inputText : null, + prompt: privacy ? prompt : null, + lang: lang, + mode: mode ? mode : null, + }; + try { + axios + .post(`${apiHerf}/tracking/vsCodeOperationRecord`, startParam) + .then((res) => { + console.log("开始请求测试", res); + let commandid = res.data.data.id || ""; + resolve(commandid); + }); + } catch (err) { + resolve(""); + } + }); +} +export function getEndData( + commandid: string, + message: string, + isAdopted: string, + acceptItem?: string | null, + completions?: Array | string +): Promise { + return new Promise((resolve) => { + let endparam = { + id: commandid, + requestPhase: "end", + outputContent: privacy ? acceptItem : null, + modelStatus: -1, + message: message, //err.message, + num: privacy ? completions?.length : 0, + numContent: privacy ? completions?.toString() : null, + whetherAdopt: isAdopted, + }; + axios + .post(`${apiHerf}/tracking/vsCodeOperationRecord`, endparam) + .then((res) => { + console.log("测试结束埋点", res); + resolve(""); + }) + .catch((e) => { + console.log("结束埋点错误", e); + resolve(""); + }); + }); +} diff --git a/src/utils/updateStatusBarItem.ts b/src/utils/updateStatusBarItem.ts new file mode 100644 index 0000000..956d519 --- /dev/null +++ b/src/utils/updateStatusBarItem.ts @@ -0,0 +1,24 @@ +import * as vscode from "vscode"; +var statusbartimer: NodeJS.Timeout; + +export async function updateStatusBarItem( + myStatusBarItem: vscode.StatusBarItem, + g_isLoading: boolean, + isLoading: boolean, + info: string +): Promise { + myStatusBarItem.show(); + if (statusbartimer) { + clearTimeout(statusbartimer); + } + if (isLoading) { + g_isLoading = true; + myStatusBarItem.text = `$(loading~spin)` + info; + } else { + g_isLoading = false; + myStatusBarItem.text = `$(codegeex-dark)` + info; + statusbartimer = setTimeout(() => { + myStatusBarItem.text = `$(codegeex-dark)`; + }, 30000); + } +} diff --git a/src/welcomePage.ts b/src/welcomePage.ts new file mode 100644 index 0000000..9628b55 --- /dev/null +++ b/src/welcomePage.ts @@ -0,0 +1,140 @@ +import { EventEmitter } from "stream"; +import { ExtensionContext, WebviewPanel } from "vscode"; +import createWebView from "./utils/createWebView"; +import { controls } from "./param/configures"; + +let panel: WebviewPanel; +export default async function welcomePage( + context: ExtensionContext +): Promise { + const content = ` +
+
+ + +
Welcome to CodeGeex +
+
+
A Free Code Generation and Completion Tool
+
+
CodeGeeX is an AI code assistant tool for developers which makes coding easier and faster. CodeGeeX supports 20+ different programming languages, including Python, C++, Java, JavaScipt, Go, C, C#, Cuda, Objective-C/C++, PHP, HTML, TypeScript, Rust, Shell, SQL, TeX, etc.
+
+ +
+ + + + +
+
+
+
+
Keep CodeGeeX activated, it will start generating codes when you stop writing (the icon at the bottom of VSCode starts spinning). When the generated code is shown in gray, just press  Tab  to insert the generated codes. You can also press  Alt/Option+[ or ]  to change between candidates. And you can press  Alt/Option+N  to get new suggestions if you are not satisfied with the current.
+ +
+
+
Press  Ctrl+Enter  to activate the interactive mode, CodeGeeX will generate X candidates and show them in the right panel (X can be modified in extension settings Candidate Num). Then, select the best candidate by clicking on it.
+ +
+
+
Select code, and press  Ctrl+Alt+T  to activate the translation mode. Then, choose the language of the selected code. You will get the code translated into the same language as your current editor. Click on the use code button to insert the result. You can also configure in the settings whether to comment out the original code or to replace it.
+ +
+
+
Select codes to be used as input, then press  Alt/Option+T  to trigger the prompt mode. It will show a list of pre-defined prompt templates and choose one to generate codes with your input. This mode is fully customizable, you can add your own templates in the extension settings Prompt Templates.
+ +
+
+ +
`; + if (!panel) { + panel = await createWebView(context, content); + } + panel.reveal(); +} + +const keys = ` + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
CommandMACWindows
Switch to Interactive Mode${controls.interactiveMode.mac}${controls.interactiveMode.win}
Switch to Prompt Mode${controls.promptMode.mac}${controls.promptMode.win}
Switch to Translation Mode${controls.translationMode.mac}${controls.translationMode.win}
Next Suggestion${controls.nextSuggestion.mac}${controls.nextSuggestion.win}
Previous Suggestion${controls.previousSuggestion.mac}${controls.previousSuggestion.win}
Get New Suggestions${controls.newSuggestion.mac}${controls.newSuggestion.win}
+`; diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..f0f590c --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,18 @@ +{ + "compilerOptions": { + "module": "commonjs", + "target": "ES2020", + "outDir": "out", + "lib": [ + "ES2020", + "ES2021.String" + ], + "sourceMap": true, + "rootDir": "src", + "strict": true /* enable all strict type-checking options */ + /* Additional Checks */ + // "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */ + // "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */ + // "noUnusedParameters": true, /* Report errors on unused parameters. */ + } +} diff --git a/vsc-extension-quickstart.md b/vsc-extension-quickstart.md new file mode 100644 index 0000000..3e36bd2 --- /dev/null +++ b/vsc-extension-quickstart.md @@ -0,0 +1,47 @@ +# Welcome to your VS Code Extension + +## What's in the folder + +* This folder contains all of the files necessary for your extension. +* `package.json` - this is the manifest file in which you declare your extension and command. + * The sample plugin registers a command and defines its title and command name. With this information VS Code can show the command in the command palette. It doesn’t yet need to load the plugin. +* `src/extension.ts` - this is the main file where you will provide the implementation of your command. + * The file exports one function, `activate`, which is called the very first time your extension is activated (in this case by executing the command). Inside the `activate` function we call `registerCommand`. + * We pass the function containing the implementation of the command as the second parameter to `registerCommand`. + +## Setup + +* install the recommended extensions (amodio.tsl-problem-matcher and dbaeumer.vscode-eslint) + + +## Get up and running straight away + +* Press `F5` to open a new window with your extension loaded. +* Run your command from the command palette by pressing (`Ctrl+Shift+P` or `Cmd+Shift+P` on Mac) and typing `Hello World`. +* Set breakpoints in your code inside `src/extension.ts` to debug your extension. +* Find output from your extension in the debug console. + +## Make changes + +* You can relaunch the extension from the debug toolbar after changing code in `src/extension.ts`. +* You can also reload (`Ctrl+R` or `Cmd+R` on Mac) the VS Code window with your extension to load your changes. + + +## Explore the API + +* You can open the full set of our API when you open the file `node_modules/@types/vscode/index.d.ts`. + +## Run tests + +* Open the debug viewlet (`Ctrl+Shift+D` or `Cmd+Shift+D` on Mac) and from the launch configuration dropdown pick `Extension Tests`. +* Press `F5` to run the tests in a new window with your extension loaded. +* See the output of the test result in the debug console. +* Make changes to `src/test/suite/extension.test.ts` or create new test files inside the `test/suite` folder. + * The provided test runner will only consider files matching the name pattern `**.test.ts`. + * You can create folders inside the `test` folder to structure your tests any way you want. + +## Go further + +* Reduce the extension size and improve the startup time by [bundling your extension](https://code.visualstudio.com/api/working-with-extensions/bundling-extension). +* [Publish your extension](https://code.visualstudio.com/api/working-with-extensions/publishing-extension) on the VSCode extension marketplace. +* Automate builds by setting up [Continuous Integration](https://code.visualstudio.com/api/working-with-extensions/continuous-integration).