Using and managing code snippets
IBM Z Open Editor ships with a library of reusable code snippets for COBOL, PL/I, and JCL to help you improve your productivity, as well as to standardize on code patterns and best practices. In addition to these out-of-the-box snippets, you can also create your own snippets and import snippets from IBM Developer for z/OS.
IBM Z Open Editor utilizes the Snippet facilities provided by VS Code. To learn more about this code snippet support, refer to the VS Code Online Help pages.
Using code snippets
To explore and insert a code snippet that ships with IBM Z Open Editor, use either the VS Code Snippet picker or the code completion feature while editing a program file.
Inserting a code snippet with a picker
Place the cursor on Column 1 in the editor as COBOL snippets come with indentations based on that column.
Open the VS Code command palette with Ctrl+Shift+P (Windows) or Cmd+Shift+P (Mac).
Insert Snippetuntil the command appears, and select it.
Based on the language that you are working with, it will then show a picker drop-down with the available snippets. All snippets are organized into a group of related snippets such as
COBOL Embedded SQLor
COBOL CICS. Each snippet also has a descriptive name and summary.
- Scroll down through the list or just type any substring of the snippet your are looking for.
- Select the snippet you want to insert and it will be added to the current cursor location.
Many snippets have place holders for variables or terminals. You can use the Tab key or Shift-Tab to navigate between them.
Type the new value to replace this and all other occurrences of the place holder and press Enter to finish.
Inserting a code snippet using code completion
When you edit a program, press Ctrl+Space to start code completion.
In the drop-down list, you will see first the list of context-sensitive code completion suggestions provided by the language server assuming you are working either in COBOL or PL/I. If you scroll down, you see the list of code snippets below.
Again, you can just type for any code snippet substring to search for a snippet that you are looking for to jump ahead in the drop-down.
Select the snippet and it will be inserted at your current cursor location.
Creating your own code snippets
You can easily complement the list of code snippets with your own as follows.
Follow the VS Code instructions to create your own snippets collection.
In the language drop-down, select type
jclas the language identifiers to start a snippet collection in that language. VS Code will open an editor window with instructions for how to write your own snippets. The help page listed above will also provide you with detailed guidance.
Once you have added your first snippet, save it. It will appear in the Snippet Picker described above placed before the IBM snippets.
Importing code snippets from IBM Developer for z/OS
If you are using the IBM Developer for z/OS (IDz) and have created custom code snippets there, you can export these code snippets and import them into the Z Open Editor.
Exporting code snippets in IDz
To display the available snippets in IDz, use the menu Window > Show View > Snippets.
Right-click anywhere in the Snippets view and select Customize...
In the dialog, find the Snippet group you want to export and click the Export button.
Specify the name and location for the export file, which will be an XML file.
Importing IDz code snippets into IBM Z Open Editor
In IBM Z Open Editor, open the VS Code command palette with Ctrl+Shift+P (Windows) or Cmd+Shift+P (Mac).
IBM Z Open Editor: Import IBM Developer for Z Code Snippetsuntil you see this command and select it.
A drop-down picker appears asking you for the program language of the snippets you want to import. Select one from the drop-down menu.
A file dialog appears. Navigate to and select the file that exported from IDz in the previous step.
Then a final text drop down appears asking you for a prefix string for the imported snippets. This prefix will be placed in front of the name of every snippet to be imported, which allows you to group the snippets by name in the resulting Snippet picker. You can leave this value empty and/or edit it again after the import.
If the import succeeds, it opens a JSON file editor with the imported snippets in the Microsoft VS Code format as documented in the help page listed above. You can now edit and make changes to polish the snippets and to add place holders.
If there were errors during the import, those errors will be displayed in a separate JSON file. That file will show up only if there were indeed any errors.