Vscode代码段变量 [英] Vscode snippet variable

查看:275
本文介绍了Vscode代码段变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

"snippet with class binding":{
    "prefix": "row.${variable}",
    "body":[
        "<table class=\"row ${same_variable_here}\">",
        "\t<tr>",
        "\t\t<td>",
        "\t\t\t$0",
        "\t\t</td>",
        "\t</tr>",
        "</table>"
    ]
}

是否有可能(以及如何创建)像 some_entity.classname 这样的变量
扩展为以下内容(例如html):

Is it possible(and how if so) to create variables like some_entity.classname expanding into something like this(in html for example):

<div class="classname"></div>


推荐答案

似乎您有两个问题。是的,emmet扩展会自动将
div.myClass 转换为< div class = myClass>< / div> 。请参见vscode中的 emmet

It looks like you have two questions there. Yes, emmet expansion will automatically turn div.myClass into <div class="myClass"></div>. See emmet in vscode.

您的另一个问题是关于全表扩展的emmet代码段。请参阅自定义emmet片段。在您的settings.json中,您将需要:

Your other question is about a emmet snippet for a full table expansion. See custom emmet snippets. In your settings.json you will need:

  "emmet.extensionsPath": "C:\\Users\\Mark\\.vscode\\extensions"

该指向文件夹包含您将创建的名为 snippets.json 的新文件。在该文件中放入:

That should point to a folder that contains a new file that you will create called snippets.json. In that file put:

{
  "html": {
    "snippets": {
        "tableR": 
          "table.row.$1>tr>td"
    }
  }
}

使用除 tableR 之外的所需前缀。然后,您必须重新加载 vscode。然后输入前缀和制表符进行扩展(假设您的设置中包含emmet制表符扩展设置。)

Use whatever prefix you want besides "tableR". Then you must reload vscode. Then type your prefix and tab to expand (assuming you have the emmet tab expansion setting in your settings.]

:根据下面的评论,也许您是寻找带有键绑定的代码段之类的简单内容:

: Based on your comment below, perhaps you are looking for something as simple as a snippet with a keybinding:

{
    "key": "ctrl+alt+n",
    "command": "editor.action.insertSnippet",
    "when": "editorTextFocus",
    "args": {
      "snippet": "${TM_SELECTED_TEXT/(.*)\\.(.*)/<$1 class=\"$2\"><\\/$1>/}"
    }
},

因此,如果您选择 anyTag.someClass 在使用任何键绑定时变为< anyTag class = someClass>< / anyTag>
已经选择了Emmet,这里只是keybindings.json中的一个简单的keybinding(可以根据需要将其限制为某些语言)Emmet扩展不允许您以其方式转换其前缀(上面的regexp)一个简单的代码段即可抓取所选内容或当前单词并将其转换。

So if you select anyTag.someClass becomes <anyTag class="someClass"></anyTag> when you use whatever keybinding you have chosen. Emmet is not involved here, this is just a simple keybinding in your keybindings.json (you can limit it to certain languages if you wish). Emmet expansion does not allow you to transform its prefix (the regexp above) the way a plain snippet can grab the selection or current word and transform it.

这篇关于Vscode代码段变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆