/**
  * Generates blocks for nested expressions like a.b.c etc.
  *
  * @param node
  * @return
  */
 private static List<Block> generateForNestedExpr(
     final ASTNode node, Alignment myAlignment, Wrap myWrap, CodeStyleSettings mySettings) {
   final ArrayList<Block> subBlocks = new ArrayList<Block>();
   ASTNode children[] = node.getChildren(null);
   if (children.length > 0 && false /* NESTED.contains(children[0].getElementType()) */) {
     addNestedChildrenRecursively(
         children[0].getPsi(), subBlocks, myAlignment, myWrap, mySettings);
   } else if (canBeCorrectBlock(children[0])) {
     subBlocks.add(
         new MoonFormattingBlock(
             children[0],
             myAlignment,
             Indent.getContinuationWithoutFirstIndent(),
             myWrap,
             mySettings));
   }
   if (children.length > 1) {
     for (ASTNode childNode : children) {
       if (canBeCorrectBlock(childNode) && children[0] != childNode) {
         subBlocks.add(
             new MoonFormattingBlock(
                 childNode,
                 myAlignment,
                 Indent.getContinuationWithoutFirstIndent(),
                 myWrap,
                 mySettings));
       }
     }
   }
   return subBlocks;
 }
 private static ASTNode[] getLuaChildren(final ASTNode node) {
   PsiElement psi = node.getPsi();
   if (psi instanceof OuterLanguageElement) {
     TextRange range = node.getTextRange();
     ArrayList<ASTNode> childList = new ArrayList<ASTNode>();
     PsiFile LuaFile =
         psi.getContainingFile().getViewProvider().getPsi(MoonFileType.MOON_LANGUAGE);
     if (LuaFile instanceof MoonPsiFileBase) {
       addChildNodes(LuaFile, childList, range);
     }
     return childList.toArray(new ASTNode[childList.size()]);
   }
   return node.getChildren(null);
 }
  public static int getCurrentParameterIndex(
      ASTNode argList, int offset, IElementType delimiterType) {
    int curOffset = argList.getTextRange().getStartOffset();
    if (offset < curOffset) return -1;
    ASTNode[] children = argList.getChildren(null);
    int index = 0;

    for (ASTNode child : children) {
      curOffset += child.getTextLength();
      if (offset < curOffset) break;

      IElementType type = child.getElementType();
      if (type == delimiterType) index++;
    }

    return index;
  }
Example #4
0
  @Nullable
  private static ASTNode findTreeForStub(
      ASTNode tree, final Iterator<StubElement<?>> stubs, final StubElement stub) {
    final IElementType type = tree.getElementType();

    if (type instanceof IStubElementType && ((IStubElementType) type).shouldCreateStub(tree)) {
      final StubElement curStub = stubs.next();
      if (curStub == stub) return tree;
    }

    for (ASTNode node : tree.getChildren(null)) {
      final ASTNode treeForStub = findTreeForStub(node, stubs, stub);
      if (treeForStub != null) return treeForStub;
    }

    return null;
  }
  public List<Block> buildNodeChildBlocks(ASTNode node, BlockFactory factory) {
    List<Block> blocks = ContainerUtil.newArrayList();

    for (ASTNode child : node.getChildren(null)) {
      if (FormatterUtil.isWhitespaceOrEmpty(child) || child.getTextLength() == 0) {
        continue;
      }

      Alignment alignment = myConfig.getAlignment(child);

      IElementType type = child.getElementType();
      Indent indent = myConfig.getIndent(type);
      Wrap wrap = myConfig.getWrap(type);

      blocks.add(factory.createBlock(child, indent, alignment, wrap));
    }

    return blocks;
  }
 /**
  * Generates blocks for binary expressions
  *
  * @param node
  * @return
  */
 private static List<Block> generateForBinaryExpr(
     final ASTNode node, Wrap myWrap, CodeStyleSettings mySettings) {
   final ArrayList<Block> subBlocks = new ArrayList<Block>();
   Alignment alignment =
       mySettings.ALIGN_MULTILINE_BINARY_OPERATION ? Alignment.createAlignment() : null;
   MoonBinaryExpression myExpr = (MoonBinaryExpression) node.getPsi();
   ASTNode[] children = node.getChildren(null);
   if (myExpr.getLeftExpression() instanceof MoonBinaryExpression) {
     addBinaryChildrenRecursively(
         myExpr.getLeftExpression(),
         subBlocks,
         Indent.getContinuationWithoutFirstIndent(),
         alignment,
         myWrap,
         mySettings);
   }
   for (ASTNode childNode : children) {
     if (canBeCorrectBlock(childNode) && !(childNode.getPsi() instanceof MoonBinaryExpression)) {
       subBlocks.add(
           new MoonFormattingBlock(
               childNode,
               alignment,
               Indent.getContinuationWithoutFirstIndent(),
               myWrap,
               mySettings));
     }
   }
   if (myExpr.getRightExpression() instanceof MoonBinaryExpression) {
     addBinaryChildrenRecursively(
         myExpr.getRightExpression(),
         subBlocks,
         Indent.getContinuationWithoutFirstIndent(),
         alignment,
         myWrap,
         mySettings);
   }
   return subBlocks;
 }
    @Nullable
    private static Object getLookupItem(@Nullable final XmlEntityDecl decl) {
      if (decl == null) {
        return null;
      }

      final String name = decl.getName();
      if (name == null) {
        return null;
      }

      final XmlAttributeValue value = decl.getValueElement();
      final ASTNode node = value.getNode();
      if (node != null) {
        final ASTNode[] nodes = node.getChildren(TokenSet.create(XmlTokenType.XML_CHAR_ENTITY_REF));
        if (nodes.length == 1) {
          final String valueText = nodes[0].getText();
          final int i = valueText.indexOf('#');
          if (i > 0) {
            String s = valueText.substring(i + 1);
            if (s.endsWith(";")) {
              s = s.substring(0, s.length() - 1);
            }

            try {
              final int unicodeChar = Integer.valueOf(s).intValue();
              return LookupValueFactory.createLookupValueWithHint(
                  name, null, new String(Character.toChars(unicodeChar)));
            } catch (NumberFormatException e) {
              return null;
            }
          }
        }
      }

      return null;
    }