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