Sunday, August 22, 2010

Enhance your ABAP Pretty Printer

I like my code to have TYPE statements lined up, and assignments as well. So I've enhanced the PRETTY_PRINTER function (yes it is a funciton module) to do that, and insert a comment block at the top as well.

If you don't know how to enhance the pretty printer function to call this code then you probably shouldn't be messing with it. 

Note that after enhancing the PRETTY_PRINTER function SAP will dump any open ABAP source that other programmers may be working on.  Best to do this early or late in the day or you will hear a lot of complaining... :-)

*--------------------------------------------------------------------------
* Developer:        Peter Chapman
* Created:          Thursday, 15. July 2010
* Modified:         Thursday, 12. August 2010
*-----------------------------------------------------------------------


function zpretty_printer.
*"----------------------------------------------------------------------
*"*"Local Interface:
*"  CHANGING
*"     REFERENCE(CT_CODE) TYPE  SWBSE_MAX_LINE_TAB
*"----------------------------------------------------------------------

*-- Indent                   type statements nicely during pretty print
  data zf                    type i.
  data zi                    type i.
  data lv_date               type string.
  data lt_days               type table of casdayattr.
  data zs(29).

  field-symbols:
                        type casdayattr,
                       type swbse_max_line.

  check sy-uname = 'PCHAPMAN'.

*-- make sure it is a program
  find first occurrence of regex '^(function|method|report|class)' in table ct_code match line zi.
  check zi > 0.

*-- Handle coding block
  call function 'DAY_ATTRIBUTES_GET'
    tables
      day_attributes         = lt_days.
  read table lt_days index 1 assigning .

  find first occurrence of regex 'Peter Chapman' in table ct_code match line zi.
  if sy-subrc <> 0 or zi > 3.
    insert initial line into ct_code index 1 assigning .
                       = '*-----------------------------------------------------------------------'.
    insert initial line into ct_code index 2 assigning .
                       = '* Developer:        Peter Chapman'.
    insert initial line into ct_code index 3 assigning .
                       = '* Created:'.
    +20                = -day_string.
    insert initial line into ct_code index 4 assigning .
                       = '* Modified:'.
    +20                = -day_string.
    insert initial line into ct_code index 5 assigning .
                       = '*-----------------------------------------------------------------------'.
    insert initial line into ct_code index 6 assigning .
  endif.

  find first occurrence of regex 'Modified' in table ct_code match line zi.
  if sy-subrc = 0 and zi < 6.
    read table ct_code index zi assigning .
                       = '* Modified:'.
    +20                = -day_string.
  endif.

  loop at ct_code assigning .

    clear: zf, zi.

*-- Handle TYPE indentation
    find first occurrence of regex '\btype\b' in  match offset zf.
    if zf > 0 and zf < 29.
      zi                     = 29 - zf.
      +29              = +zf.
      +zf(zi)          = zs.
      modify ct_code from .
    endif.

*-- Handle = indentation
    find first occurrence of regex '^\s*\S*\s*(=)[^=]*$' in .
    " start of line, blanks, alphas, blanks = no more \= end of line
    if sy-subrc = 0.
      find first occurrence of regex '=' in  match offset zf.

      if zf > 0 and zf < 29.
        zi                   = 29 - zf.
        +29            = +zf.
        +zf(zi)        = zs.
      endif.

      zs                     = 5.

    endif.

  endloop.

endfunction.

1 comment:

Unknown said...

Good to know the possibility to enhance Pretty Printer. Impressive. Thank you for the post.Can you pls guide how to plugin custom code?