Two Views on the WDSc Snippets View, Part 1
March 28, 2007 Bruce Guetzkow
Developers usually don’t like to reinvent the wheel. If we or a co-worker have programmed a task in one source member, it is common to copy that code into another source member when a similar task needs to be done. If you are using WebSphere Development Studio Client (WDSc), there is another option: the Snippets View. Code snippets can be saved and easily inserted into any source member with a simple double-click of the mouse.
A First Look
The Snippets View is typically not displayed in the Remote Systems Explorer (RSE) perspective. If you don’t see a tab labeled Snippets, you can open the view from the menu by selecting Window –> Show View –> Other. From the resulting list expand Basic then select Snippets. What you see in the Snippets View is a series of grey horizontal bars with various labels and a white background with text and/or icons.
The gray bars are called drawers and are labeled to identify their contents. The text/icons represent individual code snippet references. The purpose of the drawers is to organize code snippets into logical collections. Often snippets are organized by the language they are written in, but there is no restriction on how you organize them. For example you could have a Subfile drawer containing a DDS fragment for the display file code, and RPGLE subprocedures to load and display the subfile.
To maintain drawers and snippets, start by moving your mouse over any drawer or snippet and right-click. From the context menu, select Customize. . . This will open a pop-up window labeled Customize Palette. If you select a drawer you will see a set of options on the right side of the panel where you can:
In the initial list you may see some drawers labeled EGL, JSP, XSL, etc. If you have no need for some of these, you can select the Always hide radio button and they will no longer appear in the view.
Rolling Your Own
To create a drawer of your own click on New on the menu bar. A drop-down menu will appear. Select the option New Category. It would be nice if the terminology was consistent, but for some reason it changes here. Just remember that Category means drawer and Item means snippet. After selecting New Category you will have a new drawer labeled Unnamed Category. You can now change the values on the right side of the panel as you see fit.
Now it’s time to create your first snippet. The easiest way to do this is to first exit the Palette by clicking OK. Next open a source member in the LPEX editor containing code that you’d like to create a snippet from. Select and copy the code (we’ll paste it later). Now, open the Palette again by right-clicking in the Snippet View and selecting Customize. . .
Create the snippet by clicking on “New”, then select New Item. This creates a snippet labeled Unnamed Template. Again the terminology is inconsistent. You can again easily change the values on the right side of the Palette. For now, skip the Variables section. Place your cursor into the Template Pattern area and use the standard Windows keys to paste your copied source into the window: Control-v. You can edit the code directly in the Template Pattern window if needed. Make the snippet available by clicking on Apply or OK.
Of course, it’s rare that code can be copied from one source member to another without making some changes. For example, a “subfile load” routine probably needs to specify a unique subfile record format and relative record number for each use. If these values appear more than once in the snippet it is typically up to you to remember to change all occurrences of those values. With snippets there is an easier way to handle variable values.
You can specify Variables that can be inserted into your snippet. When inserting the snippet into your code you will be prompted for the values to use for the current instance. You can also define a variable and use it multiple times in a snippet. In the subfile example I mentioned earlier, if the subfile record format name is used multiple times in the routine, you can define a single variable but insert it into the snippet at all appropriate locations. When you insert the snippet you will be asked to supply the subfile record format once, but it will be substituted in each location.
To create a variable, click on the New button to the right of the Variables area. A new entry will be added to the table. Simply update the name, description and (optionally) default value. To insert it into the snippet code, find the location where it will be placed. Select and delete the current code, then click Insert Variable Placeholder. . . This will display a list of the variables. Click on the variable you want to insert.
Insert a variable named “sflrcdfmt” and you will see the snippet code change to include the value $(sflrcdfmt). Depending on the amount of code you are replacing and the length of the variable name, you will most likely see the format of the source line shift to accommodate the variable name. When the snippet is inserted into a source member the number of characters that you enter when prompted will replace the entire variable string. If the snippet is an “F” spec and the variable is for a file name, you should always enter exactly 10 characters or the resulting insert of the snippet will not be properly formatted.
When I create variables I always include the length of text as part of the variable name or description as a reminder (filename10) if the snippet is for fixed-length code. That way if I have a file name of only 7 characters I will remember to pad the value with 3 blanks for a total length of 10 characters.
You may use as many variables as you like with each snippet and use each variable as many times as you like in a snippet.
Inserting a Snippet
As I mentioned at the start of this article, snippets are inserted into a source member very easily. While editing a source member in the LPEX editor, place your cursor at the location where you want the snippet inserted. Double-click on the desired snippet. If variables are used in the snippet, you will be prompted to supply values. The modified snippet will then be inserted before your cursor location.
There is one quirk that you should be aware of. Sometimes, after saving a snippet, you may find that blank lines have been inserted after every line in your snippet code, effectively doubling the size of the snippet. If you find that this has happened to your snippet, simply return to the Template Pattern window and delete the blank lines. What is really curious is that this seems to only happen at most once per snippet. Once it has been corrected it doesn’t happen again. I have not determined the sequence of steps that causes this so I can’t tell you what to avoid. Just remember to review your snippets before you use them for the first time.
While it is still possible to find a piece of code to duplicate in another source member, having the Snippets View means that once I create a useful snippet of code I no longer need to search through the entire source library for common tasks. Snippets View is a great time saver.