问题
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.
回答1:
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.
来源:https://stackoverflow.com/questions/46689883/creating-a-specific-sublime-texts-snippet-using-regular-expressions