In earlier post Working With Eclipse [7] : Create your own IDE Actions, you learned about creating own IDE actions with Text output as the demo. In this post, you will learn how to create your own IDE actions for Eclipse with result type HTML and Code Change.

The IDE action framework allows you to extend the ABAP development tools for Eclipse (ADT) with additional features called IDE actions. The IDE action framework offers you a toolset to build custom functionalities that are completely server-driven using ABAP. You can also use these custom functionalities for AI integration.

Ref: https://help.sap.com/docs/abap-cloud/abap-development-tools-user-guide/working-with-ide-actions

IDE actions for Eclipse with result type HTML

1. Create IDE action using the steps in earlier post – Create your own IDE Actions.

Here is a quick recap.

Right click on the package and choose New > Other ABAP Repository Object. Select IDE Action from the node Other and create the action.

Provide Name and Description, click Next, Assign a transport request if needed and click Finish.

Provide Title, Summary, and Names for the Implementation class.

Add filter for the Object Types on which this actions can be applied by clicking on the Add button.

On the next pop-up select ‘CLAS’ as Object Type and click Add.

Now, click on the Implementing Class link to create the Implementing class.

Provide the description and add the interface IF_AIA_ACTION. This interface must be implemented in the action class.

As of now the class is empty.

Activate the class and the action.

2. For this post, we will use the action result type HTML.

Implement the method if_aia_action~run from the class zjp_ide_action_demo_class. This time the method implementation will have code to set up HTML content. The code makes use of the focused resource (class source code in this case) and gets object information about the resource .

CLASS zjp_ide_action_demo_class DEFINITION
  PUBLIC
  FINAL
  CREATE PUBLIC .

  PUBLIC SECTION.
    INTERFACES if_aia_action .
  PROTECTED SECTION.
  PRIVATE SECTION.
ENDCLASS.

CLASS zjp_ide_action_demo_class IMPLEMENTATION.


  METHOD if_aia_action~run.

    data html type string.

    DATA(focused_resources) = context->get_focused_resources( ).
    html = '<h1>Focused Resources</h1>'.
    html = |{ html }<p>Here are the currently focused resources from the IDE context.</p>|.

    html = |{ html }<h2>Source-Based Objects</h2>|.
    LOOP AT focused_resources ASSIGNING FIELD-SYMBOL(<focused_resource>).
      IF <focused_resource>->get_kind( ) = if_adt_context_resource=>kind-source_based_dev_object.
        DATA(source_based_object) = CAST if_adt_context_src_based_obj( <focused_resource> ).
        html = |{ html }<p>{ source_based_object->get_object_info( )-display_name } ({ <focused_resource>->get_type( ) })</p>|.
      ENDIF.
    ENDLOOP.

    DATA(html_result) = cl_aia_result_factory=>create_html_popup_result( ).
    html_result->set_content( html ).
    result = html_result.

  ENDMETHOD.
ENDCLASS.

Activate the class and you are ready to run the action.

3. Choose Run > Run ABAP IDE Action or use shortcut Ctrl+Alt+R.

Select the action created – IDE Action Demo and Click Run.

The action is executed and you get the HTML content set in the action class.

IDE actions for Eclipse with result type Code Change

1. Add name for the Input UI Configuration Class.

2. Create the class and add below code. Here, we are creating a dropdown option for developer to select and based on the selection, relevant code can be added. Use below code to complete the implementation. The code is explained little later.

CLASS zjp_ide_action_demo_class_ui DEFINITION
  PUBLIC
  FINAL
  CREATE PUBLIC .

  PUBLIC SECTION.

    INTERFACES if_aia_sd_action_input .
    CONSTANTS:
      BEGIN OF co_code_template,
        "! <p class="shorttext">DiscABAP comment</p>
        "! DiscABAP comment
        cada   TYPE string VALUE 'DiscABAP',
        "! <p class="shorttext">Loop</p>
        "! Loop
        caloop TYPE string VALUE 'Demo_Loop',
        "! <p class="shorttext">If Block</p>
        "! If Block
        caif   TYPE string VALUE 'Demo_If',
      END OF co_code_template.

    TYPES:
      "! <p class="shorttext">Code Template</p>
      "! Code Template
      BEGIN OF ty_input,
        "! <p class="shorttext">Code Template</p>
        "! Code Template
        "! $values {@link zjp_ide_action_demo_class_ui.data:co_code_template}
        "! $required
        template TYPE string,
      END OF        ty_input.

  PROTECTED SECTION.
  PRIVATE SECTION.
ENDCLASS.


CLASS zjp_ide_action_demo_class_ui IMPLEMENTATION.


  METHOD if_aia_sd_action_input~create_input_config.

    DATA(input) = VALUE ty_input( template = co_code_template-cada ).
    DATA(configuration) = 
      ui_information_factory->get_configuration_factory( )->create_for_data( input ).
    result = ui_information_factory->for_abap_type( abap_type = input ).

  ENDMETHOD.


  METHOD if_aia_sd_action_input~get_action_provider.
  ENDMETHOD.

  METHOD if_aia_sd_action_input~get_side_effect_provider.
  ENDMETHOD.

  METHOD if_aia_sd_action_input~get_value_help_provider.
  ENDMETHOD.
ENDCLASS.

3. Add below Code to the Implementing class ZJP_IDE_ACTION_DEMO_CLASS

CLASS zjp_ide_action_demo_class DEFINITION
  PUBLIC
  FINAL
  CREATE PUBLIC .

  PUBLIC SECTION.

    INTERFACES if_aia_action .

ENDCLASS.

CLASS zjp_ide_action_demo_class IMPLEMENTATION.


  METHOD if_aia_action~run.

    DATA input TYPE zjp_ide_action_demo_class_ui=>ty_input.
    DATA template TYPE string.
    DATA(input_config_data) = context->get_input_config_content( ).

    IF input_config_data IS BOUND.
      TRY.
          input_config_data->get_as_structure( IMPORTING result = input ).
        CATCH cx_sd_invalid_data.
          " default template
          input = VALUE #( template = zjp_ide_action_demo_class_ui=>co_code_template-cada ).
      ENDTRY.
    ENDIF.

    CASE input-template.
      WHEN 'cada'.
        template = '"Code Comment for DiscoveringABAP'.
      WHEN 'caif'.
        template = 'IF <>. ELSE. ENDIF.'.
      WHEN 'caloop'.
        template = 'LOOP AT <> INTO DATA(<>).'.
    ENDCASE.

    DATA(focused_resource) = CAST if_adt_context_src_based_obj( context->get_focused_resource(  ) ).

    DATA(position) = focused_resource->get_position(  ).

    DATA(source_change_result) = cl_aia_result_factory=>create_source_change_result( ).
    source_change_result->add_code_insertion_delta( content = template 
      cursor_position = position->get_end_position( ) ) ##NO_TEXT.
    result = source_change_result.

  ENDMETHOD.
ENDCLASS.

4. Save, Activate the class and Run the Action. Open another class and place the cursor at empty code line to see the effect clearly.

Select the IDE Action Demo and click Run.

5. Pop-up with options configured in the UI Configuration Class appear.

6. Select and run each option. Once we run the option – Code Review Dialog appears where we can accept the changes or discard the changes. Click Finish to accept.

Code is added.

Other two options produce below code.

Code Explained – Class zjp_ide_action_demo_class_ui

For creating the input UI, the server-driven UI is used. The below constants are created in Public Section of the UI config class. The unfamiliar part of the code are Configuration Options. The short texts that appear in the pop up window are derived from here.

Below code sets default input value, creates input box for the type input which is declared of type ty_input.

Code Explained – Class zjp_ide_action_demo_class

Below code gets the input data from the UI. If for some reason the data is not found then it sets the input to default option.

Based on input, the template values are set. Note that the CASE statement uses the constant names from the type and not the value.

This code gets the current cursor line, creates a source change result object, and sets the result to the selected template.

This is where the action is most useful. This option can be used to analyze source code and create our own quick fixes, more complex templates and some useful tools.

Visit ABAP on HANA series for Tutorials on CDS, AMDP, Eclipse, and ALV IDA.


If you like the content, please subscribe…

Join 515 other subscribers

Discovering ABAP YouTube Channel