In my work, I constantly have to copy and paste a large amount of the same type of code associated with markup, validation, formatting, etc. a large number of fields from the DBMS. I have long wanted to automate this task.
For example, there is a table:
id | field1 | field2 | field3
And you need to get the markup of the form:
')
<tr> <td class="fls"><GEN:FIELDLABEL NAME="Field1Label" /></td> <td class="dfv" style="white-space:nowrap;"><GEN:FIELDVALUE NAME="Field1" /></td> </tr> <tr> <td class="fls"><GEN:FIELDLABEL NAME="Field2Label" /></td> <td class="dfv" style="white-space:nowrap;"><GEN:FIELDVALUE NAME="Field2" /></td> </tr> <tr> <td class="fls"><GEN:FIELDLABEL NAME="Field3Label" /></td> <td class="dfv" style="white-space:nowrap;"><GEN:FIELDVALUE NAME="Field3" /></td> </tr>
Well, if there are 3 fields, and if there are 30 of them and pieces of code, there are also more than a dozen?
The first thought was to do everything in sql. Create a label with a list of fields and a request to generate a code block by a template. Said and done, but copying the template to the sql server management studio each time and the generated code back seemed not very convenient, so I decided to try to write a plug-in for Sublime Text 2.
So the final task:
Create a list of values in the form of a two-dimensional array, in my case I add in each line:
[fieldName, signFieldName, fieldFullName, fieldMeasurment]
In the edited file, we write templates with placeholders {0}, {1}, {2} and {3} For example:
ValidateSignedField({0},{1});
Next, select several templates using multiple selection, press Ctrl + Shift + z and get the ready code.
The implementation turned out to be quite simple, considering that this is my first plug-in for sublime text 2, and I last picked up python very superficially more than a year ago.
We create a new plugin (Tools -> New Plugin) and save it, for example, as Codegen.py in the appropriate folder.
Plugin code:
# coding=utf-8 import sublime import sublime_plugin # {0},{1},{2} {3} field_rows = [ ['dbField1','dbField1Sign','Field 1 full name','m/s'], ['dbField2','dbField2Sign','Field 2 full name','kg/m*m'], ['dbField3','dbField3Sign','Field 3 full name','V'], ['dbField4','dbField4Sign','Field 4 full name','km/h'] ] class CodegenCommand(sublime_plugin.TextCommand): def run(self, edit): # view view = self.view # for curr_sel in view.sel(): n = 0 region = curr_sel if not region.empty(): # , selection = view.substr(region) result = '' # for field_row in field_rows: result += selection.replace('{0}',field_row[0]).replace('{1}',field_row[1]).replace('{2}',field_row[2]).replace('{3}',field_row[3]).replace('{n0}',str(n)).replace('{n1}',str(n+1)) result += '\r\n' n += 1 # self.view.replace(edit, region, result)
field_rows is the same array of values to substitute into the pattern.
Already in the course of writing to the article, I had the idea to add the ability to generate indexes from zero or one, for this purpose the placeholders {n0} and {n1} were added, respectively.
Now it remains to add a hotkey to the plugin call. In Key Bindings - User write:
[
{ "keys": ["ctrl+shift+z"], "command": "codegen" }
]
I hope that this plugin will help someone save some time.