feat: 优化代码节点 main 编写体验
- 更新代码节点帮助、占位示例与补全片段 - 保留 _result 旧版兼容提示并补充补全测试
This commit is contained in:
@@ -52,6 +52,25 @@ describe('codeCompletion utils', () => {
|
||||
).toBe(true);
|
||||
});
|
||||
|
||||
it('should prefer main return snippets for javascript output', () => {
|
||||
const completions = createBusinessCompletions('javascript', []);
|
||||
const mainCompletion = completions.find((item) => item.label === 'main');
|
||||
const resultCompletion = completions.find(
|
||||
(item) => item.label === 'result-object',
|
||||
);
|
||||
|
||||
expect(mainCompletion).toMatchObject({
|
||||
type: 'snippet',
|
||||
detail: '代码节点入口',
|
||||
boost: 1000,
|
||||
});
|
||||
expect(String(mainCompletion?.apply)).toContain('function main');
|
||||
expect(String(mainCompletion?.apply)).toContain('return {');
|
||||
expect(String(resultCompletion?.apply)).toBe(
|
||||
"return { message: 'ok', data };",
|
||||
);
|
||||
});
|
||||
|
||||
it('should detect blocked nodes for comment/string/property contexts', () => {
|
||||
expect(shouldSkipBusinessCompletion('Comment')).toBe(true);
|
||||
expect(shouldSkipBusinessCompletion('LineComment')).toBe(true);
|
||||
|
||||
@@ -30,21 +30,28 @@ const BLOCKED_NODE_NAMES = new Set([
|
||||
]);
|
||||
|
||||
const JS_SNIPPETS: Array<{ label: string; insert: string; detail: string }> = [
|
||||
{
|
||||
label: 'main',
|
||||
detail: '代码节点入口',
|
||||
insert:
|
||||
'function main({ input }) {\n return {\n output: input,\n };\n}',
|
||||
},
|
||||
{
|
||||
label: 'if-else',
|
||||
detail: '条件分支',
|
||||
insert:
|
||||
'if (condition) {\n _result.value = value;\n} else {\n _result.value = null;\n}',
|
||||
'if (condition) {\n return { value };\n}\nreturn { value: null };',
|
||||
},
|
||||
{
|
||||
label: 'for-of',
|
||||
detail: '遍历数组',
|
||||
insert: 'for (const item of items) {\n // TODO\n}\n_result.done = true;',
|
||||
insert:
|
||||
'const result = [];\nfor (const item of items) {\n result.push(item);\n}\nreturn { result };',
|
||||
},
|
||||
{
|
||||
label: 'result-object',
|
||||
detail: '返回对象',
|
||||
insert: "_result.message = 'ok';\n_result.data = data;",
|
||||
insert: "return { message: 'ok', data };",
|
||||
},
|
||||
];
|
||||
|
||||
@@ -99,7 +106,7 @@ export function createBusinessCompletions(
|
||||
const resultCompletion: Completion = {
|
||||
label: '_result',
|
||||
type: 'variable',
|
||||
detail: '代码节点输出对象',
|
||||
detail: '兼容旧版输出对象',
|
||||
boost: 900,
|
||||
};
|
||||
|
||||
@@ -118,7 +125,7 @@ export function createBusinessCompletions(
|
||||
type: 'snippet',
|
||||
detail: snippet.detail,
|
||||
apply: snippet.insert,
|
||||
boost: 500,
|
||||
boost: snippet.label === 'main' ? 1000 : 500,
|
||||
}));
|
||||
|
||||
return dedupeCompletions([
|
||||
|
||||
Reference in New Issue
Block a user