It doesn't solve the problem that reformatting doesn't add blank lines
before import groups according to PEP-8 until "Optimize Imports" is
called on a file, but at least it doesn't remove blank lines inserted
manually by user.
Additionally now PyTrailingBlankLinesPostFormatProcessor modifies adjacent
whitespace when reformat action was called on a single PSI element, similarly
to the way it works for specific text range (hence the change in test data).
Python lexer and parser can split whitespaces and attach them to different parents,
so we have to scan backward through consequtive leaf elements to collect all of them.
I also added several new tests for various such corner cases.
Fix affected test data where reformat is invoked during refactoring.
Also use entire sample provided by user in the corresponding test case
for the issue.
It was added a while back for PY-6751, but corresponding test
passes without it now. Also it turned out that
PyIndentTest#testEnterInDictInTuple depended on this change, but
behavior tested there was recreated (once again) by proper use of
hasHangingIndent in formatter, i.e. once opening and closing braces of
the dict are on different lines, the literal should not have alignment.
In addition to existing detection of shebang/double hash/doc comments we
consider the following new cases when trying to insert space after '#'
in Python line comments:
* Comments that already start with whitespace are ignored by
formatter.
* Comments that span several lines (up to the first empty line or
non-whitespace element) are not altered either because they may contain
Python code fragments.
* Trailing comments however can be safely adjusted because even if
they contain any code, there cannot be any indentation inside.
Closing parenthesis is aligned inside PyFromImportStatement the same way
it was done previously for various braces in collection literals and
parameter/argument lists, i.e. it's aligned with PyImportElements if
there is no hanging indent after opening parenthesis.
It was done so already for other elements with braces that may
span multiple lines, e.g. literals of collections and parameter/argument
lists. Now formatter behavior for PyFromImportStatement is consistent
with them.
Usage of continuation indents (which presumably are wider than normal
indents) in the headers of control statements was supposed to prevent
PEP8's error E125 "Continuation line with same indent as next logical
line". However such indents is not necessary for e.g. nested function
calls where indent of the arguments is already visually distinctive
(larger) from the offset of the following statement list. To
distinguish such cases I've added check that argument list is located
on the same line as the header of the containing control statement. If
line numbers differ we use normal indent for the arguments and
continuation indent otherwise. It doesn't matter for complex
non-hanging indent cases like if foo(bar(1... because alignment will
be used there anyway.
When we use tab characters for indents in Python sources, for some
reason indents and line feeds inside literal expressions for
collections are represented as several consecutive PsiWhiteSpace
elements. Method isWhitespaceWithLineBreaks can't count new lines in
such cases correctly and hence PyBlocks for PyKeyValuePair don't get
desired indentation.
Regardless of option "Align multiline call arguments", alignment should
take place only when there is no, so called, "hanging indent" in function
call (see PEP-8). In accordance to behavior of 'pep8' utility several such
indents at the end of line are collapsed into one. E.g. fragment
'func([{\n' is considered as having "hanging indent" both for function
call, list and dict literals.
The same way FormatterModelBuilders are implemented for Java and JavaScript.
It allows to call codeStyleManager.reformat(PsiElement) directly instead of
codeStyleManager.reformatText(elt.getContainingFile(),
elt.getTextRange().getStartOffset(), elt.getTextRange().getEndOffset());