|
Comments
|
Today's Top SOA Links
Tips & Tools Bye Bye Clip Window; Hello Snippets
Increase .NET coding productivity
By: Yakov Werde
Sep. 13, 2010 09:00 AM
What Are Snippets? Classic Clips: A Review
Figure 1: Classic Clip Window Introducing VS .NET Snippets Predefined System Snippets
Figure 2: System snippets
Figure 3: Inserting a system snippet
Figure 4: System snippet list in the editor Placing a Snippet
Figure 5: Snippet Folders choice When you select a snippet, the code is placed in the editor at the cursor location and any variables defined in the snippet are assigned their default values. Variables in the snippet are highlighted. Hovering the mouse over a variable shows their tooltip. This a cue for you to adjust the variable value as needed (see Figure 5a).
Figure 5a Snippet Manager
Figure 6: Snippets Manager Defining a Snippet Snippets are defined as XML files with a .snippet file name extension. Their structure and content adhere to the Code Snippet XML schema located at http://msdn.microsoft.com/en-us/library/ms171418(v=VS.80).aspx The snippets you define are stored in the C:\Users\<current user>\Documents\Sybase\PowerBuilder 12.0\PowerBuilder .NET\Code Snippets\PowerScript\My Script Snippets folder. As your collection of snippets grows, you can organize them into subfolders. The IDE shows your snippets by folder. You can easily navigate the folders to locate the snippet you want using the mouse or tab key. Alphabetic type ahead and keyboard selection is also supported. Although, you can create and edit snippets by following the schema and entering valid XML using any text editor, by far the easiest way to get started writing snippets is by downloading and using a Snippet Editor. I found a great open source one from Bill McCarthy, a Microsoft MVP, at http://snippeteditor.codeplex.com/. Although the editor is MS Visual Studio-centric and doesn't see the isolated shell, I discovered a couple of workarounds that made it work with PowerBuilder. First, to allow access to the editor as part of my Visual Studio experience, I added it to my tools menu as an External Tool. Now I don't have to leave PB to access it. Figure 7 shows my Tools menu after adding the external tool.
Figure 7: Snippet Editor on Menu Next, the Snippet Editor is MS centric and tied to the MS VS keys in the Registry. It automatically loads C# and VB snippets and doesn't see the isolated shell keys; I had to add PowerBuilder's My Snippets folder to the folder view. The tool provides an "Add Path" context menu item that supported my action. (Unfortunately it doesn't remember added paths, so you have to do this every time you use the tool.). Figure 8 shows my folder view after adding the folder.
Figure 8: PB User Snippets in Snippet Editor Last, the editor only supports language attributes for Microsoft languages. Whenever you create a new snippet you will need to edit the generated snippet file with WordPad and change the language attribute of the Code element to PowerScript <Code Language="PowerScript". If you skip this step, PowerBuilder will not recognize the snippet and you won't see it listed it in the snippet choice list in the Painter. Fortunately, subsequent edits to the body of the snippet in the Editor don't revert the language designator change, so this is a one-time patch. (I dropped an email to the tool author asking him to add PowerScript support. The tool is open source; if you're a C# pro, you might consider modifying the code and submitting the change.) Snippet Anatomy
Figure 9: Annoted picture of Snippet Editor fields Conclusion and Limitations There are limitations in the current implementation that you should be aware of. First, there is no migration path for Classic clips into the .NET IDE. You'll have to redo all your Classic clips as Snippets. Copy and paste from Classic into the Snippet Editor should do the trick. (I added an action item to my ToDo list to build a conversion utility. Not sure when I'll get to it though.) Next, the Sybase engineers have provided equivalent functionality to that in Classic without implementing the advanced capabilities present in the Snippet specification. For example, the specification provides for elements that allow you to specify which references and imports (usings) a code object needs - these do not work in PB. If your snippet code contains code objects in a namespace or assembly, you will have to add those references and usings manually. The specification supports snippets in three scopes: (1) Class Declaration, snippets that define an entire class; (2) Member Declaration, snippets that define an event handler or function routine; and (3) Member Body, snippets inserted into existing member. Of the three, only member body snippets are supported. This is roughly equivalent to Classic clips. I'd like to see tool support for imports and references; having it would make coding calls to methods in assemblies and web services easier; I'd be less prone to forgetting or using a reference. I'd also like to see support for Class and Member snippet definition. Having it would increase my productivity by making implementing common CCUOs as well as function and events in all object types into a template-generated activity. Happy coding! Listing 1 <?xml version="1.0" encoding="utf-8"?> ]]> Reader Feedback: Page 1 of 1
Subscribe to the World's Most Powerful Newsletters
Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
|
SYS-CON Featured Whitepapers
Most Read This Week |
|||||||||||||||||||||||||||