Skip to content

Chained methods: break all when wrap_line_length is exceeded #2344

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 41 additions & 2 deletions python/jsbeautifier/javascript/beautifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -1604,6 +1604,43 @@ def handle_comment(self, current_token, preserve_statement_flags):
self.print_token(current_token)
self.print_newline(preserve_statement_flags=preserve_statement_flags)

_peek_lloc_stop = [TOKEN.SEMICOLON, TOKEN.END_BLOCK, TOKEN.EOF, TOKEN.RESERVED]

def guess_current_lloc_len(self: Beautifier):
"""Try to guess how long, in chars, this lloc would be if it was condensed to a single line."""

# empty string is the "separator" between forwards and backwards
txts = [""]

# lookbehind loop
while True:
bak_token = self._tokens.peek(-len(txts))
if bak_token is None:
break
if bak_token.type in [*_peek_lloc_stop, TOKEN.START_BLOCK]:
break
txts.append(bak_token.text)

# since we looped backwards, reverse the gathered texts here
txts = list(reversed(txts))

bak_tokens = len(txts)
# lookahead loop
while True:
fwd_token = self._tokens.peek(len(txts) - bak_tokens)
if fwd_token is None:
break
if fwd_token.type in _peek_lloc_stop:
break
txts.append(fwd_token.text)

line = "".join(txts)
# print(len(line), line)

cur_line = self._output.current_line.toString()
indent_chars = len(cur_line) - len(cur_line.lstrip())
return len(line) + indent_chars

def handle_dot(self, current_token):
if self.start_of_statement(current_token):
# The conditional starts the statement if appropriate.
Expand All @@ -1622,8 +1659,10 @@ def handle_dot(self, current_token):
# bar().baz()
self.allow_wrap_or_preserved_newline(
current_token,
self._flags.last_token.text == ")"
and self._options.break_chained_methods,
(
0 < self._options.wrap_line_length < self.guess_current_lloc_len()
or self._flags.last_token.text == ")"
) and self._options.break_chained_methods,
)

# Only unindent chained method dot if this dot starts a new line.
Expand Down