private JSStatement addStatementImpl(final JSStatement toAdd, final boolean before) throws IncorrectOperationException { final ASTNode treeParent = getNode().getTreeParent(); if(treeParent.getElementType() != JSElementTypes.BLOCK_STATEMENT && !(treeParent.getElementType() instanceof JSFileElementType) && treeParent.getElementType() != JSElementTypes.CLASS && treeParent.getElementType() != JSElementTypes.EMBEDDED_CONTENT) { if(before) { return (JSStatement) treeParent.getPsi().addBefore(toAdd, this); } else { return (JSStatement) treeParent.getPsi().addAfter(toAdd, this); } } else { final ASTNode copy = toAdd.getNode().copyElement(); addChildAndReformat(treeParent, copy, before ? getNode() : getNode().getTreeNext()); return (JSStatement) copy.getPsi(); } }
@Override public boolean isCollapsedByDefault(ASTNode node) { if(node.getTreeParent().getElementType() instanceof JSFileElementType && node.getTreePrev() == null && node.getPsi() instanceof PsiComment) { return CodeFoldingSettings.getInstance().COLLAPSE_FILE_HEADER; } if(node.getElementType() == JSElementTypes.IMPORT_STATEMENT) { return CodeFoldingSettings.getInstance().COLLAPSE_IMPORTS; } return CodeFoldingSettings.getInstance().COLLAPSE_DOC_COMMENTS && node.getElementType() == JSTokenTypes.DOC_COMMENT; }
@Override @NotNull public ChildAttributes getChildAttributes(final int newChildIndex) { Indent indent = null; final IElementType blockElementType = myNode.getElementType(); if(blockElementType == JSTokenTypes.DOC_COMMENT) { return new ChildAttributes(Indent.getSpaceIndent(1), null); } if(blockElementType == JSElementTypes.PACKAGE_STATEMENT) { final JSCodeStyleSettings customSettings = CodeStyleSettingsManager.getSettings(myNode.getPsi().getProject()).getCustomSettings (JSCodeStyleSettings.class); if(customSettings.INDENT_PACKAGE_CHILDREN == JSCodeStyleSettings.INDENT) { indent = Indent.getNormalIndent(); } else { indent = Indent.getNoneIndent(); } } else if(blockElementType == JSElementTypes.BLOCK_STATEMENT || blockElementType == JSElementTypes.CLASS || blockElementType == JSElementTypes.OBJECT_LITERAL_EXPRESSION) { indent = Indent.getNormalIndent(); } else if(blockElementType instanceof JSFileElementType || blockElementType == JSElementTypes.EMBEDDED_CONTENT) { indent = Indent.getNoneIndent(); } else if(JSElementTypes.SOURCE_ELEMENTS.contains(blockElementType) || blockElementType == JSElementTypes.FUNCTION_EXPRESSION || blockElementType == JSElementTypes.ATTRIBUTE_LIST) { indent = Indent.getNoneIndent(); } Alignment alignment = null; final List<Block> subBlocks = getSubBlocks(); for(int i = 0; i < newChildIndex; i++) { if(i == subBlocks.size()) { break; } final Alignment childAlignment = subBlocks.get(i).getAlignment(); if(childAlignment != null) { alignment = childAlignment; break; } } // in for loops, alignment is required only for items within parentheses if(blockElementType == JSElementTypes.FOR_STATEMENT || blockElementType == JSElementTypes.FOR_IN_STATEMENT) { for(int i = 0; i < newChildIndex; i++) { if(((JSBlock) subBlocks.get(i)).getNode().getElementType() == JSTokenTypes.RPAR) { alignment = null; break; } } } return new ChildAttributes(indent, alignment); }
private static ASTNode appendDescriptors(final ASTNode node, final Document document, final List<FoldingDescriptor> descriptors) { final IElementType type = node.getElementType(); if(type == JSElementTypes.BLOCK_STATEMENT || type == JSElementTypes.OBJECT_LITERAL_EXPRESSION || type == JSElementTypes.ARRAY_LITERAL_EXPRESSION || type == JSElementTypes.XML_LITERAL_EXPRESSION) { TextRange textRange = node.getTextRange(); if(textRange.getEndOffset() < document.getTextLength() && document.getLineNumber(textRange.getStartOffset()) != document.getLineNumber(textRange .getEndOffset())) { addDescriptorForNode(node, descriptors); } } else if(type == JSTokenTypes.DOC_COMMENT) { addDescriptorForNode(node, descriptors); } else if(type == JSTokenTypes.C_STYLE_COMMENT) { addDescriptorForNode(node, descriptors); } else if(type == JSTokenTypes.END_OF_LINE_COMMENT) { return collapseConsequentNodesOfSpecifiedType(node, descriptors, JSTokenTypes.END_OF_LINE_COMMENT); } else if(type == JSElementTypes.IMPORT_STATEMENT) { return collapseConsequentNodesOfSpecifiedType(node, descriptors, JSElementTypes.IMPORT_STATEMENT); } else if(type == JSElementTypes.CLASS || type == JSElementTypes.PACKAGE_STATEMENT) { ASTNode lbrace = node.findChildByType(JSTokenTypes.LBRACE); if(lbrace != null) { addDescriptorForRange(node, descriptors, new TextRange(lbrace.getStartOffset(), node.getTextRange().getEndOffset())); } } if(type instanceof JSFileElementType) { // expand chameleon node.getPsi().getFirstChild(); } ASTNode child = node.getFirstChildNode(); while(child != null) { child = appendDescriptors(child, document, descriptors).getTreeNext(); } return node; }