vscode-plugin-command-runner README
一个可以自定义 js 命令的插件, 自定义命令可以使用 vscode 的基础功能
示例
示例配置如下
-
- 在工程根目录, 创建配置文件:
command-runner.json
[
{
"label": "生成FormItem",
"jsModule": "./commands/GenFormItem.js"
}
]
示例脚本功能:
解析光标当前行的文本, 生成 FormItem
// GenFormItem.js
async ({ vscode }) => {
const editor = vscode.window.activeTextEditor;
if (editor) {
// 获取光标所在位置
const position = editor.selection.active;
// 获取当前行
const line = editor.document.lineAt(position.line);
const startIndex = line.firstNonWhitespaceCharacterIndex;
const endIndex = line.range.end.character;
// 构造有效部分的范围
const range = new vscode.Range(
position.line,
startIndex,
position.line,
endIndex
);
const lineContent = line.text;
const [formItemText, controllerText] = lineContent.trim().split(";");
const [label, name, span2 = ""] = formItemText.split(",");
const [controllerName, modelName = name, enumName = ""] =
controllerText.split(",");
let result = `<NFormItem label="${label}" name="${name}" ${span2}>
<${controllerName} v-model:value="model.${modelName}" ${
enumName ? 'name="' + enumName + '"' : ""
} />
</NFormItem>`;
/**
* 标题 name span2;NInput
* =>
* <NFormItem label="标题" name="name" span2>
<NInput v-model:value="model.name" />
</NFormItem>
*/
// 替换当前行的文本
editor.edit((builder) => {
builder.replace(range, result);
});
// vscode.env.clipboard.writeText(line).then(() => {
// vscode.window.showInformationMessage(line);
// });
}
};
a. 输入命令, > Command Runner: ShowCommands
或者 快捷键 Cmd+E(mac), Ctrl+E(win), 可以打开脚本菜单
b. 选择脚本, 回车就可以执行
Features
可运行 js 脚本
Requirements
None
Extension Settings
None for now
Known Issues
None
Release Notes
Users appreciate release notes as you update your extension.
0.0.1
a demo version
Following extension guidelines
None
Enjoy!