使用正则表达式创建特定的Sublime Text代码段 [英] Creating a specific Sublime Text's snippet, using Regular Expressions

查看:86
本文介绍了使用正则表达式创建特定的Sublime Text代码段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上下文

我有一个过程,涉及创建类似的文件/文件名结构,在其中包含名称本身,诸如此类,我每天都这样做,我看到它是重复的并有模式,然后我得到了创建Sublime Text的代码段为我生成代码的想法,大大提高了我的性能.

示例

有一个完整模型"使用我说的结构:

 Ext.define('App.model.geral.layouts.Layouts', {
    extend: 'App.ux.model.base',

    fields: [
        { name: 'Foo', type: 'string', fieldLabel: 'Foo' },
        { name: 'Bar', type: 'int', fieldLabel: 'Bar' },
        { name: 'FooTwo', type: 'boolean', fieldLabel: 'FooTwo' },
        { name: 'Date', type: 'date', fieldLabel: 'Date' },
    ],
  
    proxy: Use.util.Model.getProxy({
        controller: 'Layouts'
    })
});
 

这是使用矿山结构的文件的简单小样本.因此,遵循模式的那个文件将被放置在C:/Dev/Com/app/model/geral/layouts/Layouts.js处,因为模型位于文件夹 model 内,而 geral 是实体 layouts 属于.

我尝试过的事情

我尝试了各种各样的事情,而我做得最多的是该片段文件:

<snippet>
    <content><![CDATA[
Ext.define('App.model.${TM_FILEPATH/.+(?:model\/)(.+)\.\w+/\l$1/}', {
    extend: '',

    fields: [ ],

    proxy: '' 
});
]]></content>
    <tabTrigger>mitem</tabTrigger>
</snippet>

当我在名为C:/Dev/Com/app/model/geral/layouts/Layouts.js(作为模式)的空文件中触发该代码段时,结果为:

 Ext.define('App.model.geral/layouts/Layouts', {
    extend: '',

    fields: [ ],

    proxy: '' 
});
 

如您所见,我得到的是'App.model.geral/layouts/Layouts'而不是'App.model.geral.layouts.Layouts'.正如您在完整的模型示例中所看到的那样,我已经接近我想要的最终结果,顺便说一句,我无法走得更远,我不了解RegExp,我所做的只是研究和尝试不同的事情. /p>

如果有帮助,我发现有关Sublime片段的更完整信息是:

$PARAM1 .. $PARAMn  Arguments passed to the insert_snippet command. (Not covered here.)
$SELECTION  The text that was selected when the snippet was triggered.
$TM_CURRENT_LINE    Content of the cursor’s line when the snippet was triggered.
$TM_CURRENT_WORD    Word under the cursor when the snippet was triggered.
$TM_FILENAME    Name of the file being edited, including extension.
$TM_FILEPATH    Path to the file being edited.
$TM_FULLNAME    User’s user name.
$TM_LINE_INDEX  Column where the snippet is being inserted, 0 based.
$TM_LINE_NUMBER Row where the snippet is being inserted, 1 based.
$TM_SELECTED_TEXT   An alias for $SELECTION.
$TM_SOFT_TABS   YES if translate_tabs_to_spaces is true, otherwise NO.
$TM_TAB_SIZE    Spaces per-tab (controlled by the tab_size option).

我使用该信息获取文件路径,我尝试使用文件名之类的其他变量,但并没有达到目的.

如果有人可以帮助我获得最终结果,这将非常有用.

解决方案

您可以通过以下方法实现所需的目标:

<snippet>
    <content><![CDATA[
Ext.define('App.model.${TM_FILEPATH/(^.+\/model\/)|(\w+)|(\.\w+$)|(\/)/(?2$2)(?4.)/g}', {
    extend: '',

    fields: [ ],

    proxy: '' 
});
]]></content>
    <tabTrigger>mitem</tabTrigger>
</snippet>

顺便说一句,我强烈建议您安装 PackageDev软件包(如果尚未安装的话)代码段上的语法高亮显示和正则表达式/替换.

工作方式:

匹配:

  • (^.+\/model\/)从文件路径的开头一直匹配到/model/(包括/model/),并存储在捕获组1中
  • |
  • (\w+)匹配任何单词字符序列并存储在捕获组2中
  • |
  • (\.\w+$)匹配文件扩展名并存储在捕获组3中
  • |
  • (\/)匹配/并存储在捕获组4中

替换:

  • (?2$2)如果捕获组2参加了比赛,则将其替换为自己-即保留
  • (?4.)如果捕获组4参加了比赛,则将其替换为一个点

标志:

  • g全局修饰符以匹配尽可能多的次数

可以说,您不需要捕获组1和3,但是我将它们包括在内是为了更容易分辨匹配的对象.

Context

I have a process that envolves creating similar file/filename structures that have inside of it the name of itself, and things like that, i do this every day, and i see that is repetitive and have a pattern, then i got the idea of creating a Sublime Text's Snippet to generate the code for me, adding a significant improvement on my performance.

Example

There is a example of a complete "model" using the structure that i said:

Ext.define('App.model.geral.layouts.Layouts', {
    extend: 'App.ux.model.base',

    fields: [
        { name: 'Foo', type: 'string', fieldLabel: 'Foo' },
        { name: 'Bar', type: 'int', fieldLabel: 'Bar' },
        { name: 'FooTwo', type: 'boolean', fieldLabel: 'FooTwo' },
        { name: 'Date', type: 'date', fieldLabel: 'Date' },
    ],
  
    proxy: Use.util.Model.getProxy({
        controller: 'Layouts'
    })
});

This is a simple and small sample of a file using mine structure. So that file, following the patterns will be placed at C:/Dev/Com/app/model/geral/layouts/Layouts.js, because models, are inside the folder model and geral is the module that the entity layouts belong to.

What i've tried

I tried various things and the most far i did go was that snippet file:

<snippet>
    <content><![CDATA[
Ext.define('App.model.${TM_FILEPATH/.+(?:model\/)(.+)\.\w+/\l$1/}', {
    extend: '',

    fields: [ ],

    proxy: '' 
});
]]></content>
    <tabTrigger>mitem</tabTrigger>
</snippet>

When i trigger that snippet on a empty file named and located in: C:/Dev/Com/app/model/geral/layouts/Layouts.js (as the pattern), it results:

Ext.define('App.model.geral/layouts/Layouts', {
    extend: '',

    fields: [ ],

    proxy: '' 
});

As you can see, i got 'App.model.geral/layouts/Layouts' instead of 'App.model.geral.layouts.Layouts' that is what i want. I am close to the final result that i want, as you can see on the complete model example, by the way i cannot go far than that, i dont have any knowledge of RegExp what i did was only researching and trying different things.

If helpful, there is a more complete info about Sublime Snippets that i found is:

$PARAM1 .. $PARAMn  Arguments passed to the insert_snippet command. (Not covered here.)
$SELECTION  The text that was selected when the snippet was triggered.
$TM_CURRENT_LINE    Content of the cursor’s line when the snippet was triggered.
$TM_CURRENT_WORD    Word under the cursor when the snippet was triggered.
$TM_FILENAME    Name of the file being edited, including extension.
$TM_FILEPATH    Path to the file being edited.
$TM_FULLNAME    User’s user name.
$TM_LINE_INDEX  Column where the snippet is being inserted, 0 based.
$TM_LINE_NUMBER Row where the snippet is being inserted, 1 based.
$TM_SELECTED_TEXT   An alias for $SELECTION.
$TM_SOFT_TABS   YES if translate_tabs_to_spaces is true, otherwise NO.
$TM_TAB_SIZE    Spaces per-tab (controlled by the tab_size option).

I used that info to get the filepath, i tried using another variables like filename but did not get that far.

That will be very useful if someone can help me to get to the final result.

解决方案

You can achieve what you want with the following:

<snippet>
    <content><![CDATA[
Ext.define('App.model.${TM_FILEPATH/(^.+\/model\/)|(\w+)|(\.\w+$)|(\/)/(?2$2)(?4.)/g}', {
    extend: '',

    fields: [ ],

    proxy: '' 
});
]]></content>
    <tabTrigger>mitem</tabTrigger>
</snippet>

Btw, I highly recommend installing the PackageDev package if you haven't already, to get some syntax highlighting on the snippet and regular expression/replacement.

How it works:

Match:

  • (^.+\/model\/) match from the beginning of the file path up to and including /model/, and store in capture group 1
  • | or
  • (\w+) match any sequence of word characters and store in capture group 2
  • | or
  • (\.\w+$) match the file extension and store in capture group 3
  • | or
  • (\/) match a / and store in capture group 4

Replacement:

  • (?2$2) if capture group 2 participated in the match, replace it with itself - i.e. keep it
  • (?4.) if capture group 4 participated in the match, replace it with a dot

Flags:

  • g global modifier to match as many times as possible

Arguably you don't need the capture groups 1 and 3, but I included them to make it easier to tell what is being matched.

这篇关于使用正则表达式创建特定的Sublime Text代码段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆