Skip to content

Commit

Permalink
Added wrap/align settings for comma-separated lists
Browse files Browse the repository at this point in the history
  • Loading branch information
hurricup committed Dec 27, 2017
1 parent 43b1d03 commit 8f9c349
Show file tree
Hide file tree
Showing 19 changed files with 1,075 additions and 33 deletions.
1 change: 1 addition & 0 deletions resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
<li>New formatting options available for:<ul>
<li>wrapping and alignment of binary expressions, <a href="https://github.com/Camelcade/Perl5-IDEA/issues/1680">#1680</a></li>
<li>wrapping of chained dereference, <a href="https://github.com/Camelcade/Perl5-IDEA/issues/1681">#1681</a></li>
<li>wrapping and alignment of comma-separated sequences</li>
<li>spacing for signatures, attributes and subs prototypes</li>
<li>wrapping and alignment for signatures</li>
<li>aligning multi-line list items, by <a href="https://github.com/Camelcade/Perl5-IDEA/issues/1677">#1677</a></li>
Expand Down
4 changes: 2 additions & 2 deletions resources/messages/PerlBundle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,10 @@ perl.formatting.signature.parentheses=Sub signature/prototype parentheses
perl.formatting.empty.signature.parentheses=Empty sub signature/prototype parentheses
perl.formatting.attribute=Before sub attribute
perl.formatting.group.alignment=Alignment
perl.formatting.align.list.elements=List elements
perl.formatting.align.list.elements=Comma-separated list
perl.formatting.wrap.dereference=Chained dereference
perl.formatting.align.comments.in.list=Align in lists
perl.formatting.align.fat.comma=Fat commas
perl.formatting.align.fat.comma=Align fat commas
perl.formatting.align.qw.elements=qw// elements
perl.formatting.align.ternary=? and : in ternary expression
perl.formatting.space.inside.qw=qw// list
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ else if (childNodeType == COMMENT_LINE && myPerlSettings.ALIGN_COMMENTS_IN_LIST)
else if (parentNodeType == COMMA_SEQUENCE_EXPR &&
childNodeType != COMMA &&
childNodeType != FAT_COMMA &&
myPerlSettings.ALIGN_LIST_ELEMENTS) {
mySettings.ALIGN_MULTILINE_ARRAY_INITIALIZER_EXPRESSION) {
return myElementsALignmentsMap.get(parentNode);
}
else if (SIGNATURES_CONTAINERS.contains(parentNodeType)) {
Expand Down Expand Up @@ -407,7 +407,7 @@ else if (SIGNATURES_CONTAINERS.contains(parentNodeType) && childNodeType != COMM
return getWrapBySettings(parentNode, mySettings.METHOD_PARAMETERS_WRAP, false);
}
else if (parentNodeType == COMMA_SEQUENCE_EXPR && childNodeType != COMMA && childNodeType != FAT_COMMA) {
return getWrap(parentNode, NORMAL, false);
return getWrapBySettings(parentNode, mySettings.ARRAY_INITIALIZER_WRAP, false);
}
else if (( parentNodeType == STRING_LIST || parentNodeType == LP_STRING_QW) &&
( childNodeType == STRING_CONTENT || childNodeType == QUOTE_SINGLE_CLOSE)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ public class PerlCodeStyleSettings extends CustomCodeStyleSettings {
public boolean ALIGN_FAT_COMMA = true;
public boolean ALIGN_QW_ELEMENTS = false;
public boolean ALIGN_COMMENTS_IN_LIST = true;
public boolean ALIGN_LIST_ELEMENTS = false;

public int BRACE_STYLE_NAMESPACE = SAME_LINE;
public int BRACE_STYLE_SUB = SAME_LINE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public class PerlLanguageCodeStyleSettingsProvider extends LanguageCodeStyleSett
private static final String GROUP_SUB = PerlBundle.message("perl.formatting.brace.style.sub");
private static final String GROUP_VARIABLE_DECLARATION = PerlBundle.message("perl.formatting.wrap.variable.declarations");
private static final String GROUP_COMMENT = WRAPPING_COMMENTS;
private static final String GROUP_LIST = WRAPPING_ARRAY_INITIALIZER;

private static final String DEFAULT_CODE_SAMPLE = PerlBundle.message("perl.code.sample.nyi");
private static final String SPACING_CODE_SAMPLE = readCodeSample("spaces");
Expand Down Expand Up @@ -153,6 +154,9 @@ else if (settingsType == WRAPPING_AND_BRACES_SETTINGS) {

"WRAP_COMMENTS",

"ARRAY_INITIALIZER_WRAP",
"ALIGN_MULTILINE_ARRAY_INITIALIZER_EXPRESSION",

"METHOD_PARAMETERS_WRAP",
"ALIGN_MULTILINE_PARAMETERS",

Expand All @@ -164,6 +168,9 @@ else if (settingsType == WRAPPING_AND_BRACES_SETTINGS) {
"ALIGN_MULTILINE_TERNARY_OPERATION"
);

consumer.renameStandardOption("METHOD_PARAMETERS_WRAP", PerlBundle.message("perl.formatting.wrap.declarations.parameters"));
consumer.renameStandardOption("METHOD_CALL_CHAIN_WRAP", PerlBundle.message("perl.formatting.wrap.dereference"));

consumer.showCustomOption(PerlCodeStyleSettings.class,
"ALIGN_COMMENTS_IN_LIST",
PerlBundle.message("perl.formatting.align.comments.in.list"),
Expand All @@ -182,9 +189,6 @@ else if (settingsType == WRAPPING_AND_BRACES_SETTINGS) {
GROUP_VARIABLE_DECLARATION
);

consumer.renameStandardOption("METHOD_PARAMETERS_WRAP", PerlBundle.message("perl.formatting.wrap.declarations.parameters"));
consumer.renameStandardOption("METHOD_CALL_CHAIN_WRAP", PerlBundle.message("perl.formatting.wrap.dereference"));

consumer.showCustomOption(PerlCodeStyleSettings.class,
"BRACE_STYLE_NAMESPACE",
WRAPPING_BRACES,
Expand Down Expand Up @@ -212,16 +216,12 @@ else if (settingsType == WRAPPING_AND_BRACES_SETTINGS) {
BRACE_PLACEMENT_OPTIONS
);

consumer.showCustomOption(PerlCodeStyleSettings.class,
"ALIGN_LIST_ELEMENTS",
PerlBundle.message("perl.formatting.align.list.elements"),
GROUP_ALIGNMENT
);

consumer.renameStandardOption("ARRAY_INITIALIZER_WRAP", PerlBundle.message("perl.formatting.align.list.elements"));
consumer.showCustomOption(PerlCodeStyleSettings.class,
"ALIGN_FAT_COMMA",
PerlBundle.message("perl.formatting.align.fat.comma"),
GROUP_ALIGNMENT);
GROUP_LIST);

consumer.showCustomOption(PerlCodeStyleSettings.class,
"ALIGN_QW_ELEMENTS",
Expand Down
8 changes: 6 additions & 2 deletions test/formatter/PerlFormatterAlignTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package formatter;

import static com.intellij.psi.codeStyle.CommonCodeStyleSettings.WRAP_AS_NEEDED;

public class PerlFormatterAlignTest extends PerlFormatterTestCase {
@Override
protected String getTestDataPath() {
Expand All @@ -24,12 +26,14 @@ protected String getTestDataPath() {


public void testAlignListElementsTrue() {
getCustomSettings().ALIGN_LIST_ELEMENTS = true;
getSettings().ARRAY_INITIALIZER_WRAP = WRAP_AS_NEEDED;
getSettings().ALIGN_MULTILINE_ARRAY_INITIALIZER_EXPRESSION = true;
doTestAlignListElements();
}

public void testAlignListElementsFalse() {
getCustomSettings().ALIGN_LIST_ELEMENTS = false;
getSettings().ARRAY_INITIALIZER_WRAP = WRAP_AS_NEEDED;
getSettings().ALIGN_MULTILINE_ARRAY_INITIALIZER_EXPRESSION = false;
doTestAlignListElements();
}

Expand Down
27 changes: 24 additions & 3 deletions test/formatter/PerlFormatterWrapTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,27 @@ protected String getTestDataPath() {
return "testData/formatter/perl/wrap";
}

public void testCommaSequenceNever() {
doTestCommaSequence(DO_NOT_WRAP);
}

public void testCommaSequenceAlways() {
doTestCommaSequence(WRAP_ALWAYS);
}

public void testCommaSequenceLong() {
doTestCommaSequence(WRAP_AS_NEEDED);
}

public void testCommaSequenceChomp() {
doTestCommaSequence(WRAP_ON_EVERY_ITEM);
}

private void doTestCommaSequence(int wrapType) {
getSettings().ARRAY_INITIALIZER_WRAP = wrapType;
doWrappingTestSingleSource("commaSequence");
}

public void testBinaryNever() {
doTestBinary(DO_NOT_WRAP, false);
}
Expand Down Expand Up @@ -146,13 +167,13 @@ public void testCommentsWrapFalse() {

private void doTestCommentsWrapping() {doWrappingTestSingleSource("comments");}

public void testFatCommaWrapping() {doFormatTest();}

public void testCommaWrapping() {
public void testFatCommaWrapping() {
getSettings().ARRAY_INITIALIZER_WRAP = WRAP_AS_NEEDED;
doFormatTest();
}

public void testHeredocWrapping() {
getSettings().ARRAY_INITIALIZER_WRAP = WRAP_AS_NEEDED;
doFormatTest();
}
}
159 changes: 155 additions & 4 deletions testData/formatter/perl/align/alignListElements.code
Original file line number Diff line number Diff line change
@@ -1,4 +1,155 @@
my $var = ('this', 'is', 'list', 'of', 'strings');
sub f($variable1, $variable2, $variable3, $variable4){}
foo($variable1, $variable2, $variable3, $variable4);
my($variable1, $variable2, $variable3, $variable4);
my $var = ('this',
'is',
'list',
'of',
'strings');
sub f($variable1,
$variable2,
$variable3,
$variable4){}
foo($variable1,
$variable2,
$variable3,
$variable4);
my($variable1,
$variable2,
$variable3,
$variable4);

foo('This', 'is',
'a',
'large',
'list');
'This', 'is', 'a',
'large',
'list';
$a = 'This', 'is',
'a',
'large',
'list';
'a', 'b', 'c';
$a = 'a', 'b', 'c';
key =>
42, blah =>
42,
'long' =>
45;
$a = key =>
42, blah =>
42,
'long' =>
45;
bla =>
bla =>
bla =>
bla =>
bla =>
bla => 1;
$a = bla =>
bla =>
bla =>
bla =>
bla =>
bla => 1;
y => 1, h => 2;
$a = y => 1, h => 2;

$a = ('This', 'is',
'a',
'large',
'list');
('a', 'b', 'c');
$a = ('a', 'b',
'c');
(key =>
42, blah =>
42,
'long' =>
45);
$a = (key =>
42, blah =>
42,
'long' =>
45);
(bla =>
bla =>
bla =>
bla =>
bla =>
bla => 1);
$a = (bla =>
bla =>
bla =>
bla =>
bla =>
bla => 1);
(y => 1, h => 2);
$a = (y => 1, h =>
2);

$a = [ 'This', 'is',
'a',
'large',
'list' ];
[ 'a', 'b', 'c' ];
$a = [ 'a', 'b',
'c' ];
[ key =>
42, blah =>
42,
'long' =>
45 ];
$a = [ key =>
42, blah =>
42,
'long' =>
45 ];
[ bla =>
bla =>
bla =>
bla =>
bla =>
bla => 1 ];
$a = [ bla =>
bla =>
bla =>
bla =>
bla =>
bla => 1 ];
[ y => 1, h => 2 ];
$a = [ y => 1, h =>
2 ];

$a = { 'This', 'is',
'a',
'large',
'list' };
{ 'a', 'b', 'c' };
$a = { 'a', 'b',
'c' };
{ key =>
42, blah =>
42,
'long' =>
45 };
$a = { key =>
42, blah =>
42,
'long' =>
45 };
{ bla =>
bla =>
bla =>
bla =>
bla =>
bla => 1 };
$a = { bla =>
bla =>
bla =>
bla =>
bla =>
bla => 1 };
{ y => 1, h => 2 };
$a = { y => 1, h =>
2 };

Loading

0 comments on commit 8f9c349

Please sign in to comment.