1#
2# Copyright (C) 2009-2020 the sqlparse authors and contributors
3# <see AUTHORS file>
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
8import re
9
10from sqlparse import sql
11from sqlparse import tokens as T
12
13
14# FIXME: Doesn't work
15class RightMarginFilter:
16 keep_together = (
17 # sql.TypeCast, sql.Identifier, sql.Alias,
18 )
19
20 def __init__(self, width=79):
21 self.width = width
22 self.line = ''
23
24 def _process(self, group, stream):
25 for token in stream:
26 if token.is_whitespace and '\n' in token.value:
27 if token.value.endswith('\n'):
28 self.line = ''
29 else:
30 self.line = token.value.splitlines()[-1]
31 elif token.is_group and type(token) not in self.keep_together:
32 token.tokens = self._process(token, token.tokens)
33 else:
34 val = str(token)
35 if len(self.line) + len(val) > self.width:
36 match = re.search(r'^ +', self.line)
37 if match is not None:
38 indent = match.group()
39 else:
40 indent = ''
41 yield sql.Token(T.Whitespace, f'\n{indent}')
42 self.line = indent
43 self.line += val
44 yield token
45
46 def process(self, group):
47 # return
48 # group.tokens = self._process(group, group.tokens)
49 raise NotImplementedError