Created
April 5, 2023 11:08
-
-
Save mbtools/037bdf5bb3998d9e0ab32518f85289cc to your computer and use it in GitHub Desktop.
PO Syntax Highlighter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CLASS zcl_abapgit_syntax_po DEFINITION | |
PUBLIC | |
INHERITING FROM zcl_abapgit_syntax_highlighter | |
CREATE PUBLIC. | |
PUBLIC SECTION. | |
CONSTANTS: | |
BEGIN OF c_css, | |
keyword TYPE string VALUE 'keyword', | |
text TYPE string VALUE '', | |
comment TYPE string VALUE 'comment', | |
header TYPE string VALUE 'attr', | |
END OF c_css. | |
CONSTANTS: | |
BEGIN OF c_token, | |
keyword TYPE c VALUE 'K', | |
text TYPE c VALUE 'T', | |
comment TYPE c VALUE 'C', | |
header TYPE c VALUE 'H', | |
END OF c_token. | |
CONSTANTS: | |
BEGIN OF c_regex, | |
keyword TYPE string VALUE '^msgid|msgid_plural|msgstr|msgctxt|fuzzy', | |
comment TYPE string VALUE '^#', | |
text TYPE string VALUE '"', | |
header TYPE string VALUE 'MIME\-Version|Content\-Type|Content\-Transfer\-Encoding|\\n', | |
END OF c_regex. | |
METHODS constructor. | |
PROTECTED SECTION. | |
METHODS order_matches REDEFINITION. | |
PRIVATE SECTION. | |
ENDCLASS. | |
CLASS zcl_abapgit_syntax_po IMPLEMENTATION. | |
* <SIGNATURE>---------------------------------------------------------------------------------------+ | |
* | Instance Public Method ZCL_ABAPGIT_SYNTAX_PO->CONSTRUCTOR | |
* +-------------------------------------------------------------------------------------------------+ | |
* +--------------------------------------------------------------------------------------</SIGNATURE> | |
METHOD constructor. | |
super->constructor( ). | |
" Initialize instances of regular expression | |
add_rule( iv_regex = c_regex-keyword | |
iv_token = c_token-keyword | |
iv_style = c_css-keyword ). | |
add_rule( iv_regex = c_regex-header | |
iv_token = c_token-header | |
iv_style = c_css-header ). | |
add_rule( iv_regex = c_regex-comment | |
iv_token = c_token-comment | |
iv_style = c_css-comment ). | |
add_rule( iv_regex = c_regex-text | |
iv_token = c_token-text | |
iv_style = c_css-text ). | |
ENDMETHOD. | |
* <SIGNATURE>---------------------------------------------------------------------------------------+ | |
* | Instance Protected Method ZCL_ABAPGIT_SYNTAX_PO->ORDER_MATCHES | |
* +-------------------------------------------------------------------------------------------------+ | |
* | [--->] IV_LINE TYPE STRING | |
* | [<-->] CT_MATCHES TYPE TY_MATCH_TT | |
* +--------------------------------------------------------------------------------------</SIGNATURE> | |
METHOD order_matches. | |
DATA: | |
lv_index TYPE sy-tabix, | |
lv_line_len TYPE i, | |
lv_prev_token TYPE c. | |
FIELD-SYMBOLS: | |
<ls_prev> TYPE ty_match, | |
<ls_match> TYPE ty_match. | |
SORT ct_matches BY offset. | |
lv_line_len = strlen( iv_line ). | |
LOOP AT ct_matches ASSIGNING <ls_match>. | |
lv_index = sy-tabix. | |
* " Delete matches after open text match | |
* IF lv_prev_token = c_token-text AND <ls_match>-token <> c_token-text. | |
* DELETE ct_matches INDEX lv_index. | |
* CONTINUE. | |
* ENDIF. | |
CASE <ls_match>-token. | |
WHEN c_token-comment. | |
<ls_match>-length = lv_line_len - <ls_match>-offset. | |
DELETE ct_matches FROM lv_index + 1. | |
CONTINUE. | |
* | |
* WHEN c_token-text. | |
* <ls_match>-text_tag = substring( val = iv_line | |
* off = <ls_match>-offset | |
* len = <ls_match>-length ). | |
* IF lv_prev_token = c_token-text. | |
* IF <ls_match>-text_tag = <ls_prev>-text_tag. | |
* <ls_prev>-length = <ls_match>-offset + <ls_match>-length - <ls_prev>-offset. | |
* CLEAR lv_prev_token. | |
* ENDIF. | |
* DELETE ct_matches INDEX lv_index. | |
* CONTINUE. | |
* ENDIF. | |
* | |
ENDCASE. | |
lv_prev_token = <ls_match>-token. | |
ASSIGN <ls_match> TO <ls_prev>. | |
ENDLOOP. | |
ENDMETHOD. | |
ENDCLASS. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment