PY-31689 Support wrapping options for list literals

GitOrigin-RevId: be976ddc224983174f5566e53985e29fd49051f4
This commit is contained in:
Daniil Kalinin
2025-02-13 10:45:38 +00:00
committed by intellij-monorepo-bot
parent e85862e20c
commit f9a1e44638
20 changed files with 286 additions and 2 deletions
@@ -96,6 +96,7 @@ public class PyBlock implements ASTBlock {
private Alignment myChildAlignment = null;
private Alignment myDictAlignment = null;
private Wrap myDictWrapping = null;
private Wrap myListWrapping = null;
private Wrap myFromImportWrapping = null;
private Wrap myParameterListWrapping = null;
private Wrap myArgumentListWrapping = null;
@@ -120,6 +121,9 @@ public class PyBlock implements ASTBlock {
myDictAlignment = Alignment.createAlignment(true);
myDictWrapping = Wrap.createWrap(pySettings.DICT_WRAPPING, true);
}
else if (node.getElementType() == PyElementTypes.LIST_LITERAL_EXPRESSION) {
myListWrapping = Wrap.createWrap(pySettings.LIST_WRAPPING, pySettings.LIST_NEW_LINE_AFTER_LEFT_BRACKET);
}
else if (node.getElementType() == PyElementTypes.FROM_IMPORT_STATEMENT) {
myFromImportWrapping = Wrap.createWrap(pySettings.FROM_IMPORT_WRAPPING, false);
}
@@ -428,6 +432,12 @@ public class PyBlock implements ASTBlock {
if (childType == PyElementTypes.KEY_VALUE_EXPRESSION && isChildOfDictLiteral(child)) {
childWrap = myDictWrapping;
}
if (parentType == PyElementTypes.LIST_LITERAL_EXPRESSION &&
childType != PyTokenTypes.COMMA &&
childType != PyTokenTypes.LBRACKET &&
childType != PyTokenTypes.RBRACKET) {
childWrap = myListWrapping;
}
if (parentType == PyElementTypes.PARAMETER_LIST &&
childType != PyTokenTypes.COMMA &&
childType != PyTokenTypes.LPAR &&
@@ -744,8 +754,11 @@ public class PyBlock implements ASTBlock {
myContext.getMode() == FormattingMode.ADJUST_INDENT) {
return true;
}
return !hasHangingIndent(myNode.getPsi()) && !(myNode.getElementType() == PyElementTypes.DICT_LITERAL_EXPRESSION &&
myContext.getPySettings().DICT_NEW_LINE_AFTER_LEFT_BRACE);
return !hasHangingIndent(myNode.getPsi())
&& !(myNode.getElementType() == PyElementTypes.DICT_LITERAL_EXPRESSION &&
myContext.getPySettings().DICT_NEW_LINE_AFTER_LEFT_BRACE)
&& !(myNode.getElementType() == PyElementTypes.LIST_LITERAL_EXPRESSION &&
myContext.getPySettings().LIST_NEW_LINE_AFTER_LEFT_BRACKET);
}
if (myNode.getElementType() == PyElementTypes.ARGUMENT_LIST) {
if (!myContext.getSettings().ALIGN_MULTILINE_PARAMETERS_IN_CALLS || hasHangingIndent(myNode.getPsi())) {
@@ -9,6 +9,8 @@ import com.jetbrains.python.PySyntaxCoreBundle;
import org.intellij.lang.annotations.MagicConstant;
import org.jetbrains.annotations.NotNull;
import static com.intellij.psi.codeStyle.CommonCodeStyleSettings.WRAP_AS_NEEDED;
public class PyCodeStyleSettings extends CustomCodeStyleSettings {
@@ -71,6 +73,11 @@ public class PyCodeStyleSettings extends CustomCodeStyleSettings {
public boolean DICT_NEW_LINE_AFTER_LEFT_BRACE = false;
public boolean DICT_NEW_LINE_BEFORE_RIGHT_BRACE = false;
@CommonCodeStyleSettings.WrapConstant
public int LIST_WRAPPING = WRAP_AS_NEEDED;
public boolean LIST_NEW_LINE_AFTER_LEFT_BRACKET = false;
public boolean LIST_NEW_LINE_BEFORE_RIGHT_BRACKET = false;
public int BLANK_LINES_AFTER_LOCAL_IMPORTS = 0;
/**
* Code style for most languages use continuation indent both for parameters in function definition and for arguments in function calls.
@@ -79,6 +79,11 @@ public class PythonFormattingModelBuilder implements FormattingModelBuilder, Cus
.afterInside(LBRACE, DICT_LITERAL_EXPRESSION).spaceIf(pySettings.SPACE_WITHIN_BRACES, pySettings.DICT_NEW_LINE_AFTER_LEFT_BRACE)
.beforeInside(RBRACE, DICT_LITERAL_EXPRESSION).spaceIf(pySettings.SPACE_WITHIN_BRACES, pySettings.DICT_NEW_LINE_BEFORE_RIGHT_BRACE)
.beforeInside(RBRACKET, LIST_LITERAL_EXPRESSION)
.spaceIf(commonSettings.SPACE_WITHIN_BRACKETS, pySettings.LIST_NEW_LINE_BEFORE_RIGHT_BRACKET)
.afterInside(LBRACKET, LIST_LITERAL_EXPRESSION)
.spaceIf(commonSettings.SPACE_WITHIN_BRACKETS, pySettings.LIST_NEW_LINE_AFTER_LEFT_BRACKET)
.between(COMMA, RBRACE).spaceIf(pySettings.SPACE_WITHIN_BRACES || commonSettings.SPACE_AFTER_COMMA)
.withinPair(LBRACE, RBRACE).spaceIf(pySettings.SPACE_WITHIN_BRACES)