Skip to content

Commit 803d8a5

Browse files
andfoyccordoba12
authored andcommitted
Prevent extra folding regions for iterables and flow nodes (#692)
1 parent 45702ec commit 803d8a5

1 file changed

Lines changed: 40 additions & 5 deletions

File tree

pyls/plugins/folding.py

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,16 +102,51 @@ def __check_if_node_is_valid(node):
102102
valid = True
103103
if isinstance(node, tree_nodes.PythonNode):
104104
kind = node.type
105-
valid = kind not in {'decorated', 'parameters'}
105+
valid = kind not in {'decorated', 'parameters', 'dictorsetmaker',
106+
'testlist_comp'}
106107
if kind == 'suite':
107108
if isinstance(node.parent, tree_nodes.Function):
108109
valid = False
109110
return valid
110111

111112

113+
def __handle_flow_nodes(node, end_line, stack):
114+
from_keyword = False
115+
if isinstance(node, tree_nodes.Keyword):
116+
from_keyword = True
117+
if node.value in {'if', 'elif', 'with', 'while', 'except'}:
118+
body = stack[2]
119+
children = [body]
120+
if hasattr(body, 'children'):
121+
children = body.children
122+
stack = stack[:2] + children + stack[3:]
123+
node = body
124+
end_line, _ = body.end_pos
125+
elif node.value in {'for'}:
126+
body = stack[4]
127+
children = [body]
128+
if hasattr(body, 'children'):
129+
children = body.children
130+
stack = stack[:4] + children + stack[5:]
131+
node = body
132+
end_line, _ = body.end_pos
133+
elif node.value in {'else'}:
134+
body = stack[1]
135+
children = [body]
136+
if hasattr(body, 'children'):
137+
children = body.children
138+
stack = stack[:1] + children + stack[2:]
139+
node = body
140+
end_line, _ = body.end_pos
141+
return end_line, from_keyword, node, stack
142+
143+
112144
def __compute_start_end_lines(node, stack):
113145
start_line, _ = node.start_pos
114146
end_line, _ = node.end_pos
147+
modified = False
148+
end_line, from_keyword, node, stack = __handle_flow_nodes(
149+
node, end_line, stack)
115150

116151
last_leaf = node.get_last_leaf()
117152
last_newline = isinstance(last_leaf, tree_nodes.Newline)
@@ -121,8 +156,7 @@ def __compute_start_end_lines(node, stack):
121156

122157
end_line -= 1
123158

124-
modified = False
125-
if isinstance(node.parent, tree_nodes.PythonNode):
159+
if isinstance(node.parent, tree_nodes.PythonNode) and not from_keyword:
126160
kind = node.type
127161
if kind in {'suite', 'atom', 'atom_expr', 'arglist'}:
128162
if len(stack) > 0:
@@ -133,7 +167,7 @@ def __compute_start_end_lines(node, stack):
133167
modified = True
134168
if not last_newline and not modified and not last_operator:
135169
end_line += 1
136-
return start_line, end_line
170+
return start_line, end_line, stack
137171

138172

139173
def __compute_folding_ranges(tree, lines):
@@ -158,7 +192,8 @@ def __compute_folding_ranges(tree, lines):
158192
elif not isinstance(node, SKIP_NODES):
159193
valid = __check_if_node_is_valid(node)
160194
if valid:
161-
start_line, end_line = __compute_start_end_lines(node, stack)
195+
start_line, end_line, stack = __compute_start_end_lines(
196+
node, stack)
162197
if end_line > start_line:
163198
current_end = folding_ranges.get(start_line, -1)
164199
folding_ranges[start_line] = max(current_end, end_line)

0 commit comments

Comments
 (0)