egomobile.vscode-powertools 插件使用,生成代码片段

  • 添加配置到编辑器

"ego.power-tools": {
    "buttons": [
      {
        "text": "orm_json_set",
        "tooltip": "生成php-orm的get/setter",
        "action": {
          "type": "script",
          "script": "orm_json_set.js"
        }
      },
      {
        "text": "orm_money_set",
        "tooltip": "生成php-orm的get/setter",
        "action": {
          "type": "script",
          "script": "orm_money_set.js"
        }
      }
    ]
  }
  • vscode的api

  • 根据配置文件在.vscode文件夹创建对应的js

    • .vscode\orm_json_set.js

exports.execute = async (args) => {
    const vscode = args.require('vscode');
    const activeEditor = vscode.window.activeTextEditor;
    // vscode.window.showInformationMessage(
    //     getter("info_data")
    // );
    function rand(min, max) {
        return Math.floor(Math.random() * (max - min)) + min;
    }
    const commodName = 'case-transform.pascal' + rand(10000, 99999);
    vscode.commands.registerTextEditorCommand(commodName, (textEditor, edit) => {
        textEditor.selections.forEach((item) => {
            var select_str = textEditor.document.getText(item);
            if (select_str) {
                edit.replace(item, setter(select_str) + getter(select_str))
            }
        })
    })

    // 转帕斯卡
    function addSeparator(str) {
        let arr = str.split('_');
        for (let i = 0; i < arr.length; i++) {
            arr[i] = arr[i].slice(0, 1).toUpperCase() + arr[i].slice(1)
        }
        return arr.join('')
    }
    vscode.commands.executeCommand(commodName);
    function setter($filed_name) {
        var s = addSeparator($filed_name);
        var sl = $filed_name;
        return `
    public function set${s}Attribute($value)
    {
        $this->attributes['${sl}'] = json_encode(empty($value) ? [] : $value, 256 + 512);
    }
        `;
    }
    function getter($filed_name) {
        var s = addSeparator($filed_name);
        return `
    public function get${s}Attribute($value)
    {
        if (empty($value)) {
            return [];
        }
        $v = jsonToArray($value);
        if (empty($v)) {
            return [];
        }
        return $v;
    }
        `;
    }

};
  • .vscode\orm_money_set.js

exports.execute = async (args) => {
    const vscode = args.require('vscode');
    const activeEditor = vscode.window.activeTextEditor;
    // vscode.window.showInformationMessage(
    //     getter("info_data")
    // );
    function rand(min, max) {
        return Math.floor(Math.random() * (max - min)) + min;
    }
    const commodName = 'case-transform.pascal' + rand(10000, 99999);
    vscode.commands.registerTextEditorCommand(commodName, (textEditor, edit) => {
        textEditor.selections.forEach((item) => {
            var select_str = textEditor.document.getText(item);
            if (select_str) {
                edit.replace(item, setter(select_str) + getter(select_str))
            }
        })
    })

    // 转帕斯卡
    function addSeparator(str) {
        let arr = str.split('_');
        for (let i = 0; i < arr.length; i++) {
            arr[i] = arr[i].slice(0, 1).toUpperCase() + arr[i].slice(1)
        }
        return arr.join('')
    }
    vscode.commands.executeCommand(commodName);
    function setter($filed_name) {
        var s = addSeparator($filed_name);
        var sl = $filed_name;
        return `
    public function set${s}Attribute($value)
    {
        $this->attributes['${sl}'] = bcmul((string)$value, '100', 2);
    }
        `;
    }
    function getter($filed_name) {
        var s = addSeparator($filed_name);
        return `
    public function get${s}Attribute($value)
    {
        return bcdiv((string)$value, '100', 2);
    }
        `;
    }

};
  • 使用,选中文本然后点击按钮

ptbfR.png