MCPcopy
hub / github.com/psf/black / _get_max_string_length

Method _get_max_string_length

src/black/trans.py:1176–1283  ·  view source on GitHub ↗

Calculates the max string length used when attempting to determine whether or not the target string is responsible for causing the line to go over the line length limit. WARNING: This method is tightly coupled to both StringSplitter and (especially) StringPa

(self, line: Line, string_idx: int)

Source from the content-addressed store, hash-verified

1174 return Ok(None)
1175
1176 def _get_max_string_length(self, line: Line, string_idx: int) -> int:
1177 """
1178 Calculates the max string length used when attempting to determine
1179 whether or not the target string is responsible for causing the line to
1180 go over the line length limit.
1181
1182 WARNING: This method is tightly coupled to both StringSplitter and
1183 (especially) StringParenWrapper. There is probably a better way to
1184 accomplish what is being done here.
1185
1186 Returns:
1187 max_string_length: such that `line.leaves[string_idx].value >
1188 max_string_length` implies that the target string IS responsible
1189 for causing this line to exceed the line length limit.
1190 """
1191 LL = line.leaves
1192
1193 is_valid_index = is_valid_index_factory(LL)
1194
1195 # We use the shorthand "WMA4" in comments to abbreviate "We must
1196 # account for". When giving examples, we use STRING to mean some/any
1197 # valid string.
1198 #
1199 # Finally, we use the following convenience variables:
1200 #
1201 # P: The leaf that is before the target string leaf.
1202 # N: The leaf that is after the target string leaf.
1203 # NN: The leaf that is after N.
1204
1205 # WMA4 the whitespace at the beginning of the line.
1206 offset = line.depth * 4
1207
1208 if is_valid_index(string_idx - 1):
1209 p_idx = string_idx - 1
1210 if (
1211 LL[string_idx - 1].type == token.LPAR
1212 and LL[string_idx - 1].value == ""
1213 and string_idx >= 2
1214 ):
1215 # If the previous leaf is an empty LPAR placeholder, we should skip it.
1216 p_idx -= 1
1217
1218 P = LL[p_idx]
1219 if P.type in self.STRING_OPERATORS:
1220 # WMA4 a space and a string operator (e.g. `+ STRING` or `== STRING`).
1221 offset += len(str(P)) + 1
1222
1223 if P.type == token.COMMA:
1224 # WMA4 a space, a comma, and a closing bracket [e.g. `), STRING`].
1225 offset += 3
1226
1227 if P.type in [token.COLON, token.EQUAL, token.PLUSEQUAL, token.NAME]:
1228 # This conditional branch is meant to handle dictionary keys,
1229 # variable assignments, 'return STRING' statement lines, and
1230 # 'else STRING' ternary expression lines.
1231
1232 # WMA4 a single space.
1233 offset += 1

Callers 1

_validateMethod · 0.95

Calls 4

count_chars_in_widthFunction · 0.90
is_valid_index_factoryFunction · 0.85
is_valid_indexFunction · 0.85
comments_afterMethod · 0.80

Tested by

no test coverage detected