Monday, December 03, 2012

Using Code Snippets in PowerBuilder.Net

clipwindow.PNGIf you are a big fan of the clip window in Classic PowerBuilder, you may have wondered what happened to it in PowerBuilder.Net.  You might have even been told that code snippets replaced it, but have no idea how to use them.  This article will hopefully address that issue.   We're going to walk through creating a code snippet, adding it to the PowerBuilder.Net IDE, and then using it in a scripting window.

Code Snippets actually have a lot more capabilities than the Clip Window, but they also require a little more work to setup.  The first thing you need to do when you want to create a new Code Snippet is create a new text file (doesn't matter where) with a ".snippet" extension.  Code Snippets are actually stored in XML, and most of the XML tags in the snippet contain metadata about the snippet.  Go ahead and paste the following into the top of that new text document you just created.

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <CodeSnippets  
  3.     xmlns="">  
  4.   <CodeSnippet Format="1.0.0">  
  5.     <Header>  
  6.       <Title>WCF Client</Title>  
  7.       <Author>Bruce Armstrong</Author>  
  8.       <Description>Sample code for calling the Amazon Web Service via WCF</Description>  
  9.       <Shortcut>wcfsample</Shortcut>  
  10.     </Header>  
  11.     <Snippet>  
  12.       <Code Language="PowerScript">  
  13.         <![CDATA[  

You're going to want to replace the title, author, description and shortcut with values of your own.  You don't have to provide all that metadata about your snippet, but some of it will become useful later.  Leave some blank lines after the CDATA declaration, and then add the following to close out the document.

  1.            ]]>  
  2.       </Code>  
  3.     </Snippet>  
  4.   </CodeSnippet>  
  5. </CodeSnippets>  

At this point, the only thing left to do is add the actual PowerScript that you want to insert between the beginning and the end of the CDATA tag.  For example, a portion of what I've included in my code snippet is shown below (with the periods representing a large chuck of code I left out for brevity.  Surrounding the script with the CDATA tag ensures that you don't have to encode any characters in the script that are also XML special characters.

  1.         <![CDATA[ 
  2. awsecommerceservice_AWSECommerceServicePortTypeClient_AWSECommerceServiceBinding _client 
  3. // create a WCF Amazon ECS client 
  4. _client = create awsecommerceservice_AWSECommerceServicePortTypeClient_AWSECommerceServiceBinding 
  6. // Configure the client 
  7. _client.wcfConnectionObject.EndpointAddress.URL = "" 
  8. _client.wcfConnectionObject.BasicHttpBinding.Security.SecurityMode = PBWCF.BasicHttpSecurityMode.TRANSPORT! 
  9. . 
  10. . 
  11. . 
  12. . 
  13.  catch ( System.Exception e ) 
  14.     MessageBox ( "Exception", e.ToString() ) 
  15. end try 
  16.                 ]]>  

That's all you need to create your first code snippet.  Just save it and then fire up the PowerBuilder.Net IDE.  The first place we need to go is the Code Snippet Manager, which is found under the Tools menu item.  When that opens, change the drop down under Language to read PowerScript if it's not already there.  What you should see are two folders, one called "My Script Snippets" which doesn't contain any items yet, and another called "PowerScript" which contains a number of code snippets that SAP provided for you.


Click on the Import button at the bottom left corner of the window and use the dialog that appears to navigate to the text file you created earlier.  Another dialog will then prompt you for the location to store the imported snippet.  Choose the My Script Snippets folder (which is usually checked by default) and then press the Finish button.

At this point, you can open up a script editing window and test it out.  There are a couple of different ways you can add the new code snippet to your script.  The first is to right click in the scripting window and select the Insert -> Insert Snippet option.  A popup window will display in the editor that will allow you to navigate through the folders of the PowerBuilder code snippets.  Select the My Script Snippets folder and then the code snippet your created.


Note how the description you provided in the metadata in the XML shows up in the script editor as a tooltip for the code snippet.  Selecting that code snippet will then insert the code snippet and the cursor position in the editor.

If you added a shortcut in the metadata, you can also add your code snippet by typing that shortcut ("wcfclient" in my example) in the editor, make sure that the intellisense isn't going to modify what you entered, and then hit tab.  The editor will replace the shortcut with the script from the code snippet.

That's a start.  There's a lot more code snippets can do.  For example, you can define replaceable parameters in your code snippet.  When the script is copied into the script editor, the replaceable parameters are then highlighted and the user can navigate through them with the tab key replacing their values with values that are meaningful within the script.  More information on what code snippets can do is available on the MSDN code snippet schema reference.

No comments: