vscode 代码片段 php.json

1.设置-》用户代码片段-》新建全局代码片段文件-》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"
    // }
    "public": {
        "prefix": "public",
        "body": [
       
            "public function $1($2):$3",
            "{",
            "    $4",
            "}"
        ],
        "description": "Log output to console"
    },
    "protected": {
        "prefix": "protected",
        "body": [
       
            "protected function $1($2):$3",
            "{",
            "    $4",
            "}"
        ],
        "description": "Log output to console"
    },
    "private": {
        "prefix": "private",
        "body": [
       
            "private function $1($2):$3",
            "{",
            "    $4",
            "}"
        ],
        "description": "Log output to console"
    },
    "static": {
        "prefix": "static",
        "body": [
       
            "public static function $1($2):$3",
            "{",
            "    $4",
            "}"
        ],
        "description": "Log output to console"
    },
    "foreach": {
        "prefix": "foreach",
        "body": [
            "foreach($1 as \\$key=>\\$item){",
            "    $2",
            "}"
        ],
        "description": "Log output to console"
    },
    "if": {
        "prefix": "if",
        "body": [
            "if($1){",
            "    $2",
            "}"
        ],
        "description": "Log output to console"
    },
    "startTrans": {
        "prefix": "startTrans",
        "body": [
            "Db::startTrans();",
            "try {",
            "    $1",
            "    // 提交事务",
            "    Db::commit();",
            "} catch (\\Exception \\$e) {",
            "    // 回滚事务",
            "    Db::rollback();",
            "    throwError(\\$e->getMessage());",
            "}",
            "return true;",
        ],
        "description": "Log output to console"
    },
    "transaction": {
        "prefix": "transaction",
        "body": [
            "transaction(function () use ($1) {",
            "    $2",
            "});",
        ],
        "description": "Log output to console"
    },
}

2.在设置中搜索到editor.snippetSuggestions这个设置,其中有四个选项top、bottom、inline、none四个选项,选择top即可
作用:使用户自定义的代码片段在提示的最上面

 

posted @ 2024-01-02 09:51  会飞的鹏  阅读(70)  评论(0编辑  收藏  举报