Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 5x 5x 1x 1x 1x 1x 1x 1x 1x 4x 4x 4x 4x 4x 4x 4x 39x 39x 39x 39x 40x 127x 127x 127x 127x 1x 1x 1x 126x 126x 126x 126x 126x 126x 126x 58x 58x 58x 58x 14x 14x 14x 14x 14x 14x 58x 14x 14x 13x 13x 13x 14x 30x 30x 58x 58x 127x 127x 5x 5x 5x 5x 127x 1x 1x 1x 1x 1x 8x 8x 8x 8x 127x 127x 127x 16x 16x 127x 127x 2x 2x 2x 127x 127x 127x 127x 127x 127x 127x 39x 39x 39x 40x 40x 40x 1x 1x 1x 1x 1x 40x 40x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 5x 5x 1x 1x 1x 1x 1x 4x 4x 4x 4x 4x 4x 39x 39x 64x 64x 64x 26x 26x 38x 38x 38x 38x 38x 28x 28x 10x 10x 10x 10x 10x 64x 2x 2x 10x 10x 10x 10x 10x 5x 5x 5x 5x 5x 10x 10x 10x 10x 10x 10x 10x 39x 39x 39x 39x 1x 111x 111x 1x 1x 1x 1x | CLASS zcl_abapgit_syntax_xml DEFINITION
PUBLIC
INHERITING FROM zcl_abapgit_syntax_highlighter
CREATE PUBLIC .
PUBLIC SECTION.
CONSTANTS:
BEGIN OF c_css,
xml_tag TYPE string VALUE 'xml_tag',
attr TYPE string VALUE 'attr',
attr_val TYPE string VALUE 'attr_val',
comment TYPE string VALUE 'comment',
END OF c_css .
CONSTANTS:
BEGIN OF c_token,
xml_tag TYPE c VALUE 'X',
attr TYPE c VALUE 'A',
attr_val TYPE c VALUE 'V',
comment TYPE c VALUE 'C',
END OF c_token .
CONSTANTS:
BEGIN OF c_regex,
"for XML tags, we will use a submatch
" main pattern includes quoted strings so we can ignore < and > in attr values
xml_tag TYPE string VALUE '(?:"[^"]*")|(?:''[^'']*'')|(?:`[^`]*`)|([<>])',
attr TYPE string VALUE '(?:^|\s)[-a-z:_.0-9]+\s*(?==\s*["''`])',
attr_val TYPE string VALUE '("[^"]*")|(''[^'']*'')|(`[^`]*`)',
" comments <!-- ... -->
comment TYPE string VALUE '<!--(?:(?!-->).)*-->|<!--|-->',
END OF c_regex .
METHODS constructor .
PROTECTED SECTION.
DATA mv_comment TYPE abap_bool.
METHODS order_matches REDEFINITION.
METHODS parse_line REDEFINITION.
PRIVATE SECTION.
ENDCLASS.
CLASS zcl_abapgit_syntax_xml IMPLEMENTATION.
METHOD constructor.
super->constructor( ).
" Reset indicator for multi-line comments
CLEAR mv_comment.
" Initialize instances of regular expressions
add_rule( iv_regex = c_regex-xml_tag
iv_token = c_token-xml_tag
iv_style = c_css-xml_tag
iv_submatch = 1 ).
add_rule( iv_regex = c_regex-attr
iv_token = c_token-attr
iv_style = c_css-attr ).
add_rule( iv_regex = c_regex-attr_val
iv_token = c_token-attr_val
iv_style = c_css-attr_val ).
add_rule( iv_regex = c_regex-comment
iv_token = c_token-comment
iv_style = c_css-comment ).
ENDMETHOD.
METHOD order_matches.
DATA:
lv_match TYPE string,
lv_line_len TYPE i,
lv_cmmt_end TYPE i,
lv_comment_end TYPE i,
lv_prev_end TYPE i,
ls_comment TYPE ty_match,
lv_index TYPE sy-tabix,
lv_prev_token TYPE c,
lv_state TYPE c VALUE 'O'. " O - for open tag; C - for closed tag;
FIELD-SYMBOLS:
<ls_prev> TYPE ty_match,
<ls_match> TYPE ty_match.
lv_line_len = strlen( iv_line ).
" A continued comment ends at the first delimiter, regardless of its content.
IF mv_comment = abap_true.
FIND FIRST OCCURRENCE OF '-->' IN iv_line MATCH OFFSET lv_comment_end.
IF sy-subrc <> 0.
CLEAR ct_matches.
APPEND INITIAL LINE TO ct_matches ASSIGNING <ls_match>.
<ls_match>-token = c_token-comment.
<ls_match>-offset = 0.
<ls_match>-length = lv_line_len.
RETURN.
ENDIF.
lv_comment_end = lv_comment_end + 3.
DELETE ct_matches WHERE offset < lv_comment_end.
ls_comment-token = c_token-comment.
ls_comment-length = lv_comment_end.
APPEND ls_comment TO ct_matches.
mv_comment = abap_false.
ENDIF.
" Longest matches, including any continued comment prefix.
SORT ct_matches BY offset length DESCENDING.
LOOP AT ct_matches ASSIGNING <ls_match>.
lv_index = sy-tabix.
" Ignore comment delimiters and nested quotes inside an accepted match.
IF <ls_match>-offset < lv_prev_end.
DELETE ct_matches INDEX lv_index.
CONTINUE.
ENDIF.
lv_match = substring( val = iv_line
off = <ls_match>-offset
len = <ls_match>-length ).
CASE <ls_match>-token.
WHEN c_token-xml_tag.
<ls_match>-text_tag = lv_match.
" No other matches between two tags
IF <ls_match>-text_tag = '>' AND lv_prev_token = c_token-xml_tag.
lv_state = 'C'.
<ls_prev>-length = <ls_match>-offset - <ls_prev>-offset + <ls_match>-length.
DELETE ct_matches INDEX lv_index.
CONTINUE.
" Adjust length and offset of closing tag
ELSEIF <ls_match>-text_tag = '>' AND lv_prev_token <> c_token-xml_tag.
lv_state = 'C'.
IF <ls_prev> IS ASSIGNED.
<ls_match>-length = <ls_match>-offset - <ls_prev>-offset - <ls_prev>-length + <ls_match>-length.
<ls_match>-offset = <ls_prev>-offset + <ls_prev>-length.
ENDIF.
ELSE.
lv_state = 'O'.
ENDIF.
WHEN c_token-comment.
lv_state = 'C'.
IF lv_match = '<!--'.
DELETE ct_matches WHERE offset > <ls_match>-offset.
DELETE ct_matches WHERE offset = <ls_match>-offset AND token = c_token-xml_tag.
<ls_match>-length = lv_line_len - <ls_match>-offset.
mv_comment = abap_true.
ELSEIF lv_match = '-->'.
DELETE ct_matches WHERE offset < <ls_match>-offset.
<ls_match>-length = <ls_match>-offset + 3.
<ls_match>-offset = 0.
mv_comment = abap_false.
ELSE.
lv_cmmt_end = <ls_match>-offset + <ls_match>-length.
DELETE ct_matches WHERE offset > <ls_match>-offset AND offset < lv_cmmt_end.
DELETE ct_matches WHERE offset = <ls_match>-offset AND token = c_token-xml_tag.
ENDIF.
WHEN OTHERS.
IF lv_prev_token = c_token-xml_tag.
<ls_prev>-length = <ls_match>-offset - <ls_prev>-offset. " Extend length of the opening tag
ENDIF.
IF lv_state = 'C'. " Delete all matches between tags
DELETE ct_matches INDEX lv_index.
CONTINUE.
ENDIF.
ENDCASE.
lv_prev_end = <ls_match>-offset + <ls_match>-length.
lv_prev_token = <ls_match>-token.
ASSIGN <ls_match> TO <ls_prev>.
ENDLOOP.
"if the last XML tag is not closed, extend it to the end of the tag
IF lv_prev_token = c_token-xml_tag
AND <ls_prev> IS ASSIGNED
AND <ls_prev>-length = 1
AND <ls_prev>-text_tag = '<'.
FIND REGEX '<\s*[^\s]*' IN iv_line+<ls_prev>-offset MATCH LENGTH <ls_prev>-length ##REGEX_POSIX.
IF sy-subrc <> 0.
<ls_prev>-length = 1.
ENDIF.
ENDIF.
ENDMETHOD.
METHOD parse_line.
DATA:
lv_line_len TYPE i,
lv_segment_start TYPE i,
lv_scan_offset TYPE i,
lv_pattern TYPE string,
lv_comment_end TYPE i,
lv_found_offset TYPE i,
lv_found_length TYPE i,
lv_segment TYPE string,
lv_comment_length TYPE i,
lt_segment_matches TYPE ty_match_tt,
ls_comment TYPE ty_match.
FIELD-SYMBOLS <ls_segment_match> TYPE ty_match.
lv_line_len = strlen( iv_line ).
lv_pattern = c_regex-attr_val && '|<!--'.
" Comments are parsed separately so their quotes cannot hide subsequent tags.
IF mv_comment = abap_true.
FIND FIRST OCCURRENCE OF '-->' IN iv_line MATCH OFFSET lv_comment_end.
IF sy-subrc <> 0.
ls_comment-token = c_token-comment.
ls_comment-length = lv_line_len.
APPEND ls_comment TO rt_matches.
RETURN.
ENDIF.
lv_segment_start = lv_comment_end + 3.
lv_scan_offset = lv_segment_start.
ls_comment-token = c_token-comment.
ls_comment-length = lv_segment_start.
APPEND ls_comment TO rt_matches.
ENDIF.
WHILE lv_scan_offset < lv_line_len.
FIND FIRST OCCURRENCE OF REGEX lv_pattern IN iv_line+lv_scan_offset
MATCH OFFSET lv_found_offset MATCH LENGTH lv_found_length ##REGEX_POSIX.
IF sy-subrc <> 0.
EXIT.
ENDIF.
lv_found_offset = lv_found_offset + lv_scan_offset.
lv_scan_offset = lv_found_offset + lv_found_length.
IF substring( val = iv_line
off = lv_found_offset
len = lv_found_length ) <> '<!--'.
CONTINUE.
ENDIF.
lv_segment = substring( val = iv_line
off = lv_segment_start
len = lv_found_offset - lv_segment_start ).
lt_segment_matches = super->parse_line( lv_segment ).
LOOP AT lt_segment_matches ASSIGNING <ls_segment_match>.
<ls_segment_match>-offset = <ls_segment_match>-offset + lv_segment_start.
ENDLOOP.
APPEND LINES OF lt_segment_matches TO rt_matches.
lv_comment_length = 4.
FIND FIRST OCCURRENCE OF '-->' IN iv_line+lv_scan_offset MATCH OFFSET lv_comment_end.
IF sy-subrc = 0.
lv_scan_offset = lv_scan_offset + lv_comment_end + 3.
lv_comment_length = lv_scan_offset - lv_found_offset.
ELSE.
lv_scan_offset = lv_line_len.
ENDIF.
CLEAR ls_comment.
ls_comment-token = c_token-comment.
ls_comment-offset = lv_found_offset.
ls_comment-length = lv_comment_length.
APPEND ls_comment TO rt_matches.
lv_segment_start = lv_scan_offset.
ENDWHILE.
lv_segment = substring( val = iv_line
off = lv_segment_start ).
lt_segment_matches = super->parse_line( lv_segment ).
LOOP AT lt_segment_matches ASSIGNING <ls_segment_match>.
<ls_segment_match>-offset = <ls_segment_match>-offset + lv_segment_start.
ENDLOOP.
APPEND LINES OF lt_segment_matches TO rt_matches.
ENDMETHOD.
ENDCLASS.
|