V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
banxi1988
V2EX  ›  Visual Studio Code

无痛创建复杂的 VSCode snippets 扩展的项目模板

  •  
  •   banxi1988 ·
    banxi1988 · 2018-05-14 17:21:23 +08:00 · 3436 次点击
    这是一个创建于 2145 天前的主题,其中的信息可能已经有所发展或是发生改变。

    无痛创建复杂的 VSCode snippets 扩展的项目模板, 了解一下。 https://github.com/banxi1988/vscode-snippets-ext-template

    核心要点: 原来很多 代码片段的内容段是直接在 snippets.json 文件中编写的。 这导致当代码片段中的内容比较复杂时,处理转义和缩进就比较麻烦了。比如:

    "Vue Single File Component": {
    		"prefix": "vbase",
    		"body": [
    			"<template>",
    			"\t<div>",
    			"",
    			"\t</div>",
    			"</template>",
    			"",
    			"<script>",
    			"\texport default {",
    			"\t\t${0}",
    			"\t}",
    			"</script>",
    			"",
    			"<style scoped>",
    			"",
    			"</style>"
    		],
    		"description": "Base for Vue File"
    	}
    

    正确的做法是将其中的 body 内容提取到一个单独的文件中。然后再合并生成最终的代码片段配置。

    在我的项目中,创建了一个 NodeJS 脚本用来处理这其中的合并。 简化之后配置可以这样写了。

    "vbase": {
     "description": "Vue Single File Component,Base for Vue File"
    }
    

    详细说明见 : https://github.com/banxi1988/vscode-snippets-ext-template 中的示例代码。

    一个已经采用以上项目做法的 VSCode 代码片段扩展项目见:

    https://github.com/wleven/wepy-snippets

    了解一下。

    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   981 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 22:00 · PVG 06:00 · LAX 15:00 · JFK 18:00
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.