vscode自定义代码片段

发布时间:2022-10-09 00:08:52 阅读:711次

之前使用过phpstorm的自定义代码段

这可以很大程度的提高编码的效率

在vscode中如何实现

只需要点击

文件|首选项|配置用户代码片段

选择php.json

输入以下内容

{
    // Place your snippets for php here. Each snippet is defined under a snippet name and has a prefix, body and 
    // description. The prefix is what is used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
    // $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders. Placeholders with the 
    // same ids are connected.
    // Example:
    "Print to console": {
        "prefix": "log",
        "body": [
            "console.log('$1');",
            "$2"
        ],
        "description": "Log output to console"
    },    

    "Print to exception": {
        "prefix": "prexception",
        "body": [        
            "try{"
            "    throw new \\Exception('$1');"
            "}catch (\\Exception $$e){"
            "    return $$this->jsonError($$e->getMessage());"
            "}"
        ],
        "description": "Log output to printr"
    },

    "Print to transaction": {
        "prefix": "prtrans",
        "body": [
            echo "DB::beginTransaction();"
            echo "try{"                
            echo "    DB::commit();"                
            echo "}catch(\\Exception $$e){"
            echo "    echo $$e->getMessage().'<br>'.$$e->getCode().'<br>';"
            echo "    DB::rollBack();"                
            echo "}"
        ],
        "description": "Log output to printr"
    },
    
    "Print to function": {
        "prefix": "prfunc",
        "body": [
            "public $1function $2(){"
            ""
            "}"
        ],
        "description": "Log output to printr"
    },

    "Print to db": {
        "prefix": "usedb",
        "body": [
            "use Illuminate\\Support\\Facades\\DB;"
        ],
        "description": "Log output to printr"
    },

    "Print to return": {
        "prefix": "ret",
        "body": [
            "return $1;"
        ],
        "description": "Log output to printr"
    },

    "Print to pre": {
        "prefix": "pre",
        "body": [
            echo "echo \"<pre>\";"
            echo "print_r($1);"            
            echo "echo \"</pre>\";"
        ],
        "description": "Log output to printr"
    },

    "Print to log": {
        "prefix": "prlog",
        "body": [
            "\\Log::info($1);"
        ],
        "description": "Log output to printr"
    },

}

这样当你输入prtrans会自动输出以下代码

    DB::beginTransaction();
    try{
        DB::commit();
    }catch(\Exception $e){
        echo $e->getMessage().'<br>'.$e->getCode().'<br>';
        DB::rollBack();
    }

大大提升编码效率

如有问题,可以QQ搜索群1028468525加入群聊,欢迎一起研究技术

支付宝 微信

有疑问联系站长,请联系QQ:QQ咨询

转载请注明:vscode自定义代码片段 出自老鄢博客 | 欢迎分享