Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 21 additions & 22 deletions pyls/plugins/folding.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,34 +110,33 @@ def __check_if_node_is_valid(node):
return valid


def __handle_skip(stack, skip):
body = stack[skip]
children = [body]
if hasattr(body, 'children'):
children = body.children
stack = stack[:skip] + children + stack[skip + 1:]
node = body
end_line, _ = body.end_pos
return node, end_line


def __handle_flow_nodes(node, end_line, stack):
from_keyword = False
if isinstance(node, tree_nodes.Keyword):
from_keyword = True
if node.value in {'if', 'elif', 'with', 'while', 'except'}:
body = stack[2]
children = [body]
if hasattr(body, 'children'):
children = body.children
stack = stack[:2] + children + stack[3:]
node = body
end_line, _ = body.end_pos
if node.value in {'if', 'elif', 'with', 'while'}:
node, end_line = __handle_skip(stack, 2)
elif node.value in {'except'}:
first_node = stack[0]
if isinstance(first_node, tree_nodes.Operator):
node, end_line = __handle_skip(stack, 1)
else:
node, end_line = __handle_skip(stack, 2)
elif node.value in {'for'}:
body = stack[4]
children = [body]
if hasattr(body, 'children'):
children = body.children
stack = stack[:4] + children + stack[5:]
node = body
end_line, _ = body.end_pos
node, end_line = __handle_skip(stack, 4)
elif node.value in {'else'}:
body = stack[1]
children = [body]
if hasattr(body, 'children'):
children = body.children
stack = stack[:1] + children + stack[2:]
node = body
end_line, _ = body.end_pos
node, end_line = __handle_skip(stack, 1)
return end_line, from_keyword, node, stack


Expand Down
6 changes: 5 additions & 1 deletion test/plugins/test_folding.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ def inner():
pass
finally:
raise SomeException()

def testC():
pass
""")

SYNTAX_ERR = dedent("""
Expand Down Expand Up @@ -141,7 +144,8 @@ def test_folding():
{'startLine': 59, 'endLine': 65},
{'startLine': 60, 'endLine': 61},
{'startLine': 62, 'endLine': 63},
{'startLine': 64, 'endLine': 65}]
{'startLine': 64, 'endLine': 65},
{'startLine': 67, 'endLine': 68}]
assert ranges == expected


Expand Down