(tlist)
| 15 | |
| 16 | @staticmethod |
| 17 | def _process(tlist): |
| 18 | def get_next_comment(idx=-1): |
| 19 | # TODO(andi) Comment types should be unified, see related issue38 |
| 20 | return tlist.token_next_by(i=sql.Comment, t=T.Comment, idx=idx) |
| 21 | |
| 22 | def _get_insert_token(token): |
| 23 | """Returns either a whitespace or the line breaks from token.""" |
| 24 | # See issue484 why line breaks should be preserved. |
| 25 | # Note: The actual value for a line break is replaced by \n |
| 26 | # in SerializerUnicode which will be executed in the |
| 27 | # postprocessing state. |
| 28 | m = re.search(r'([\r\n]+) *$', token.value) |
| 29 | if m is not None: |
| 30 | return sql.Token(T.Whitespace.Newline, m.groups()[0]) |
| 31 | else: |
| 32 | return sql.Token(T.Whitespace, ' ') |
| 33 | |
| 34 | sql_hints = (T.Comment.Multiline.Hint, T.Comment.Single.Hint) |
| 35 | tidx, token = get_next_comment() |
| 36 | while token: |
| 37 | # skipping token remove if token is a SQL-Hint. issue262 |
| 38 | is_sql_hint = False |
| 39 | if token.ttype in sql_hints: |
| 40 | is_sql_hint = True |
| 41 | elif isinstance(token, sql.Comment): |
| 42 | comment_tokens = token.tokens |
| 43 | if len(comment_tokens) > 0: |
| 44 | if comment_tokens[0].ttype in sql_hints: |
| 45 | is_sql_hint = True |
| 46 | |
| 47 | if is_sql_hint: |
| 48 | # using current index as start index to search next token for |
| 49 | # preventing infinite loop in cases when token type is a |
| 50 | # "SQL-Hint" and has to be skipped |
| 51 | tidx, token = get_next_comment(idx=tidx) |
| 52 | continue |
| 53 | |
| 54 | pidx, prev_ = tlist.token_prev(tidx, skip_ws=False) |
| 55 | nidx, next_ = tlist.token_next(tidx, skip_ws=False) |
| 56 | # Replace by whitespace if prev and next exist and if they're not |
| 57 | # whitespaces. This doesn't apply if prev or next is a parenthesis. |
| 58 | if ( |
| 59 | prev_ is None or next_ is None |
| 60 | or prev_.is_whitespace or prev_.match(T.Punctuation, '(') |
| 61 | or next_.is_whitespace or next_.match(T.Punctuation, ')') |
| 62 | ): |
| 63 | # Insert a whitespace to ensure the following SQL produces |
| 64 | # a valid SQL (see #425). |
| 65 | if prev_ is not None and not prev_.match(T.Punctuation, '('): |
| 66 | tlist.tokens.insert(tidx, _get_insert_token(token)) |
| 67 | tlist.tokens.remove(token) |
| 68 | tidx -= 1 |
| 69 | else: |
| 70 | tlist.tokens[tidx] = _get_insert_token(token) |
| 71 | |
| 72 | # using current index as start index to search next token for |
| 73 | # preventing infinite loop in cases when token type is a |
| 74 | # "SQL-Hint" and has to be skipped |
no test coverage detected