-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathresolver.py
120 lines (96 loc) · 3.57 KB
/
resolver.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import re
def select(
text,
filename=None,
lines=None,
from_token=None,
to_token=None,
block=None,
inside_block=None,
lang=None,
block_throw=False
):
selected_lines = []
if lines:
for line_range in lines.split(","):
range_match = re.match(r"(\d+)-(\d+)", line_range)
if range_match:
start = int(range_match.group(1))
end = int(range_match.group(2))
for i in range(start, end + 1):
selected_lines.append(i)
elif line_range.strip() != "":
selected_lines.append(int(line_range))
if block:
i = 0
delim_count = 0
found_block = False
for line in text.splitlines():
first_line_of_block = False
i = i + 1
if block in line and delim_count <= 0:
found_block = True
delim_count = 0
first_line_of_block = True
delim_count += line.count("{")
if delim_count > 0:
if not first_line_of_block:
delim_count += line.count("{")
selected_lines.append(i)
delim_count -= line.count("}")
if block_throw and not found_block:
raise ValueError(f"Block {block} not found to inject from {filename}")
if inside_block:
delim_count = 0
inside_matching = False
found_block = False
for line_number, line in enumerate(text.splitlines(), start=1):
first_line_of_block = False
# Detect the block beginning
if inside_block in line and delim_count <= 0:
found_block = True
delim_count = 0
first_line_of_block = True
inside_matching = True
# Don't process lines that are outside the matching block
if not inside_matching:
continue
# Count the brackets in the line
delim_count += line.count("{")
delim_count -= line.count("}")
# If we closed the opening bracket (= dropped below 0), the matching block has ended
if delim_count <= 0:
inside_matching = False
# Append the lines inside the matching block, skipping the first matching
if inside_matching and not first_line_of_block:
selected_lines.append(line_number)
if block_throw and not found_block:
raise ValueError(f"Block {inside_block} not found to inject from {filename}")
if from_token and to_token:
i = 0
active = False
for line in text.splitlines():
i = i + 1
if not active and from_token in line:
active = True
if active:
selected_lines.append(i)
if active and to_token in line:
active = False
result = ""
source_lines = text.splitlines()
last_selected = 0
for i in sorted(selected_lines):
if i > (last_selected + 1) and last_selected != 0:
# Add an ellipsis between non-adjacent lines
last_line = source_lines[last_selected - 1]
# Use the last line indent so that the result can be un-indented by the caller.
indent = leading_spaces(last_line)
result += f"\n{indent}⋯\n\n"
result += source_lines[i - 1] + "\n"
last_selected = i
if result == "":
return text
return result
def leading_spaces(s: str) -> str:
return " " * (len(s) - len(s.lstrip()))