Thursday 19 January 2017

Bypass CDS view name case conversion in ABAP source code pretty printer

Some colleagues complained that when they consume CDS view in ABAP source code, for example the view below: I_Product

Bypass CDS view name case conversion in ABAP source code pretty printer
Bypass CDS view name case conversion in ABAP source code pretty printer

The reason is our team use “Keyword Uppercase” in Pretty Printer setting, and the printer implementation will not differentiate CDS view and normal ABAP variable.

Bypass CDS view name case conversion in ABAP source code pretty printer

Our requirement is, after Pretty Printer button is clicked, the CDS view name in ABAP source code could remain unchanged. In my example above, it should still be “I_Product”.

They are many ways to achieve this requirement via enhancement, unfortunately the package of pretty printer is configured as non-extensible.

Bypass CDS view name case conversion in ABAP source code pretty printer

So the only feasible approach is to directly change standard implementation, which is not a good solution but it works.

Open include program LSPPRP04, add a new line in line 167:

CHECK zcl_cds_view_case_tool=>is_cds_view( <abap_token>-lexem ) = abap_false.

The idea is to ignore the LOWER CASE handling of current ABAP token, once it is detected to represent an CDS view.

Bypass CDS view name case conversion in ABAP source code pretty printer

Test again:before Pretty Printer is clicked, the red line represents the name to be converted, the blue line for CDS view name which will not be touched:

Bypass CDS view name case conversion in ABAP source code pretty printer

After clicked, it works as expected.

Bypass CDS view name case conversion in ABAP source code pretty printer

Source code of zcl_cds_view_case_tool:

class ZCL_CDS_VIEW_CASE_TOOL definition
  public
  final
  create public .

public section.

  class-methods IS_CDS_VIEW
    importing
      !IV_NAME type ABPTOKEN-LEXEM
    returning
      value(RV_RESULT) type ABAP_BOOL .
protected section.
private section.

  TYPES: BEGIN OF ty_result,
            name TYPe ABPTOKEN-LEXEM,
            result TYPE ABAP_BOOL,
        end of ty_result.

  TYPES: tt_result TYPE STANDARD TABLE OF ty_result WITH KEY name.

  class-data:      mt_result TYPE tt_result.
ENDCLASS.



CLASS ZCL_CDS_VIEW_CASE_TOOL IMPLEMENTATION.


* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Static Public Method ZCL_CDS_VIEW_CASE_TOOL=>IS_CDS_VIEW
* +-------------------------------------------------------------------------------------------------+
* | [--->] IV_NAME                        TYPE        ABPTOKEN-LEXEM
* | [<-()] RV_RESULT                      TYPE        ABAP_BOOL
* +--------------------------------------------------------------------------------------</SIGNATURE>
  method IS_CDS_VIEW.
    CHECK sy-uname = 'WANGJER'.

    READ TABLE mt_result ASSIGNING FIELD-SYMBOL(<result>) WITH KEY name = iv_name.
    IF sy-subrc = 0.
       rv_result = <result>-result.
       RETURN.
    ENDIF.

    SELECT single obj_name INTO @data(ls) FROM TADIR
       WHERE pgmid = 'R3TR' and object = 'DDLS' and obj_name = @iv_name.

    data(ls_result) = value ty_result( name = iv_name result = boolc( sy-subrc = 0 ) ).
    APPEND ls_result to mt_result.

    rv_result = ls_result-result.

  endmethod.
ENDCLASS.

No comments:

Post a Comment