简体   繁体   English

如何在 Visual Studio Code 中为文件扩展名创建模板?

[英]How can I create templates for file extensions in Visual Studio Code?

In my Vue.js projects almost all the times I need this code snippet as a template.在我的 Vue.js 项目中,几乎所有时候我都需要这个代码片段作为模板。

<template>
    <div>
    </div>
<template>

<script>

export default{
    data(){
        return{
        }
    },
    methods:{
    },
    created(){
    }
}
</script>

<style scoped>
</style>

Is there a way to tell Visual Studio Code each and every time I create a file with the extension .vue to automatically add that snippet to the file?每次我创建一个扩展名为.vue的文件时,有没有办法告诉 Visual Studio Code 自动将该代码段添加到文件中?

Simply, when I create new file with a certain extension, the predefined template for that extension should automatically be added to the file.简单地说,当我创建具有特定扩展名的新文件时,该扩展名的预定义模板应自动添加到文件中。

There isn't, not natively.没有,不是本地的。 But there is an extension called File Templates for VSCode that allows you to create your own file templates and generate from them.但是有一个名为File Templates for VSCode的扩展它允许您创建自己的文件模板并从中生成。 But I think you'd benefit from making an extension to do just that and maybe even more.但我认为你会从扩展中受益,甚至更多。

In the meantime, you can use a snippet to generate this instead of having to copy paste.同时,您可以使用代码片段来生成它,而不必复制粘贴。

Go to File > Preferences > User Snippets and choose Vue from the dropdown.转到File > Preferences > User Snippets,然后从下拉列表中选择Vue Vue will only show up if you have installed an extension that supports this file type. Vue 仅在您安装了支持此文件类型的扩展程序时才会显示。 In this case, I'd recommend Vetur , but you probably have it already.在这种情况下,我会推荐Vetur ,但您可能已经拥有它。

Then just add this entry to your vue.json file:然后只需将此条目添加到您的vue.json文件中:

"vuetpl" : {
        "body": [
            "<template>",
            "\t<div>",
            "\t\t$0",
            "\t</div>",
            "</template>",

            "<script>",

            "export default{",
            "\tdata(){",
                "\t\treturn{",
                    "\t\t\t",
                "\t\t}",
            "\t},",
            "\tmethods:{",
                "\t\t",
            "\t},",
            "\tcreated(){",
                "\t\t",
            "\t}",
            "}",
            "</script>",

            "<style scoped>",
            "</style>",
        ],
        "prefix": "vuetpl",
        "description": "Creates a new template."
    }

Then, when you create a new .vue file, just type vuetpl and press tab to autocomplete, and you'll have this:然后,当您创建一个新的.vue文件时,只需输入vuetpl并按tab键自动完成,您将拥有以下内容:

模板

Of course, you can also use a Snippet Generator to make your own snippets.当然,您也可以使用代码段生成器来制作自己的代码段。

The extension Auto Snippet does exactly that.扩展Auto Snippet正是这样做的。

You only need to configure two things:您只需要配置两件事:

  • The snippet's name片段名称
  • A filename pattern or a language for when the snippet should be applied应应用代码段的文件名模式语言

Recommendation推荐

The author has some very custom defaults, so as soon as you install it, modify its settings and remove those patterns that you won't need.作者有一些非常自定义的默认值,所以一旦你安装它,修改它的设置并删除那些你不需要的模式。

Otherwise, it will complain every time you create a file and doesn't find the snippet configured.否则,每次创建文件时它都会抱怨并且找不到配置的代码段。

This is being worked on now and is built-in to vscode v1.70 Insiders and may be in Stable v1.70 early August, 2022.此功能目前正在开发中,内置于 vscode v1.70 Insiders,可能会在 2022 年 8 月上旬在 Stable v1.70 中提供。

1. Make sure you haven't disabled this setting by setting it to hidden : 1. 确保您没有通过将其设置为hidden来禁用此设置:

// Controls if the untitled text hint should be visible in the editor.  
"workbench.editor.untitled.hint": "text",   // "text" is the default

2. Make some snippets that will serve as templates for whatever languages you are interested in in a snippets file (here they are in a global snippets file): 2. 制作一些片段,作为您在片段文件中感兴趣的任何语言的模板(这里它们位于全局片段文件中):

"vue template": {
    "isFileTemplate": true,  // changing to this soon
    "isTopLevel": true,  // was this
    "scope": "vue",

    "prefix": "vueTemplate",
    "body": [
        "const a = 'vue template'"
    ],
    "description": "vue template"
},

"javascript template": {
    "isFileTemplate": true,
    "scope": "javascript",

    "prefix": "jsTemplate",
    "body": [
        "const a = 'javascript template'"
    ],
    "description": "javascript template"
},

The isFileTemplate option is key here. isFileTemplate选项是这里的关键。 Any snippet with this option will appear in the following workflows.任何带有此选项的代码段都将出现在以下工作流程中。

If you have the "scope": "someLangID here" set in the keybinding then vscode can and will automatically change the current editor's language to that language ID.如果您在键绑定中设置了"scope": "someLangID here" ,那么 vscode 可以并且会自动将当前编辑器的语言更改为该语言 ID。

3. Create a new file: 3. 创建一个新文件:

a.一个。 with the command File: New Untitled File Ctrl + N使用命令File: New Untitled File Ctrl + N

Then you will see the message at the top of the file as in this demo:然后您将在文件顶部看到此演示中的消息:

start with snippet

Clicking on that will show any snippets with that "isFileTemplate": true, set.单击该按钮将显示带有"isFileTemplate": true, set。 Choosing one from the resulting QuickPick thaht opens up will input the snippet contents AND change the editor's language association to the scope value.从打开的生成的 QuickPick 中选择一个将输入片段内容并将编辑器的语言关联更改为scope值。

新文件的片段模板

b.湾。 You can also modify an existing file to the snippet content and language by using the command (found in the Command Palette)您还可以使用命令(在命令面板中找到)将现有文件修改为片段内容和语言
Snippets: Populate from Snippet

[This command workbench.action.populateFileFromSNippet does not have a default keybinding.] [此命令workbench.action.populateFileFromSNippet没有默认键绑定。]

As you can see in the demo, using this command will delete all the current contents of the file AND change the language association of that editor.正如您在演示中看到的,使用此命令将删除文件的所有当前内容并更改该编辑器的语言关联。

带有现有文件的片段模板


So making your initial snippets will probably be the hardest part, you might look into the snippet generator app .所以制作你的初始片段可能是最难的部分,你可能会查看片段生成器应用程序

There is a pretty good plugin called " Snippet Creator " that makes creating snippets painless.有一个名为“ Snippet Creator ”的相当不错的插件,可以轻松创建片段。

I wanted a quick "template" file that I could re-use.我想要一个可以重复使用的快速“模板”文件。 Copied the text, then used command "Create Snippet", and it was done in a couple of steps.复制文本,然后使用命令“创建代码段”,分几步完成。

It could also be used to create the same Vue templates mentioned above.它还可以用于创建与上述相同的 Vue 模板。 You can edit the snippet, insert your tab stops etc, just visit Code > Preferences > Configure User Snippets once created您可以编辑代码段、插入制表位等,只需访问代码 > 首选项 > 创建后配置用户代码段

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM