同步代码和同步代码块的区别_在vs代码中创建和同步个人片段

同步代码和同步代码块的区别

I recently started a new side project in a language (Perl) and framework (Logitech Media Server aka Slim Server) I’d never used before. This combination and my lack of experience meant there was no way of debugging. It was logging everything and muddling your way through.

我最近用一种语言( Perl )和框架( Logitech Media Server aka Slim Server)开始了一个我从未使用过的新项目。 这种结合以及我缺乏经验意味着无法进行调试。 它正在记录所有内容,使您迷惑不解。

Don’t panic! What I’m going to describe is completely agnostic to the language/frameworks used.

不要惊慌! 我要描述的内容完全与所使用的语言/框架无关。

I use Visual Studio Code as my editor of choice for most things these days and I was getting fed up with typing the same old things over and over, so I looked into what options VS Code had to make some of this repetition easier.

如今,我将Visual Studio Code用作大多数事情的首选编辑器,而且我一遍又一遍地键入相同的旧内容,已经受够了,因此我研究了VS Code必须使用哪些选项才能使这种重复变得更容易。

It turns out VS Code has a very mature snippet facility and getting it working was pretty trivial. This isn’t going to be a tutorial on everything snippet-related, but it will get you started with what I found useful.

事实证明,VS Code具有非常成熟的摘要功能,并且使其正常工作是微不足道的。 这不会成为与片段相关的所有内容的教程,但是它将使您开始使用我认为有用的内容。

Snippets are at three levels: project, language, and global. For my use case, I wanted some language-level ones and some global ones since I’m looking at more than one project using Slim.

片段分为三个级别:项目,语言和全局。 对于我的用例,我需要一些语言级别的语言和一些全局语言的语言,因为我正在研究使用Slim的多个项目。

Within VS Code, you can add snippets using File > Preferences > User Snippets (Code > Preferences > User Snippets on macOS).

在VS Code中,您可以使用文件>首选项>用户代码段(在macOS上为代码>首选项>用户代码段)添加代码段。

  • If you are creating a language snippet, choose the language.

    如果要创建语言片段,请选择语言。
  • If you are creating a global (framework) snippet, choose New Global Snippets File.

    如果要创建全局(框架)代码段,请选择“新建全局代码段文件”。
  • If you are creating a project-level snippet, choose New Snippets File for <your project>.

    如果要创建项目级别的代码段,请为<您的项目>选择“新建代码段文件”。

With all of these, you get a commented-out starter JSON file.

通过所有这些,您将获得注释掉的入门JSON文件。

First, I created some Perl language helpers.

首先,我创建了一些Perl语言助手。

语言片段 (Language Snippets)

{
    "if": {
        "prefix": "if",
        "body": "if (${1:condition}) {\n\t$0\n}\n",
        "description": "An if statement"
    },
    "for": {
        "prefix": "for",
        "body": "for my \\$$1 (@\\$$2) {\n\t$0\n}\n",
        "description": "A for statement"
    }
}

This is fairly straightforward: a simple if and for statement. The $1 and $2 are completion tabs, while the $0 is where you want your cursor after the completions are made. Unfortunately, Perl makes extensive use of $ symbols itself, so on the for statement, I had to escape them. The prefix is what you need to type to have these snippets suggested.

这非常简单:一个简单的iffor语句。 $1$2是补全选项卡,而$0是补全后希望光标所在的位置。 不幸的是,Perl本身大量使用了$符号,因此在for语句中,我不得不转义它们。 prefix是您需要键入以提示这些摘要的内容。

With this file saved, when I type if within a Perl file, I get a suggestion. If the snippet is actually the second one offered, pressing down then tab inserts the snippet and positions the cursor at the $1 position ready for you to type your condition.

保存此文件后,当我在Perl文件中键入内容if ,会得到建议。 如果该代码段实际上是提供的第二个代码段,请按下,然后按Tab键插入该代码段,并将光标定位在$1位置,以便您键入条件。

全球片段 (Global Snippets)

Global snippets are very similar to language snippets, except they can contain elements relevant to multiple languages. I wanted one relevant to the Slim framework, which is a mixture of Perl and HTML code. Here’s an excerpt of what I ended up with:

全局代码段与语言代码段非常相似,只不过它们可以包含与多种语言相关的元素。 我想要一个与Slim框架有关的东西,它是Perl和HTML代码的混合体。 这是我最后得到的摘录:

{
    "Slim Log Error": {
        "scope": "perl",
        "prefix": "slimlogerror",
        "body": [
            "\\$log->error(\"$1\");"
        ],
        "description": "Slim Error Log"
    },
    "Slim Setting Title": {
        "scope": "html",
        "prefix": "slimsettingtitle",
        "body": [
            "[% WRAPPER setting title=\"${1:titlestring}\" desc=\"\" %]",
            "<div class=\"prefDesc\">[% \"${2:descriptionstring}\" | string %]</div>",
            "[% END %]"
        ],
        "description": "Slim Setting Title"
    }
}

The only thing to highlight here is the scope element to indicate which language a snippet is relevant for. For the prefix element, I started all my snippets with slim… to make it easy to get suggestions for something to do with the slim framework.

这里唯一要强调的是scope元素,用于指示摘要与哪种语言相关。 对于prefix元素,我所有的代码片段都以slim…开头的,以便轻松获取有关slim框架的建议。

项目片段 (Project Snippets)

These are exactly the same as global snippets, but they are located within the .vscode folder of your project.

这些片段与全局片段完全相同,但是它们位于项目的.vscode文件夹中。

正在同步 (Syncing)

I also wanted to share these snippets I’d created with my partner in crime on this project, so I wanted a way to sync them. With project-level snippets, that’s easy. Just check in the relevant files within the .vscode folder and it’s done. But I wanted to share the language and global snippets.

我还想与我的犯罪伙伴分享这个项目上创建的这些代码片段,因此我想要一种同步它们的方法。 使用项目级代码片段,这很容易。 只需签入.vscode文件夹中的相关文件.vscode 。 但是我想分享语言和全球摘要。

These weren’t going to change much, so I wasn’t after an automated continual syncing mechanism — just a way to easily grab them and update our local copies. I opted to use GitHub to share a project containing these snippets and set up symbolic links to put them in the right folder for VS Code to use.

这些更改不会有太大变化,因此我不需要自动持续同步机制,而只是一种轻松获取它们并更新我们的本地副本的方法。 我选择使用GitHub共享一个包含这些片段的项目,并设置符号链接以将它们放置在正确的文件夹中,以供VS Code使用。

On macOS, these files are stored in your User/Library. In Windows, it’s User/AppData. Here are the commands I used to create those symlinks from my usual projects folder over to where VS Code is looking for them. If you already have a snippets folder in your app data folder, the symlink command will fail. You’ll have to delete the snippets folder to be able to create the symlink (just make sure to back up any existing snippets you may have).

在macOS上,这些文件存储在您的用户/库中。 在Windows中,它是User / AppData。 这是我用来从通常的项目文件夹创建这些符号链接的命令,这些命令一直指向VS Code寻找它们的位置。 如果您的应用程序数据文件夹中已经有一个摘要文件夹,则symlink命令将失败。 您必须删除片段目录才能创建符号链接(只需确保备份您可能拥有的所有现有片段)。

苹果电脑 (Mac)

ln -s /Users/<USER>/<YOUR FOLDER>/vscode-snippets/snippets/ /Users/<USER>/Library/Application\ Support/Code/User/snippets

视窗 (Windows)

mklink /D c:\Users\<USER>\AppData\Roaming\Code\User\snippets c:\<YOUR FOLDER>\vscode-snippets\snippets

Any changes you make to the snippets within either the app data folder or your checkout folder will be instantly updated (they are actually the same file), so you can push/pull your Git repo to update your snippets.

您对app数据文件夹或checkout文件夹中的代码片段所做的任何更改都会立即更新(它们实际上是相同的文件),因此您可以推/拉Git存储库以更新代码片段。

发布片段作为扩展 (Publishing Snippets as an Extension)

While I am not going to publish these snippets as an extension, if you create a great set with wide appeal, you can wrap up your snippets into an extension. I’m not going to cover that, but if you are interested, there’s more info on the official Microsoft VS Code help site.

虽然我不会将这些片段作为扩展发布,但是如果您创建了一个具有广泛吸引力的优秀片段,则可以将您的片段包装为扩展。 我不会对此进行介绍,但是如果您有兴趣,可以在Microsoft VS Code官方帮助网站上找到更多信息。

I hope you found this article useful and it inspires you to create some snippets of your own. Hopefully, you’ll share them to aid a newbie with a particular language/framework/project.

我希望您觉得这篇文章对您有所帮助,并能激发您创建一些自己的摘录。 希望您能与他们分享,以帮助具有特定语言/框架/项目的新手。

翻译自: https://medium.com/@codechimp_org/creating-and-syncing-personal-snippets-in-vs-code-d03a8d441019

同步代码和同步代码块的区别

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值