Skip to content
This repository was archived by the owner on Apr 4, 2024. It is now read-only.

Commit b1d7957

Browse files
committed
fixed typing problem
1 parent 941d91d commit b1d7957

File tree

1 file changed

+29
-17
lines changed

1 file changed

+29
-17
lines changed

python/selfie-lib/selfie_lib/SourceFile.py

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from .Slice import Slice
2-
from .Literals import Language
2+
from .Literals import Language, LiteralFormat, LiteralValue
33
from .EscapeLeadingWhitespace import EscapeLeadingWhitespace
44
from typing import Any
55

@@ -8,9 +8,9 @@ class SourceFile:
88
TRIPLE_QUOTE = '"""'
99

1010
def __init__(self, filename: str, content: str) -> None:
11-
self.unix_newlines = "\r" not in content
12-
self.content_slice = Slice(content).__str__().replace("\r\n", "\n")
13-
self.language = Language.from_filename(filename)
11+
self.unix_newlines: bool = "\r" not in content
12+
self.content_slice: Slice = Slice(content.replace("\r\n", "\n"))
13+
self.language: Language = Language.from_filename(filename)
1414
self.escape_leading_whitespace = EscapeLeadingWhitespace.appropriate_for(
1515
self.content_slice.__str__()
1616
)
@@ -25,13 +25,18 @@ def as_string(self) -> str:
2525

2626
class ToBeLiteral:
2727
def __init__(
28-
self, dot_fun_open_paren: str, function_call_plus_arg: Slice, arg: Slice
28+
self,
29+
dot_fun_open_paren: str,
30+
function_call_plus_arg: Slice,
31+
arg: Slice,
32+
language: Language,
33+
escape_leading_whitespace: EscapeLeadingWhitespace,
2934
) -> None:
3035
self.dot_fun_open_paren = dot_fun_open_paren
3136
self.function_call_plus_arg = function_call_plus_arg
3237
self.arg = arg
33-
self.language = Language
34-
self.escape_leading_whitespace = EscapeLeadingWhitespace
38+
self.language = language
39+
self.escape_leading_whitespace = escape_leading_whitespace
3540

3641
def set_literal_and_get_newline_delta(self, literal_value: LiteralValue) -> int:
3742
encoded = literal_value.format.encode(
@@ -61,30 +66,35 @@ def parse_literal(self, literal_format: LiteralFormat) -> Any:
6166
return literal_format.parse(self.arg.__str__(), self.language)
6267

6368
def remove_selfie_once_comments(self) -> None:
64-
self.content_slice = self.content_slice.replace("//selfieonce", "").replace(
69+
content_str = self.content_slice.__str__()
70+
updated_content = content_str.replace("//selfieonce", "").replace(
6571
"// selfieonce", ""
6672
)
73+
self.content_slice = Slice(updated_content)
6774

6875
def find_on_line(self, to_find: str, line_one_indexed: int) -> Slice:
6976
line_content = self.content_slice.unixLine(line_one_indexed)
70-
idx = line_content.find(to_find)
77+
idx = line_content.indexOf(to_find)
7178
if idx == -1:
7279
raise AssertionError(
7380
f"Expected to find `{to_find}` on line {line_one_indexed}, "
7481
f"but there was only `{line_content}`"
7582
)
76-
return line_content[idx : idx + len(to_find)]
83+
start_index = idx
84+
end_index = idx + len(to_find)
85+
return line_content.subSequence(start_index, end_index)
7786

7887
def replace_on_line(self, line_one_indexed: int, find: str, replace: str) -> None:
7988
assert "\n" not in find
8089
assert "\n" not in replace
81-
slice_ = self.find_on_line(find, line_one_indexed)
82-
self.content_slice = slice_.replaceSelfWith(replace)
90+
line_content = self.content_slice.unixLine(line_one_indexed).__str__()
91+
new_content = line_content.replace(find, replace)
92+
self.content_slice = Slice(self.content_slice.replaceSelfWith(new_content))
8393

8494
def parse_to_be_like(self, line_one_indexed: int) -> ToBeLiteral:
8595
line_content = self.content_slice.unixLine(line_one_indexed)
8696
dot_fun_open_paren = min(
87-
(line_content.find(t) for t in TO_BE_LIKES if t in line_content),
97+
((line_content.indexOf(t), t) for t in TO_BE_LIKES if t in line_content),
8898
key=lambda x: x[0] if x[0] != -1 else float("inf"),
8999
)
90100
dot_fun_open_paren = (
@@ -95,8 +105,8 @@ def parse_to_be_like(self, line_one_indexed: int) -> ToBeLiteral:
95105
f"Expected to find inline assertion on line {line_one_indexed}, "
96106
f"but there was only `{line_content}`"
97107
)
98-
dot_function_call_in_place = line_content.find(dot_fun_open_paren)
99-
dot_function_call = dot_function_call_in_place + line_content.start_index
108+
dot_function_call_in_place = line_content.indexOf(dot_fun_open_paren)
109+
dot_function_call = dot_function_call_in_place + line_content.startIndex
100110
arg_start = dot_function_call + len(dot_fun_open_paren)
101111
if self.content_slice.__len__ == arg_start:
102112
raise AssertionError(
@@ -114,8 +124,8 @@ def parse_to_be_like(self, line_one_indexed: int) -> ToBeLiteral:
114124
end_arg = -1
115125
end_paren = 0
116126
if self.content_slice[arg_start] == '"':
117-
if self.content_slice[arg_start:].startswith(self.TRIPLE_QUOTE):
118-
end_arg = self.content_slice.find(
127+
if self.content_slice[arg_start].startswith(self.TRIPLE_QUOTE):
128+
end_arg = self.content_slice.indexOf(
119129
self.TRIPLE_QUOTE, arg_start + len(self.TRIPLE_QUOTE)
120130
)
121131
if end_arg == -1:
@@ -170,6 +180,8 @@ def parse_to_be_like(self, line_one_indexed: int) -> ToBeLiteral:
170180
dot_fun_open_paren.replace("_TODO", ""),
171181
self.content_slice.subSequence(dot_function_call, end_paren + 1),
172182
self.content_slice.subSequence(arg_start, end_arg),
183+
self.language,
184+
self.escape_leading_whitespace,
173185
)
174186

175187

0 commit comments

Comments
 (0)