Skip to content

Commit cf40bdc

Browse files
Indent the ellipsis between non-adjacent lines:
Use the last line indent so that the result can be un-indented by the caller. It prevents results like this: ``` /* block 1 */ ... /* block 2 */ ``` turning them into ``` /* block 1 */ ... /* block 2 */ ``` which will be dedented by the caller.
1 parent 8540916 commit cf40bdc

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

codeinclude/resolver.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,19 @@ def select(
8989
last_selected = 0
9090
for i in sorted(selected_lines):
9191
if i > (last_selected + 1) and last_selected != 0:
92-
result += "\n\n\n"
92+
# Add an ellipsis between non-adjacent lines
93+
last_line = source_lines[last_selected - 1]
94+
# Use the last line indent so that the result can be un-indented by the caller.
95+
indent = leading_spaces(last_line)
96+
result += f"\n{indent}\n\n"
9397
result += source_lines[i - 1] + "\n"
9498
last_selected = i
9599

96100
if result == "":
97101
return text
98102

99103
return result
104+
105+
106+
def leading_spaces(s: str) -> str:
107+
return ' ' * (len(s) - len(s.lstrip()))

tests/codeinclude/test_resolver.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ def test_inside_block_several_matching_blocks(self):
207207
inside_block="matching_block")
208208
self.maxDiff = None
209209
self.assertEquals((" /* inside first */\n"
210-
"\n\n\n"
210+
"\n"
211+
" ⋯\n\n"
211212
" /* inside second */\n"),
212213
result)

0 commit comments

Comments
 (0)