Skip to content

Commit

Permalink
Implemented enter indentation in empty signature and after comma
Browse files Browse the repository at this point in the history
Implemented better test approach for multi-similar cases (need to update functionParameters tests)

#1674
  • Loading branch information
hurricup committed Apr 26, 2020
1 parent d62225c commit dd24e7d
Show file tree
Hide file tree
Showing 58 changed files with 488 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,10 @@ public final ChildAttributes getChildAttributes(@NotNull PerlAstBlock block, int
public Alignment getChildAlignment(@NotNull PerlAstBlock block, int newChildIndex) {
ASTNode node = block.getNode();
IElementType elementType = PsiUtilCore.getElementType(node);
if (elementType == SIGNATURE_CONTENT) {
return mySettings.ALIGN_MULTILINE_PARAMETERS ? myElementsALignmentsMap.get(block.getNode()) : null;
}

if (PerlFormattingTokenSets.COMMA_LIKE_SEQUENCES.contains(elementType)) {
return null;
}
Expand All @@ -563,7 +567,7 @@ public Boolean isIncomplete(@NotNull PerlAstBlock block) {
return null;
}
IElementType elementType = block.getElementType();
if (elementType == COMMA_SEQUENCE_EXPR) {
if (COMMA_LIKE_SEQUENCES.contains(elementType)) {
IElementType lastNodeType = PsiUtilCore.getElementType(blockNode.getLastChildNode());
if (lastNodeType == COMMA || lastNodeType == FAT_COMMA) {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ public interface PerlFormattingTokenSets extends PerlElementTypes {
ATTRIBUTES
);
TokenSet BLOCK_LIKE_CONTAINERS = TokenSet.create(
BLOCK
BLOCK, SIGNATURE_CONTENT
);
TokenSet MULTI_PARAM_BLOCK_CONTAINERS = TokenSet.create(
GREP_EXPR, MAP_EXPR, SORT_EXPR, REPLACEMENT_REGEX
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@
import java.util.List;
import java.util.Objects;

import static com.perl5.lang.perl.idea.formatter.PerlFormattingTokenSets.BLOCK_CLOSERS;
import static com.perl5.lang.perl.idea.formatter.PerlFormattingTokenSets.BLOCK_OPENERS;
import static com.perl5.lang.perl.idea.formatter.PerlFormattingTokenSets.*;
import static com.perl5.lang.perl.lexer.PerlTokenSets.HEREDOC_BODIES_TOKENSET;
import static com.perl5.lang.perl.lexer.PerlTokenSets.VARIABLE_DECLARATIONS;
import static com.perl5.lang.perl.psi.stubs.PerlStubElementTypes.NO_STATEMENT;
Expand Down Expand Up @@ -169,6 +168,10 @@ public Indent getNodeIndent(@NotNull ASTNode node) {
public Indent getChildIndent(@NotNull PerlAstBlock block, int newChildIndex) {
IElementType elementType = block.getElementType();

if (SUB_OR_MODIFIER_DEFINITIONS_TOKENSET.contains(elementType) && block.getChildElementType(newChildIndex - 1) == LEFT_PAREN) {
return Indent.getNormalIndent();
}

if (getUnindentableContainers().contains(elementType)) {
return Indent.getNoneIndent();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2015-2017 Alexandr Evstigneev
* Copyright 2015-2020 Alexandr Evstigneev
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,12 +17,14 @@
package com.perl5.lang.perl.idea.formatter.blocks;

import com.intellij.formatting.ASTBlock;
import com.intellij.formatting.Block;
import com.intellij.formatting.Indent;
import com.intellij.psi.tree.IElementType;
import com.intellij.psi.util.PsiUtilCore;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;
import java.util.Objects;

public interface PerlAstBlock extends ASTBlock {
Expand All @@ -36,4 +38,13 @@ default ASTBlock getRealBlock() {
default IElementType getElementType() {
return Objects.requireNonNull(PsiUtilCore.getElementType(getNode()));
}

@Nullable
default IElementType getChildElementType(int blockIndex) {
List<Block> subBlocks = getSubBlocks();
if (subBlocks.size() <= blockIndex) {
return null;
}
return ASTBlock.getElementType(subBlocks.get(blockIndex));
}
}
14 changes: 13 additions & 1 deletion plugin/test/formatter/PerlFormatterTestCase.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2015-2019 Alexandr Evstigneev
* Copyright 2015-2020 Alexandr Evstigneev
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -18,12 +18,15 @@

import base.PerlLightTestCase;
import com.intellij.application.options.CodeStyle;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.codeStyle.CommonCodeStyleSettings;
import com.perl5.lang.perl.PerlLanguage;
import com.perl5.lang.perl.idea.formatter.settings.PerlCodeStyleSettings;
import org.jetbrains.annotations.NotNull;

public abstract class PerlFormatterTestCase extends PerlLightTestCase {
private static final String PER_TEST_CODE = "<per test code>";

@Override
protected void setUp() throws Exception {
super.setUp();
Expand All @@ -33,6 +36,15 @@ protected void setUp() throws Exception {
options.CONTINUATION_INDENT_SIZE = 8;
}

protected final String getPatchedContent(@NotNull String content) {
return StringUtil.replace(content, PER_TEST_CODE, getPerTestCode());
}

@NotNull
protected String getPerTestCode() {
throw new RuntimeException("Not implemented");
}

protected void doWrappingFormatTest() {
getSettings().RIGHT_MARGIN = 20;
doFormatTest();
Expand Down
236 changes: 236 additions & 0 deletions plugin/test/formatter/PerlSubsFormatterEnterTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,236 @@
/*
* Copyright 2015-2020 Alexandr Evstigneev
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package formatter;

import org.jetbrains.annotations.NotNull;
import org.junit.Test;

public abstract class PerlSubsFormatterEnterTest extends PerlFormatterTestCase {

@Override
protected final String getBaseDataPath() {
return "testData/formatter/perl/enter/subs";
}

@Test
public void testSignatureEmpty() {
doTest();
}

@Test
public void testSignatureAfterComma() {
doTest();
}

@Test
public void testSignatureAfterCommaContinuation() {
getSettings().ALIGN_MULTILINE_PARAMETERS = false;
doTest();
}

@Test
public void testSignatureAfterCommaContinuationAligned() {
getSettings().ALIGN_MULTILINE_PARAMETERS = true;
doTest();
}

@NotNull
@Override
protected String computeAnswerFileName(@NotNull String appendix) {
return super.computeAnswerFileName("." + getResultAppendix());
}

@NotNull
protected abstract String getResultAppendix();

@Override
public void initWithFileContent(String filename, String extension, String content) {
super.initWithFileContent(filename, extension, getPatchedContent(content));
}

protected void doTest() {
doTestEnter();
}

public static class After extends PerlSubsFormatterEnterTest {
@NotNull
@Override
protected String getResultAppendix() {
return "after";
}

@NotNull
@Override
protected String getPerTestCode() {
return "after somemethod";
}
}

public static class Around extends PerlSubsFormatterEnterTest {
@NotNull
@Override
protected String getResultAppendix() {
return "around";
}

@NotNull
@Override
protected String getPerTestCode() {
return "around somemethod";
}
}

public static class Augment extends PerlSubsFormatterEnterTest {
@NotNull
@Override
protected String getResultAppendix() {
return "augment";
}

@NotNull
@Override
protected String getPerTestCode() {
return "augment somemethod";
}
}

public static class Before extends PerlSubsFormatterEnterTest {
@NotNull
@Override
protected String getResultAppendix() {
return "before";
}

@NotNull
@Override
protected String getPerTestCode() {
return "before somemethod";
}
}

public static class Fun extends PerlSubsFormatterEnterTest {
@NotNull
@Override
protected String getResultAppendix() {
return "fun";
}

@NotNull
@Override
protected String getPerTestCode() {
return "fun somefun";
}
}

public static class Func extends PerlSubsFormatterEnterTest {
@NotNull
@Override
protected String getResultAppendix() {
return "func";
}

@NotNull
@Override
protected String getPerTestCode() {
return "func somefunc";
}
}

public static class FunExpr extends PerlSubsFormatterEnterTest {
@NotNull
@Override
protected String getResultAppendix() {
return "funExpr";
}

@NotNull
@Override
protected String getPerTestCode() {
return "fun";
}
}

public static class Method extends PerlSubsFormatterEnterTest {
@NotNull
@Override
protected String getResultAppendix() {
return "method";
}

@NotNull
@Override
protected String getPerTestCode() {
return "method somemethod";
}
}

public static class MethodExpr extends PerlSubsFormatterEnterTest {
@NotNull
@Override
protected String getResultAppendix() {
return "methodExpr";
}

@NotNull
@Override
protected String getPerTestCode() {
return "method";
}
}

public static class OverrideKw extends PerlSubsFormatterEnterTest {
@NotNull
@Override
protected String getResultAppendix() {
return "override";
}

@NotNull
@Override
protected String getPerTestCode() {
return "override somemethod";
}
}

public static class Sub extends PerlSubsFormatterEnterTest {
@NotNull
@Override
protected String getResultAppendix() {
return "sub";
}

@NotNull
@Override
protected String getPerTestCode() {
return "sub somesub";
}
}

public static class SubExpr extends PerlSubsFormatterEnterTest {
@NotNull
@Override
protected String getResultAppendix() {
return "subExpr";
}

@NotNull
@Override
protected String getPerTestCode() {
return "sub";
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
after somemethod(
$var,
<caret>
) {
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
around somemethod(
$var,
<caret>
) {
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
augment somemethod(
$var,
<caret>
) {
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
before somemethod(
$var,
<caret>
) {
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<per test code>(
$var,<caret>
) {
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
fun somefun(
$var,
<caret>
) {
};
Loading

0 comments on commit dd24e7d

Please sign in to comment.