<commit_before>/*
 * Copyright 2000-2007 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 org.jetbrains.plugins.groovy.actions;

import com.intellij.CommonBundle;
import com.intellij.ide.IdeView;
import com.intellij.ide.actions.CreateElementActionBase;
import com.intellij.openapi.actionSystem.*;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.ProjectFileIndex;
import com.intellij.openapi.roots.ProjectRootManager;
import com.intellij.openapi.ui.Messages;
import com.intellij.psi.*;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.plugins.groovy.GroovyBundle;
import org.jetbrains.plugins.groovy.config.GroovyGrailsConfiguration;

import javax.swing.*;

public abstract class NewActionBase extends CreateElementActionBase {

  public NewActionBase(String text, String description, Icon icon) {
    super(text, description, icon);
  }

  @NotNull
  protected final PsiElement[] invokeDialog(final Project project, final PsiDirectory directory) {
    MyInputValidator validator = new MyInputValidator(project, directory);
    Messages.showInputDialog(project, getDialogPrompt(), getDialogTitle(), Messages.getQuestionIcon(), "", validator);

    return validator.getCreatedElements();
  }

  protected abstract String getDialogPrompt();

  protected abstract String getDialogTitle();

  public void update(final AnActionEvent e) {
    super.update(e);

    final Presentation presentation = e.getPresentation();

    boolean isEnabled = presentation.isEnabled() && isUnderSourceRoots(e) && isGroovyConfigured(e);
    presentation.setEnabled(isEnabled);
    presentation.setVisible(isEnabled);
  }

  public static boolean isGroovyConfigured(AnActionEvent e) {
    Project project = (Project) e.getDataContext().getData(DataConstants.PROJECT);
    if (project != null) {
      PsiPackage groovyPackage = PsiManager.getInstance(project).findPackage("groovy.lang");
      if (groovyPackage != null) return true;
    }
    return false;
  }

  public static boolean isUnderSourceRoots(final AnActionEvent e) {
    final DataContext context = e.getDataContext();
    Module module = (Module) context.getData(DataConstants.MODULE);
    if (!GroovyGrailsConfiguration.isSuitableModule(module)) {
      return false;
    }
    final IdeView view = (IdeView) context.getData(DataConstants.IDE_VIEW);
    final Project project = (Project) context.getData(DataConstants.PROJECT);
    if (view != null && project != null) {
      ProjectFileIndex projectFileIndex = ProjectRootManager.getInstance(project).getFileIndex();
      PsiDirectory[] dirs = view.getDirectories();
      for (PsiDirectory dir : dirs) {
        if (projectFileIndex.isInSourceContent(dir.getVirtualFile()) && dir.getPackage() != null) {
          return true;
        }
      }
    }

    return false;
  }

  @NotNull
  protected PsiElement[] create(String newName, PsiDirectory directory) throws Exception {
    return doCreate(newName, directory);
  }

  @NotNull
  protected abstract PsiElement[] doCreate(String newName, PsiDirectory directory) throws Exception;

  protected static PsiFile createClassFromTemplate(final PsiDirectory directory, String className, String templateName,
                                                   @NonNls String... parameters) throws IncorrectOperationException {
    return GroovyTemplatesFactory.createFromTemplate(directory, className, className + ".groovy", templateName, parameters);
  }


  protected String getErrorTitle() {
    return CommonBundle.getErrorTitle();
  }

  protected String getActionName(PsiDirectory directory, String newName) {
    return GroovyBundle.message("newclass.progress.text", newName);
  }

  protected void checkBeforeCreate(String newName, PsiDirectory directory) throws IncorrectOperationException {
    directory.checkCreateClass(newName);
  }
}
<commit_msg>cleanup<commit_after>/*
 * Copyright 2000-2007 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 org.jetbrains.plugins.groovy.actions;

import com.intellij.CommonBundle;
import com.intellij.ide.IdeView;
import com.intellij.ide.actions.CreateElementActionBase;
import com.intellij.openapi.actionSystem.*;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.ProjectFileIndex;
import com.intellij.openapi.roots.ProjectRootManager;
import com.intellij.openapi.ui.Messages;
import com.intellij.psi.*;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.plugins.groovy.GroovyBundle;
import org.jetbrains.plugins.groovy.config.GroovyGrailsConfiguration;

import javax.swing.*;

public abstract class NewActionBase extends CreateElementActionBase {

  public NewActionBase(String text, String description, Icon icon) {
    super(text, description, icon);
  }

  @NotNull
  protected final PsiElement[] invokeDialog(final Project project, final PsiDirectory directory) {
    MyInputValidator validator = new MyInputValidator(project, directory);
    Messages.showInputDialog(project, getDialogPrompt(), getDialogTitle(), Messages.getQuestionIcon(), "", validator);

    return validator.getCreatedElements();
  }

  protected abstract String getDialogPrompt();

  protected abstract String getDialogTitle();

  public void update(final AnActionEvent e) {
    super.update(e);

    final Presentation presentation = e.getPresentation();

    boolean isEnabled = presentation.isEnabled() && isUnderSourceRoots(e) && isGroovyConfigured(e);
    presentation.setEnabled(isEnabled);
    presentation.setVisible(isEnabled);
  }

  private static boolean isGroovyConfigured(AnActionEvent e) {
    Project project = (Project) e.getDataContext().getData(DataConstants.PROJECT);
    if (project != null) {
      PsiPackage groovyPackage = PsiManager.getInstance(project).findPackage("groovy.lang");
      if (groovyPackage != null) return true;
    }
    return false;
  }

  public static boolean isUnderSourceRoots(final AnActionEvent e) {
    final DataContext context = e.getDataContext();
    Module module = (Module) context.getData(DataConstants.MODULE);
    if (!GroovyGrailsConfiguration.isSuitableModule(module)) {
      return false;
    }
    final IdeView view = (IdeView) context.getData(DataConstants.IDE_VIEW);
    final Project project = (Project) context.getData(DataConstants.PROJECT);
    if (view != null && project != null) {
      ProjectFileIndex projectFileIndex = ProjectRootManager.getInstance(project).getFileIndex();
      PsiDirectory[] dirs = view.getDirectories();
      for (PsiDirectory dir : dirs) {
        if (projectFileIndex.isInSourceContent(dir.getVirtualFile()) && dir.getPackage() != null) {
          return true;
        }
      }
    }

    return false;
  }

  @NotNull
  protected PsiElement[] create(String newName, PsiDirectory directory) throws Exception {
    return doCreate(newName, directory);
  }

  @NotNull
  protected abstract PsiElement[] doCreate(String newName, PsiDirectory directory) throws Exception;

  protected static PsiFile createClassFromTemplate(final PsiDirectory directory, String className, String templateName,
                                                   @NonNls String... parameters) throws IncorrectOperationException {
    return GroovyTemplatesFactory.createFromTemplate(directory, className, className + ".groovy", templateName, parameters);
  }


  protected String getErrorTitle() {
    return CommonBundle.getErrorTitle();
  }

  protected String getActionName(PsiDirectory directory, String newName) {
    return GroovyBundle.message("newclass.progress.text", newName);
  }

  protected void checkBeforeCreate(String newName, PsiDirectory directory) throws IncorrectOperationException {
    directory.checkCreateClass(newName);
  }
}

{'fraction_non_alphanumeric': 0.09234857712324217, 'fraction_numerical': 0.0026575130107407818, 'mean_word_length': 4.517409896151497, 'pattern_counts': {'":': 0, '<': 3, '<?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': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5219508', 'n_tokens_mistral': 2499, 'n_tokens_neox': 2394, 'n_words': 640}