Skip to Content
Author's profile photo Thomas Fiedler

Surround your code using ABAP in Eclipse code templates

A very powerful feature in Eclipse is the usage of code templates. Few months ago I already described in my blogInsert Code Patterns and Templates within Eclipse how to use it. Just start typing the name of the template and use code code completion to insert it into the editor. But often you have the situation that you already have some code that you want to surround with some code. I give you some examples:

  • You call some ABAP methods in your code not taking care for exceptions in the first shot. Later on you want to catch the exceptions from this code block with a TRY/CATCH clause.
  • You have some code written and want to put this into a conditional clause. So you want to surround the code block with an IF/ELSE/ENDIF clause.
  • You have written some code and want to put this into a CASE/ENDCASE clause.
  • You have some code that you need to surround with a LOOP/ENDLOOP clause.

The code templates in eclipse helps you to do that job very quickly as you will see in the following section:

Here I have a coded a very simple “Hello World” application in SAP style:

/wp-content/uploads/2013/05/code1_221651.png

Perfectly working. Next step is a small enhancement in case there are no flights selected.

So I want to check the SY-SUBRC of the select statement and only if there are flights I want to display them in the ALV.

With Eclipse I do this by marking the code that I want to put into the IF/ENDIF clause and press CTRL-SPACE – the magic shortcut to open the template selection.    

/wp-content/uploads/2013/05/code2_221660.png

After selecting my template of choice the code is generated as you see here in the screenshot:

/wp-content/uploads/2013/05/code3_221653.png

Also very helpful here is that the cursor is already positioned correctly so that I can directly go on with defining my expression.Try it out, this will really speed you up. 

ONE MORE THING: In case you have additonal requirements concerning new templates you can also create your own templates via the templates view in eclipse. This I will explain in my next blog.

Regads,

Thomas.  

Assigned tags

      5 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Ashwani Kumar Sharma
      Ashwani Kumar Sharma

      Nice tip !

      Author's profile photo Uwe Fetzer
      Uwe Fetzer

      Nice 🙂

      Maybe we can create an SAP Code Exchange project, where we can post useful (customer) code templates.

      Author's profile photo Thomas Fiedler
      Thomas Fiedler
      Blog Post Author

      Hi Uwe,

      there is a mechanism to import and export eclipse code templates to the file system. Ashwani describes this in his blog about WD templates. I can imagine that we can create an eclipse plugin that automatically syncs the local templates with the central template repository on code exchange. Can't we use your SAP Link infrastructure for that. What do you think: Another community project?

      Regards,

      Thomas.

      Author's profile photo Uwe Fetzer
      Uwe Fetzer

      Hi Thomas,

      sorry for the late answer.

      Yes, this is a nice idea. Unfortunately currently we can't create new projects on Code Exchange (see announcement on Code Exchange ).

      Another showstopper atm is the availability of JAVA devs. We are still searching one for our "SAPlink for ADT" project.

      Author's profile photo Joachim Rees
      Joachim Rees

      Hi Thomas,

      thanks, very helpful!

      The next blog in your series is https://blogs.sap.com/2013/05/24/creating-new-code-templates-with-abap-in-eclipse/ , how about you edit that link into your blog?!

      -> a nice service to readers, and more important now in SAP Community (compared to SCN), as we don't have the "incoming links"-box anymore 🙁  .

      best
      Joachim