You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

right_margin.py 1.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2009-2018 the sqlparse authors and contributors
  4. # <see AUTHORS file>
  5. #
  6. # This module is part of python-sqlparse and is released under
  7. # the BSD License: https://opensource.org/licenses/BSD-3-Clause
  8. import re
  9. from sqlparse import sql, tokens as T
  10. from sqlparse.compat import text_type
  11. # FIXME: Doesn't work
  12. class RightMarginFilter(object):
  13. keep_together = (
  14. # sql.TypeCast, sql.Identifier, sql.Alias,
  15. )
  16. def __init__(self, width=79):
  17. self.width = width
  18. self.line = ''
  19. def _process(self, group, stream):
  20. for token in stream:
  21. if token.is_whitespace and '\n' in token.value:
  22. if token.value.endswith('\n'):
  23. self.line = ''
  24. else:
  25. self.line = token.value.splitlines()[-1]
  26. elif token.is_group and type(token) not in self.keep_together:
  27. token.tokens = self._process(token, token.tokens)
  28. else:
  29. val = text_type(token)
  30. if len(self.line) + len(val) > self.width:
  31. match = re.search(r'^ +', self.line)
  32. if match is not None:
  33. indent = match.group()
  34. else:
  35. indent = ''
  36. yield sql.Token(T.Whitespace, '\n{0}'.format(indent))
  37. self.line = indent
  38. self.line += val
  39. yield token
  40. def process(self, group):
  41. # return
  42. # group.tokens = self._process(group, group.tokens)
  43. raise NotImplementedError