<commit_before>package com.intellij.psi.impl.source.codeStyle;

import com.intellij.codeFormatting.PseudoText;
import com.intellij.codeFormatting.PseudoTextBuilder;
import com.intellij.lang.ASTNode;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.application.ex.ApplicationEx;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.fileTypes.FileType;
import com.intellij.openapi.fileTypes.LanguageFileType;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.PsiFile;
import com.intellij.psi.codeStyle.CodeStyleSettings;
import com.intellij.psi.impl.source.Constants;
import com.intellij.psi.impl.source.SourceTreeToPsiMap;
import com.intellij.psi.impl.source.codeStyle.java.JavaCodeFormatter;
import com.intellij.psi.impl.source.codeStyle.javadoc.CommentFormatter;
import com.intellij.psi.impl.source.parsing.ChameleonTransforming;
import com.intellij.psi.impl.source.tree.CompositeElement;

/**
 *
 */
public class CodeFormatterFacade implements Constants {
  private static final Logger LOG = Logger.getInstance("#com.intellij.psi.impl.source.codeStyle.CodeFormatterFacade");

  private CodeStyleSettings mySettings;
  private Helper myHelper;
  private IndentAdjusterFacade myIndentAdjuster;
  private CodeWrapperFacade myWrapper;
  private CommentFormatter myCommentFormatter;

  private ASTNode child1;
  private ASTNode child2;
  private BraceEnforcer myBraceEnforcer;
  private CodeFormatter myCodeFormatter = new JavaCodeFormatter();

  public static int USE_NEW_CODE_FORMATTER = -1;

  public CodeFormatterFacade(CodeStyleSettings settings, Helper helper) {
    if (USE_NEW_CODE_FORMATTER < 0) {
      ApplicationEx application = ((ApplicationEx)ApplicationManager.getApplication());
      boolean internal = application.isInternal();
      boolean unitTestMode = application.isUnitTestMode();
      USE_NEW_CODE_FORMATTER = (internal || unitTestMode) ? 1 : 0;
    }

    mySettings = settings;
    myHelper = helper;
    myIndentAdjuster = new IndentAdjusterFacade(settings, helper);
    myWrapper = new CodeWrapperFacade(settings, helper, myIndentAdjuster);
    myBraceEnforcer = new BraceEnforcer(mySettings);
    myCommentFormatter = new CommentFormatter(helper.getProject());
  }

  private void formatComments(ASTNode element, int startOffset, int endOffset) {
    TextRange range = element.getTextRange();
    if (range.getStartOffset() >= startOffset && range.getEndOffset() <= endOffset) {
      myCommentFormatter.process(element);
    }

    if (element instanceof CompositeElement) {
      for (ASTNode elem = element.getFirstChildNode(); elem != null; elem = elem.getTreeNext()) {
        formatComments(elem, startOffset, endOffset);
      }
    }
  }

  public ASTNode process(ASTNode element, int parent_indent) {
    if (useNewFormatter(myHelper.getFileType())) {
      TextRange range = element.getTextRange();
      int startOffset = range.getStartOffset();
      int endOffset = range.getEndOffset();

      processRange(element, startOffset, endOffset);
      return element;
    }

    if (element instanceof CompositeElement) {
      ASTNode parent = element;
      ChameleonTransforming.transformChildren(parent);

      child1 = null;
      child2 = Helper.shiftForwardToNonSpace(parent.getFirstChildNode());
      int indent = -1;
      for (; child2 != null; child1 = child2, child2 = Helper.shiftForwardToNonSpace(child2.getTreeNext())) {
        if (child1 != null || SourceTreeToPsiMap.treeElementToPsi(parent) instanceof PsiFile) {
          child2 = myCodeFormatter.format(SourceTreeToPsiMap.treeElementToPsi(parent), child1, child2, mySettings, myHelper);
        }

        if (indent < 0) {
          indent = myIndentAdjuster.calculateIndent(child2, -1);
        }

        if (child2 != null) {
          child2 = myIndentAdjuster.adjustIndent(child2, indent);
        }
      }

      element = myWrapper.wrap(element);
      element = myIndentAdjuster.adjustIndent(element, parent_indent);
      myCommentFormatter.process(element);

      indent = -1;
      for (ASTNode child = element.getFirstChildNode(); child != null; child = child.getTreeNext()) {
        if (indent < 0) {
          indent = myIndentAdjuster.calculateIndent(child, -1);
        }
        child = process(child, indent);
      }
    }
    return element;
  }

  public ASTNode processRange(ASTNode element, int startOffset, int endOffset) {
    FileType fileType = myHelper.getFileType();
    if (useNewFormatter(fileType)) {
      PseudoTextBuilder pseudoTextBuilder = ((LanguageFileType)fileType).getLanguage().getFormatter();
      if (pseudoTextBuilder == null) {
        return element;
      }
      else {
        try {
          PseudoText pseudoText = pseudoTextBuilder.build(myHelper.getProject(), mySettings, SourceTreeToPsiMap.treeElementToPsi(element));
          GeneralCodeFormatter.createSimpleInstance(pseudoText, mySettings, fileType, startOffset, endOffset).format();

        }<commit_msg>SCR43263 fixed<commit_after>package com.intellij.psi.impl.source.codeStyle;

import com.intellij.codeFormatting.PseudoText;
import com.intellij.codeFormatting.PseudoTextBuilder;
import com.intellij.lang.ASTNode;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.application.ex.ApplicationEx;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.fileTypes.FileType;
import com.intellij.openapi.fileTypes.LanguageFileType;
import com.intellij.openapi.progress.ProcessCanceledException;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.PsiFile;
import com.intellij.psi.codeStyle.CodeStyleSettings;
import com.intellij.psi.impl.source.Constants;
import com.intellij.psi.impl.source.SourceTreeToPsiMap;
import com.intellij.psi.impl.source.codeStyle.java.JavaCodeFormatter;
import com.intellij.psi.impl.source.codeStyle.javadoc.CommentFormatter;
import com.intellij.psi.impl.source.parsing.ChameleonTransforming;
import com.intellij.psi.impl.source.tree.CompositeElement;

/**
 *
 */
public class CodeFormatterFacade implements Constants {
  private static final Logger LOG = Logger.getInstance("#com.intellij.psi.impl.source.codeStyle.CodeFormatterFacade");

  private CodeStyleSettings mySettings;
  private Helper myHelper;
  private IndentAdjusterFacade myIndentAdjuster;
  private CodeWrapperFacade myWrapper;
  private CommentFormatter myCommentFormatter;

  private ASTNode child1;
  private ASTNode child2;
  private BraceEnforcer myBraceEnforcer;
  private CodeFormatter myCodeFormatter = new JavaCodeFormatter();

  public static int USE_NEW_CODE_FORMATTER = -1;

  public CodeFormatterFacade(CodeStyleSettings settings, Helper helper) {
    if (USE_NEW_CODE_FORMATTER < 0) {
      ApplicationEx application = ((ApplicationEx)ApplicationManager.getApplication());
      boolean internal = application.isInternal();
      boolean unitTestMode = application.isUnitTestMode();
      USE_NEW_CODE_FORMATTER = (internal || unitTestMode) ? 1 : 0;
    }

    mySettings = settings;
    myHelper = helper;
    myIndentAdjuster = new IndentAdjusterFacade(settings, helper);
    myWrapper = new CodeWrapperFacade(settings, helper, myIndentAdjuster);
    myBraceEnforcer = new BraceEnforcer(mySettings);
    myCommentFormatter = new CommentFormatter(helper.getProject());
  }

  private void formatComments(ASTNode element, int startOffset, int endOffset) {
    TextRange range = element.getTextRange();
    if (range.getStartOffset() >= startOffset && range.getEndOffset() <= endOffset) {
      myCommentFormatter.process(element);
    }

    if (element instanceof CompositeElement) {
      for (ASTNode elem = element.getFirstChildNode(); elem != null; elem = elem.getTreeNext()) {
        formatComments(elem, startOffset, endOffset);
      }
    }
  }

  public ASTNode process(ASTNode element, int parent_indent) {
    if (useNewFormatter(myHelper.getFileType())) {
      TextRange range = element.getTextRange();
      int startOffset = range.getStartOffset();
      int endOffset = range.getEndOffset();

      processRange(element, startOffset, endOffset);
      return element;
    }

    if (element instanceof CompositeElement) {
      ASTNode parent = element;
      ChameleonTransforming.transformChildren(parent);

      child1 = null;
      child2 = Helper.shiftForwardToNonSpace(parent.getFirstChildNode());
      int indent = -1;
      for (; child2 != null; child1 = child2, child2 = Helper.shiftForwardToNonSpace(child2.getTreeNext())) {
        if (child1 != null || SourceTreeToPsiMap.treeElementToPsi(parent) instanceof PsiFile) {
          child2 = myCodeFormatter.format(SourceTreeToPsiMap.treeElementToPsi(parent), child1, child2, mySettings, myHelper);
        }

        if (indent < 0) {
          indent = myIndentAdjuster.calculateIndent(child2, -1);
        }

        if (child2 != null) {
          child2 = myIndentAdjuster.adjustIndent(child2, indent);
        }
      }

      element = myWrapper.wrap(element);
      element = myIndentAdjuster.adjustIndent(element, parent_indent);
      myCommentFormatter.process(element);

      indent = -1;
      for (ASTNode child = element.getFirstChildNode(); child != null; child = child.getTreeNext()) {
        if (indent < 0) {
          indent = myIndentAdjuster.calculateIndent(child, -1);
        }
        child = process(child, indent);
      }
    }
    return element;
  }

  public ASTNode processRange(ASTNode element, int startOffset, int endOffset) {
    FileType fileType = myHelper.getFileType();
    if (useNewFormatter(fileType)) {
      PseudoTextBuilder pseudoTextBuilder = ((LanguageFileType)fileType).getLanguage().getFormatter();
      if (pseudoTextBuilder == null) {
        return element;
      }
      else {
        try {
          PseudoText pseudoText = pseudoTextBuilder.build(myHelper.getProject(), mySettings, SourceTreeToPsiMap.treeElementToPsi(element));
          GeneralCodeFormatter.createSimpleInstance(pseudoText, mySettings, fileType, startOffset, endOffset).format();

        }
        catch (ProcessCanceledException processCanceledException) {
          throw processCanceledException;
        }
{'fraction_non_alphanumeric': 0.09762791609488336, 'fraction_numerical': 0.005783179768672809, 'mean_word_length': 4.068554396423249, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4556611', 'n_tokens_mistral': 3015, 'n_tokens_neox': 2918, 'n_words': 572}