简体   繁体   English

WebStorm和PhpStorm的自定义代码段

[英]Custom code snippet for WebStorm and PhpStorm

I use custom code snippet for Sublime Text like custom comments, function, reusable block code, CDN and more.. but I could not do that in WebStorm and PhpStorm IDE. 我对Sublime Text使用了自定义代码段,例如自定义注释,功能,可重用的块代码,CDN等。.但是我无法在WebStorm和PhpStorm IDE中做到这一点。

Here is my building block code snippet (comments) for Sublime Text: 这是我的Sublime Text的构建基代码片段(注释):

 /*============================
            comments
   ============================*/

and this code blocks for HTML5 comments 和此代码块的HTML5注释

 <!-----------------------
           comments
   ----------------------->

Moreover I'm new user for JetBrains software. 此外,我是JetBrains软件的新用户。 Can I use custom code snippet above in JetBrains software ? 我可以在JetBrains软件中使用上面的自定义代码段吗?

It's called Live Templates in JetBrains IDEs. 在JetBrains IDE中称为实时模板

Available at Settings/Preferences | Editor | Live Templates 可在Settings/Preferences | Editor | Live Templates Settings/Preferences | Editor | Live Templates Settings/Preferences | Editor | Live Templates . Settings/Preferences | Editor | Live Templates

You can use existing Live Templates as is, alter them to your needs or create your own. 您可以按原样使用现有的实时模板,根据需要对其进行更改或创建自己的模板。

Creating own is better be done in own group -- they will be stored in separate config file so easier to share, no possible conflicts with built-in ones (easier to update between versions etc.). 最好在自己的组中创建自己的组-将它们存储在单独的配置文件中,以便共享,更不会与内置文件冲突(更容易在版本之间进行更新等)。 It also makes perfect sense to use separate group per language -- the same abbreviation can be used for different languages/context but abbreviation within the same group must be unique. 每种语言使用单独的组也很有意义-相同的缩写可以用于不同的语言/上下文,但是同一组中的缩写必须唯一。

BTW -- I'd say -- do not edit built-ins at all -- just disable specific built-in template and create your own version of it in separate group. 顺便说一句-我想说-根本不编辑内置程序-只需禁用特定的内置模板并在单独的组中创建自己的版本即可。 This way you can always see what fix/change devs have made in new IDE version etc. 这样,您始终可以看到在新的IDE版本等中所做的修订/更改。

Full official tutorial/how-to is available here: https://confluence.jetbrains.com/display/PhpStorm/Live+Templates+%28Snippets%29+in+PhpStorm 完整的官方教程/操作方法可以在这里找到: https : //confluence.jetbrains.com/display/PhpStorm/Live+Templates+%28Snippets%29+in+PhpStorm


You may also be interested in other articles: 您可能也对其他文章感兴趣:

Finally I get a tips ! 最后我得到一个提示! PhpStorm allows you to create your own live templates (code snippets) to optimise your workflows. PhpStorm允许您创建自己的实时模板(代码段)以优化工作流程。

Open the settings dialog and head into Editor | 打开设置对话框,然后进入编辑器| Live Templates , you can see the available live templates grouped by language. 实时模板 ,您可以查看按语言分组的可用实时模板。 To add a new template click the + (plus) button and select Live Template. 要添加新模板,请单击+(加号)按钮,然后选择实时模板。 Specify the abbreviation (the short bit of text you type that will be expanded to the full code snippet) and a description. 指定缩写(您键入的一小段文本,它将被扩展为完整的代码片段)和描述。

Then provide the full code snippet in the Template text field. 然后在“模板”文本字段中提供完整的代码段。 You can include variables in the template in the format $<variable name>$ , which will allow you to provide values when the template is expanded. 您可以使用$<variable name>$格式将变量包含在模板中,这样可以在扩展模板时提供值。 PhpStorm recognises $END$ as a special variable indicating the final position of the cursor after the template has been expanded and values have been provided for all variables. PhpStorm将$END$识别为特殊变量,指示在模板已扩展并为所有变量提供了值之后光标的最终位置。

Next click the Define warning text to specify which language the template is for and optionally the context it is available in. 接下来,单击“定义警告”文本,以指定模板使用的语言以及可选的上下文。

Now the template is ready to be used. 现在可以使用模板了。 Open a file and type the abbreviation that was specified earlier, then hit Tab to expand the template. 打开一个文件并键入先前指定的缩写,然后单击Tab键以展开模板。 The cursor will be positioned on the first variable, provide a value then hit Tab to keep moving through all available variables. 光标将定位在第一个变量上,提供一个值,然后按Tab键以继续移动所有可用变量。 The final position of the cursor will be the location of the $END$ variable. 光标的最终位置将是$END$变量的位置。

在此处输入图片说明

Further Reading 进一步阅读

I don't think you can do this, however you can add custom tags in Settings > Editor > TODO . 我认为您无法执行此操作,但是可以在“设置”>“编辑器”>“ TODO”中添加自定义标签。

//TODO & //FIXME are already implemented. //TODO//FIXME已经实现。

But this custom tags are not working for HTML. 但是此自定义标签不适用于HTML。

Maybe you can find an extension to do that in Settings > Plugins . 也许您可以在“设置”>“插件”中找到扩展名。

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

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