Yield Tokens from a ``tokens`` iterable of Token objects (built from a query- side scanned file or string) that are inside a ``match_qspan`` matched Span starting at `start_line` and ending at ``end_line``. If whole_lines is True, also yield unmatched Tokens that are before and afte
(
tokens,
match_qspan,
start_line,
end_line,
whole_lines=False,
trace=TRACE_MATCHED_TEXT_DETAILS,
)
| 3049 | |
| 3050 | |
| 3051 | def reportable_tokens( |
| 3052 | tokens, |
| 3053 | match_qspan, |
| 3054 | start_line, |
| 3055 | end_line, |
| 3056 | whole_lines=False, |
| 3057 | trace=TRACE_MATCHED_TEXT_DETAILS, |
| 3058 | ): |
| 3059 | """ |
| 3060 | Yield Tokens from a ``tokens`` iterable of Token objects (built from a query- |
| 3061 | side scanned file or string) that are inside a ``match_qspan`` matched Span |
| 3062 | starting at `start_line` and ending at ``end_line``. If whole_lines is True, |
| 3063 | also yield unmatched Tokens that are before and after the match and on the |
| 3064 | first and last line of a match (unless the lines are very long text lines or |
| 3065 | the match is from binary content.) |
| 3066 | |
| 3067 | As a side effect, known matched tokens are tagged as "is_matched=True" if |
| 3068 | they are matched. |
| 3069 | |
| 3070 | If ``whole_lines`` is True, any token within matched lines range is |
| 3071 | included. Otherwise, a token is included if its position is within the |
| 3072 | matched ``match_qspan`` or it is a punctuation token immediately after the |
| 3073 | matched ``match_qspan`` even though not matched. |
| 3074 | """ |
| 3075 | start = match_qspan.start |
| 3076 | end = match_qspan.end |
| 3077 | |
| 3078 | started = False |
| 3079 | finished = False |
| 3080 | |
| 3081 | end_pos = 0 |
| 3082 | last_pos = 0 |
| 3083 | for real_pos, tok in enumerate(tokens): |
| 3084 | if trace: |
| 3085 | logger_debug('reportable_tokens: processing', real_pos, tok) |
| 3086 | |
| 3087 | # ignore tokens outside the matched lines range |
| 3088 | if tok.line_num < start_line: |
| 3089 | if trace: |
| 3090 | logger_debug(' tok.line_num < start_line:', tok.line_num, '<', start_line) |
| 3091 | |
| 3092 | continue |
| 3093 | |
| 3094 | if tok.line_num > end_line: |
| 3095 | if trace: |
| 3096 | logger_debug(' tok.line_num > end_line', tok.line_num, '>', end_line) |
| 3097 | |
| 3098 | break |
| 3099 | |
| 3100 | if trace: |
| 3101 | logger_debug('reportable_tokens:', real_pos, tok) |
| 3102 | |
| 3103 | is_included = False |
| 3104 | |
| 3105 | # tagged known matched tokens (useful for highlighting) |
| 3106 | if tok.pos != -1 and tok.is_known and tok.pos in match_qspan: |
| 3107 | tok = attr.evolve(tok, is_matched=True) |
| 3108 | is_included = True |