Skip to content

Commit 67cc97b

Browse files
authored
Merge pull request #10 from lowcoding/v1.6.0
V1.6.0
2 parents 0ec229c + 797c39e commit 67cc97b

File tree

11 files changed

+103
-29
lines changed

11 files changed

+103
-29
lines changed

.eslintrc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,6 @@ module.exports = {
3737
'no-unused-expressions': 'off',
3838
'max-len': 'off',
3939
'class-methods-use-this': 'off',
40+
'no-eval': 'off',
4041
},
4142
};

package.json

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"displayName": "lowcode",
44
"description": "lowcode tool, support ChatGPT",
55
"author": "wjkang <[email protected]>",
6-
"version": "1.5.0",
6+
"version": "1.6.0",
77
"icon": "asset/icon.png",
88
"publisher": "wjkang",
99
"repository": "https://github.com/lowcoding/lowcode-vscode",
@@ -54,19 +54,19 @@
5454
"contributes": {
5555
"commands": [
5656
{
57-
"command": "yapi-code.generateCodeByWebview",
57+
"command": "lowcode.generateCodeByWebview",
5858
"title": "可视化生成代码"
5959
},
6060
{
61-
"command": "yapi-code.generateCode",
61+
"command": "lowcode.generateCode",
6262
"title": "生成代码"
6363
},
6464
{
6565
"command": "lowcode.openDownloadMaterials",
6666
"title": "下载物料"
6767
},
6868
{
69-
"command": "yapi-code.addSnippet",
69+
"command": "lowcode.addSnippet",
7070
"title": "添加代码片段"
7171
},
7272
{
@@ -94,6 +94,10 @@
9494
{
9595
"command": "lowcode.askChatGPTWithTemplate",
9696
"title": "Ask ChatGPT With Template"
97+
},
98+
{
99+
"command": "lowcode.runSnippetScript",
100+
"title": "Run Snippet Script"
97101
}
98102
],
99103
"menus": {
@@ -112,19 +116,19 @@
112116
],
113117
"lowcode/editor/context": [
114118
{
115-
"command": "yapi-code.generateCodeByWebview",
119+
"command": "lowcode.generateCodeByWebview",
116120
"group": "lowcode@1"
117121
},
118122
{
119-
"command": "yapi-code.generateCode",
123+
"command": "lowcode.generateCode",
120124
"group": "lowcode@2"
121125
},
122126
{
123127
"command": "lowcode.openDownloadMaterials",
124128
"group": "lowcode@3"
125129
},
126130
{
127-
"command": "yapi-code.addSnippet",
131+
"command": "lowcode.addSnippet",
128132
"group": "lowcode@4"
129133
},
130134
{
@@ -134,6 +138,10 @@
134138
{
135139
"command": "lowcode.askChatGPTWithTemplate",
136140
"group": "lowcode@5"
141+
},
142+
{
143+
"command": "lowcode.runSnippetScript",
144+
"group": "lowcode@6"
137145
}
138146
],
139147
"lowcode/explorer/context": [
@@ -142,7 +150,7 @@
142150
"group": "lowcode@1"
143151
},
144152
{
145-
"command": "yapi-code.generateCodeByWebview",
153+
"command": "lowcode.generateCodeByWebview",
146154
"group": "lowcode@2"
147155
},
148156
{

src/commands/addSnippet.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { showWebView } from '../webview';
44

55
export const createOrShowWebview = (context: vscode.ExtensionContext) => {
66
context.subscriptions.push(
7-
vscode.commands.registerTextEditorCommand('yapi-code.addSnippet', () => {
7+
vscode.commands.registerTextEditorCommand('lowcode.addSnippet', () => {
88
const content = getSelectedText() || getClipboardText();
99
showWebView({
1010
key: 'main',

src/commands/createOrShowWebview.ts

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,18 @@ import { showWebView } from '../webview';
44

55
export const createOrShowWebview = (context: vscode.ExtensionContext) => {
66
context.subscriptions.push(
7-
vscode.commands.registerCommand(
8-
'yapi-code.generateCodeByWebview',
9-
(args) => {
10-
const path = formatPath(args?.path);
11-
showWebView({
12-
key: 'main',
13-
viewColumn: vscode.ViewColumn.Two,
14-
task: path
15-
? {
16-
task: 'updateSelectedFolder',
17-
data: { selectedFolder: path },
18-
}
19-
: undefined,
20-
});
21-
},
22-
),
7+
vscode.commands.registerCommand('lowcode.generateCodeByWebview', (args) => {
8+
const path = formatPath(args?.path);
9+
showWebView({
10+
key: 'main',
11+
viewColumn: vscode.ViewColumn.Two,
12+
task: path
13+
? {
14+
task: 'updateSelectedFolder',
15+
data: { selectedFolder: path },
16+
}
17+
: undefined,
18+
});
19+
}),
2320
);
2421
};

src/commands/generateCode.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const { window } = vscode;
1010
export const generateCode = (context: vscode.ExtensionContext) => {
1111
context.subscriptions.push(
1212
vscode.commands.registerTextEditorCommand(
13-
'yapi-code.generateCode',
13+
'lowcode.generateCode',
1414
async () => {
1515
const rawClipboardText = getClipboardText();
1616
let clipboardText = rawClipboardText.trim();

src/commands/openSnippet.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { showWebView } from '../webview';
88
export const openSnippet = (context: vscode.ExtensionContext) => {
99
context.subscriptions.push(
1010
vscode.commands.registerTextEditorCommand(
11-
'yapi-code.openSnippetByWebview',
11+
'lowcode.openSnippetByWebview',
1212
(textEditor: TextEditor, edit: TextEditorEdit, ...args: any[]) => {
1313
const name = args[0];
1414
const template = args[1];

src/commands/registerCompletion.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export const registerCompletion = (context: vscode.ExtensionContext) => {
3434
// 无法直接通过 ejs 编译,说明模板中需要额外的数据,触发命令打开 webview
3535
completionItem.insertText = '';
3636
completionItem.command = {
37-
command: 'yapi-code.openSnippetByWebview',
37+
command: 'lowcode.openSnippetByWebview',
3838
title: '',
3939
arguments: [s.name, s.template],
4040
};

src/commands/runSnippetScript.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import * as vscode from 'vscode';
2+
import * as path from 'path';
3+
import * as fs from 'fs-extra';
4+
import { getSnippets } from '../utils/materials';
5+
import { getEnv, rootPath } from '../utils/vscodeEnv';
6+
import { getInnerLibs } from '../utils/lib';
7+
import { getOutputChannel } from '../utils/outputChannel';
8+
import { createChatCompletionForScript } from '../utils/openai';
9+
10+
const { window } = vscode;
11+
12+
export const registerRunSnippetScript = (context: vscode.ExtensionContext) => {
13+
context.subscriptions.push(
14+
vscode.commands.registerTextEditorCommand(
15+
'lowcode.runSnippetScript',
16+
async () => {
17+
const templateList = getSnippets().filter(
18+
(s) => !s.preview.notShowInCommand,
19+
);
20+
if (templateList.length === 0) {
21+
window.showErrorMessage('请配置模板(代码片段)');
22+
}
23+
const templateResult = await window.showQuickPick(
24+
templateList.map((s) => s.name),
25+
{ placeHolder: '请选择模板' },
26+
);
27+
if (!templateResult) {
28+
return;
29+
}
30+
const template = templateList.find((s) => s.name === templateResult);
31+
const scriptFile = path.join(template!.path, 'script/index.js');
32+
if (fs.existsSync(scriptFile)) {
33+
delete eval('require').cache[eval('require').resolve(scriptFile)];
34+
const script = eval('require')(scriptFile);
35+
if (script.onSelect) {
36+
const context = {
37+
vscode,
38+
workspaceRootPath: rootPath,
39+
env: getEnv(),
40+
libs: getInnerLibs(),
41+
outputChannel: getOutputChannel(),
42+
log: getOutputChannel(),
43+
createChatCompletion: createChatCompletionForScript,
44+
materialPath: template!.path,
45+
code: '',
46+
};
47+
try {
48+
await script.onSelect(context);
49+
} catch (ex: any) {
50+
window.showErrorMessage(ex.toString());
51+
}
52+
} else {
53+
window.showErrorMessage('脚本中未实现 onSelect 方法');
54+
}
55+
} else {
56+
window.showErrorMessage('当前模板中未添加脚本');
57+
}
58+
},
59+
),
60+
);
61+
};

src/extension.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { init, setLastActiveTextEditorId } from './context';
99
import { registerQuickGenerateBlock } from './commands/quickGenerateBlock';
1010
import { registerChatGPTViewProvider } from './webview';
1111
import { registerChatGPTCommand } from './commands/chatGPT';
12+
import { registerRunSnippetScript } from './commands/runSnippetScript';
1213

1314
export function activate(context: vscode.ExtensionContext) {
1415
vscode.window.onDidChangeActiveTextEditor(
@@ -26,6 +27,8 @@ export function activate(context: vscode.ExtensionContext) {
2627

2728
generateCode(context);
2829

30+
registerRunSnippetScript(context);
31+
2932
createOrShowWebview(context);
3033

3134
createOrShowAddSnippetWebview(context);
@@ -42,7 +45,7 @@ export function activate(context: vscode.ExtensionContext) {
4245
vscode.StatusBarAlignment.Left,
4346
-1,
4447
);
45-
statusBarItem.command = 'yapi-code.generateCodeByWebview';
48+
statusBarItem.command = 'lowcode.generateCodeByWebview';
4649
statusBarItem.text = '$(octoface) Low Code';
4750
statusBarItem.tooltip = '可视化生成代码';
4851
statusBarItem.show();

src/utils/generate.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ export const genCodeByBlock = async (data: {
9393
.join(data.path, ...data.createPath)
9494
.replace(/\\/g, '/'),
9595
createChatCompletion: createChatCompletionForScript,
96+
materialPath: block,
9697
};
9798
data.model = {
9899
...data.model,
@@ -196,6 +197,7 @@ export const genCodeBySnippet = async (data: {
196197
outputChannel: getOutputChannel(),
197198
log: getOutputChannel(),
198199
createChatCompletion: createChatCompletionForScript,
200+
materialPath: snippetPath,
199201
code: '',
200202
};
201203
const extendModel = await hook.beforeCompile(context);

0 commit comments

Comments
 (0)