Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
SatoshiMatsuzaki authored and matsuzaki215 committed Oct 21, 2019
1 parent 37c72ea commit 28033cf
Show file tree
Hide file tree
Showing 11 changed files with 215 additions and 173 deletions.
8 changes: 5 additions & 3 deletions src/checker/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def _check(tree: SyntaxTree, keyword_style: str) -> List[Violation]:

for leaf in tree.leaves:
for idx, token in enumerate(leaf.tokens):
if token.kind != Token.KEYWORD:
if token.kind not in [Token.KEYWORD, Token.FUNCTION]:
continue

word: str = token.word
Expand Down Expand Up @@ -382,6 +382,7 @@ def _check_context(tree: SyntaxTree, expected_list: Dict[str, str]) -> List[Viol
"""Checks whether join are described fully, for example [inner join], [left outer join], [right outer join] """
violation_list: List[Violation] = list()

# TODO: too deeply nest and complex code
for leaf in tree.leaves:
join_indexes = [i for i, x in enumerate(leaf.tokens) if x.word.upper() == 'JOIN']

Expand Down Expand Up @@ -465,8 +466,9 @@ def _check_blank_line(tree: SyntaxTree, blank_count: int) -> Tuple[List[Violatio
# If this line is not blank and 2 or more previous lines are blank, stack violation.
if is_blank:
blank_count += 1
elif blank_count >= 2:
violation_list.append(violation.MultiBlankLineViolation(tree=leaf, index=0))
else:
if blank_count >= 2:
violation_list.append(violation.MultiBlankLineViolation(tree=leaf, index=0))
blank_count = 0

v_list, blank_count, last_tree = LineChecker._check_blank_line(leaf, blank_count)
Expand Down
4 changes: 2 additions & 2 deletions src/config/config_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ def comma_position(self) -> str:
@property
def keyword_style(self) -> str:
result = self.loader.get('keyword-style')
if result not in ['upper', 'lower', 'upper-head']:
warnings.warn(f'keyword-style value must be "upper", "lower" or "upper-head", but {result}.'
if result not in ['upper-all', 'lower', 'upper-head']:
warnings.warn(f'keyword-style value must be "upper-all", "lower" or "upper-head", but {result}.'
f' So defualt value(lower) was used.')
return 'lower'

Expand Down
56 changes: 19 additions & 37 deletions src/formatter/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,72 +63,54 @@ def _gather_tokens(tree: SyntaxTree) -> List[Token]:
def _reshape_tree(tree: SyntaxTree, config: Config):
parent, children, sibling = _split_tokens(tree)

if len(parent) and parent[0].word.lower() == 'when':
""" DEBUG:
if parent and parent[0].word.lower() == 'when':
print('-'*10)
print(f'parent = {parent}')
print(f'children = {children}')
print(f'sibling = {sibling}')
print('-'*10)
"""

siblings = [sibling]

tree.tokens = parent

# checks tokens(line) length
tokens_and_spaces = fmt.WhiteSpacesFormatter.format_tokens(parent)
length = sum([len(tkn) for tkn in tokens_and_spaces])
length = sum([len(tkn) for tkn in tokens_and_spaces]) + config.indent_steps*(tree.depth-1)
max_length = config.max_line_length

if length > max_length:
_p, _c, _s = grp.LongLineGrouper.group(parent, tree)
if len(_p) and _p[0].word.lower() == 'when':
print('x' * 10)
print(f'parent = {_p}')
print(f'children = {_c}')
print(f'sibling = {_s}')
print('x' * 10)

tree.tokens = _p
if _c:
children = _c + children
siblings.insert(0, _s)

for chn in children:
if chn:
_tree: SyntaxTree = SyntaxTree(
depth=tree.depth + 1,
depth=tree.depth+1,
line_num=0,
tokens=_c,
tokens=chn,
parent=tree,
is_abstract=True)
tree.add_leaf(_tree)
_reshape_tree(_tree, config)

if _s:
for sbg in siblings:
if sbg:
_tree: SyntaxTree = SyntaxTree(
depth=tree.depth,
line_num=0,
tokens=_s,
tokens=sbg,
parent=tree.parent,
is_abstract=True)
tree.parent.add_leaf(_tree)
_reshape_tree(_tree, config)

if children:
children_tree: SyntaxTree = SyntaxTree(
depth=tree.depth+1,
line_num=0,
tokens=children,
parent=tree,
is_abstract=True)
tree.add_leaf(children_tree)
_reshape_tree(children_tree, config)

if sibling:
sibling_tree: SyntaxTree = SyntaxTree(
depth=tree.depth,
line_num=0,
tokens=sibling,
parent=tree.parent,
is_abstract=True)
tree.parent.add_leaf(sibling_tree)
_reshape_tree(sibling_tree, config)


def _split_tokens(tree: SyntaxTree) -> Tuple[List[Token], List[Token], List[Token]]:

def _split_tokens(tree: SyntaxTree) -> Tuple[List[Token], List[List[Token]], List[Token]]:
"""
Args:
Expand All @@ -142,7 +124,7 @@ def _split_tokens(tree: SyntaxTree) -> Tuple[List[Token], List[Token], List[Toke
if not tokens:
return [], [], []

if tokens[0].kind == Token.KEYWORD:
if tokens[0].kind in [Token.KEYWORD, Token.FUNCTION]:
return grp.KeywordGrouper.group(tokens, tree)
elif tokens[0].kind == Token.COMMA:
return grp.CommaGrouper.group(tokens, tree)
Expand Down
15 changes: 9 additions & 6 deletions src/formatter/formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def format(cls, tree: SyntaxTree, config: Config):
def _format(cls, tree: SyntaxTree, keyword_style: str):
for leaf in tree.leaves:
for token in leaf.tokens:
if token.kind == Token.KEYWORD:
if token.kind in [Token.KEYWORD, Token.FUNCTION]:
token.word = format_keyword(token.word, keyword_style)
cls._format(leaf, keyword_style)

Expand All @@ -40,6 +40,7 @@ def format(cls, tree: SyntaxTree, config: Config):
def _format_head(cls, tree: SyntaxTree):
for idx, leaf in enumerate(tree.leaves):
if leaf.tokens[-1].kind != Token.COMMA:
cls._format_head(leaf)
continue

if idx < len(tree.leaves)-1:
Expand All @@ -54,12 +55,13 @@ def _format_end(cls, tree: SyntaxTree):
for idx, leaf in enumerate(tree.leaves):
# ignores comma at zero indent because it may be with-comma
if leaf.depth <= 1 or leaf.tokens[0].kind != Token.COMMA:
return
cls._format_end(leaf)
continue

if idx > 0:
tree.leaves[idx-1].tokens.insert(-1, leaf.tokens.pop(0))
tree.leaves[idx-1].tokens.insert(len(tree.leaves), leaf.tokens.pop(0))
elif leaf.parent and leaf.parent.depth > 0:
leaf.parent.tokens.insert(-1, leaf.tokens.pop(0))
leaf.parent.tokens.insert(len(tree.leaves), leaf.tokens.pop(0))

cls._format_end(leaf)

Expand Down Expand Up @@ -103,8 +105,9 @@ def format_tokens(tokens: List[Token]):

for idx, token in enumerate(tokens):
result.append(token)
# next of ( must not be WHITESPACE
if (token.kind in [Token.BRACKET_LEFT, Token.WHITESPACE]) or \

# next of "(", functions, or whitespaces must not be WHITESPACE
if (token.kind in [Token.BRACKET_LEFT, Token.WHITESPACE, Token.FUNCTION]) or \
(idx >= len(tokens) - 1):
continue

Expand Down
Loading

0 comments on commit 28033cf

Please sign in to comment.