简体   繁体   English

vscode 将用户片段映射到文件扩展名

[英]vscode map user snippets to file extensions

VSCode has the great feature of per-language user snippets (eg in xml.json ). VSCode 具有每个语言用户片段的强大功能(例如在xml.json )。

Is there a way to map those snippets to file extensions ?有没有办法将这些片段映射到文件扩展名

My situation: I develop a lot of xml lately.我的情况:我最近开发了很多xml。 We have roughly three xsd files and name instances of them with file extensions (so an instance of foo.xsd is named my-instance.foo rather than my-instance.xml ).我们有大约三个 xsd 文件并用文件扩展名命名它们的实例(因此foo.xsd的实例被命名为my-instance.foo而不是my-instance.xml )。 I build the most common parts as user snippets to save my time.我将最常见的部分构建为用户片段以节省我的时间。 However the different xsds create similar elements with different meanings (eg one creates <foo bar="baz"/> , one creates <foo baz="bar"/> ), so defining a user snippet with prefix "foo" for the xml language would be ambique.然而,不同的 xsds 创建了具有不同含义的相似元素(例如,一个创建了<foo bar="baz"/> ,一个创建了<foo baz="bar"/> ),因此为xml定义一个带有前缀"foo"的用户片段语言将是模棱两可的。

I could prefix the prefixes (creating eg afoo and bfoo ), but this feels unnatural.我可以添加前缀(例如创建afoobfoo ),但这感觉不自然。 Is there a better way to say "although both foo s belong to the xml language, one belongs to files of type x and one to y "?有没有更好的说法“虽然foo属于 xml 语言,但一个属于x类型的文件,一个属于y类型”?

Visual Studio Code doesn't currently supoport different snippets per file extension that work on the same language. Visual Studio Code 当前不支持针对同一语言的每个文件扩展名使用不同的片段。

But there is a hacky workaround for it by defining a separate language for xsd .但是通过为xsd定义一种单独的语言,有一个hacky 解决方法。 Decide yourself if you want to do it.自己决定是否要这样做。 I tested it on Windows with version 0.8.0 (insider preview).我在 Windows 版本 0.8.0(内部预览)上对其进行了测试。

  1. Go to the plugins folder of VSCode: %CODE%\\resources\\app\\plugins and create a copy of vs.language.xml .转到 VSCode 的 plugins 文件夹: %CODE%\\resources\\app\\plugins并创建vs.language.xml的副本。 Name the copied folder vs.language.xsd .将复制的文件夹vs.language.xsd

  2. Open vs.language.xsd in VSCode.打开vs.language.xsd在VSCode。

  3. Replace the content of ticino.plugin.json withticino.plugin.json的内容替换为

    { "pluginId": "vs.language.xsd", "activationEvents": ["textModel:xsd"], "mainModule": "./out/xmlMain", "contributes": { "language": [{ "id": "xsd", "extensions": [ ".xsd", ".foo"], "firstLine" : "(\\\\<\\\\?xml.*)|(\\\\<svg)|(\\\\<\\\\!doctype\\\\s+svg)", "aliases": [ "XSD", "xsd" ], "mimetypes": ["text/xml", "application/xml", "application/xaml+xml"] }] }, "scripts": { "compile": "node ../../node_modules/gulp/bin/gulp.js --gulpfile ../../gulpfile.plugins.js compile-plugin:vs.language.xsd" }, "_bundling": [ { "name": "./out/xmlMain" }, { "name": "./out/xmlWorker" } ] }
  4. Open to xmlDef.json .打开xmlDef.json Change the value of displayName from XML to XSD .displayName的值从XML更改为XSD Change the value of name from xml to xsd .name的值从xml更改为xsd

  5. List Replace the content of xmlMain.js with List 将xmlMain.js的内容替换为

    'use strict'; define(["require", "exports", './xmlDef', 'monaco'], function (require, exports, languageDef, monaco) { function activate() { monaco.Modes.registerMonarchDefinition('xsd', languageDef.language); var myWorker = monaco.Modes.loadInBackgroundWorker(require.toUrl('./xmlWorker.js')); function format(resource, range, options) { return myWorker.then(function (w) { var model = monaco.Services.ModelService.getModel(resource); var value = range ? model.getValueInRange(range) : model.getValue(); if (value.length < 1024 * 1024) { return w.beautify(value, { indent_size: options.insertSpaces ? options.tabSize : 1, indent_char: options.insertSpaces ? ' ' : '\\t', wrap_line_length: 256 }); } else { return null; } }).then(function (result) { if (result) { return [{ range: range, text: result }]; } else { return null; } }); } monaco.Modes.FormattingSupport.register('xsd', { formatDocument: function (resource, options) { return format(resource, null, options); }, formatRange: function (resource, range, options) { return format(resource, range, options); } }); } exports.activate = activate; });
  6. Restart VSCode.重新启动 VSCode。 Now you are able to go to File -> Preferences - User Snippets and select the language XSD .现在您可以转到File -> Preferences - User Snippets并选择语言XSD You can define all .xsd and .foo related snippets here.您可以在此处定义所有与.xsd.foo相关的片段。

As I mentioned before: This is really hacky.正如我之前提到的:这真的很糟糕。 The code highlighting looks slightly different in XSD files after applying this.应用此代码后,XSD 文件中的代码突出显示略有不同。 If you don't like the result then you simply need to delete the folder vs.language.xsd and everything works like it did before.如果您不喜欢结果,那么您只需删除文件夹vs.language.xsd ,一切都像以前一样。

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

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