Advertisement
Guest User

spacing2

a guest
Jun 1st, 2016
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.71 KB | None | 0 0
  1. package com.gameforge.idea.Corona.lang.formatter;
  2.  
  3. import com.gameforge.idea.Corona.lang.parser.CoronaElementTypes;
  4. import com.gameforge.idea.Corona.lang.psi.CoronaTableConstructor;
  5. import com.intellij.formatting.Spacing;
  6. import com.intellij.lang.ASTNode;
  7.  
  8. /**
  9.  * Created by felix.doerschner on 18.05.2016.
  10.  */
  11. public class CoronaSpacing implements CoronaElementTypes {
  12.     private static final Spacing NO_SPACING_KEEP_NEWLINE = Spacing.createSpacing(0, 0, 0, true, 0);
  13.     private static final Spacing COMMON_SPACING = Spacing.createSpacing(1, 1, 0, false, 100);
  14.  
  15.     private static final Spacing NO_SPACING_WITH_NEWLINE = Spacing.createSpacing(0, 0, 1, false, 0);
  16.     private static final Spacing SPACING_WITH_NEWLINE = Spacing.createSpacing(4, 4, 1, false, 0);
  17.     private static final Spacing NO_SPACING_WITH_EXTRA_LINE = Spacing.createSpacing(0, 0, 2, false, 0);
  18.     private static final Spacing NO_SPACING = Spacing.createSpacing(0, 0, 0, false, 0);
  19.  
  20.     public static Spacing getSpacing(CoronaFormatBlock child1, CoronaFormatBlock child2) {
  21.         ASTNode leftNode = null;
  22.         if (child1 != null)
  23.             leftNode = child1.getNode();
  24.         ASTNode rightNode = child2.getNode();
  25.  
  26.         if (rightNode.getElementType() == STATEMENTS) {
  27.             if (rightNode.getFirstChildNode().getElementType() == FUNCTION_DECLARATION)
  28.                 return NO_SPACING_WITH_EXTRA_LINE;
  29.             return NO_SPACING_WITH_NEWLINE;
  30.         }
  31.  
  32.         if (leftNode == null)
  33.             return null;
  34.  
  35.         if (rightNode.getElementType() == BLOCK)
  36.             return SPACING_WITH_NEWLINE;
  37.  
  38.         if (rightNode.getElementType() == END && leftNode.getElementType() == BLOCK)
  39.             return NO_SPACING_WITH_NEWLINE;
  40.  
  41. //      if (leftNode.getElementType() == FUNCTION_PARAMETER &&
  42. //              (rightNode.getElementType() == END || rightNode.getTreeNext().getElementType() == END)) {
  43. //          return NO_SPACING_WITH_EXTRA_LINE;
  44. //      }
  45.  
  46.         if (leftNode.getElementType() == FUNCTION_CALL_TOKEN || rightNode.getElementType() == FUNCTION_CALL_TOKEN)
  47.             return NO_SPACING;
  48.  
  49.         return null;
  50.     }
  51.  
  52.     private static Spacing getLuaPluginSpacing(ASTNode leftNode, ASTNode rightNode) {
  53.  
  54.         if (leftNode.getElementType() == MINUS && rightNode.getElementType() == MINUS)
  55.             return COMMON_SPACING;
  56.  
  57.         if (leftNode.getElementType() == UNARY_OPERATOR) {
  58.             if (!leftNode.getText().equals("not"))
  59.                 return NO_SPACING;
  60.             else
  61.                 return COMMON_SPACING;
  62.         }
  63.  
  64.         if (leftNode.getElementType() == LUADOC_COMMENT && rightNode.getElementType() == FUNCTION)
  65.             return NO_SPACING_WITH_NEWLINE;
  66.  
  67.         // No spacing inside brackets
  68.         if (rightNode.getElementType() == RBRACK || leftNode.getElementType() == LBRACK || rightNode.getElementType() == LBRACK)
  69.             return NO_SPACING;
  70.  
  71.         // No spacing inside parens
  72.         if (rightNode.getElementType() == RPAREN || leftNode.getElementType() == LPAREN || rightNode.getElementType() == LPAREN)
  73.             return NO_SPACING;
  74.  
  75.         // Only 1 newline before end, and at least one space
  76. //      if (rightNode.getElementType() == END)
  77. //          return Spacing.createDependentLFSpacing(1, 1, rightNode.getPsi().getParent().getTextRange(), false, 0);
  78.  
  79.         // No spacing between parens and arg/param lists
  80. //      if (PARAMETER_LIST.equals(rightNode.getElementType())) /*||
  81. //              ANONYMOUS_FUNCTION_EXPRESSION.equals(rightNode.getElementType())*/ {
  82. //          return rightNode.getFirstChildNode().getElementType() == LPAREN ? NO_SPACING : COMMON_SPACING;
  83. //      }
  84. //
  85. //      if (rightNode.getElementType() == ASSIGNMENT && leftNode.getElementType() == COMMA)
  86. //          return Spacing.createDependentLFSpacing(1, 1, rightNode.getPsi().getParent().getTextRange(), false, 0);
  87. //
  88. //      // separate functions by at least 1 line (2 linefeeds)
  89. //      if (FUNCTION.equals(leftNode.getElementType()))
  90. //          return Spacing.createSpacing(1, 1, 2, true, 100);
  91. //      ;
  92. //
  93. //      // No spadicing between fields a . b. c -> a.b.c
  94. //      if (rightNode.getElementType() == FIELD)
  95. //          return NO_SPACING;
  96.  
  97.         // Table constructors should be {} if empty and { /n} if they have data
  98.         if (rightNode.getPsi().getContext() instanceof CoronaTableConstructor) {
  99.             if (leftNode.getElementType() == LCURLY) {
  100.                 CoronaTableConstructor tc = (CoronaTableConstructor) rightNode.getPsi().getContext();
  101. //              if (tc.getInitializers().length==0)
  102. //                  return NO_SPACING;
  103.                 return Spacing.createDependentLFSpacing(1, 1, rightNode.getPsi().getParent().getTextRange(), false, 0);
  104.             }
  105.             if (rightNode.getElementType() == RCURLY) {
  106.                 return Spacing.createDependentLFSpacing(1, 1, rightNode.getPsi().getParent().getTextRange(), false, 0);
  107.             }
  108.         }
  109.  
  110.         // no spacing on the right side of punctuation type operators i.e  a, b, c, d
  111.         if ((PUNCTUATION_SIGNS.contains(rightNode.getElementType())) ||
  112.                 (COLON.equals(rightNode.getElementType()))) {
  113.             return NO_SPACING;
  114.         }
  115.  
  116.         // Space out everything else
  117.         return COMMON_SPACING;
  118.     }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement