1
1
from .Slice import Slice
2
- from .Literals import Language
2
+ from .Literals import Language , LiteralFormat , LiteralValue
3
3
from .EscapeLeadingWhitespace import EscapeLeadingWhitespace
4
4
from typing import Any
5
5
@@ -8,9 +8,9 @@ class SourceFile:
8
8
TRIPLE_QUOTE = '"""'
9
9
10
10
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 )
14
14
self .escape_leading_whitespace = EscapeLeadingWhitespace .appropriate_for (
15
15
self .content_slice .__str__ ()
16
16
)
@@ -25,13 +25,18 @@ def as_string(self) -> str:
25
25
26
26
class ToBeLiteral :
27
27
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 ,
29
34
) -> None :
30
35
self .dot_fun_open_paren = dot_fun_open_paren
31
36
self .function_call_plus_arg = function_call_plus_arg
32
37
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
35
40
36
41
def set_literal_and_get_newline_delta (self , literal_value : LiteralValue ) -> int :
37
42
encoded = literal_value .format .encode (
@@ -61,30 +66,35 @@ def parse_literal(self, literal_format: LiteralFormat) -> Any:
61
66
return literal_format .parse (self .arg .__str__ (), self .language )
62
67
63
68
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 (
65
71
"// selfieonce" , ""
66
72
)
73
+ self .content_slice = Slice (updated_content )
67
74
68
75
def find_on_line (self , to_find : str , line_one_indexed : int ) -> Slice :
69
76
line_content = self .content_slice .unixLine (line_one_indexed )
70
- idx = line_content .find (to_find )
77
+ idx = line_content .indexOf (to_find )
71
78
if idx == - 1 :
72
79
raise AssertionError (
73
80
f"Expected to find `{ to_find } ` on line { line_one_indexed } , "
74
81
f"but there was only `{ line_content } `"
75
82
)
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 )
77
86
78
87
def replace_on_line (self , line_one_indexed : int , find : str , replace : str ) -> None :
79
88
assert "\n " not in find
80
89
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 ))
83
93
84
94
def parse_to_be_like (self , line_one_indexed : int ) -> ToBeLiteral :
85
95
line_content = self .content_slice .unixLine (line_one_indexed )
86
96
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 ),
88
98
key = lambda x : x [0 ] if x [0 ] != - 1 else float ("inf" ),
89
99
)
90
100
dot_fun_open_paren = (
@@ -95,8 +105,8 @@ def parse_to_be_like(self, line_one_indexed: int) -> ToBeLiteral:
95
105
f"Expected to find inline assertion on line { line_one_indexed } , "
96
106
f"but there was only `{ line_content } `"
97
107
)
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
100
110
arg_start = dot_function_call + len (dot_fun_open_paren )
101
111
if self .content_slice .__len__ == arg_start :
102
112
raise AssertionError (
@@ -114,8 +124,8 @@ def parse_to_be_like(self, line_one_indexed: int) -> ToBeLiteral:
114
124
end_arg = - 1
115
125
end_paren = 0
116
126
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 (
119
129
self .TRIPLE_QUOTE , arg_start + len (self .TRIPLE_QUOTE )
120
130
)
121
131
if end_arg == - 1 :
@@ -170,6 +180,8 @@ def parse_to_be_like(self, line_one_indexed: int) -> ToBeLiteral:
170
180
dot_fun_open_paren .replace ("_TODO" , "" ),
171
181
self .content_slice .subSequence (dot_function_call , end_paren + 1 ),
172
182
self .content_slice .subSequence (arg_start , end_arg ),
183
+ self .language ,
184
+ self .escape_leading_whitespace ,
173
185
)
174
186
175
187
0 commit comments