Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.gameforge.idea.Corona.lang.formatter;
- import com.gameforge.idea.Corona.lang.parser.CoronaElementTypes;
- import com.gameforge.idea.Corona.lang.psi.CoronaTableConstructor;
- import com.intellij.formatting.Spacing;
- import com.intellij.lang.ASTNode;
- /**
- * Created by felix.doerschner on 18.05.2016.
- */
- public class CoronaSpacing implements CoronaElementTypes {
- private static final Spacing NO_SPACING_KEEP_NEWLINE = Spacing.createSpacing(0, 0, 0, true, 0);
- private static final Spacing COMMON_SPACING = Spacing.createSpacing(1, 1, 0, false, 100);
- private static final Spacing NO_SPACING_WITH_NEWLINE = Spacing.createSpacing(0, 0, 1, false, 0);
- private static final Spacing SPACING_WITH_NEWLINE = Spacing.createSpacing(4, 4, 1, false, 0);
- private static final Spacing NO_SPACING_WITH_EXTRA_LINE = Spacing.createSpacing(0, 0, 2, false, 0);
- private static final Spacing NO_SPACING = Spacing.createSpacing(0, 0, 0, false, 0);
- public static Spacing getSpacing(CoronaFormatBlock child1, CoronaFormatBlock child2) {
- ASTNode leftNode = null;
- if (child1 != null)
- leftNode = child1.getNode();
- ASTNode rightNode = child2.getNode();
- if (rightNode.getElementType() == STATEMENTS) {
- if (rightNode.getFirstChildNode().getElementType() == FUNCTION_DECLARATION)
- return NO_SPACING_WITH_EXTRA_LINE;
- return NO_SPACING_WITH_NEWLINE;
- }
- if (leftNode == null)
- return null;
- if (rightNode.getElementType() == BLOCK)
- return SPACING_WITH_NEWLINE;
- if (rightNode.getElementType() == END && leftNode.getElementType() == BLOCK)
- return NO_SPACING_WITH_NEWLINE;
- // if (leftNode.getElementType() == FUNCTION_PARAMETER &&
- // (rightNode.getElementType() == END || rightNode.getTreeNext().getElementType() == END)) {
- // return NO_SPACING_WITH_EXTRA_LINE;
- // }
- if (leftNode.getElementType() == FUNCTION_CALL_TOKEN || rightNode.getElementType() == FUNCTION_CALL_TOKEN)
- return NO_SPACING;
- return null;
- }
- private static Spacing getLuaPluginSpacing(ASTNode leftNode, ASTNode rightNode) {
- if (leftNode.getElementType() == MINUS && rightNode.getElementType() == MINUS)
- return COMMON_SPACING;
- if (leftNode.getElementType() == UNARY_OPERATOR) {
- if (!leftNode.getText().equals("not"))
- return NO_SPACING;
- else
- return COMMON_SPACING;
- }
- if (leftNode.getElementType() == LUADOC_COMMENT && rightNode.getElementType() == FUNCTION)
- return NO_SPACING_WITH_NEWLINE;
- // No spacing inside brackets
- if (rightNode.getElementType() == RBRACK || leftNode.getElementType() == LBRACK || rightNode.getElementType() == LBRACK)
- return NO_SPACING;
- // No spacing inside parens
- if (rightNode.getElementType() == RPAREN || leftNode.getElementType() == LPAREN || rightNode.getElementType() == LPAREN)
- return NO_SPACING;
- // Only 1 newline before end, and at least one space
- // if (rightNode.getElementType() == END)
- // return Spacing.createDependentLFSpacing(1, 1, rightNode.getPsi().getParent().getTextRange(), false, 0);
- // No spacing between parens and arg/param lists
- // if (PARAMETER_LIST.equals(rightNode.getElementType())) /*||
- // ANONYMOUS_FUNCTION_EXPRESSION.equals(rightNode.getElementType())*/ {
- // return rightNode.getFirstChildNode().getElementType() == LPAREN ? NO_SPACING : COMMON_SPACING;
- // }
- //
- // if (rightNode.getElementType() == ASSIGNMENT && leftNode.getElementType() == COMMA)
- // return Spacing.createDependentLFSpacing(1, 1, rightNode.getPsi().getParent().getTextRange(), false, 0);
- //
- // // separate functions by at least 1 line (2 linefeeds)
- // if (FUNCTION.equals(leftNode.getElementType()))
- // return Spacing.createSpacing(1, 1, 2, true, 100);
- // ;
- //
- // // No spadicing between fields a . b. c -> a.b.c
- // if (rightNode.getElementType() == FIELD)
- // return NO_SPACING;
- // Table constructors should be {} if empty and { /n} if they have data
- if (rightNode.getPsi().getContext() instanceof CoronaTableConstructor) {
- if (leftNode.getElementType() == LCURLY) {
- CoronaTableConstructor tc = (CoronaTableConstructor) rightNode.getPsi().getContext();
- // if (tc.getInitializers().length==0)
- // return NO_SPACING;
- return Spacing.createDependentLFSpacing(1, 1, rightNode.getPsi().getParent().getTextRange(), false, 0);
- }
- if (rightNode.getElementType() == RCURLY) {
- return Spacing.createDependentLFSpacing(1, 1, rightNode.getPsi().getParent().getTextRange(), false, 0);
- }
- }
- // no spacing on the right side of punctuation type operators i.e a, b, c, d
- if ((PUNCTUATION_SIGNS.contains(rightNode.getElementType())) ||
- (COLON.equals(rightNode.getElementType()))) {
- return NO_SPACING;
- }
- // Space out everything else
- return COMMON_SPACING;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement