It is possible to write VS Code extensions that are based on Code for IBM i. That means your extension can use the connection that the user creates in your extension. This is not an extension tutorial, but an intro on how to access the APIs available within Code for IBM i.
For example, you might be a vendor that produces lists or HTML that you’d like to be accessible from within Visual Studio Code.
Exports
As well as the basic VS Code command API, you can get access to the Code for IBM i API with the VS Code getExtension API.
Typings
We provide TS type definitions to make using the Code for IBM i API easier. They can be installed via npm:
It can then be imported and used in combination with getExtension:
Example import
This example can be used as a simple way to access the Code for IBM i instance.
Event listener
The Code for IBM i API provides an event listener. This allows your extension to fire an event when something happens in Code for IBM i.
Available events
ID
Event
connected
When Code for IBM i connects to a system
disconnected
When Code for IBM i is disconnected from a system
deployLocation
When a deploy location changes
deploy
When a deploy successfully finishes
APIs
Running commands with the user library list
Code for IBM i ships an API (via VS Code command) that can be used by an extension to execute a remote command on the IBM i.
It has a parameter which is an object with some properties:
Both member and streamfile file systems support the following query parameters:
Name
Type
Description
readonly
boolean
Open the text editor in read only mode. Defaults to false.
Connect to a system
It is possible for your API to automate connecting to an IBM i instead of the user using the connection view.
VS Code integration
Right click options
It is possible for your extension to add right click options to:
objects in the Object Browser
members in the Object Browser
directories in the IFS Browser
streamfiles in the IFS Browser
much more
You would register a command as you’d normally expect, but expect a parameter for the chosen node from the tree view. Here is the sample for deleting a streamfile in the IFS Browser.
Following that, we need to register the command so it has a label. We do this in package.json
Finally, we add it to a context menu:
When adding your command to a menu context, there are lots of possible values for your when clause:
view can be ifsBrowser or objectBrowser.
viewItem can be different depending on the view:
for ifsBrowser, it can be directory or streamfile
for objectBrowser, it can be member (source member), object (any object), SPF (source file) or filter.
This allows your extension to provide commands for specific types of objects or specific items in the treeview.
Code for IBM i provides a context so you can control when a command, view, etc, can work. code-for-ibmi.connected can and should be used if your view depends on a connection. For example
This will show a welcome view when there is no connection:
This will show a view when there is a connection:
FAQs
Getting the temporary library
Please remember that you cannot use QTEMP between commands since each command runs in a new job. Please refer to instance.getConfig().tempLibrary for the user temporary library.
Is there a connection?
You can use instance.getConnection() to determine if there is a connection:
connected context
If you refer to the Views section, you can make it so the view is only shown when connected. This means by the time the view is used, there should be a connection.
Examples
See the following code bases for large examples of extensions that use Code for IBM i: