Skip to content

Latest commit

 

History

History
455 lines (377 loc) · 20 KB

example.md

File metadata and controls

455 lines (377 loc) · 20 KB

Info

This File is using with the VIM Emulator . The Goal is to describe VIM Refactorings, that are useful for Developers.

Try to create Examples that don't have more then 14 VIM Refactoring Moves! Code Snippet shouldn't be more then 10 Lines of Code.

Examples

Example-1

@ Delete the Word 'Damir'.

register(|Damir|);
WHAT HOW
search for 'D' /Denter
search next Finding n
delete Word dw

Example-1

@ Rename 'width' to 'length'

  METHOD get_text.
    result = |Invalid width.|.
  ENDMETHOD.
WHAT HOW
5 lines down 5j
forward to first occurrence of w fw
change word cw
enter text lengthesc

Example-2

@ Correct typing error 'ont' to 'not'

  METHOD get_text.
    result = |File could ont be read.|.
  ENDMETHOD.
WHAT HOW
search for 'ont' /ontenter
next occurrence n
delete character x
paste content of default register (o) after cursor p

Example-3

@ Remove prefixes of parameters

  METHODS:
    process
      IMPORTING
        i_text_before       TYPE string
        i_width             TYPE i
      RETURNING
        VALUE(r_text_after) TYPE stringtab.
WHAT HOW
search for 'i_'; this will jump to the beginning of i_text_before /i_enter
rename in file; this will rename all occurrences of i_text_before ctrl2r
remove character under cursor and next one 2x
end renaming enter
find next occurrence of last searched string ('i_'); this will jump to the beginning of i_width n
rename in file; this will rename all occurrences of i_width ctrl2r
remove character under cursor and next one 2x
end renaming enter
2 lines down 2j
forward to first occurrence of r fr
rename in file; this will rename all occurrences of r_text_after ctrl2r
remove character under cursor and next one 2x
end renaming enter

Example-4

@ Remove prefix 'lv_' from multiple variables in local scope

  METHOD multiplication.
    DATA(lv_left) = iv_left.
    DATA(lv_right) = iv_right.
    add_line(
      EXPORTING
        iv_left   = lv_left
        iv_right  = lv_right
    ).
    WHILE lv_left > 1.
      calculate_next_line(
        CHANGING
          cv_left  = lv_left
          cv_right = lv_right
      ).
      add_line(
        EXPORTING
          iv_left   = lv_left
          iv_right  = lv_right
      ).
    ENDWHILE.
    rv_result = calculate_result( ).
    IF iv_write_calculation = abap_true.
      write_calculation( iv_result = rv_result ).
    ENDIF.
  ENDMETHOD.
WHAT HOW
Enter command mode :
For the whole file %
Substitute s
Find lv_ /lv_
Substitute with nothing //
Substitute all occurrences g
Run command enter

Example-5

@ Unchain method declarations

CLASS ltc_russian_peasant_multipl DEFINITION FINAL FOR TESTING
  DURATION SHORT
  RISK LEVEL HARMLESS.

  PRIVATE SECTION.
    METHODS:
      acceptance_test FOR TESTING RAISING cx_static_check,
      simple_multiplication FOR TESTING RAISING cx_static_check,
      calculate_result FOR TESTING RAISING cx_static_check,
      calculate_next_line FOR TESTING RAISING cx_static_check,
      add_line_left_even FOR TESTING RAISING cx_static_check,
      add_line_left_odd FOR TESTING RAISING cx_static_check,
      calculate_column_width FOR TESTING RAISING cx_static_check.
ENDCLASS.
WHAT HOW
9 lines down 9j
Jump to end of line (:) $
Delete character x
Delete whole word ('METHODS') daw
One line down j
Start recording macro 'q' qq
Jump to beginning of line 0
Jump to next word w
Move left two times hh
Paste from register ('METHODS') p
Jump to end of line $
Replace character under cursor with '.' r.
One line down j
End recording macro q
Apply macro 'q' six times 6@q

Example-6

@ Unchain few method declarations

    METHODS:
      text_is_broken_length_9 FOR TESTING RAISING cx_static_check,
      text_is_broken_length_14 FOR TESTING RAISING cx_static_check.
WHAT HOW
Search ':' /:enter
Delete character x
Delete whole word ('METHODS') daw
One line down j
Jump to next word w
Move left two times hh
Block selection ctrlv
One line down j
Paste from register ('METHODS') p
Jump to last character $
Replace character under cursor with '.' r.

Example-7

@ Increase all arguments of the roll-calls by one

    DO 1000 TIMES.
      mo_cut->roll( 6 ).
      mo_cut->roll( 9 ).
      mo_cut->roll( 1 ).
      mo_cut->roll( 8 ).
      mo_cut->roll( 2 ).
      mo_cut->roll( 7 ).
      mo_cut->roll( 3 ).
      mo_cut->roll( 6 ).
      mo_cut->roll( 4 ).
      mo_cut->roll( 5 ).
      mo_cut->roll( 5 ).
      mo_cut->roll( 4 ).
      mo_cut->roll( 6 ).
      mo_cut->roll( 3 ).
      mo_cut->roll( 7 ).
      mo_cut->roll( 2 ).
      mo_cut->roll( 8 ).
      mo_cut->roll( 1 ).
      mo_cut->roll( 9 ).
      mo_cut->roll( 0 ).
      CLEAR mo_cut.
      mo_cut = NEW lcl_game( ).
    ENDDO.
WHAT HOW
Five lines down 5j
Jump to '6' f6
Block selection ctrlv
19 lines down 19j
Increase number under cursor by 1 ctrla

Example-8

@ Change X to lower case

result = VALUE #( ( rolls = 'X X X X X X X X X X X X' result = 300 ) ).
WHAT HOW
Four lines down 4j
Jump to 'X' fX
Select everything within the current ''-block vi'
Turn selection to lower case u

Example-9

@ Insert line breaks after method name and parameter group

METHODS get_element_at_position IMPORTING iv_position      TYPE i
                                RETURNING VALUE(rv_result) TYPE string.
WHAT HOW
Four lines down 4j
Jump to space fspace
Jump two more times ;;
Block selection ctrlv
One line down j
Replace selection with line break renter
Jump to previous space Fspace
Replace character under cursor with line break renter
Format code shiftf1

Example-10

@ Introduce parameter

CLASS lcl_frame DEFINITION.
  PUBLIC SECTION.
    DATA attribute TYPE string.
    
    METHODS constructor.
ENDCLASS.

CLASS lcl_frame IMPLEMENTATION.

  METHOD constructor.
    attribute = parameter.
  ENDMETHOD.

ENDCLASS.
WHAT HOW
Search and jump to''' /'enter
Change rest of line C
Enter text parameteresc
One line up k
Two times left hh
Jump to definition gd
One word forward w
Enter insert-mode i
Enter text enterimportingenterparameter type string
Exit insert-mode esc

Example-11

@ Extract method

CLASS lcl_russian_peasant_multipl DEFINITION
  CREATE PUBLIC .
  
    PUBLIC SECTION.
        METHODS multiplication
          IMPORTING
            iv_left              TYPE i
            iv_right             TYPE i
            iv_write_calculation TYPE abap_bool DEFAULT abap_false
          RETURNING
            VALUE(rv_result)     TYPE i.

"...

ENDCLASS.

CLASS lcl_russian_peasant_multipl IMPLEMENTATION.

  METHOD multiplication.
    DATA(lv_left) = iv_left.
    DATA(lv_right) = iv_right.
    add_line(
      EXPORTING
        iv_left   = lv_left
        iv_right  = lv_right
    ).
    WHILE lv_left > 1.
      calculate_next_line(
        CHANGING
          cv_left  = lv_left
          cv_right = lv_right
      ).
      add_line(
        EXPORTING
          iv_left   = lv_left
          iv_right  = lv_right
      ).
    ENDWHILE.
    rv_result = calculate_result( ).
    IF iv_write_calculation = abap_true.
      write_calculation( iv_result = rv_result ).
    ENDIF.
  ENDMETHOD.

"...

ENDCLASS.
WHAT HOW
Search and jump to 'WHILE' /WHILEenter
One line down j
Line-selection V
9 lines down / include next 9 lines in selection 9j
Replace selection, moving it to register 'i' "ic
Enter text create_new_line( iv_left = iv_left iv_right = iv_right ).esc
Jump to first non-blank character in line ^
Select to end of word ve
Yank selection into register 'd' "dy
8 lines down 8j
Insert new line below o
Enter text METHOD esc
Paste content of register 'd' "dp
Insert '.' after cursor a.esc
Paste content of register 'i' "ip
10 lines down 10j
Insert new line below o
Enter text ENDMETHOD.esc
Backward-search to 'ENDCL' ?ENDCLenter
Insert new line above O
Enter text METHOD esc
Paste content of register 'd' "dp
9 lines up 9k
Lines-selection V
2 lines down / include next 2 lines in selection 2j
Yank selection y
9 lines down 9j
Paste p
Two lines down 2j
Insert '.' at end of line A.esc

Example-12

@

Code Snippet
..
Code Snippet
WHAT HOW
... ...
... ...
... ...

Example-13

@

Code Snippet
..
Code Snippet
WHAT HOW
... ...
... ...
... ...

Example-14

@

Code Snippet
..
Code Snippet
WHAT HOW
... ...
... ...
... ...
Jump to @ sign, this is your vim-refactoring starting point.