diff options
| author | Victor Uriarte <victor.m.uriarte@intel.com> | 2016-06-06 22:05:29 -0700 |
|---|---|---|
| committer | Victor Uriarte <victor.m.uriarte@intel.com> | 2016-06-09 20:37:05 -0700 |
| commit | dc52b32bc1e43155be22c1fa77eac0916bc35735 (patch) | |
| tree | 876b09f442c50e5a0a3f2c3aaa7790f64412ec40 /sqlparse | |
| parent | 99881c1144413c6b45d97e1bf6e5ff929ae7b6b9 (diff) | |
| download | sqlparse-dc52b32bc1e43155be22c1fa77eac0916bc35735.tar.gz | |
refactor process case
Diffstat (limited to 'sqlparse')
| -rw-r--r-- | sqlparse/filters/aligned_indent.py | 21 |
1 files changed, 9 insertions, 12 deletions
diff --git a/sqlparse/filters/aligned_indent.py b/sqlparse/filters/aligned_indent.py index 7c275b9..05539fa 100644 --- a/sqlparse/filters/aligned_indent.py +++ b/sqlparse/filters/aligned_indent.py @@ -62,29 +62,26 @@ class AlignedIndentFilter(object): self._process_default(tlist) def _process_case(self, tlist): - base_offset = len('case ') - case_offset = len('when ') + offset_ = len('case ') + len('when ') cases = tlist.get_cases(skip_ws=True) # align the end as well end_token = tlist.token_next_by(m=(T.Keyword, 'END')) cases.append((None, [end_token])) - condition_width = max( - len(' '.join(map(str, cond))) for cond, value in cases if cond) + condition_width = [len(' '.join(map(text_type, cond))) if cond else 0 + for cond, _ in cases] + max_cond_width = max(condition_width) + for i, (cond, value) in enumerate(cases): - if cond is None: # else or end - stmt = value[0] - line = value - else: - stmt = cond[0] - line = cond + value + # cond is None when 'else or end' + stmt = cond[0] if cond else value[0] if i > 0: tlist.insert_before(stmt, self.nl( - base_offset + case_offset - len(str(stmt)))) + offset_ - len(text_type(stmt)))) if cond: ws = sql.Token(T.Whitespace, self.char * ( - condition_width - len(' '.join(map(text_type, cond))))) + max_cond_width - condition_width[i])) tlist.insert_after(cond[-1], ws) def _next_token(self, tlist, idx=0): |
