【VSCode】自定义用户代码片段

VSCode添加代码片段操作方法

1、操作方式

  ①点击左下角【齿轮】打开【代码片段】;
  ②输入文件名(无后缀)并按Enter
  ③粘贴用户代码片段,并保存
在这里插入图片描述

2、代码

{
    // Verilog主代码
    // module 初始化
    "module_zcj": {
        "scope": "verilog, systemverilog",
        "prefix": "module_zcj",
        "body": [
            "`timescale 1ns/1ps",
            "",
            "module ${1:moduleName} (",
            "    input                       clk             ,",
            "    input                       rst             ,",
            "",
            "    ",
            ");",
            "",
            "",
            "",
            "",
            "",
            "endmodule"
        ],
        "description": "Custom modules"
    },
    // input 片段
    "input_1b_zcj": {
        "scope": "verilog, systemverilog",
        "prefix": "input_1b_zcj",
        "body": "input                       ${1:Interface},",
        "description": "Custom input"
    },
    "input_xb_zcj": {
        "scope": "verilog, systemverilog",
        "prefix": "input_xb_zcj",
        "body": "input               [ x:0]  ${1:Interface},",
        "description": "Custom input"
    },
    // output 片段
    "output_reg_1b_zcj": {
        "scope": "verilog, systemverilog",
        "prefix": "output_reg_1b_zcj",
        "body": "output  reg                 ${1:Interface}",
        "description": "Custom output"
    },
    "output_reg_xb_zcj": {
        "scope": "verilog, systemverilog",
        "prefix": "output_reg_xb_zcj",
        "body": "output  reg         [ x:0]  ${1:Interface}",
        "description": "Custom output"
    },
    "output_wire_1b_zcj": {
        "scope": "verilog, systemverilog",
        "prefix": "output_wire_1b_zcj",
        "body": "output                      ${1:Interface}",
        "description": "Custom output"
    },
    "output_wire_xb_zcj": {
        "scope": "verilog, systemverilog",
        "prefix": "output_wire_xb_zcj",
        "body": "output              [ x:0]  ${1:Interface}",
        "description": "Custom output"
    },
    // reg 片段
    "reg_1b_zcj": {
        "scope": "verilog, systemverilog",
        "prefix": "reg_1b_zcj",
        "body": "reg                             ${1:signal};",
        "description": "Custom reg"
    },
    "reg_xb_zcj": {
        "scope": "verilog, systemverilog",
        "prefix": "reg_xb_zcj",
        "body": "reg                 [ x:0]      ${1:signal};",
        "description": "Custom reg"
    },
    "reg_xb_signed_zcj": {
        "scope": "verilog, systemverilog",
        "prefix": "reg_xb_signed_zcj",
        "body": "reg     signed      [ x:0]      ${1:signal};",
        "description": "Custom reg"
    },
    // wire 片段
    "wire_1b_zcj": {
        "scope": "verilog, systemverilog",
        "prefix": "wire_1b_zcj",
        "body": "wire                            ${1:signal};",
        "description": "Custom wire"
    },
    "wire_xb_zcj": {
        "scope": "verilog, systemverilog",
        "prefix": "wire_xb_zcj",
        "body": "wire                [ x:0]      ${1:signal};",
        "description": "Custom wire"
    },
    "wire_xb_signed_zcj": {
        "scope": "verilog, systemverilog",
        "prefix": "wire_xb_signed_zcj",
        "body": "wire    signed      [ x:0]      ${1:signal};",
        "description": "Custom reg"
    },
    // 逻辑描述片段
    "Sequential logic circuit high": {
        "scope": "verilog, systemverilog",
        "prefix": "shixu_high_zcj",
        "body": [
            "always @(posedge clk or posedge rst) begin",
            "    if (rst) begin",
            "        ${1:varName} <= 'd0;",
            "    end",
            "    else if ($2) begin",
            "        $3",
            "    end",
            "    else begin",
            "        $4",
            "    end",
            "end"
        ],
        "description": "Sequential logic circuit"
    },
    // 逻辑描述片段
    "Sequential logic circuit low": {
        "scope": "verilog, systemverilog",
        "prefix": "shixu_low_zcj",
        "body": [
            "always @(posedge clk or negedge rst_n) begin",
            "    if (!rst_n) begin",
            "        ${1:varName} <= 'd0;",
            "    end",
            "    else if ($2) begin",
            "        $3",
            "    end",
            "    else begin",
            "        $4",
            "    end",
            "end"
        ],
        "description": "Sequential logic circuit"
    },
    "Combinatorial logic circuit": {
        "scope": "verilog, systemverilog",
        "prefix": "zuhe_zcj",
        "body": [
            "always @(*) begin",
            "    ${1:body}",
            "end"
        ],
        "description": "Combinatorial logic circuit"
    },
    "if-else_zcj": {
        "scope": "verilog, systemverilog",
        "prefix": "ifel_zcj",
        "body": [
            "if (${1:condition}) begin",
            "    $2",
            "end",
            "else begin",
            "    $3",
            "end",
        ],
        "description": "if-else"
    },
    "if-elseif_zcj": {
        "scope": "verilog, systemverilog",
        "prefix": "ifelif_zcj",
        "body": [
            "if (${1:condition}) begin",
            "    $2",
            "end",
            "else if ($3) begin",
            "    $4",
            "end",
            "else begin",
            "    $5",
            "end"
        ],
        "description": "if-elseif"
    },
    "elseif_zcj": {
        "scope": "verilog, systemverilog",
        "prefix": "elif_zcj",
        "body": [
            "else if (${1:condition}) begin",
            "    $2",
            "end",
        ],
        "description": "else if"
    },
    "else_zcj": {
        "scope": "verilog, systemverilog",
        "prefix": "else_zcj",
        "body": [
            "else begin",
            "    $1",
            "end",
        ],
        "description": "else"
    },
    "assign_zcj": {
        "scope": "verilog, systemverilog",
        "prefix": "assign_zcj",
        "body": [
            "assign $1 = $2;"
        ],
        "description": "assign"
    },
    "jishu_high": {
        "scope": "verilog, systemverilog",
        "prefix": "jishu_high_zcj",
        "body": [
            "reg                 [ x:0]      cnt_demo;",
            "wire                            add_cnt_demo;",
            "wire                            end_cnt_demo;",
            "",
            "always @(posedge clk or posedge rst) begin",
            "    if (rst) begin",
            "        cnt_demo <= 'd0;",
            "    end",
            "    else if (add_cnt_demo) begin",
            "        if (end_cnt_demo) begin",
            "            cnt_demo <= 'd0;",
            "        end",
            "        else begin",
            "            cnt_demo <= cnt_demo + 1'b1;",
            "        end",
            "    end",
            "    else begin",
            "        cnt_demo <= 'd0;",
            "    end",
            "end",
            "assign add_cnt_demo = ;",
            "assign end_cnt_demo = add_cnt_demo && cnt_demo >= ;"
        ],
        "description": "Sequential logic circuit"
    },
    "jishu_low": {
        "scope": "verilog, systemverilog",
        "prefix": "jishu_low_zcj",
        "body": [
            "reg                 [ x:0]      cnt_demo;",
            "wire                            add_cnt_demo;",
            "wire                            end_cnt_demo;",
            "",
            "always @(posedge clk or negedge rst_n) begin",
            "    if (!rst_n) begin",
            "        cnt_demo <= 'd0;",
            "    end",
            "    else if (add_cnt_demo) begin",
            "        if (end_cnt_demo) begin",
            "            cnt_demo <= 'd0;",
            "        end",
            "        else begin",
            "            cnt_demo <= cnt_demo + 1'b1;",
            "        end",
            "    end",
            "    else begin",
            "        cnt_demo <= 'd0;",
            "    end",
            "end",
            "assign add_cnt_demo = ;",
            "assign end_cnt_demo = add_cnt_demo && cnt_demo >= ;"
        ],
        "description": "Sequential logic circuit"
    },
    // 仿真文件
    "module_tb_zcj": {
        "scope": "verilog, systemverilog",
        "prefix": "module_tb_zcj",
        "body": [
            "`timescale 1ns/1ps",
            "",
            "module tb_${1:moduleName};",
            "",
            "    localparam  CYC_256     =   1       ,",
            "                CYC_200     =   1.28    ,",
            "                CYC_128     =   2       ,",
            "                CYC_125     =   2.048   ,",
            "                CYC_64      =   4       ,",
            "                CYC_32      =   8       ,",
            "                CYC_16      =   16      ;",
            "",
            "    reg                             clk_256m        ;",
            "    reg                             clk_200m        ;",
            "    reg                             clk_128m        ;",
            "    reg                             clk_125m        ;",
            "    reg                             clk_64m         ;",
            "    reg                             clk_32m         ;",
            "    reg                             clk_16m         ;",
            "    reg                             rst             ;",
            "",
            "",
            "",
            "",
            "",
            "    initial begin",
            "        clk_256m = 1'b1;",
            "        clk_200m = 1'b1;",
            "        clk_128m = 1'b1;",
            "        clk_125m = 1'b1;",
            "        clk_64m  = 1'b1;",
            "        clk_32m  = 1'b1;",
            "        clk_16m  = 1'b1;",
            "        rst      = 1'b1;",
            "        #(3 * CYC_16);",
            "        rst      = 1'b0;",
            "    end",
            "    always #(0.5  ) clk_256m = ~clk_256m;",
            "    always #(0.64 ) clk_200m = ~clk_200m;",
            "    always #(1    ) clk_128m = ~clk_128m;",
            "    always #(1.024) clk_125m = ~clk_125m;",
            "    always #(2    ) clk_64m  = ~clk_64m;",
            "    always #(4    ) clk_32m  = ~clk_32m;",
            "    always #(8    ) clk_16m  = ~clk_16m;",
            "",
            "    initial begin",
            "        ",
            "        ",
            "    end",
            "",
            "",
            "",
            "endmodule"
        ],
        "description": "Custom simulation modules"
    },
    "task_zcj": {
        "scope": "verilog, systemverilog",
        "prefix": "task_zcj",
        "body": [
            "task ${1:name};",
            "    begin",
            "        $2",
            "    end",
            "endtask"
        ],
        "description": "task"
    },
    "fork-join": {
        "scope": "verilog, systemverilog",
        "prefix": "fork-join",
        "body": [
            "fork",
            "    $1",
            "join"
        ],
        "description": "task"
    },
    // 管脚分配
    "set_pin": {
        "scope": "verilog, systemverilog",
        "prefix": "guanjiao_zcj",
        "body": [
            "set_location_assignment PIN_E1  -to clk",
            "set_location_assignment PIN_E15 -to rst_n",
            "set_location_assignment PIN_${1:x} -to ",
        ],
        "description": "task"
    },
    // 注释
    "note1": {
        "scope": "verilog, systemverilog",
        "prefix": "zhushi_main_zcj",
        "body": [
            "//=======================================================//",
            "// ${1:note}",
            "//=======================================================//"
        ],
        "description": "note1"
    },
    "note2": {
        "scope": "verilog, systemverilog",
        "prefix": "zhushi_minor_zcj",
        "body": [
            "//------------------- "
        ],
        "description": "note2"
    }
}
### 如何在 VSCode 中创建和使用自定义代码片段 #### 一、VSCode 代码片段概述 代码片段是在编写代码过程中可以重用的小部分代码模板。通过这些预设好的结构化代码段,开发者能够显著提高编码效率并减少错误的发生率[^1]。 #### 二、创建自定义代码片段的具体步骤 ##### 打开代码片段配置 为了开始设置个人化的快捷方式或者常用的函数模式,在 Visual Studio Code 内部可以通过命令面板 (`Ctrl+Shift+P` 或 `Cmd+Shift+P`) 输入 "Preferences: Configure User Snippets" 来访问全局或特定语言环境下的代码片段编辑界面。 ##### 选择代码片段的适用范围 当首次尝试添加新的代码片段时,会提示选择该片段适用于哪种编程语言。这一步骤决定了所创建的代码片段将在哪些类型的文件中可用。如果希望某个代码片段可以在任何地方被调用,则可以选择 Global (全局)。 ##### 创建和编辑代码片段 一旦选择了合适的上下文之后就可以正式着手构建自己的代码片段了。对于每一个新加入的内容来说,都需要指定触发词(prefix)、描述以及主体(body),其中 body 部分支持多种语法特性如占位符、选项列表等特殊标记来增强灵活性[^2]。 ```json { "Print to console": { "prefix": "log", "body": [ "console.log('$1');", "$2" ], "description": "Log output to console" } } ``` 上述 JSON 片段展示了如何定义一个简单的 JavaScript 控制台日志记录语句作为可复用单元;这里 `$1`, `$2` 表示光标的停留位置或是待填充区域[^3]。 ##### 代码片段示例 考虑这样一个场景——频繁地书写 HTML 结构标签,那么完全可以按照上面提到的方式建立如下所示的一个简单却实用的例子: ```json { "HTML Template": { "prefix": "htmltemp", "body": [ "<!DOCTYPE html>", "<html lang=\"en\">", "\t<head>", "\t\t<meta charset=\"UTF-8\">", "\t\t<title>$1</title>", "\t</head>", "\t<body>", "\t\t$0", "\t</body>", "</html>" ], "description": "Basic HTML template with DOCTYPE and meta tags." } } ``` 此代码片段允许快速生成标准的 HTML 文件框架,并且提供了两个插入点用于进一步定制页面标题和其他内部元素。 #### 三、自定义代码片段的高级用法 除了基本的功能之外,还可以利用更多复杂的机制来自定义工作流中的常用表达式或者是更复杂的数据处理逻辑。例如引入动态变量替换、多行同时操作等功能以满足不同层次的需求。 #### 四、代码片段的管理与分享 随着积累越来越多个性化的代码片段库,有效的管理和分类变得尤为重要。一方面要保持良好的命名习惯以便于查找检索;另一方面也可以借助社区力量或者其他插件工具来进行跨平台同步更新。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值