3from .EscapeLeadingWhitespace
import EscapeLeadingWhitespace
4from .Literals
import Language, LiteralFormat, LiteralRepr, LiteralValue
5from .Slice
import Slice
11 def __init__(self, filename: str, content: str) ->
None:
12 self.__unix_newlines: bool =
"\r" not in content
14 self.
__language: Language = Language.from_filename(filename)
27 if "# selfieonce" in line:
28 cleaned_line = line.split(
"# selfieonce")[0].strip()
29 elif "#selfieonce" in line:
30 cleaned_line = line.split(
"#selfieonce")[0].strip()
32 new_lines.append(line)
37 new_lines.append(cleaned_line)
40 new_content =
"\n".join(new_lines)
45 if not self.__unix_newlines:
52 if self.__unix_newlines
60 dot_fun_open_paren: str,
61 function_call_plus_arg: Slice,
64 escape_leading_whitespace: EscapeLeadingWhitespace,
80 encoded = literal_value.format.encode(
85 if not isinstance(literal_value.format, LiteralRepr):
87 round_tripped = literal_value.format.parse(encoded, self.
__language)
88 if round_tripped != literal_value.actual:
90 f
"There is an error in {literal_value.format.__class__.__name__}, "
91 "the following value isn't round tripping.\n"
92 f
"Please report this error and the data below at "
93 "https://github.com/diffplug/selfie/issues/new\n"
95 f
"ORIGINAL\n{literal_value.actual}\n"
96 f
"ROUNDTRIPPED\n{round_tripped}\n"
97 f
"ENCODED ORIGINAL\n{encoded}\n"
101 new_newlines = encoded.count(
"\n")
104 f
"{self.__dot_fun_open_paren}{encoded})"
108 return new_newlines - existing_newlines
115 idx = line_content.indexOf(to_find)
117 raise AssertionError(
118 f
"Expected to find `{to_find}` on line {line_one_indexed}, but there was only `{line_content}`"
120 return line_content.subSequence(idx, idx + len(to_find))
123 assert "\n" not in find
124 assert "\n" not in replace
131 dot_fun_open_paren =
None
133 for to_be_like
in TO_BE_LIKES:
134 idx = line_content.indexOf(to_be_like)
136 dot_fun_open_paren = to_be_like
138 if dot_fun_open_paren
is None:
139 raise AssertionError(
140 f
"Expected to find inline assertion on line {line_one_indexed}, but there was only `{line_content}`"
143 dot_function_call_in_place = line_content.indexOf(dot_fun_open_paren)
144 dot_function_call = dot_function_call_in_place + line_content.startIndex
145 arg_start = dot_function_call + len(dot_fun_open_paren)
148 raise AssertionError(
149 f
"Appears to be an unclosed function call `{dot_fun_open_paren}` "
150 f
"on line {line_one_indexed}"
155 raise AssertionError(
156 f
"Appears to be an unclosed function call `{dot_fun_open_paren}` "
157 f
"on line {line_one_indexed}"
162 line_one_indexed, arg_start, dot_fun_open_paren
166 line_one_indexed, arg_start, dot_fun_open_paren
170 dot_fun_open_paren.replace(
"_TODO",
""),
171 self.
_content_slice.subSequence(dot_function_call, end_paren + 1),
179 line_one_indexed: int,
181 dot_fun_open_paren: str,
184 parenthesis_count = 1
185 string_delimiter =
None
193 if not string_delimiter:
194 string_delimiter = char
195 elif char == string_delimiter:
196 string_delimiter =
None
204 parenthesis_count += 1
206 parenthesis_count -= 1
209 if parenthesis_count == 0:
212 return (end_paren, end_arg)
214 raise AssertionError(
215 f
"Appears to be an unclosed function call `{dot_fun_open_paren}` "
216 f
"starting at line {line_one_indexed}"
221 line_one_indexed: int,
223 dot_fun_open_paren: str,
232 raise AssertionError(
233 f
"Appears to be an unclosed multiline string literal `{self.TRIPLE_QUOTE}` "
234 f
"on line {line_one_indexed}"
240 end_arg = arg_start + 1
247 raise AssertionError(
248 f
'Appears to be an unclosed string literal `"` '
249 f
"on line {line_one_indexed}"
255 raise AssertionError(
256 f
"Non-primitive literal in `{dot_fun_open_paren}` starting at "
257 f
"line {line_one_indexed}: error for character "
258 f
"`{self._content_slice[end_paren]}` on line "
259 f
"{self._content_slice.baseLineAtOffset(end_paren)}"
263 raise AssertionError(
264 f
"Appears to be an unclosed function call `{dot_fun_open_paren}` "
265 f
"starting at line {line_one_indexed}"
267 return (end_paren, end_arg)
274 ".to_be_base64_TODO(",
Any parse_literal(self, LiteralFormat literal_format)
int set_literal_and_get_newline_delta(self, LiteralValue literal_value)
None __init__(self, "SourceFile" parent, str dot_fun_open_paren, Slice function_call_plus_arg, Slice arg, Language language, EscapeLeadingWhitespace escape_leading_whitespace)
__escape_leading_whitespace
_get_function_call_plus_arg(self)
ToBeLiteral parse_to_be_like(self, int line_one_indexed)
None __init__(self, str filename, str content)
_parse_string(self, int line_one_indexed, int arg_start, str dot_fun_open_paren)
__escape_leading_whitespace
_parse_code(self, int line_one_indexed, int arg_start, str dot_fun_open_paren)
Slice find_on_line(self, str to_find, int line_one_indexed)
remove_selfie_once_comments(self)
None replace_on_line(self, int line_one_indexed, str find, str replace)