<commit_before>/*
 * Copyright 2000-2009 JetBrains s.r.o.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package com.intellij.ide.actions;

import com.intellij.CommonBundle;
import com.intellij.history.LocalHistory;
import com.intellij.history.LocalHistoryAction;
import com.intellij.ide.IdeBundle;
import com.intellij.openapi.application.Result;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;
import com.intellij.psi.PsiElement;
import com.intellij.psi.SmartPointerManager;
import com.intellij.psi.SmartPsiElementPointer;
import com.intellij.util.IncorrectOperationException;
import com.intellij.util.SmartList;
import com.intellij.util.containers.ContainerUtil;
import org.jetbrains.annotations.NotNull;

import java.util.List;

/**
 * @author peter
 */
public abstract class ElementCreator {
  private final Project myProject;
  private final String myErrorTitle;

  protected ElementCreator(Project project, String errorTitle) {
    myProject = project;
    myErrorTitle = errorTitle;
  }

  protected abstract void checkBeforeCreate(String newName) throws IncorrectOperationException;
  protected abstract PsiElement[] create(String newName) throws Exception;
  protected abstract String getActionName(String newName);

  public PsiElement[] tryCreate(@NotNull final String inputString) {
    if (inputString.length() == 0) {
        Messages.showMessageDialog(myProject, IdeBundle.message("error.name.should.be.specified"), CommonBundle.getErrorTitle(),
                                   Messages.getErrorIcon());
        return PsiElement.EMPTY_ARRAY;
      }

      try {
        checkBeforeCreate(inputString);
      }
      catch (IncorrectOperationException e) {
        Messages.showMessageDialog(myProject, CreateElementActionBase.filterMessage(e.getMessage()), myErrorTitle, Messages.getErrorIcon());
        return PsiElement.EMPTY_ARRAY;
      }

      final Exception[] exception = new Exception[1];
    final SmartPsiElementPointer[][] myCreatedElements = {null};

    final String commandName = getActionName(inputString);
    new WriteCommandAction(myProject, commandName) {
      protected void run(Result result) throws Throwable {
        LocalHistoryAction action = LocalHistoryAction.NULL;
        try {
          action = LocalHistory.startAction(myProject, commandName);

          PsiElement[] psiElements = create(inputString);
          myCreatedElements[0] = new SmartPsiElementPointer[psiElements.length];
          SmartPointerManager manager = SmartPointerManager.getInstance(myProject);
          for (int i = 0; i < myCreatedElements[0].length; i++) {
            myCreatedElements[0][i] = manager.createSmartPsiElementPointer(psiElements[i]);
          }
        }
        catch (Exception ex) {
          exception[0] = ex;
        }
        finally {
          action.finish();
        }
      }
    }.execute();

      if (exception[0] != null) {
        String errorMessage = CreateElementActionBase.filterMessage(exception[0].getMessage());
        if (errorMessage == null || errorMessage.length() == 0) {
          errorMessage = exception[0].toString();
        }
        Messages.showMessageDialog(myProject, errorMessage, myErrorTitle, Messages.getErrorIcon());

      }
<commit_msg>exception logging added<commit_after>/*
 * Copyright 2000-2009 JetBrains s.r.o.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package com.intellij.ide.actions;

import com.intellij.CommonBundle;
import com.intellij.history.LocalHistory;
import com.intellij.history.LocalHistoryAction;
import com.intellij.ide.IdeBundle;
import com.intellij.openapi.application.Result;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.psi.PsiElement;
import com.intellij.psi.SmartPointerManager;
import com.intellij.psi.SmartPsiElementPointer;
import com.intellij.util.IncorrectOperationException;
import com.intellij.util.SmartList;
import com.intellij.util.containers.ContainerUtil;
import org.jetbrains.annotations.NotNull;

import java.util.List;

/**
 * @author peter
 */
public abstract class ElementCreator {
  private static final Logger LOG = Logger.getInstance("#com.intellij.ide.actions.ElementCreator");
  private final Project myProject;
  private final String myErrorTitle;

  protected ElementCreator(Project project, String errorTitle) {
    myProject = project;
    myErrorTitle = errorTitle;
  }

  protected abstract void checkBeforeCreate(String newName) throws IncorrectOperationException;
  protected abstract PsiElement[] create(String newName) throws Exception;
  protected abstract String getActionName(String newName);

  public PsiElement[] tryCreate(@NotNull final String inputString) {
    if (inputString.length() == 0) {
        Messages.showMessageDialog(myProject, IdeBundle.message("error.name.should.be.specified"), CommonBundle.getErrorTitle(),
                                   Messages.getErrorIcon());
        return PsiElement.EMPTY_ARRAY;
      }

      try {
        checkBeforeCreate(inputString);
      }
      catch (IncorrectOperationException e) {
        Messages.showMessageDialog(myProject, CreateElementActionBase.filterMessage(e.getMessage()), myErrorTitle, Messages.getErrorIcon());
        return PsiElement.EMPTY_ARRAY;
      }

      final Exception[] exception = new Exception[1];
    final SmartPsiElementPointer[][] myCreatedElements = {null};

    final String commandName = getActionName(inputString);
    new WriteCommandAction(myProject, commandName) {
      protected void run(Result result) throws Throwable {
        LocalHistoryAction action = LocalHistoryAction.NULL;
        try {
          action = LocalHistory.startAction(myProject, commandName);

          PsiElement[] psiElements = create(inputString);
          myCreatedElements[0] = new SmartPsiElementPointer[psiElements.length];
          SmartPointerManager manager = SmartPointerManager.getInstance(myProject);
          for (int i = 0; i < myCreatedElements[0].length; i++) {
            myCreatedElements[0][i] = manager.createSmartPsiElementPointer(psiElements[i]);
          }
        }
        catch (Exception ex) {
          exception[0] = ex;
        }
        finally {
          action.finish();
        }
      }
    }.execute();

      if (exception[0] != null) {
        LOG.info(exception[0]);
        String errorMessage = CreateElementActionBase.filterMessage(exception[0].getMessage());
        if (errorMessage == null || errorMessage.length() == 0) {
          errorMessage = exception[0].toString();
        }
        Messages.showMessageDialog(myProject, errorMessage, myErrorTitle, Messages.getErrorIcon());

      }

{'fraction_non_alphanumeric': 0.0945071673220855, 'fraction_numerical': 0.005962197133071166, 'mean_word_length': 3.656822209096279, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4889723', 'n_tokens_mistral': 2155, 'n_tokens_neox': 2104, 'n_words': 525}