Skip to content

Commit

Permalink
More formatting fixes and tests for Function::Parameters
Browse files Browse the repository at this point in the history
Still some things:
- invocant are not chopped in `around`
- indentations are wired when signature elements starting from new line

#1674
  • Loading branch information
hurricup committed Apr 24, 2020
1 parent 241a4aa commit e0cdfdc
Show file tree
Hide file tree
Showing 37 changed files with 7,190 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@
import com.perl5.lang.perl.idea.formatter.settings.PerlCodeStyleSettings;
import com.perl5.lang.perl.lexer.PerlElementTypes;
import com.perl5.lang.perl.lexer.PerlTokenSets;
import com.perl5.lang.perl.psi.PerlVariableDeclarationElement;
import com.perl5.lang.perl.psi.PsiPerlStatementModifier;
import com.perl5.lang.perl.psi.utils.PerlPsiUtil;
import com.perl5.lang.perl.psi.utils.PerlSubArgument;
import gnu.trove.THashMap;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -358,7 +361,7 @@ else if (mySettings.ALIGN_MULTILINE_ARRAY_INITIALIZER_EXPRESSION) {
return myElementsALignmentsMap.get(parentNode);
}
}
else if (SIGNATURES_CONTAINERS.contains(parentNodeType)) {
else if (PerlTokenSets.SIGNATURES_CONTAINERS.contains(parentNodeType)) {
return mySettings.ALIGN_MULTILINE_PARAMETERS ? myElementsALignmentsMap.get(parentNode) : null;
}
else if ((childNodeType == VARIABLE_DECLARATION_ELEMENT ||
Expand All @@ -377,7 +380,8 @@ else if (myPerlSettings.ALIGN_CONSECUTIVE_ASSIGNMENTS == ALIGN_IN_STATEMENT) {
return myElementsALignmentsMap.get(parentNode);
}
}
else if (parentNodeType == ATTRIBUTES && childNodeType == COLON && myPerlSettings.ALIGN_ATTRIBUTES) {
else if (myPerlSettings.ALIGN_ATTRIBUTES && parentNodeType == ATTRIBUTES &&
(childNodeType == COLON || isAttributeWithoutColon(childNode))) {
return myElementsALignmentsMap.get(parentNode);
}
return null;
Expand Down Expand Up @@ -438,8 +442,13 @@ else if (childNodeType != COLON && childNodeType != QUESTION) {
return getWrapBySettings(parentNode, mySettings.TERNARY_OPERATION_WRAP, false);
}
}
else if (SIGNATURES_CONTAINERS.contains(parentNodeType) && childNodeType != COMMA && childNodeType != FAT_COMMA) {
return getWrapBySettings(parentNode, mySettings.METHOD_PARAMETERS_WRAP, false);
// fixme assign operator should probably use assignment wrapping settings
else if (PerlTokenSets.SIGNATURES_CONTAINERS.contains(parentNodeType) &&
childNodeType != COMMA && childNodeType != FAT_COMMA && childNodeType != OPERATOR_ASSIGN) {
PsiElement psiElement = childNode.getPsi();
if (!PerlVariableDeclarationElement.isNamedParameter(psiElement) && !PerlSubArgument.isDefaultValue(childNode.getPsi())) {
return getWrapBySettings(parentNode, mySettings.METHOD_PARAMETERS_WRAP, false);
}
}
else if (parentNodeType == COMMA_SEQUENCE_EXPR && childNodeType != COMMA && childNodeType != FAT_COMMA) {
IElementType grandParentNodeType = PsiUtilCore.getElementType(parentNode.getTreeParent());
Expand Down Expand Up @@ -473,12 +482,17 @@ else if (parentNodeType == ASSIGN_EXPR &&
OPERATORS_ASSIGNMENT.contains(childNodeType) == mySettings.PLACE_ASSIGNMENT_SIGN_ON_NEXT_LINE) {
return getWrapBySettings(parentNode, mySettings.ASSIGNMENT_WRAP, OPERATORS_ASSIGNMENT.contains(childNodeType));
}
else if (parentNodeType == ATTRIBUTES && childNodeType == COLON) {
else if (parentNodeType == ATTRIBUTES && (childNodeType == COLON || isAttributeWithoutColon(childNode))) {
return getWrapBySettings(parentNode, myPerlSettings.ATTRIBUTES_WRAP, false);
}
return null;
}

private boolean isAttributeWithoutColon(@NotNull ASTNode node) {
return PsiUtilCore.getElementType(node) == ATTRIBUTE &&
PsiUtilCore.getElementType(PerlPsiUtil.getPrevSignificantSibling(node.getPsi())) != COLON;
}

@NotNull
private Wrap getWrapBySettings(@NotNull ASTNode parentNode, int settingsOption, boolean wrapFirst) {
return getWrap(parentNode, getWrapType(settingsOption), wrapFirst);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 +298,6 @@ public interface PerlFormattingTokenSets extends PerlElementTypes {
SEMICOLON
);

TokenSet SIGNATURES_CONTAINERS = TokenSet.orSet(TokenSet.create(SUB_SIGNATURE), SIGNATURE_CONTAINERS_EX);

TokenSet COMMA_LIKE_SEQUENCES = TokenSet.orSet(SIGNATURES_CONTAINERS, TokenSet.create(COMMA_SEQUENCE_EXPR));

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,16 @@
import org.jetbrains.annotations.NotNull;

import static com.perl5.lang.perl.idea.formatter.PerlFormattingTokenSets.ADD_EXPR;
import static com.perl5.lang.perl.idea.formatter.PerlFormattingTokenSets.AFTER_MODIFIER;
import static com.perl5.lang.perl.idea.formatter.PerlFormattingTokenSets.ANON_ARRAY;
import static com.perl5.lang.perl.idea.formatter.PerlFormattingTokenSets.ANON_HASH;
import static com.perl5.lang.perl.idea.formatter.PerlFormattingTokenSets.AROUND_MODIFIER;
import static com.perl5.lang.perl.idea.formatter.PerlFormattingTokenSets.AROUND_SIGNATURE_CONTENT;
import static com.perl5.lang.perl.idea.formatter.PerlFormattingTokenSets.ARRAY_INDEX;
import static com.perl5.lang.perl.idea.formatter.PerlFormattingTokenSets.ATTRIBUTE;
import static com.perl5.lang.perl.idea.formatter.PerlFormattingTokenSets.ATTRIBUTES;
import static com.perl5.lang.perl.idea.formatter.PerlFormattingTokenSets.AUGMENT_MODIFIER;
import static com.perl5.lang.perl.idea.formatter.PerlFormattingTokenSets.BEFORE_MODIFIER;
import static com.perl5.lang.perl.idea.formatter.PerlFormattingTokenSets.BLOCK;
import static com.perl5.lang.perl.idea.formatter.PerlFormattingTokenSets.CALL_ARGUMENTS;
import static com.perl5.lang.perl.idea.formatter.PerlFormattingTokenSets.COLON;
Expand All @@ -43,6 +47,7 @@
import static com.perl5.lang.perl.idea.formatter.PerlFormattingTokenSets.FOR_STATEMENT_MODIFIER;
import static com.perl5.lang.perl.idea.formatter.PerlFormattingTokenSets.FUNC_DEFINITION;
import static com.perl5.lang.perl.idea.formatter.PerlFormattingTokenSets.FUNC_SIGNATURE_CONTENT;
import static com.perl5.lang.perl.idea.formatter.PerlFormattingTokenSets.FUN_EXPR;
import static com.perl5.lang.perl.idea.formatter.PerlFormattingTokenSets.HASH_INDEX;
import static com.perl5.lang.perl.idea.formatter.PerlFormattingTokenSets.HEREDOC_END;
import static com.perl5.lang.perl.idea.formatter.PerlFormattingTokenSets.IF_STATEMENT_MODIFIER;
Expand All @@ -53,6 +58,7 @@
import static com.perl5.lang.perl.idea.formatter.PerlFormattingTokenSets.LP_CODE_BLOCK_WITH_TRYCATCH;
import static com.perl5.lang.perl.idea.formatter.PerlFormattingTokenSets.LP_STRING_QW;
import static com.perl5.lang.perl.idea.formatter.PerlFormattingTokenSets.METHOD_DEFINITION;
import static com.perl5.lang.perl.idea.formatter.PerlFormattingTokenSets.METHOD_EXPR;
import static com.perl5.lang.perl.idea.formatter.PerlFormattingTokenSets.METHOD_SIGNATURE_CONTENT;
import static com.perl5.lang.perl.idea.formatter.PerlFormattingTokenSets.NAMESPACE_DEFINITION;
import static com.perl5.lang.perl.idea.formatter.PerlFormattingTokenSets.NUMBER_CONSTANT;
Expand Down Expand Up @@ -159,6 +165,12 @@ static SpacingBuilder createSpacingBuilder(CommonCodeStyleSettings settings, Per
.betweenInside(LEFT_PAREN, RIGHT_PAREN, METHOD_DEFINITION).spaceIf(settings.SPACE_WITHIN_EMPTY_METHOD_PARENTHESES)
.betweenInside(LEFT_PAREN, RIGHT_PAREN, FUNC_DEFINITION).spaceIf(settings.SPACE_WITHIN_EMPTY_METHOD_PARENTHESES)
.betweenInside(LEFT_PAREN, RIGHT_PAREN, SUB_EXPR).spaceIf(settings.SPACE_WITHIN_EMPTY_METHOD_PARENTHESES)
.betweenInside(LEFT_PAREN, RIGHT_PAREN, FUN_EXPR).spaceIf(settings.SPACE_WITHIN_EMPTY_METHOD_PARENTHESES)
.betweenInside(LEFT_PAREN, RIGHT_PAREN, METHOD_EXPR).spaceIf(settings.SPACE_WITHIN_EMPTY_METHOD_PARENTHESES)
.betweenInside(LEFT_PAREN, RIGHT_PAREN, AFTER_MODIFIER).spaceIf(settings.SPACE_WITHIN_EMPTY_METHOD_PARENTHESES)
.betweenInside(LEFT_PAREN, RIGHT_PAREN, BEFORE_MODIFIER).spaceIf(settings.SPACE_WITHIN_EMPTY_METHOD_PARENTHESES)
.betweenInside(LEFT_PAREN, RIGHT_PAREN, AROUND_MODIFIER).spaceIf(settings.SPACE_WITHIN_EMPTY_METHOD_PARENTHESES)
.betweenInside(LEFT_PAREN, RIGHT_PAREN, AUGMENT_MODIFIER).spaceIf(settings.SPACE_WITHIN_EMPTY_METHOD_PARENTHESES)

.afterInside(LEFT_PAREN, SUB_OR_MODIFIER_DEFINITIONS_TOKENSET).spaceIf(settings.SPACE_WITHIN_METHOD_PARENTHESES)
.beforeInside(RIGHT_PAREN, SUB_OR_MODIFIER_DEFINITIONS_TOKENSET).spaceIf(settings.SPACE_WITHIN_METHOD_PARENTHESES)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -361,4 +361,5 @@ public interface PerlTokenSets extends PerlElementTypes, MooseElementTypes {
TokenSet SIGNATURE_CONTAINERS_EX = TokenSet.create(
METHOD_SIGNATURE_CONTENT, FUNC_SIGNATURE_CONTENT, AROUND_SIGNATURE_CONTENT
);
TokenSet SIGNATURES_CONTAINERS = TokenSet.orSet(TokenSet.create(SUB_SIGNATURE), SIGNATURE_CONTAINERS_EX);
}
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 @@ -16,18 +16,25 @@

package com.perl5.lang.perl.psi;

import com.intellij.psi.PsiElement;
import com.intellij.psi.StubBasedPsiElement;
import com.intellij.psi.util.PsiUtilCore;
import com.perl5.PerlIcons;
import com.perl5.lang.perl.idea.configuration.settings.PerlSharedSettings;
import com.perl5.lang.perl.psi.properties.PerlIdentifierOwner;
import com.perl5.lang.perl.psi.stubs.variables.PerlVariableDeclarationStub;
import com.perl5.lang.perl.psi.utils.PerlPsiUtil;
import com.perl5.lang.perl.psi.utils.PerlVariableAnnotations;
import com.perl5.lang.perl.psi.utils.PerlVariableType;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;

import static com.perl5.lang.perl.lexer.PerlElementTypesGenerated.COLON;
import static com.perl5.lang.perl.lexer.PerlTokenSets.SIGNATURES_CONTAINERS;


public interface PerlVariableDeclarationElement
extends StubBasedPsiElement<PerlVariableDeclarationStub>, PerlIdentifierOwner, PerlCompositeElement, PerlVariableDeclaration {
Expand Down Expand Up @@ -104,4 +111,15 @@ default boolean isSelf() {
*/
@Nullable
PerlVariableAnnotations getExternalVariableAnnotations();

/**
* @return true iff {@code psiElement} is variable declaration element preceding by colon. See {@code Function::Parameters} named
* parameters syntax.
*/
@Contract("null -> false")
static boolean isNamedParameter(@Nullable PsiElement psiElement) {
return psiElement instanceof PerlVariableDeclarationElement &&
SIGNATURES_CONTAINERS.contains(PsiUtilCore.getElementType(psiElement.getParent())) &&
PsiUtilCore.getElementType(PerlPsiUtil.getPrevSignificantSibling(psiElement)) == COLON;
}
}
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,10 +18,13 @@

import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.PsiElement;
import com.intellij.psi.stubs.StubInputStream;
import com.intellij.psi.stubs.StubOutputStream;
import com.intellij.psi.util.PsiUtilCore;
import com.perl5.lang.perl.idea.configuration.settings.PerlSharedSettings;
import com.perl5.lang.perl.lexer.PerlLexer;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand All @@ -30,6 +33,8 @@
import java.util.Collections;
import java.util.List;

import static com.perl5.lang.perl.lexer.PerlElementTypesGenerated.OPERATOR_ASSIGN;
import static com.perl5.lang.perl.lexer.PerlTokenSets.SIGNATURES_CONTAINERS;
import static com.perl5.lang.perl.util.PerlScalarUtil.DEFAULT_SELF_NAME;


Expand Down Expand Up @@ -215,4 +220,18 @@ public static void serializeList(@NotNull StubOutputStream dataStream, List<Perl
argument.serialize(dataStream);
}
}

/**
* @return true iff {@code psiElement} is default value of sub/method/modifier parameter specified in signature
*/
@Contract("null->false")
public static boolean isDefaultValue(@Nullable PsiElement psiElement) {
if (psiElement == null) {
return false;
}
if (!SIGNATURES_CONTAINERS.contains(PsiUtilCore.getElementType(psiElement.getParent()))) {
return false;
}
return PsiUtilCore.getElementType(PerlPsiUtil.getPrevSignificantSibling(psiElement)) == OPERATOR_ASSIGN;
}
}
Loading

0 comments on commit e0cdfdc

Please sign in to comment.