Wednesday 23 August 2017

ABAP syntax check in Notepad++

Introduction


Notepad++ is a free text editor with a lot of good functions to work on ABAP source code files. Several developers have shown how to improve your work with this editor. That’s great to read and write ABAP source code without using transaction SE80 or Eclipse – have a try, it’s interesting to write source code without system assistance.

As I played a little bit around with this tool and its possibilities, I found the “Run…” menu. With the help of this menu, you can run an application of your choice. But what would be an interesting application in connection with ABAP? After a little while, I had the idea of implementing a connection between Notepad++ and SAP Netweaver. So it would be possible to check syntax.

ABAP, SAP ABAP Certifications, Notepad++

Please consider that this blog is just a technical demo. Replacing a great tool such as SE80 or Eclipse is naturally not a good idea. But it’s a good chance to see a practical benefit of statement SYNTAX-CHECK in an own report.

Steps to implement


To build the bridge between Notepad++ and SAP Netweaver, we use “sapshcut.exe” of your SAP GUI and a small report. The report will read the ABAP source code file your are currently editing with Notepad++, checks syntax and shows output via ALV.

Steps on your PC


Define your own command in Notepad++ via menue “Run”, entry “Run…”. Use following command but set all values in angle brackets in accordance with your system environment (have a look at your SAP GUI). Pay attention to the parameter “pw”. It’s not necessary to use but handy when you test. Anyway it’s insecure to save your password in clear text. You can replace shortcut “CTRL+F2” (toggle bookmark) with your own command thus it’s more SE80 style.

"C:\Program Files (x86)\SAP\FrontEnd\SAPgui\sapshcut.exe" -system=<SID> -pw="<password>" -client=<client> -user="<user>" -language=<language> -type="Transaction" -command="*ZNPP2SAP PA_FILE=$(FULL_CURRENT_PATH);"

Here is an example:

"C:\Program Files (x86)\SAP\FrontEnd\SAPgui\sapshcut.exe" -system=IDS -pw="insecure" -client=001 -user="DEVUSER" -language=EN -type="Transaction" -command="*ZNPP2SAP PA_FILE=$(FULL_CURRENT_PATH);"

Hint: I didn’t find an easy way to edit my own command after saving. Perhaps I missed the function. But I found the file where commands are stored under “C:\Users\<username>\AppData\Roaming\Notepad++\shortcuts.xml”. You can edit it but you have to restart Notepad++ to take notice of your changes.

Steps on your SAP Netweaver system


Copy the ABAP source code below into a report called ZNPP2SAP on your development system and activate it. Create a transaction for the report called ZNPP2SAP. That’s all.

*&---------------------------------------------------------------------*
*& Report ZNPP2SAP
*&---------------------------------------------------------------------*
*&
*& ABAP syntax check in Notepad++
*&
*& written by Michael Keller
*&
*&---------------------------------------------------------------------*
REPORT znpp2sap.

TYPES: BEGIN OF result,
         icon   TYPE icon_d,
         line   TYPE i,
         msg    TYPE string,
         word   TYPE string,
         msgkey TYPE trmsg_key,
         color  TYPE lvc_t_scol,
       END OF result.

DATA: gt_fcat   TYPE lvc_t_fcat,
      gt_result TYPE TABLE OF result.

PARAMETERS pa_file TYPE string.

START-OF-SELECTION.
  PERFORM main.

*&---------------------------------------------------------------------*
*&      Form MAIN
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*
*
*----------------------------------------------------------------------*
FORM main.

  DATA: lt_scode  TYPE TABLE OF string,
        ls_result LIKE LINE OF gt_result,
        ls_trdir  TYPE trdir,
        ls_scol   TYPE lvc_s_scol,
        ls_color  TYPE lvc_s_colo.

  sy-title = 'Notepad++ ABAP syntax check'.

  CALL METHOD cl_gui_frontend_services=>gui_upload
    EXPORTING
      filename                = pa_file
      filetype                = 'ASC'
*     has_field_separator     = SPACE
*     header_length           = 0
*     read_by_line            = 'X'
*     dat_mode                = SPACE
*     codepage                = SPACE
*     ignore_cerr             = ABAP_TRUE
*     replacement             = '#'
*     virus_scan_profile      =
*   IMPORTING
*     filelength              =
*     header                  =
    CHANGING
      data_tab                = lt_scode
*     isscanperformed         = SPACE
    EXCEPTIONS
      file_open_error         = 1
      file_read_error         = 2
      no_batch                = 3
      gui_refuse_filetransfer = 4
      invalid_type            = 5
      no_authority            = 6
      unknown_error           = 7
      bad_data_format         = 8
      header_not_allowed      = 9
      separator_not_allowed   = 10
      header_too_long         = 11
      unknown_dp_error        = 12
      access_denied           = 13
      dp_out_of_memory        = 14
      disk_full               = 15
      dp_timeout              = 16
      not_supported_by_gui    = 17
      error_no_gui            = 18
      OTHERS                  = 19.

  IF sy-subrc <> 0.
    MESSAGE 'File read error occured.' TYPE 'I'.
    CALL 'SYST_LOGOFF'.
  ENDIF.

  IF lt_scode IS INITIAL.
    MESSAGE 'No source code found.' TYPE 'I'.
    CALL 'SYST_LOGOFF'.
  ENDIF.

  SELECT SINGLE *
         FROM trdir
         INTO ls_trdir
         WHERE name = sy-repid.

  IF sy-subrc <> 0.
    MESSAGE 'No directory entry found.' TYPE 'I'.
    CALL 'SYST_LOGOFF'.
  ENDIF.

  SYNTAX-CHECK FOR lt_scode MESSAGE ls_result-msg
                            LINE ls_result-line
                            WORD ls_result-word
                            DIRECTORY ENTRY ls_trdir
                            MESSAGE-ID ls_result-msgkey.

  IF sy-subrc <> 0.
    ls_result-icon = icon_status_critical.
    ls_color-col = 6.
    ls_color-int = 1.
    ls_color-inv = 0.
    ls_scol-color = ls_color.
    APPEND ls_scol TO ls_result-color.
  ELSE.
    ls_result-msg = 'Syntax check ok. Good work.'.
    ls_result-icon = icon_status_ok.
    ls_color-col = 5.
    ls_color-int = 1.
    ls_color-inv = 0.
    ls_scol-color = ls_color.
    APPEND ls_scol TO ls_result-color.
  ENDIF.

  APPEND ls_result TO gt_result.

  PERFORM prepare_fcat.
  PERFORM show_alv.

ENDFORM.
*&---------------------------------------------------------------------*
*&      Form PREPARE_FCAT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*
*
*----------------------------------------------------------------------*
FORM prepare_fcat.

  DATA: ls_fcat TYPE lvc_s_fcat,
        lv_pos  TYPE i VALUE 1.

  CLEAR ls_fcat.
  ls_fcat-col_pos   = lv_pos.
  ls_fcat-icon      = abap_true.
  ls_fcat-fieldname = 'ICON'.
  ls_fcat-scrtext_s = 'Status'.
  ls_fcat-scrtext_m = 'Status'.
  ls_fcat-scrtext_l = 'Status'.
  APPEND ls_fcat TO gt_fcat.

  CLEAR ls_fcat.
  ls_fcat-col_pos   = lv_pos + 1.
  ls_fcat-fieldname = 'LINE'.
  ls_fcat-scrtext_s = 'Line'.
  ls_fcat-scrtext_m = 'Line'.
  ls_fcat-scrtext_l = 'Line'.
  APPEND ls_fcat TO gt_fcat.

  CLEAR ls_fcat.
  ls_fcat-col_pos   = lv_pos + 1.
  ls_fcat-fieldname = 'WORD'.
  ls_fcat-scrtext_s = 'Word'.
  ls_fcat-scrtext_m = 'Word'.
  ls_fcat-scrtext_l = 'Word'.
  APPEND ls_fcat TO gt_fcat.

  CLEAR ls_fcat.
  ls_fcat-col_pos   = lv_pos + 1.
  ls_fcat-fieldname = 'MSG'.
  ls_fcat-scrtext_s = 'Message'.
  ls_fcat-scrtext_m = 'Message'.
  ls_fcat-scrtext_l = 'Message'.
  APPEND ls_fcat TO gt_fcat.

  CLEAR ls_fcat.
  ls_fcat-col_pos   = lv_pos + 1.
  ls_fcat-fieldname = 'COLOR'.
  ls_fcat-ref_field = 'TABCOL'.
  ls_fcat-ref_table = 'BAL_S_SHOW_COL'.
  ls_fcat-tech      = 'X'.
  ls_fcat-no_out    = 'X'.
  APPEND ls_fcat TO gt_fcat.

ENDFORM.
*&---------------------------------------------------------------------*
*&      Form SHOW_ALV
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*
*
*----------------------------------------------------------------------*
FORM show_alv.

  DATA ls_layout TYPE lvc_s_layo.

  ls_layout-ctab_fname = 'COLOR'.
  ls_layout-cwidth_opt = abap_true.

  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
    EXPORTING
*     i_interface_check     = ' '
      i_bypassing_buffer    = 'X'
*     i_buffer_active       =
*     i_callback_program    =
*     i_callback_pf_status_set = ' '
*     i_callback_user_command  = ' '
*     i_callback_top_of_page   = ' '
*     i_callback_html_top_of_page = ' '
*     i_callback_html_end_of_list = ' '
*     i_structure_name      =
*     i_background_id       = ' '
*     i_grid_title          = ' '
*     i_grid_settings       =
      is_layout_lvc         = ls_layout
      it_fieldcat_lvc       = gt_fcat
*     it_excluding          =
*     it_special_groups_lvc =
*     it_sort_lvc           =
*     it_filter_lvc         =
*     it_hyperlink          =
*     is_sel_hide           =
*     i_default             = ' '
*     i_save                = ' '
*     is_variant            =
*     it_events             =
*     it_event_exit         =
*     is_print_lvc          =
*     is_reprep_id_lvc      =
      i_screen_start_column = 10
      i_screen_start_line   = 10
      i_screen_end_column   = 120
      i_screen_end_line     = 11
*     i_html_height_top     =
*     i_html_height_end     =
*     it_alv_graphics       =
*     it_except_qinfo_lvc   =
*     ir_salv_fullscreen_adapter  =
*     IMPORTING
*     e_exit_caused_by_caller  =
*     es_exit_caused_by_user   =
    TABLES
      t_outtab              = gt_result
    EXCEPTIONS
      program_error         = 1
      OTHERS                = 2.

  IF sy-subrc <> 0. " without error handling
  ENDIF.

  CALL 'SYST_LOGOFF'.

ENDFORM.

No comments:

Post a Comment