@Nullable
 public PsiElement getPsiOperator() {
   ASTNode node = getNode();
   final ASTNode child = node.findChildByType(PyElementTypes.BINARY_OPS);
   if (child != null) return child.getPsi();
   return null;
 }
 private static TextRange buildRangeForBraces(
     TextRange range, @NotNull ASTNode astNode, IElementType lbraceType, IElementType rbraceType) {
   ASTNode lBrace = astNode.findChildByType(lbraceType);
   ASTNode rBrace = astNode.findChildByType(rbraceType);
   if (lBrace != null && rBrace != null) {
     range = buildRange(range, lBrace.getStartOffset() + 1, rBrace.getStartOffset());
   }
   return range;
 }
  @Override
  public List<TextRange> getRanges() {
    List<TextRange> list = new ArrayList<TextRange>();
    JetValueArgumentList valueArgumentList = getExpression().getValueArgumentList();
    if (valueArgumentList != null) {
      if (valueArgumentList.getArguments().size() > 0) {
        ASTNode valueArgumentListNode = valueArgumentList.getNode();
        ASTNode lPar = valueArgumentListNode.findChildByType(JetTokens.LPAR);
        if (lPar != null) {
          list.add(getRange(lPar));
        }

        ASTNode rPar = valueArgumentListNode.findChildByType(JetTokens.RPAR);
        if (rPar != null) {
          list.add(getRange(rPar));
        }
      } else {
        list.add(getRange(valueArgumentList.getNode()));
      }
    }

    List<JetExpression> functionLiteralArguments = getExpression().getFunctionLiteralArguments();
    for (JetExpression functionLiteralArgument : functionLiteralArguments) {
      while (functionLiteralArgument instanceof JetPrefixExpression) {
        functionLiteralArgument =
            ((JetPrefixExpression) functionLiteralArgument).getBaseExpression();
      }

      if (functionLiteralArgument instanceof JetFunctionLiteralExpression) {
        JetFunctionLiteralExpression functionLiteralExpression =
            (JetFunctionLiteralExpression) functionLiteralArgument;
        list.add(getRange(functionLiteralExpression.getLeftCurlyBrace()));
        ASTNode rightCurlyBrace = functionLiteralExpression.getRightCurlyBrace();
        if (rightCurlyBrace != null) {
          list.add(getRange(rightCurlyBrace));
        }
      }
    }

    return list;
  }
  private static void addFoldingDescriptors(
      final List<FoldingDescriptor> descriptors, final PsiElement tag, @NotNull Document document) {

    TextRange elementRange = tag.getTextRange();
    final int start = elementRange.getStartOffset();
    final int end = elementRange.getEndOffset();

    if (start + 1 < end) {
      TextRange range = null;
      ASTNode astNode = tag.getNode();
      IElementType astType = astNode.getElementType();

      if (tag instanceof CfmlTag) {
        // if (tag instanceof CfmlTagFunctionImpl || tag instanceof CfmlTagComponentImpl || tag
        // instanceof CfmlTagScriptImpl) {
        range =
            buildRangeForBraces(
                range, astNode, CfmlTokenTypes.R_ANGLEBRACKET, CfmlTokenTypes.LSLASH_ANGLEBRACKET);
        // }
      } else if (astType == CfmlElementTypes.FUNCTIONBODY
          || astType == CfmlElementTypes.BLOCK_OF_STATEMENTS) {
        range = buildRange(range, start, end);
      } else if (astType == CfmlElementTypes.SWITCHEXPRESSION) {
        ASTNode lparen = astNode.findChildByType(CfscriptTokenTypes.L_CURLYBRACKET);
        ASTNode rparen = astNode.findChildByType(CfscriptTokenTypes.R_CURLYBRACKET);
        if (lparen != null && rparen != null) {
          range = buildRange(range, lparen.getStartOffset(), rparen.getTextRange().getEndOffset());
        }
      } else if (tag instanceof PsiComment) {
        boolean isColdFusionComment = astNode.getElementType() == CfmlTokenTypes.COMMENT;
        int endIndex = astNode.getText().lastIndexOf(isColdFusionComment ? "--->" : "*/");
        if (endIndex != -1) {
          String commentText = astNode.getText().substring(0, endIndex);
          if (commentText.contains("\n")) {
            int startOffset = tag.getTextRange().getStartOffset();
            range =
                buildRange(
                    range,
                    startOffset + (isColdFusionComment ? "<!---" : "/*").length(),
                    startOffset + commentText.length());
          }
        }
      }

      if (range != null) {
        descriptors.add(new FoldingDescriptor(astNode, range));
      }

      // TODO: insert condition
      addFoldingDescriptorsFromChildren(descriptors, tag, document);
    }
  }
 private static void highlightMissingArguments(
     PyArgumentList node, ProblemsHolder holder, CallArgumentsMapping result) {
   ASTNode our_node = node.getNode();
   if (our_node != null) {
     ASTNode close_paren = our_node.findChildByType(PyTokenTypes.RPAR);
     if (close_paren != null) {
       for (PyNamedParameter param : result.getUnmappedParams()) {
         holder.registerProblem(
             close_paren.getPsi(),
             PyBundle.message("INSP.parameter.$0.unfilled", param.getName()));
       }
     }
   }
 }
 /**
  * Tries to parse given element as <a
  * href="http://msdn.microsoft.com/en-us/library/ms537512(v=vs.85).aspx">conditional comment</a>.
  *
  * @param host target element to parse
  * @return <code>null</code> if given element is not a conditional comment; pair like <code>
  *     (conditional comment start element; conditional comment end element)</code> otherwise
  */
 @Nullable
 private static Pair<ASTNode, ASTNode> parseConditionalCommentBoundaries(
     @NotNull PsiElement host) {
   if (!(host instanceof XmlComment)) {
     return null;
   }
   final ASTNode comment = host.getNode();
   if (comment == null) {
     return null;
   }
   final ASTNode conditionalStart =
       comment.findChildByType(TokenSet.create(XmlTokenType.XML_CONDITIONAL_COMMENT_START_END));
   if (conditionalStart == null) {
     return null;
   }
   final ASTNode conditionalEnd =
       comment.findChildByType(TokenSet.create(XmlTokenType.XML_CONDITIONAL_COMMENT_END_START));
   if (conditionalEnd == null) {
     return null;
   }
   final ASTNode endOfEnd =
       comment.findChildByType(TokenSet.create(XmlTokenType.XML_CONDITIONAL_COMMENT_END));
   return endOfEnd == null ? null : new Pair<ASTNode, ASTNode>(conditionalStart, conditionalEnd);
 }
  private static boolean isEmbraced(@Nullable PsiElement element, int offset) {
    if (element == null) {
      return false;
    }
    ASTNode node = element.getNode();
    if (node == null) {
      return false;
    }
    final ASTNode lbrace = node.findChildByType(CfscriptTokenTypes.L_BRACKET);
    final ASTNode rbrace = node.findChildByType(CfscriptTokenTypes.R_BRACKET);

    if (lbrace == null || rbrace == null) {
      return false;
    }
    return lbrace.getStartOffset() < offset && rbrace.getStartOffset() >= offset;
  }
 private static void generateRaw(final @NotNull XmlTag newTag) {
   XmlElementDescriptor selected = newTag.getDescriptor();
   if (selected == null) return;
   switch (selected.getContentType()) {
     case XmlElementDescriptor.CONTENT_TYPE_EMPTY:
       newTag.collapseIfEmpty();
       ASTNode node = newTag.getNode();
       assert node != null;
       ASTNode elementEnd = node.findChildByType(XmlTokenType.XML_EMPTY_ELEMENT_END);
       if (elementEnd == null) {
         LeafElement emptyTagEnd =
             Factory.createSingleLeafElement(
                 XmlTokenType.XML_EMPTY_ELEMENT_END, "/>", 0, 2, null, newTag.getManager());
         node.addChild(emptyTagEnd);
       }
       break;
     case XmlElementDescriptor.CONTENT_TYPE_MIXED:
       newTag.getValue().setText("");
   }
   for (XmlAttributeDescriptor descriptor : selected.getAttributesDescriptors(newTag)) {
     if (descriptor.isRequired()) {
       newTag.setAttribute(descriptor.getName(), "");
     }
   }
   List<XmlElementDescriptor> tags = getRequiredSubTags(selected);
   for (XmlElementDescriptor descriptor : tags) {
     if (descriptor == null) {
       XmlTag tag =
           XmlElementFactory.getInstance(newTag.getProject())
               .createTagFromText("<", newTag.getLanguage());
       newTag.addSubTag(tag, false);
     } else {
       XmlTag subTag = newTag.addSubTag(createTag(newTag, descriptor), false);
       generateRaw(subTag);
     }
   }
 }
  @NotNull
  @Override
  public Runnable processFile(final PsiFile file) {
    VirtualFile vFile = file.getVirtualFile();
    if (vFile instanceof VirtualFileWindow) vFile = ((VirtualFileWindow) vFile).getDelegate();
    final Project project = file.getProject();
    if (vFile == null
        || !ProjectRootManager.getInstance(project).getFileIndex().isInSourceContent(vFile)) {
      return EmptyRunnable.INSTANCE;
    }
    final List<Pair<String, Boolean>> names = new ArrayList<Pair<String, Boolean>>();
    final Set<String> demandedForNested = new HashSet<>();
    collectNamesToImport(names, demandedForNested, (XmlFile) file);
    Collections.sort(names, (o1, o2) -> StringUtil.compare(o1.first, o2.first, true));
    final CodeStyleSettings settings = CodeStyleSettingsManager.getSettings(project);
    final List<Pair<String, Boolean>> sortedNames =
        ImportHelper.sortItemsAccordingToSettings(names, settings);
    final HashSet<String> onDemand = new HashSet<String>();
    ImportHelper.collectOnDemandImports(sortedNames, onDemand, settings);
    onDemand.addAll(demandedForNested);
    final Set<String> imported = new HashSet<String>();
    final List<String> imports = new ArrayList<String>();
    for (Pair<String, Boolean> pair : sortedNames) {
      final String qName = pair.first;
      final String packageName = StringUtil.getPackageName(qName);
      if (imported.contains(packageName) || imported.contains(qName)) {
        continue;
      }
      if (onDemand.contains(packageName)) {
        imported.add(packageName);
        imports.add("<?import " + packageName + ".*?>");
      } else {
        imported.add(qName);
        imports.add("<?import " + qName + "?>");
      }
    }
    final PsiFileFactory factory = PsiFileFactory.getInstance(file.getProject());

    final XmlFile dummyFile =
        (XmlFile)
            factory.createFileFromText(
                "_Dummy_.fxml", StdFileTypes.XML, StringUtil.join(imports, "\n"));
    final XmlDocument document = dummyFile.getDocument();
    final XmlProlog newImportList = document != null ? document.getProlog() : null;
    if (newImportList == null) return EmptyRunnable.getInstance();
    return () -> {
      final XmlDocument xmlDocument = ((XmlFile) file).getDocument();
      final XmlProlog prolog = xmlDocument != null ? xmlDocument.getProlog() : null;
      if (prolog != null) {
        final Collection<XmlProcessingInstruction> instructions =
            PsiTreeUtil.findChildrenOfType(prolog, XmlProcessingInstruction.class);
        for (final XmlProcessingInstruction instruction : instructions) {
          final ASTNode node = instruction.getNode();
          final ASTNode nameNode = node.findChildByType(XmlTokenType.XML_NAME);
          if (nameNode != null && nameNode.getText().equals("import")) {
            instruction.delete();
          }
        }
        prolog.add(newImportList);
      } else {
        document.addBefore(newImportList, document.getRootTag());
      }
    };
  }