Development of an internal social media platform with personalised dashboards for students
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

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