A Snippet Above the Rest
September 26, 2007 Paul Tuohy
You all know that a lot of code consists of similar patterns. In my previous article Be Content with Content Assist, I explained how Content Assist could be used to insert a standard piece of code. This is well and good, but it only applies to free form RPG or COBOL. What about fixed form specs or code that need some alteration? Although not specifically designed with RPG or COBOL in mind, RSE contains a Snippets view that allows you to define snippets of code that may be amended as you insert them into your program. The Snippets View On the RSE menu, select “Window>Show View>Other.” In the resulting window, expand “Basic” and select the “Snippets” view to see the window shown in Figure 1. Right-click in the “Snippets” view and select “Customize” to see the “Customize Palette,” also shown in Figure 1.
A New Category The first step (you will only have to do this once) is to define a new drawer for your Snippets. In the “Customize Palette” window select “New Category” from the drop-down menu on the “New” button and complete the description of the “Category” as shown in Figure 2.
Provide a name and description for the drawer and customize how and when you want it displayed in the “Snippets” view window. Click on the “Apply” button and your new Snippets drawer is defined. While you are here, note the “Import” and “Export” buttons on the Toolbar. You can use them to share all the wonderful Snippets you are about to define. Defining a Snippet It is time to define a Snippet of code that you might find useful. As a simple example let’s use a standard “Do While” loop for performing a sequential read of a file. (Feel free to change it to a “Do Until” loop if you prefer.) In the “Customize Palette” window, right-click on the drawer name (RPG in the example) and select “New Item” from the drop-down menu. Start by providing a name and a description for the Snippet, as shown in Figure 3.
Now you define “Variables” and the “Template Pattern.” Variables are values that you can change when you use the Snippet and the template pattern is the code you want to insert with special placeholders for the variable values. Define a variable by selecting the “New” button and completing the name of the variable, a description and, if required, a default value. When you have defined all the variables you think you might need, proceed to define the “Template Pattern” by entering the code for the snippet. Wherever you want a variable value inserted, simply click on the “Insert Variable Placeholder” button and select the required variable. Figure 4 shows the definition of a variable for the file name and the Snippet code for the “Do While” loop. The placeholders have already been inserted for the file name on the first read and in the “EOF BIF” and it is being inserted for the second read.
You might want to copy and paste code from the editor to the “Template Pattern,” especially if you a coding a fixed-format spec. When you are finished, click on the “OK” or “Apply” button. You now have a Snippet ready to use – it is listed in the RPG drawer in the Snippets view. Using a Snippet To make use of a Snippet while editing a source member simply double-click on the snippet (in the “Snippets View”), change the values for any variables and click “Insert” to have the Snippet inserted. In the example shown in Figure 5, the source member consists of a /Free directive. You position the cursor on the next line and double-click on the “Do While File Loop” Snippet and change the value of the “FileName” variable from “MyFile” to “Customer.” When you click “Insert the Do While” loop for a sequential read, the “Customer” file is inserted in the code.
It’s a Snip There you have it. Snippets are a very useful feature when it comes to defining any type of “standard” code with variable information that needs to be changed when you use it. There is also the added benefit that Snippets may be used for standard specification types as well as free form. It’s time to start building that useful library of snippets. RELATED STORY Two Views on the WDSc Snippets View, Part 1
|